I've got an HTML5 app built with Phonegap and jQueryMobile and I'm testing it for Android. I want to include Analytics, but every approach I take hits roadblocks.
This solution really got me excited: just include a JS file, initialize Analytics in the html , and I'm all set. It looks so very promising. And it works in the browser. But not on my Nexus S running Android 4.1.2.
I put this script at the end of the document head:
<script type="text/javascript" src="GALocalStorage.js"></script>
<script>
ga_storage._setAccount('UA-XXXXXXXX-1');
ga_storage._trackPageview('/index.html');
ga_storage._trackEvent('Startup','Starting scripts all loaded')
</script>
Early on during the app startup, I get these encouraging debug signs:
09-05 21:47:49.558: V/GAV3(22963): Thread[GAThread,5,main]: connecting to Analytics service
09-05 21:47:49.609: V/GAV3(22963): Thread[GAThread,5,main]: connect: bindService returned true for Intent { act=com.google.android.gms.analytics.service.START (has extras) }
09-05 21:47:49.656: V/GAV3(22963): Thread[GAThread,5,main]: Loaded clientId
...
09-05 21:47:49.660: I/GAV3(22963): Thread[GAThread,5,main]: No campaign data found.
09-05 21:47:49.664: V/GAV3(22963): Thread[GAThread,5,main]: putHit called
Shortly after that appear a number of "Unknown Chromium error: 0". When I set GALocalStorage to debug mode, I see that this is almost always right behind a "Tracking event".
Then, much later, these lines show up:
09-05 21:47:52.558: V/GAV3(22963): Thread[Failed Connect,5,main]: falling back to local store
09-05 21:47:52.609: V/GAV3(22963): Thread[GAThread,5,main]: Sending hit to store PATH: https: PARAMS: v=1, ul=nl-nl, t=appview, ht=1378410463883, sr=480x800, an=True-Budget, tid=UA-XXXXXXX-1, aid=com.mappingtheforest.truebudget, cid=c1c63a4d-0dfd-48fb-9e8a-1371e06d28b4, av=1.0.0, _u=.KnL,
09-05 21:47:52.777: V/GAV3(22963): Thread[GAThread,5,main]: PowerSaveMode initiated.
09-05 21:47:52.937: V/GAV3(22963): Thread[GAThread,5,main]: PowerSaveMode terminated.
09-05 21:47:52.937: V/GAV3(22963): Thread[GAThread,5,main]: Dispatch running...
09-05 21:47:52.976: V/GAV3(22963): Thread[GAThread,5,main]: User-Agent: GoogleAnalytics/3.0 (Linux; U; Android 4.1.2; nl-nl; Nexus S Build/JZO54K)
09-05 21:47:52.976: V/GAV3(22963): Host: ssl.google-analytics.com
09-05 21:47:52.976: V/GAV3(22963): GET /collect?v=1&ul=nl-nl&t=appview&ht=1378410463883&sr=480x800&an=True-Budget&tid=UA-XXXXXXXX-1&aid=com.mappingtheforest.truebudget&cid=c1c63a4d-0dfd-48fb-9e8a-1371e06d28b4&av=1.0.0&_u=.KnL&_v=ma3.0.0&qt=9079&z=1 HTTP/1.1
I've already made sure that the google-analytics.com domain is whitelisted in Phonegap's config.xml file.
What's going on?
Okay, so the warnings about Android 4.1 not supporting Google Analytics in the above setup are wrong!
The problem is in the domain whitelisting, which needs to be really precise in order to work.
In config.xml, make sure that the following permissions exist:
<access origin="http://*.google-analytics.com"/>
<access origin="https://*.google-analytics.com"/>
The lesson: you need to have separate permissions for http and https. And in both cases you need to use the asterisk to allow Google Analytics to make calls to any subdomain it needs. This removes the "Unknown Chromium error: 0" problem, which was a signal that the request was being blocked.