Ionic + Google Analytics, Passive Aggressive Behavior

I’m at a complete loss here with Google Analytics and Ionic. So, here’s the scenario(s).

I have an app that runs, well. It uses other plugins without problem. I’ve tried each of the variants on Google Analytics (in Ionic 1) without any luck whatsoever. With just Google’s code I can get it to report back to GA in the browser. But I’ve tried everything I can think of to get the actual app on device to report back to GA when it loads without any level of success. I’ve followed every tutorial I could find. I installed a whitelist plugin and listed Google and GA. I even tried using the GA code, loaded from index.html and grabbing it through $window.ga after a 3 second delay. Generally, the whole thing comes back to a scenario where the plugin I have installed at that moment is not throwing an error, but calling window.ga.whatever or analytics.whatever just won’t take action even enough to start the session. Sometimes I can get one of them to tell me that window.ga = {}. Sometimes it reports back that it’s undefined. Again, that’s even with waiting for a device to be ready and a 3 second timeout.

I thought it might have been some odd architectural issue, so I spun up a completely new Ionic 2 app and followed those instructions with just the GA plugins and while it runs, it never reports over to Google Analytics. There, too, it seems that Google Analytics isn’t being loaded even with no code of my own added.

I have two GA codes that I’ve tried, too. One is a web property, the other a mobile. Neither seem to matter.

So the question is, what am I not thinking of? I’m guessing that everyone’s Google Analytics reporting didn’t magically stop at one point. Is there some kind of whitelisting that I have to do that maybe I botched in the use of cordova-plugin-whitelist? What about security settings on the device?

Typically I’d post code, but when it’s happening over three or four plugins without error, it doesn’t really feel like a code problem, but more like a policy/device issue. Any ideas? Also, thanks for reading.

Apparently it only took a week and a half more of confusion before I nailed it down. I’m posting this here for the next person who might run into issues.

First issue: When you use Google Analytics ids, make sure your ID is for an app, not a site. If you use the site version, it won’t work. Also, be sure to watch the “Screens” tab in Analytics, that’s where it registers data. If you’re like me and tried both, but didn’t see any data coming through, it could be that it’s just in the other tab and you’re missing it. Also, even real-time data isn’t real-time. Sometimes it’s delayed 5-10 seconds.

Second issue: You can’t have it both ways. (Maybe you can, but I opted to ignore the original.) You can’t seem to post your GA code in the index file AND run the plugin. The two things will be in quiet conflict, and reaching window.ga will give you inconsistent results. Or just undefined. Therefore, if you check for things like if (window.ga) { // your code }} it may or may not fire. Sometimes, you can even get window.ga = {}, which will still throw errors if you run trackView or trackEvent.

Third issue: It’s not a typo that they say window.ga. It’s not $window.ga. $window.ga will work on occasion, but not consistently. Use window.ga like the docs say. It’s also not window.analytics, as some use. or $cordova… If you’re using an up-to-date version of the library, you should be fine with window.ga. I also didn’t need a timeout afterall, $ionicPlatform.ready() was good enough.

I hope this saves someone else the trial and error it took me.

1 Like