I’ve just been experimenting with my first app using Google Analytics. I set up it all nicely to track each activity that is visited, track when certain buttons are pressed and also what orientation the user is currently using my app in. It all looked fine and dandy on the code side. Next I waited the mandatory 24hours (I actually waited 3 days) and NO stats where appearing on http://www.google.com/analytics/!! I debugged to the max, re-wrote my implementation twice. During all this I must have come across every bug possible with Google Analytics and none of them fixed my issue. This is a blog post collating these issues and the final outcome of working analytics yayy!
Reasons for using Google Analytics for Android:
Google analytics is a powerful tool you can use to improve your android application. Say you have a settings screen with 10 different settings on it, you could ‘track’ when each setting has been pressed by your users. Using this data you can then reorder your settings so the most used is at the top! Also you could remove a setting that is never used, tidying up your code base.
Lets cut to the chase, the main issues people have when using raw Google Analytics or the EasyTracker library.
- Not waiting long enough for the analytics to come through
Analytics from googles servers usually take about 24hours to appear the first time, depending on your traffic you can get your analytics updated hourly to 3 hourly. Make sure you wait this out. To not waste your time in waiting for this data blindly you can enable debugging to see that the requests are being sent.
This is set like this for raw analytics:
And like this for EasyTracker:
- Not actually calling dispatch to send the tracking events
If you start your analytics session in the following way, you have to also manage dispatching tracking events yourself.
Dispatching events is as easy as:
Just don’t do it too often or you’ll be spamming your users internet connection.
The other option is to start a new session using this method:
GoogleAnalyticsTracker.getInstance().startNewSession("UI-XXX-YY", 30, this);
This will now automatically dispatch every 30 seconds.
Ok so you’ve got both of these issues down and you’ve enabled debugging, this is where you have to watch the Google dispatcher. This is a queue of events that need to be sent off to the analytics server, if these are never dispatched you will never receive any data to your analytics account!
- Having spaces in your analytics keywords
This is not so much of an issue with the updated Google Analytics, but take note. When you are sending events, page views or custom vars ensure that all your string variables don’t have any random spaces in them. Remember the Google Analytics jar is just creating a URL and hitting it, if you have a space in the URL then it is broken! (I believe this is fixed in the latest jar but FYI).
- Corrupt data – corrupts absolutely
For the reason named above and for many other reasons you could be attempting to track corrupt data. Once you have requested Google Analytics to track something, it is added to it’s queue (the dispatcher) and in the background it is also added to an analytics database. This database stores all the unsent tracks used in the dispatcher. For example, if you track three events and the user does not have an internet connection these will be saved to be tracked when a connection is re-established and your app is foregrounded. The issue arises when you have a corrupt track, any tracks that are attempted after the corrupt one will not be fired as the dispatcher keeps failing on your corrupt one first. The fix is to delete the database (in a dev environment this equals uninstalling and reinstalling) if you believe this is live within your app it is a bit more involved to uninstall the database on a remote device.
A symptom of corrupt data can be a LogCat that says “Called Dispatcher“, “Scheduled Next Dispatch“, “Sending 3 hits to dispatcher” and so on in an infinite loop every time dispatch() is called (either by you or at a timed interval). This can happen in raw Google Analytics and EasyTracker (EasyTracker is not that clever).
Finally the above LogCat can also be a symptom of you using an incorrect Property ID / Web Id / UA-XXX-Y whatever you want to call it. If its incorrect you will never receive your data.
Here is what took me 5 hours of constant debugging to figure out.
- Having your Web ID in strings.XML and Lint
In the recent update to Android Development Tools (ADT) for eclipse version 16.0.0 (December 2011). Lint was added that lets you see the common errors, bad practice, coding mishaps that have wriggled into your code.
One of their ‘improvements’ is to recommend:
Replace “-” with an “en dash” character (–, &&;#8211;) ?
Yes its asking to replace the dash with an encoded dash. This is all great for valid data, but it breaks Google Analytics 🙁 !
To the naked eye that is the smallest difference, but it stops Google Analytics from recognising your Web ID.
If you where debugging this issue, you would see the Analytics dispatcher is constantly attempting to send the track events request and I actually thought it might have been completing. It looks like this:
03-07 11:53:01.710: I/GoogleAnalyticsTracker(4316): Host: www.google-analytics.com 03-07 11:53:01.710: I/GoogleAnalyticsTracker(4316): User-Agent: GoogleAnalytics/1.4.2 (Linux; U; Android 2.3.6; en-gb; Nexus S Build/GRK39F) 03-07 11:53:01.710: I/GoogleAnalyticsTracker(4316): GET /__utm.gif?utmwv=4.8.1ma&utmn=830133257&utmt=event&utme=5(PageStart*Orientation*Portrait)(1)&utmcs=UTF-8&utmsr=480x800&utmul=en-GB&utmac=UA-29349410-1&utmcc=__utma%3D1.1221977572.1331121154.1331121154.1331121154.1%3B&utmhid=-758178458&utmht=1331121155499&utmqt=23211 HTTP/1.1
So I thought great its sending them! However on the next dispatch the number of hits the dispatcher was sending would be the same. I was thinking it was just sending them over and over, but the fact was because the ID was corrupt it was never sending them and my dispatcher was royally screwed.
Now that I have noticed this Lint error and put it back, I get a Lint warning that I am ignoring but more importantly the correct LogCat for a completed Google Analytics Track should look like this in your console:
03-07 11:53:02.195: V/GoogleAnalyticsTracker(4316): HTTP Response Code: 200
You will get this once for each track/pageView that you send. Also when the dispatcher has cleared it’s queue and it sends the next lot (because you called dispatch() or the timer interval elapsed):
03-07 11:53:14.355: V/GoogleAnalyticsTracker(4316): Called dispatch 03-07 11:53:14.355: V/GoogleAnalyticsTracker(4316): ...but there was nothing to dispatch
Yayy this is good it means all your previous Analytics tracks where sent!
So that’s it, I came across each bug one at a time, but the hardest one and the only tip I couldn’t find on the internet was don’t let Lint encode the dashes in your Web ID. This is more likely to happen if you use the EasyTracker library but it could also happen if you store your Analytics API key within your strings.xml. Which is what I did!
Happy Debugging 🙂