我有大部分时间在后台工作,即服务的应用程序。有很多的例子和教程网上关于如何使用谷歌Analytics(分析)API与EasyTracker库跟踪多个活动,但没有一个解释,如何使用谷歌Analytics(分析)API的服务。它甚至有可能?
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?
好消息!您可以。这是相当容易的。
Good news! You can. and it's quite easy.
您需要的应用程序上下文姑且称之为mCtx 当你有这个需要GoogleAnalytics的一个实例,您可以通过调用得到它
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);
现在你需要设置你想要的任何参数(其中使用EasyTracker时,你会normaly放在analytics.xml)。
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(....);
等。
希望这有助于。这是最基础的,但GoogleAnalytics和Tracker更换EasyTracker。
Hope this helps. This is the very basics but GoogleAnalytics and Tracker replace the EasyTracker.
您可以阅读更多关于它在这里: 高级配置 - Android SDK中
You can read more about it here: Advanced Configuration - Android SDK
只是注意,直到你会看到GA网站上的报告,它可能需要长达24小时..所以要耐心等待:)或使用mGaInstance.setDebug(真)在logcat中看到,它已发送
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
上一篇:间歇性连接被对方错误的Android重置连接到.NET REST端点间歇性、连接到、对方、错误
下一篇:System.FormatException:该字符串未被识别为有效的DateTime字符串、未被、有效、System