revolunet / angular-google-analytics

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

Set tracking id in controller #146

Closed apien closed 8 years ago

apien commented 8 years ago

Hello, I have problem with set tracking id in the controller. I have a brief look and saw like I don't have that possibility by service, only by provider directly. I want to set up it in one of my main controller because of tracking id is fetch from the server after successful log in

Btw. I tied to manually set up configuration.accounts then invoke createAnalyticsScriptTag but not effect.

Do you know some solution at my issue?

Update. Sorry I found the same question https://github.com/revolunet/angular-google-analytics/issues/140 . So it not possible any way?

Toxantron commented 8 years ago

At least right now it isn't as @justinsa pointed out.

As far as I understand this is due to the way angular modules are supposed to be designed. I also wouldn't know how such an API would look like?

// In provider
app.config(function (AnalyticsProvider) {
  AnalyticsProvider.delayCreation(true);
);

app.run(function (Analytics) {
  Analytics.setAccount('UA-XXXX-XX');
  Analytics.initialize();
});

I was however wondering how this use case keeps popping up. Why can't you deliver the the tracking id when the app is opened rather then using an AJAX call later?

justinsa commented 8 years ago

Thanks for providing an answer to this @Toxantron. You are correct in your assertions and your quandary is the same one I have had when this question comes up.

If you have configuration data that needs to come from the server, then pre-load that prior to running Angular. There is no reason to have Angular fetch that data since it contains information to configure providers in Angular, and providers can only be configured during the configuration stage of the Angular boot sequence.

Now, if you really wanted to get around this you could with the existing code, but it is hack to do it.

app.config(function (AnalyticsProvider) {
  AnalyticsProvider.setAccount([]);
  AnalyticsProvider.delayScriptTag(true);
});

app.run(function (Analytics) {
  Analytics.configuration.accounts.push({ tracker: '1234567890' });
  Analytics.creeateAnalyticsScriptTag();
});