在 Google Analytics SDK v4 for Android ,文档说您可以创建一个跟踪器,而无需先使用跟踪 ID 对其进行初始化:
If the trackingId is empty, you can still get a tracker, but you must set the tracking id before sending any hits. This is useful if you do not know the tracking id at the time of tracker creation, or if you want to use the same tracker instance to track multiple tracking ids.
- Tracker 的类概述说:
A Tracker must be initialized with a tracking id of an app profile before you can send any hits. You can change the id to send hits to a different profile.
创建跟踪器后,您调用什么方法来设置跟踪 ID?
动机
在v3版本的Android SDK中,配置文件可以被拆分,我可以使用资源合并来定义通用值,例如src/main/res/values/analytics.xml
中的跟踪ID并包含调试属性,例如 ga_dryRun
在src/debug/res/values/analytics.xml
.
但 v4 SDK 现在从 res/xml
读取配置不支持资源合并AFAICT。我想在一个地方(Java 或 XML)定义跟踪 ID,但也可以灵活地为不同的构建变体使用不同的配置文件。
最佳答案
目前,创建跟踪器后无法设置跟踪 ID。一种 HACKY 方法是使用 Tracker.set()
方法,其中 key = "&tid"
且 value 作为跟踪 ID。
感谢您描述您的动机。我们会在未来的版本中牢记这一点。
注意:跟踪 ID 只能在创建期间设置的原因是跟踪器对象包含大量状态数据。强烈建议初始创建后不要更改跟踪 ID。
PS:在可预见的将来,将支持 hacky 方法,但如果您决定使用它,请在发布应用程序之前确认可以在实时报告中看到数据。
关于android - 在 Google Analytics v4 for Android 中创建跟踪器后如何设置跟踪 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23390963/