mokipedia / angular-google-tag-manager

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

Home Page:https://itnext.io/how-to-add-google-tag-manager-to-an-angular-application-fc68624386e2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Angular Google Tag Manager Service

A service library for integrate google tag manager in your angular project This library was generated with Angular CLI version 7.2.0. 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}
    ],

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

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

Authors

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

License

This project is licensed under the MIT License

Acknowledgments

About

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

https://itnext.io/how-to-add-google-tag-manager-to-an-angular-application-fc68624386e2


Languages

Language:TypeScript 79.9%Language:JavaScript 10.7%Language:HTML 8.9%Language:CSS 0.5%