monitor with html tag

Add Google Tag Manager to an Angular application

First add the noscript tag to handle cases where javascript is turned off and you still want to capture analytics data.

<!-- Google Tag Manager (noscript) -->
  <iframe src=""
height="0" width="0" style="display:none;visibility:hidden">
<!-- End Google Tag Manager (noscript) -->

Next add the Google Tag Manager script as far up in the head of your index.html as possible

window.dataLayer = window.dataLayer || [];
<!-- Google Tag Manager -->
	new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
<!-- End Google Tag Manager -->

Now the GTM will be ready to use

The DataLayer

You can see in the above code snippet that the dataLayer is setup before the GTM script, this is necessary for it to work correctly. In your root component, probably app.component.ts, setup GTM to run in the Angular app.

private setupGoogleTagManager() {
  this.googleTagManagerIframeUrl = this.sanitizer.bypassSecurityTrustResourceUrl(

// then in the constructor


The Google Tag Manager use a special data layer variable called event that is used by JavaScript event listeners to fire tags when a user interacts with website elements. Events are fired by pushing the event and event name onto the data layer queue. This is vanilla JavaScript where where we initialize an array and then push an object into it.

window.dataLayer.push({'event': 'event_name'})

Now in the component where we want to fire a click event for example

handleEventWeWantToCaptureClick(): void {

private setupGoogleTagManagerForEventClicks(): void {
  window.dataLayer.push({'event': 'clickEventYouWantToCapture'})

Then in the component HTML

  Example link

That should be it. Good luck!





Leave a Reply

Your email address will not be published. Required fields are marked *