Socket
Socket
Sign inDemoInstall

angular-google-tag-manager

Package Overview
Dependencies
6
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    angular-google-tag-manager

A service library for integrate google tag manager in your angular project


Version published
Weekly downloads
33K
decreased by-15.63%
Maintainers
1
Install size
139 kB
Created
Weekly downloads
 

Changelog

Source

[1.9.0] - 2023-11-30

Added

  • Angular 17 support

Readme

Source

Angular Google Tag Manager Service

A service library for integrate google tag manager in your angular project This library was generated with Angular CLI For more info see this how to install google tag manager article

Getting Started

After installing it you need to provide your GTM id in app.module.ts

    providers: [
        ...
        {provide: 'googleTagManagerId',  useValue: YOUR_GTM_ID}
    ],

Or use the module's forRoot method

import { GoogleTagManagerModule } from 'angular-google-tag-manager';

imports: [
    ...
    GoogleTagManagerModule.forRoot({
      id: YOUR_GTM_ID,
    })
]

Or use the APP_INITIALIZER

import { GoogleTagManagerConfiguration } from 'angular-google-tag-manager-config.service';

imports: [
    ...
    GoogleTagManagerModule.forRoot()
]

providers: [
    {
      ...
      provide: APP_INITIALIZER,
      useFactory: configInitializer,
      deps: [
        HttpBackend,
        GoogleTagManagerConfiguration,
      ],
      multi: true,
    },
  ],

set the config in the method assigned to useFactory

googleTagManagerConfiguration.set(googleTagManagerConfiguration);

inject the gtmService in your controller

constructor(
        ...
        private gtmService: GoogleTagManagerService,
    ) { }

then you can start pushing events on your gtm

 this.router.events.forEach(item => {
            if (item instanceof NavigationEnd) {
                const gtmTag = {
                    event: 'page',
                    pageName: item.url
                };

                this.gtmService.pushTag(gtmTag);
            }
        });

if you want to recive tags without pushing events simply call the function to enable it

    this.gtmService.addGtmToDom();

Installing

In your Angular project run

npm i --save  angular-google-tag-manager

Custom configuration and GTM environments

You can pass gtm_preview and gtm_auth optional variables to your GTM by providing them in app.module.ts

In case you'll need to fetch your gtm.js resource trough a first party proxy ( so to bypass any ad-blocker interference in on the client browser ) you can use the gtm_resource_path parameter. In that case the resource will be fetched from the specified path instead that from 'https://www.googletagmanager.com/gtm.js' (all of the other queryparams will be mantained)

    providers: [
        ...
        {provide: 'googleTagManagerId',  useValue: YOUR_GTM_ID},
        {provide: 'googleTagManagerAuth',  useValue: YOUR_GTM_AUTH},
        {provide: 'googleTagManagerPreview',  useValue: YOUR_GTM_ENV},
        {provide: 'googleTagManagerResourcePath',  useValue: YOUR_GTM_RESOURCE_PATH}
    ],

Or using forRoot

import { GoogleTagManagerModule } from 'angular-google-tag-manager';

imports: [
    ...
    GoogleTagManagerModule.forRoot({
      id: YOUR_GTM_ID,
      gtm_auth: YOUR_GTM_AUTH,
      gtm_preview: YOUR_GTM_ENV,
      gtm_resource_path: YOUR_GTM_RESOURCE_PATH
    })
]

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License

Acknowledgments

FAQs

Last updated on 30 Nov 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc