简体   繁体   English

实施具有产品风格的Google Analytics V4

[英]Implementing Google Analytics V4 with product flavors

I am following this guide https://developers.google.com/analytics/devguides/collection/android/v4/ and it talks about creating a method in the Application class for getting the default tracker. 我按照本指南https://developers.google.com/analytics/devguides/collection/android/v4/进行操作 ,并讨论如何在Application类中创建一个获取默认跟踪器的方法。

In getDefaultTracker() it has reference to R.xml.global_tracker. 在getDefaultTracker()中,它引用了R.xml.global_tracker。 What it doesn't tell you is that this file is automatically generated when the json file is put into the root of the application module. 它没有告诉你的是,当json文件放入应用程序模块的根目录时,会自动生成此文件。

This works fine for a new project. 这适用于新项目。

The problem 问题

My existing project (which is currently using analytics V2) has product flavors so it doesn't make sense to put the json into the root of the application module as the package name and UA id is different for each flavor. 我现有的项目(目前正在使用analytics V2)具有产品风格,因此将json放入应用程序模块的根目录是没有意义的,因为包名称和UA id对于每种风格都是不同的。

I've tried to get it to work in a new project (for simplicity) with product flavors and it doesn't work. 我试图让它在一个新项目(为简单起见)中使用产品风味,但它不起作用。

Possible solution 可能解决方案

As I'm just upgrading from V2 is the auto-generated file the same as my existing analytics.xml. 由于我刚从V2升级,因此自动生成的文件与我现有的analytics.xml相同。 Both seem to contain the same attribute 'ga_trackingId'. 两者似乎都包含相同的属性'ga_trackingId'。

Can I just pass in the same xml into analytics.newTracker() and don't add the json at all. 我可以将相同的xml传入analytics.newTracker()并且根本不添加json。 If this is the case can I also ignore the below two parts in my build script 如果是这种情况,我也可以忽略构建脚本中的以下两部分

classpath 'com.google.gms:google-services:1.4.0-beta3'

and

apply plugin: 'com.google.gms.google-services'

Bonus Question 奖金问题

I currently set the screen names in my analytics.xml 我目前在analytics.xml中设置了屏幕名称

<string name="[full screen path]">[screen name]</string>

In V4 for tracking a screen it says 在V4中跟踪屏幕它说

mTracker.setScreenName("Image~" + name);

Do I have to explicitly set the screen names in code now? 我现在必须在代码中明确设置屏幕名称吗?

Not sure if you found an answer to this yet, but I've been researching this as well and found a solution which works for me. 不确定你是否找到了答案,但我也一直在研究这个问题,并找到了适合我的解决方案。 It seems that if you generate the JSON file over and over for each of your product flavor application IDs separately, then the JSON file will accumulate all your app IDs into it, so you end up with one JSON file with all your product flavors accounted for. 看起来如果您为每个产品风味应用程序ID分别反复生成JSON文件,那么JSON文件会将所有应用程序ID累积到其中,因此您最终得到一个JSON文件,其中包含您的所有产品风格。

In other words, each time you generate a JSON file per application ID at https://developers.google.com/mobile/add it just adds the info to the already existing JSON file. 换句话说,每次在https://developers.google.com/mobile/add上为每个应用程序ID生成一个JSON文件时,它只会将信息添加到现有的JSON文件中。 So when you finish, you will end up with one JSON file containing it all. 所以当你完成后,你将得到一个包含它的JSON文件。 (I guess, this is assuming you use one dev console app name). (我想,这是假设您使用一个开发控制台应用程序名称)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM