可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am reading a new google tutorial for Android analytics and it's strange, first of all when I add this strings in my gradle:
classpath 'com.google.gms:google-services:1.3.0-beta1' apply plugin: 'com.google.gms.google-services'
I can't sync my project (plugin not found error). I don't know is it important or not. I only can add compile 'com.google.android.gms:play-services-analytics:7.3.0'
. And second, in tutorial there is a step where I should subclass Application:
package com.google.samples.quickstart.analytics; import android.app.Application; import com.google.android.gms.analytics.GoogleAnalytics; import com.google.android.gms.analytics.Logger; import com.google.android.gms.analytics.Tracker; /** * This is a subclass of {@link Application} used to provide shared objects for this app, such as * the {@link Tracker}. */ public class AnalyticsApplication extends Application { private Tracker mTracker; /** * Gets the default {@link Tracker} for this {@link Application}. * @return tracker */ synchronized public Tracker getDefaultTracker() { if (mTracker == null) { GoogleAnalytics analytics = GoogleAnalytics.getInstance(this); // To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG mTracker = analytics.newTracker(R.xml.global_tracker); } return mTracker; } }
And I can't understand where to get R.xml.global_tracker file?? I thought that I will use json file, that I generated before. Have some1 faced this problem? It is funny, but for iOs tutorial was better.
Update
This approach has better documentation by the way, if some1 is interesting.
回答1:
I agree with you. New documentation is not so helpful.
Here is my Application class and all you need is that, you don't need any other thing for basic integration. Even not need a xml. Use this tracker object where you want.
import android.app.Activity; import android.content.Context; import com.google.android.gms.analytics.GoogleAnalytics; import com.google.android.gms.analytics.Tracker; import java.io.IOException; public class Application extends android.app.Application { public static GoogleAnalytics analytics; public static Tracker tracker; @Override public void onCreate() { super.onCreate(); analytics = GoogleAnalytics.getInstance(this); analytics.setLocalDispatchPeriod(1800); tracker = analytics.newTracker("UA-XXXXXX-X"); tracker.enableExceptionReporting(true); tracker.enableAdvertisingIdCollection(true); tracker.enableAutoActivityTracking(true); } }
Also keep 'com.google.android.gms:play-services-analytics:7.3.0' dependency in your "build.gradle".
Edit: i think my answer is not valid anymore. Don't force and just use json file :)
回答2:
With V4 of Google Analytics, Google has implemented various changes in the approach. They generate a google-services.json
file which you download and add to your project; here are the definitive guides on the various steps https://developers.google.com/analytics/devguides/collection/android/v4/start and https://developers.google.com/analytics/devguides/collection/android/v4/
However, like the rest of the people here, when I used the sample code for the Application Object from Google there was a compilation error for R.xml.global_tracker
. In Android Studio I decided to 'clean' the project and that did the trick. However the magic works, the xml object is now generated as part of R.java
For reference: I'm running Android Studio 2.2.3 on OSX. Presumably the fix should work on future instances and other OS's until Google change their practices again.