Using Gtag In Angular
Google Analytics with gtag.js
I recently wanted to include Google site tagging capabilities to a simple single-page application of mine and that’s what I came up with:
In the example above we see the GtagDirective at work (look closely in the .html).
The gtag="home"
statement in the .html sends a page-view
event to the Google Analytics, Google Ads or Google Marketing Platform account you configured during the initialization (more next).
The “home” text will appear in the report to be the page title while the page location is automatically detected by the directive provided the application makes use of a Router.
By simply adding this directive to every page of your app you’ll be tracking page views.
The same can be applied to part of your views when displayed conditionally: