How to programmatically set ga_trackingId property

2020-07-23 05:43发布

I have a single app, but depending of user's choice, it will be monitored totally separated in google analytics, so I NEED to set "ga_trackingId" for EasyTracker programmatically.

I've tried EasyTracker.getTracker().setAppId(id) with no luck.

7条回答
你好瞎i
2楼-- · 2020-07-23 06:06

For changing the id for the EasyTracker, do this:

onStart()

EasyTracker.getInstance(this).set(Fields.TRACKING_ID, "<your_ga_id");
EasyTracker.getInstance(this).activityStart(this);

onStop()

EasyTracker.getInstance(this).activityStop(this);
查看更多
对你真心纯属浪费
3楼-- · 2020-07-23 06:08

i found myInstance.setDefaultTracker(myNewTracker); not working. I did this instead: Set up a common library project. Each apk project gets a analytics.xml in res\values folder with its unique id.

查看更多
Emotional °昔
4楼-- · 2020-07-23 06:13

The accepted answer didn't work for me, as I saw this in my logs:

`W/GAV3﹕ Thread[main,5,main]: Missing tracking id (&tid)`

The solution for analytics v3 is:

EasyTracker tracker = EasyTracker.getInstance(getContext());
tracker.set("&tid", "UA-XXXX-2");

Calling it this way got rid of the error and I can see my events on the analytics page.

查看更多
Viruses.
5楼-- · 2020-07-23 06:15
GoogleAnalytics ga = GoogleAnalytics.getInstance(getActivity());
  tracker = ga.getTracker(getString(R.string.ga_trackingId));
查看更多
beautiful°
6楼-- · 2020-07-23 06:21

I didn't try it myself, but from description in here, you can create a new tracker by

Context mCtx = this; // Get current context.
GoogleAnalytics myInstance = GoogleAnalytics.getInstance(mCtx.getApplicationContext());
Tracker myNewTracker = myInstance.getTracker("UA-XXXX-2") // A new tracking ID.
查看更多
倾城 Initia
7楼-- · 2020-07-23 06:28

You can have two different config xml files and set it like this:

GoogleAnalytics.getInstance(context).newTracker(R.xml.app_tracker);

Note, that you are passing xml file, not the trackingId file:

GoogleAnalytics.getInstance(context).newTracker(R.string.ga_trackingId);

Note the difference xml.app_tracker vs string.ga_trackingId!

Use xml.app_tracker configuration file!

查看更多
登录 后发表回答