• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

mzuccaroli/angular-google-tag-manager: A service library for integrate google ta ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

mzuccaroli/angular-google-tag-manager

开源软件地址(OpenSource Url):

https://github.com/mzuccaroli/angular-google-tag-manager

开源编程语言(OpenSource Language):

TypeScript 87.0%

开源软件介绍(OpenSource Introduction):

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

    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},
        {provide: 'googleTagManagerCSPNonce',  useValue: YOUR_CSP_NONCE}
    ],

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
    })
]

Authors

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

License

This project is licensed under the MIT License

Acknowledgments




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap