Skip to content

Instantly share code, notes, and snippets.

@StephenFluin
Last active July 22, 2016 01:36
Show Gist options
  • Save StephenFluin/7449a1ef0c304bba4e950853f18ae691 to your computer and use it in GitHub Desktop.
Save StephenFluin/7449a1ef0c304bba4e950853f18ae691 to your computer and use it in GitHub Desktop.
Angular 2 Analytics via Routing
//...
import { Router, NavigationEnd } from '@angular/router';
//...
declare var ga : any;
//..
constructor(private router : Router ) {
router.events.filter(e => e instanceof NavigationEnd).subscribe( (n:NavigationEnd) => {
ga('send', 'pageview', n.urlAfterRedirects);
});
}
//..
@wardbell
Copy link

Interesting. It is not. We haven't talked about router events yet.

This would be a cool example.

@Splaktar
Copy link

I had to do a slightly different implementation with an angular-cli project and rxjs 5.0.0-beta.6 (router.events.filter is undefined). I tried with 5.0.0-beta.10 but it breaks other dependencies.

I also don't add the GA script in dev builds, so I added a production check as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment