0

I'm using angulartics2 for tracking events and for tracking some other info to google analytics.

So, I'm just using

Angulartics2Module.forRoot({ pageTracking: { autoTrackVirtualPages: true } }) - in main app module and Angulartics2GoogleAnalytics.startTracking() - in main app component.

This method tracking all the routes existing in the angular router changes, but I need to avoid tracking the root (/) route.

I've tried to configure angulartics module as { pageTracking: { autoTrackVirtualPages: true, excludedRoutes: ['/'] } } or { pageTracking: { autoTrackVirtualPages: true, excludedRoutes: [/\//] } } but it's not working like I expect.

Is there a simple way to exclude this route from tracking?

Art Olshansky
  • 3,032
  • 1
  • 19
  • 25

1 Answers1

1

One of the options, for those who will be faced with the same problem:

Create separate service which will use global analytics script and just avoid track action when the route matches your case

export class AnalyticsService {
  constructor(private readonly router: Router) {}

  startRouterTracking(): void {
    this.router.events.subscribe(event => {
      if (event instanceof NavigationEnd) {
        const { urlAfterRedirects } = event;

        if (urlAfterRedirects.trim() === '/') {
          return;
        }

        // note: angulartics.pageTrack works incorrect (but by docs should work fine, have no idea why doesn't work)
        gtag('event', 'page_view', { page_path: urlAfterRedirects });
      }
    });
  }
}

Art Olshansky
  • 3,032
  • 1
  • 19
  • 25