简体   繁体   English

Android:我可以在服务中使用Google Analytics(分析)吗?

[英]Android: can I use Google Analytics inside a Service?

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. 在线上有很多示例和教程,介绍如何使用带有EasyTracker库的Google Analytics(分析)API来跟踪多个活动,但是没有一个单独的示例和教程说明如何在服务中使用Google Analytics(分析)API。 Is it even possible? 可能吗?

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 您将需要应用程序上下文,将其称为mCtx。拥有此上下文时,您需要一个GoogleAnalytics实例,您可以通过调用

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). 现在,您需要设置所需的任何参数(通常在使用EasyTracker时将其放在analytics.xml中)。

now you need a Tracker instance: 现在您需要一个Tracker实例:

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. 这是非常基础的内容,但是GoogleAnalytics和Tracker取代了EasyTracker。

You can read more about it here: Advanced Configuration - Android SDK 您可以在此处了解更多信息: 高级配置-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 请注意,在您可以看到GA网站上的报告之前,该报告最多可能需要24个小时。.请耐心等待:)或使用mGaInstance.setDebug(true)在logcat中查看该报告是否已发送

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) ". 我在自己的服务中实施了GA事件,但它破坏了我的许多GA统计信息,例如会话时长新会话的每日百分比每日会话等。GA认为这些事件是在屏幕浏览后引起的,因此它以一个屏幕名称“ (未设置) ”。 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. 活动用户的实时显示也出错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:我可以在Firebase App中使用Google Analytics(分析) - Android: Can I use Google Analytics with Firebase App Android:Google Analytics(分析)是Google Play服务的一部分 - Android: Google Analytics as part of Google Play Service 我可以使用什么分析服务来跟踪用户的偏好? - What analytics service can I use to track user preferences? 我可以将两个Google Analytics(分析)帐户添加到一个android项目中吗 - Can i add two Google Analytics account to one android project 我可以在针对点燃火灾商店发布的应用中使用谷歌分析吗? - Can I use google analytics in an app released for the kindle fire store? 我已经在 Flutter 中使用了 Google Analytics,如何从原生 iOS/Android 代码向它发送事件? - I use Google Analytics in Flutter already, how can I also send events to it from native iOS/Android code? android google analytics v3:“服务不可用” - android google analytics v3: “Service unavailable” Android Google Analytics与服务的连接失败 - Android Google Analytics connection to service failed 在Android服务应用程序中使用Google Analytics(分析) - Using Google Analytics in Android service application Google Analytics(分析)是否可以在Android上使用服务? - Does google analytics work for service on android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM