Android:我可以在服务中使用 Google Analytics(分析)吗? [英] Android: can I use Google Analytics inside a Service?

查看:19
本文介绍了Android:我可以在服务中使用 Google Analytics(分析)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它大部分时间都在后台运行,即服务.网上有很多关于如何使用带有 EasyTracker 库的 Google Analytics API 来跟踪多个活动的示例和教程,但没有一个解释如何在服务中使用 Google 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 时通常会将这些参数放入 analytics.xml 中).

now you need to set any parameters you want (which you would normaly put in analytics.xml when using EasyTracker).

现在你需要一个 Tracker 实例:

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(true) 在 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:我可以在服务中使用 Google Analytics(分析)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆