Google Analytics πŸ“ˆ in Angular App β€” Example

Google Analytics in Angular App

Do you need to measure traffic on your Angular routes? Then you are here right. In this example I will demonstrate how to create simple and elegant Angular service to measure traffic on your Angular App with Google Analytics.

Prerequisites

You should have already set up you Google Analytics account and have yourΒ UA-XXXXXXXX-X.

Example

First of all we willΒ include Google Analytics in index.html.

Then we will create our Google Analytics service. Here we will subscribe for routing events, but only when we are in production.

The last step is app.module.ts, where we will inject our GoogleAnalyticsService.

And we are set up. Now we can test it, don’t forgot to comment out production mode line in GoogleAnalyticsService, when in dev environment. Here is screen from one of my apps.

Google Analytics with Angular 2

Google Analytics with Angular App

Buy me a coffeeBuy me a coffee