Fork me on GitHub
#reitit
<
2022-03-14
>
pez13:03:56

When doing pageView analytics logging it seems that the router is a good place to do it. It's not fully clear to me how to do it with reitit, though. Anyone has some tips to throw my way? 🙏 Edit: This is frontend reitit.

valtteri16:03:04

My first idea would be to add a controller to appropriate place up in the route tree :thinking_face:

valtteri16:03:59

Second idea would be to jack logging into on-navigate-fn .

valtteri16:03:13

However the first idea is perhaps better

pez16:03:21

Thanks. I tried with a controller first, but I could only make it log on initial page load. Using the navigate fn worked fine!

👍 1
valtteri16:03:36

Just checked and in a real project I’ve actually done it as in the second idea. 😄

😂 1
valtteri16:03:43

More particularly: on-navigate-fn dispatches a re-frame event :navigated that causes an effect that reports the page view

pez16:03:33

I should do it like that as well. Thanks!

valtteri16:03:46

No problem!

wevrem00:03:44

Just curious, from the standpoint of how often it fires, what is the practical difference between infiltrating the on-navigate-fn and using controllers? Don’t they both fire essentially once per page? This is mostly a followup question for @U0ETXRFEW who said that, using controllers, “I could only make it log on initial page load” but I can’t see how that would be different using on-navigate-fn which it would seem would also only fire once. (At least, with navigate you only have to write your logic in one place, instead of peppering all your routes with controllers, but I’m really just wanting to understand if this makes any difference with the once-per-page timing.)

valtteri06:03:42

It seems like “upper” controllers in nested route trees are applied only once when you enter that “branch” in the routing tree. And this is by design

valtteri06:03:22

So my intuitive mental model was just wrong. I think I compared them with middleware

pez07:03:52

Just checking, middleware is not supposed to be side-effecty, right?

pez07:03:11

@UTFAPNRPT when I tried it, I did add the controller to all routes. But I couldn't get them to fire more than once, each. There is some semantic around a controller being started that I don't grasp. The on-navigate-fn is called each time a route is entered.

valtteri19:03:28

I think side-effects in middleware might work but it sounds like a really bad idea. Good question though.