revolunet / angular-google-analytics

Google Analytics tracking for your AngularJS apps
MIT License
652 stars 179 forks source link

`disableAnalytics` requires an account #226

Open lapo-luchini opened 5 years ago

lapo-luchini commented 5 years ago

Hi, for the sake of avoiding an if around every Analytics.trackEvent line we have in our code, I'd like to have a way to disable this module when a UA account is not defined (that's an option that our users will have to use or not).

Unfortunately disableAnalytics requires an account or gives a stack trace exception, so I worked it around like this:

mod.config(/*@ngInject*/ function (AnalyticsProvider, analyticsId) {
    if (analyticsId)
        AnalyticsProvider.setAccount(analyticsId); 
    else {
        AnalyticsProvider.setAccount(''); // else fails on next line
        AnalyticsProvider.disableAnalytics(true);
    }
})

This works ok (and any page change and event tracking generate no errors), but is there a recommended way to achieve this?

justinsa commented 5 years ago

I need to understand what you are encountering a little better. The disableAnalytics() config method sets a variable during configuration, that is all, so it is not generating an error on its own.

The reason this requires an account is because this is a feature of GA and something we are exposing during the script injection on the page. This is the code performing this:

if (disableAnalytics === true) {
  accounts.forEach(function (trackerObj) {
      that._log('info', 'Analytics disabled: ' + trackerObj.tracker);
      $window['ga-disable-' + trackerObj.tracker] = true;
  });
}

As you can tell this requires at least one account to exist. This entire library is premised on at least 1 account being defined for operation and a warning is issued during tracker registration if the list is undefined or empty.

Your particular use case sounds like what you expected was a way to disable the Analytics service and not just the GA behaviors.