codediodeio / angular-gtag

:bookmark: Google Analytics gtag.js for Angular
109 stars 20 forks source link

How to use with variable GAID? #2

Open deejbee opened 6 years ago

deejbee commented 6 years ago

If I need to load a different GAID when the app loads (from a config file that varies between different applications), what is the best approach since I can't put a hard coded GAI in the app.module.ts imports @NgModule imports array since the GtagModule.forRoot() is run very early in the liefecycle?

dj-at-work commented 6 years ago

Same issue here, we have different environments like DEV and PROD and can't go with the same GAID for both. I would like to initialize it in the app.component.ts, how can I achieve this?

chaitanyasm commented 6 years ago

I defined tracking ids in environment files and initialize Gtag in app module like this.

GtagModule.forRoot({trackingId: '', trackPageviews: true })

and provide environment specific tracking id in app component constructor.

this.gtag = new Gtag({trackingId: environment.gaTrackingID, trackPageviews: true }, this.router);

This enabled me to track on different GAIDs based on environment the app is built for.