angulartics / angulartics-google-analytics

Google Analytics plugin for Angulartics
MIT License
133 stars 86 forks source link

Setting UA-XXXXXXXX-X tracking code dynamically based on environment #76

Closed amkoehler closed 8 years ago

amkoehler commented 8 years ago

I'm currently setting the tracking code via ga('create', 'UA-XXXXXXXX-X', 'auto'); but would like to set a different tracking code depending on my node environment. This would help ensure my analytics data from development doesn't carry over to my production analytics dashboard. Is it possible to set it via $analyticsProvider in my Angular config? It's possible to set additional accounts via $analyticsProvider.settings.ga.additionalAccountNames but I'm only looking to set an initial tracking number. This is different from #7.

timelf123 commented 8 years ago

Passing in env variables is kind of outside the scope of the project. There are several options depending on how your stack is configured.

http://www.jvandemo.com/how-to-configure-your-angularjs-application-using-environment-variables/

amkoehler commented 8 years ago

Thanks, Tim. I wasn't thinking you should be handling env variables, but was curious if I could set the tracking id via $analyticsProvider instead of having to set it via ga('create', 'UA-XXXXXXXX-X', 'auto');. On my end it would be something like

if (ENVIRONMENT === 'production') {
    $analyticsProvider.settings.ga.account = 'UA-123456-1';
} else {
    $analyticsProvider.settings.ga.account = 'UA-123456-2';
}

angular-google-analytics handles this with AnalyticsProvider.setAccount('UA-XXXXX-xx');. But for now I can just set developerMode to true and disable tracking altogether.

timelf123 commented 8 years ago

I think that should work -- let me know what you find. Any reason you want to avoid the call to ga?

amkoehler commented 8 years ago

I'm only setting developerMode to true if ENVIRONMENT === 'development'. Will continue tracking in production environments.

We are tracking a lot of events and user timings in GA (REST methods) and would prefer those results in dev environments not get mixed in with the analytics for our production environment. There may be some filtering options in GA's dashboard that could also accomplish this, but I'm glad we can at least use developerMode. Thanks again.