Last active
July 22, 2016 01:36
-
-
Save StephenFluin/7449a1ef0c304bba4e950853f18ae691 to your computer and use it in GitHub Desktop.
Angular 2 Analytics via Routing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//... | |
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); | |
}); | |
} | |
//.. |
Interesting. It is not. We haven't talked about router events yet.
This would be a cool example.
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
@StephenFluin - this is very cool. Is this in the Angular 2 Router docs ?