Fork me on GitHub

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.


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


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


However the first idea is perhaps better


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

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

😂 1

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


I should do it like that as well. Thanks!


No problem!


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.)


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


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


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


@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.


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