When I used Google analytics SDK v2 for mobile android app, I was pretty impressed with it. With very little code I was able to implement it. I initially tried to make legacy API 1.5 work thinking that 2.0 is in beta and I had some existing open source code using 1.5. But After digging more into it, I decided to give 2.0 beta a try. I’m glad that I used 2.0 API as it turned out to be simpler implementation
Here is some code snippet.
// init code within onCreate EasyTracker.getInstance().activityStart(activity); // Code to track a pageview EasyTracker.getTracker().trackView(path); // track event EasyTracker.getTracker().trackEvent(category, action, label, val); // stop tracking. Should be part of onStop in Activity EasyTracker.getInstance().activityStop(activity);
In addition you also need analytics.xml in res/values as shown below. You may want to experiment with various values. See official
doc for more parameters.
<?xml version="1.0" encoding="utf-8" ?> <resources> <string name="ga_trackingId">UA-xxxxxxxx-1</string> <bool name="ga_autoActivityTracking">true</bool> <integer name="ga_dispatchPeriod">10</integer> <bool name="ga_reportUncaughtExceptions">true</bool> </resources>
You may want to change ga_dispatchPeriod to higher value in case you want to dispatch less frequently. The trackingId can be obtained by creating a new account and mobile app in google analytics account as shown below.
The code is pretty simple and does lot of tracking like app version, devices, location, views, etc. And you also can view real time tracking when someone is using the app. That makes it pretty useful when you are running some ad campaign (e.g. admob). The results can be viewed immediately and you can know if everything is all right. Here are some screenshots of real time tracking: