I have built a new site using Angular 2 as the front-end. As everything is done via push state, there are no page loads which typically trigger the Google Analytics code to send a page view to Google's servers.
How can I manually send page view events to Google so that I can track what users of my site are viewing?
Adding Google Analytics to your Angular app is easy. Well, adding it to an Angular SPA is not quite as easy compared to static websites but it can be done quickly if you know what you're doing. There's no need to sweat like a pig.
I managed to get this working by subscribing to changes on the router, checking that the route had actually changed (I was getting multiple events on some routes at times) and then sending the new path to Google.
app.component.ts
import { ... } from '...'; // Declare ga function as ambient declare var ga:Function; @Component({ ... }) export class AppComponent { private currentRoute:string; constructor(_router:Router) { // Using Rx's built in `distinctUntilChanged ` feature to handle url change c/o @dloomb's answer router.events.distinctUntilChanged((previous: any, current: any) => { // Subscribe to any `NavigationEnd` events where the url has changed if(current instanceof NavigationEnd) { return previous.url === current.url; } return true; }).subscribe((x: any) => { ga('set', 'page', x.url); ga('send', 'pageview') }); } } }
You also need to include the google analytics code in your main index file before loading your angular2 app so that the global ga
object exists, but you don't want to send the initial view twice. In order to do this, remove the following line from the GA script
index.html
<script> (function(i,s,o,g,r,a,m){...})(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-XXXXXXXX-X', 'auto'); // Remove this line to avoid sending the first page view twice. //ga('send', 'pageview'); </script> <!-- Load your ng2 app after ga. This style of deferred script loading doesn't guarantee this will happen but you can use Promise's or what works for your particular project. --> <script defer type="text/javascript" src="/app.js"></script>
Using a third party library
As an alternative to implementing GA yourself, the library Angulartics2 is also a popular tool for implementing GA tracking and also integrates with other analytics vendors as well.
Expanding on Ian's answer. You can use Rx's built in features to handle the distinction between current and new routes.
import { NavigationEnd, Router } from '@angular/router'; declare var ga: any; export class AppComponent { constructor(public router: Router) { router.events.distinctUntilChanged((previous: any, current: any) => { if(current instanceof NavigationEnd) { return previous.url === current.url; } return true; }).subscribe((x: any) => { console.log('router.change', x); ga('send', 'pageview', x.url); }); } }
We are using the distinctUntilChanged operator to make the observer only emit items that are of type NavigationEnd and do not have the same route as the previously emitted item.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With