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

Related Post

Right Click to Create New File on Mac OS X TL;DR; Windows has a lot of cons but it has also some pros. One of them is a smooth way to create empty file on right-click. Mac OS doesn't come with...
FREE ePub Quicklook and Spotlight Plugins for Mac ... When you have a lot of ePub books, it can be useful to see excerpt and meta data information in quicklook. Even more important...
Convert HEIC to JPG on Right Click for Free Convert HEIC to JPG on Right-Click In iOS 11 Apple is replacing the JPEG image format with the new HEIC alternative. But what to do when device or p...
Free Better Battery Stats and Time Remaining for M... Better Battery Stats and Time Remaining for Mac OS TL;DR; Do you miss the time remaining indicator on your MacBook, MacBook Air, or MacBook Pro? He...
Convert HEIC to PNG on Right Click for Free Convert HEIC to PNG on Right-Click In iOS 11 Apple is replacing the PNG image format with the new HEIC alternative. But what to do when device or pr...
FAQ: Mac OS X Services HEIC to JPG on Right-Click Service Q: Where’d all my services go? I don’t see anything in the Services menu. A: If you don’t select anything before...
Buy me a coffeeOut of coffee 😱, please help!