IronRouter's onAfterAction called multiple times per page (Google Analytics problems)

277 views
Skip to first unread message

Eliezer Steinbock

unread,
Feb 14, 2015, 11:44:51 AM2/14/15
to meteo...@googlegroups.com
Hi,

Iron Router's onAfterAction is called multiple times per page and this causing me problems with Google Analytics. I log my analytics using the technique used in the Telesc.pe project, which looks as follows:

Router.onAfterAction(analyticsRequest);

analyticsRequest = function() {
  noOfAnalyticsRequests++;
  console.log('analyticsRequest');
  
  // Google Analytics
  if (typeof window.ga !== 'undefined'){
    ga('send', 'pageview', {
      'page': window.location.pathname
    }); 
  }
}

The problem is that each page request is running onAfterAction multiple times per page. How can I stop this and thereby accurately see my Analytics results?

On thing I did that has helped is remove the multiply:iron-router-progress package from my project and now onAfterAction is only being called once or twice per page instead of 3 times per page. Is there a way to completely get rid of the problem? And is there a way to track analytics accurately while still using multiply:iron-router-progress?

Thanks

Diego Sampaio

unread,
Feb 15, 2015, 8:05:33 AM2/15/15
to meteo...@googlegroups.com
Hi there.

Probably you have some reactive in you action or before action. This cause the entire route to re-run.

Eliezer Steinbock

unread,
Feb 15, 2015, 8:10:10 AM2/15/15
to meteo...@googlegroups.com

Thanks. I'll have to take a closer look. There's nothing obviously reactive in my routes. It might be a global onbeforeaction for my router. Is there a way to use iron router progress without it rerunning routes?

On 15 Feb 2015 15:05, "Diego Sampaio" <chin...@gmail.com> wrote:
Hi there.

Probably you have some reactive in you action or before action. This cause the entire route to re-run.

--
You received this message because you are subscribed to a topic in the Google Groups "meteor-talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/meteor-talk/adi3TyyU_Ak/unsubscribe.
To unsubscribe from this group and all its topics, send an email to meteor-talk...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/meteor-talk/2358caf3-5814-4043-90a4-d97d734a0f22%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Paulo Borges

unread,
Feb 15, 2015, 9:29:40 AM2/15/15
to meteo...@googlegroups.com
You can try to use the onRun hook.

From https://github.com/iron-meteor/iron-router/blob/devel/Guide.md#available-hook-methods:
  • onRun: Called when the route is first run. It is not called again if the route reruns because of a computation invalidation. This makes it a good candidate for things like analytics where you want be sure the hook only runs once. Note that this hook won't run again if the route is reloaded via hot code push.


--
You received this message because you are subscribed to the Google Groups "meteor-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to meteor-talk...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/meteor-talk/CAP4v0bkwJ7STs-Sz1%2BmckeUHxoLQxONWvy1ORX07osqZ7EUp3g%40mail.gmail.com.

Eliezer Steinbock

unread,
Feb 15, 2015, 9:32:30 AM2/15/15
to meteo...@googlegroups.com

Wow. That's perfect. Thanks a lot

Reply all
Reply to author
Forward
0 new messages