简体   繁体   中英

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.

In getDefaultTracker() it has reference to 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.

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.

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. Both seem to contain the same attribute 'ga_trackingId'.

Can I just pass in the same xml into analytics.newTracker() and don't add the json at all. 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

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

In V4 for tracking a screen it says

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.

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. So when you finish, you will end up with one JSON file containing it all. (I guess, this is assuming you use one dev console app name).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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