I use to have such a problem. I ended up writting a service which I use to handle all my GA tracking events. I added an array to push in all events which would be triggered before the plugin was ready. Funny thing, as soon as I added my service, I didn’t faced anymore the error “tracker not started”, I guess it was a problem in my initialization flow.
Anyway there I posted the all code of my service (see answers from @peterpeterparker):
I tried creating a service as well, so now all the initialization code is in the service, but I’m still getting the “Tracker not started” error :-/
This is my service’s code (its simpler than yours):
I think the first thing first to test is: what version of the plugin are you using?
Just yesterday, users of the plugin found out that current version of the plugin doesn’t really work in Android but with previous version, 1.6.0 everything was working. Maybe there is your solution too?
See
About your code, I think it is fine. I just see a possible improvement, if you want to use enableUncaughtExceptionReporting, is to move the start of the tracker in the promise. Otherwise, if I understand, both operations gonna be done in parallel and honestly I don’t know if that could have a side effect…but maybe none
I made the change you suggested of calling the startTrackerWithId inside the promise for enableUncaughtExceptionReporting, and yes, that works better because now on every call to the service I get the “Tracker not started” error. Here is some additional output from the adb logcat. This is the only cordova plugin I’m using in the app, so I assume the “CordovaPlugin” are for GoogleAnalytics.
11-28 11:53:42.018 1230 1632 D hwcomposer: hw_composer sent 276 syncs in 65s
11-28 11:53:45.846 3859 3859 I chromium: [INFO:CONSOLE(796)] “GoogleAnalytics service platform ready”, source: file:///android_asset/www/build/js/app.bundle.js (796)
11-28 11:53:45.846 3859 3970 W CordovaPlugin: Attempted to send a second callback for ID: UniversalAnalytics809557534
11-28 11:53:45.846 3859 3970 W CordovaPlugin: Result was: “Invalid action”
11-28 11:53:45.847 3859 3970 W CordovaPlugin: Attempted to send a second callback for ID: UniversalAnalytics809557535
11-28 11:53:45.847 3859 3970 W CordovaPlugin: Result was: “Invalid action”
11-28 11:53:45.849 3859 3859 I chromium: [INFO:CONSOLE(813)] “Tracking page view: Players”, source: file:///android_asset/www/build/js/app.bundle.js (813)
11-28 11:53:45.849 3859 3859 I chromium: [INFO:CONSOLE(824)] “GoogleAnalytics: Tracked pageview for mobile: Players_android”, source: file:///android_asset/www/build/js/app.bundle.js (824)
11-28 11:53:45.851 3859 3859 I chromium: [INFO:CONSOLE(805)] “GoogleAnalytics error: Tracker not started”, source: file:///android_asset/www/build/js/app.bundle.js (805)
Maybe also worth mentioning, I’m using a simulator, not a real device. Could that be related?
No luck Heard people fixing that error using version 1.6.0 of the plugin but you already do…
When I look at your code I may see a possible improvement. The thing is, you’ve got your initialization (startTrackerWithId) in your constructor. That will start an own promise but the service object, if I understand correctly, gonna be constructed without waiting for the end of that promise.
In parallel, you’ve got page view triggering (track_page_view) which is made in a separate method of your class which gonna be available as soon as the object exists.
So, again, if I understand correctly, there is still a chance that the startTrackerPromise won’t be finished before the first time you will call your tracker, right?
What you could maybe do then, move your startTrackerWithId in your method track_page_view. Then with a class variable (like initialized:boolean = false;) detect if the initialization was already performed or still have to be done, like
I put the call for startTracker outside the constructor and into the track_view method, and then after the promise comes back, I execute the call to track view:
Now I get a “tracker started” message in the success of the enableUncaughtExceptionReporting, which is great, but still, for some reason, the call to trackView doesn’t seem to be doing anything (no activity in the real time view in the Google Analytics dashboard).
Could this be a missing permission in the manifest file? some .aar or .jar I need to put somewhere? No errors this time, just not working.
Do I need to copy the google-services.json as stated in the guide for Android?
Also, about having a class variable to avoid calling startTrackerWithId every time, that’s not working for me. It seems a new instance its being created everytime my service is called. Is there a side effect on calling startTrackerWithId multiple times?
Ok, I realized I had this as a provider and not as a service and that’s why the class variable was not being persisted. So I fixed that (not it is a service) and I don’t run the startTrackerWithId everytime.
Still the call to GoogleAnalytics.trackView is not doing anything. Not an error, nothing.
Maybe it takes a little couple of minutes till the event is displayed in the analytics console.
Well, again not sure, but when I compare your code with the pseudo I suggested you is that it seems you don’t trigger the track event when the start tracker was done, but maybe I don’t see it clearly. Won’t you mind posting the all current code of your injectable service?
Thx @jeudyx maybe someone else may have another thought but your code looks good to me and you are using the plugin version 1.6.0 and still nothing triggered
In your log you do see “GoogleAnalytics: Tracked view for mobile…” and no error prior to that right?
Another idea is, did you had a more recent version of the plugin than 1.6.0 before installing that version? If yes, did you remove the plugin before adding it again? Maybe the installation of 1.6.0 didn’t overrided the previous installation ?
Did you also double check that your ID is the correct one (“UA-xxxx-13”), no characters mistakes, the ID for the account you used to check your results … you know sometimes sh*t happens
Yeah. I even created a new property in case the original one had some issue. Its very suspicious that I don’t see any error. Hopefully building from a different machine will help.
Hi @reedrichards I got some updates: I got it to work on iOS, I was able to see the realtime data in teh GA dashboard, but no luck on Android yet! But this means the code is fine, the problem is related to the platform.
So, some questions:
Did you add a google-services.json file somewhere?
Did you add any external library, either a .aar or .jar into your project?
Are you using any additional permissions in your android manifest?
In my case it was the contrary, at first it was working in android and not in iOS. Finally I extended my service by adding my own initialization stack of events (see code in the GitHub ticket) and that fixed both terminal…if that could help too