angulartics / angulartics2

Vendor-agnostic analytics for Angular2 applications.
MIT License
1.01k stars 193 forks source link

Should events be buffered, while navigation is in progress? #356

Open asgerjensen opened 5 years ago

asgerjensen commented 5 years ago

For support questions, please consider using Gitter - Chat

I'm submitting a ...

When tracking events in ngOnInit, (at least with GTM), these events are pushed to the data layer ahead of the pageTrack event.

This seems to be due, to ngOnInit being invoked on the component before the NavigationEnd event is fired.

This is especially noticeable when getting data from internal services, that rely on subscriptions on behaviorsubjects, where data may already be ready, and will be delivered immediatly.

i.e.

   this.myService.subscribe( x => {
       // record that 
       this.angulartics2.eventTrack.next({ action: 'productImpression', properties: { product: x.partnumber }});
  });

Analytics events triggered by a component, comes after the page tracking for the page.

I was considering, that maybe this could be solved, if angulartics itself buffered all eventTracks, that happen while a navigation is in progress. I.e. not only react to NavigationEnd by putting out a pageTrack event, but also using the NavigationStart event to put the eventTrack queue into a "buffer mode", where any events registered would be postponed temporarily until the pageTrack occurs.

Environment: