How to programmatically set ga_trackingId property

2020-07-23 06:04发布

问题:

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.

回答1:

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);


回答2:

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.



回答3:

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!



回答4:

GoogleAnalytics ga = GoogleAnalytics.getInstance(getActivity());
  tracker = ga.getTracker(getString(R.string.ga_trackingId));


回答5:

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.


回答6:

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.



回答7:

In values folder create xml file which name is analytics.xml

analytics.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!-- Replace placeholder ID with your tracking ID -->
    <string name="ga_trackingId">UA-37995114-1</string>

    <!-- Enable automatic activity tracking -->
    <bool name="ga_autoActivityTracking">true</bool>

    <!-- Enable automatic exception tracking -->
    <bool name="ga_reportUncaughtExceptions">true</bool>

</resources>

Add new V2 library file(jar file)

link :- https://developers.google.com/analytics/devguides/collection/android/v2/events

I Hop its useful to you.