Android: Can I Use Google Analytics Inside A Service?
Solution 1:
Good news! You can. and it's quite easy.
You'll need the application context let's call it mCtx When you have this you need an instance of GoogleAnalytics, you can get it by calling
GoogleAnalyticsmGaInstance= GoogleAnalytics.getInstance(mCtx);
now you need to set any parameters you want (which you would normaly put in analytics.xml when using EasyTracker).
now you need a Tracker instance:
TrackermTracker= mGaInstance.getTracker("UA-XXXX-Y"); // your ID here
and that's basically it.. now you can send events with this tracker
mTracker.sendEvent(....);
etc..
Hope this helps. This is the very basics but GoogleAnalytics and Tracker replace the EasyTracker.
You can read more about it here: Advanced Configuration - Android SDK
Just note that until you'll see the reports on the GA website, it can take up to 24 hours.. so be patient :) or use mGaInstance.setDebug(true) to see in the logcat that it has been sent
Solution 2:
I would suggest not to do so unless you are very sure what you are doing.
I implemented GA events in my service but it corrupted a lot of my GA stats such as session duration, daily percentage of new sessions, daily sessions etc. GA thinks that events are caused after a screen view and so it pushes the GA event with a screen name "(not set)". Since services ran in the background a lot of times, it ended up corrupting the various stats. The real-time display of active users also went wrong.
Post a Comment for "Android: Can I Use Google Analytics Inside A Service?"