Just to help others, Google support is a lot unclear in this piece of code:
Enable advertising features
// Get tracker.
Tracker t = ((AnalyticsSampleApp) getActivity().getApplication()).getTracker( TrackerName.APP_TRACKER);
// Enable advertising features.
t.enableAdvertisingIdCollection(true);
A tracker contains a tracker initialized in your Application class, and the tracking function is a function in which you must create a list of trackers and get them by name. If you use only one tracker, you can do:
public static Tracker tracker; @Override public void onCreate() { super.onCreate(); startGoogleAnalytics(); } public void startGoogleAnalytics() { analytics = GoogleAnalytics.getInstance(this); analytics.setLocalDispatchPeriod(1800); tracker = analytics.newTracker(R.xml.global_tracker); // here you can add just your id value too tracker.enableExceptionReporting(true); tracker.enableAdvertisingIdCollection(true); tracker.enableAutoActivityTracking(true); } public synchronized Tracker getTracker() { if (tracker == null) { startGoogleAnalytics(); } return tracker; }
Then you can do the following:
Tracker t = ((YouApplicationClassName)getApplication()).getTracker(); t.enableAdvertisingIdCollection(true);
sagits
source share