I have an application which most of the time works in the background, as a Service. There is a lot of examples and tutorials online on how you can use Google Analytics API with EasyTracker library to track multiple Activities, but there is not a single one that explains, how to use Google Analytics API in a Service. Is it even possible?
Access your Analytics accounts and data using a mobile device. The Analytics app gives you access to your Analytics accounts and data whole on the go. Use the app to get high-level performance metrics from your phone or tablet.
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
GoogleAnalytics mGaInstance = 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:
Tracker mTracker = 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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With