在ANDROID应用中初始化Google Analytics的问题

时间:2016-04-01 18:28:58

标签: android google-analytics initialization analytics

我在Android应用中使用Google Analytics时遇到问题。

我正在使用Android Studio生成的默认类

    import android.content.Context;

import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;

import java.util.HashMap;
import java.util.Map;

/**
 * A collection of Google Analytics trackers. Fetch the tracker you need using
 * {@code AnalyticsTrackers.getInstance().get(...)}
 * <p/>
 * This code was generated by Android Studio but can be safely modified by
 * hand at this point.
 * <p/>
 * TODO: Call {@link #initialize(Context)} from an entry point in your app
 * before using this!
 */
public final class AnalyticsTrackers {

  public enum Target {
    APP,
    // Add more trackers here if you need, and update the code in #get(Target) below
  }

  private static AnalyticsTrackers sInstance;

  public static synchronized void initialize(Context context) {
    if (sInstance != null) {
      throw new IllegalStateException("Extra call to initialize analytics trackers");
    }

    sInstance = new AnalyticsTrackers(context);
  }

  public static synchronized AnalyticsTrackers getInstance() {
    if (sInstance == null) {
      throw new IllegalStateException("Call initialize() before getInstance()");
    }

    return sInstance;
  }

  private final Map<Target, Tracker> mTrackers = new HashMap<Target, Tracker>();
  private final Context mContext;

  /**
   * Don't instantiate directly - use {@link #getInstance()} instead.
   */
  private AnalyticsTrackers(Context context) {
    mContext = context.getApplicationContext();
  }

  public synchronized Tracker get(Target target) {
    if (!mTrackers.containsKey(target)) {
      Tracker tracker;
      switch (target) {
        case APP:
          tracker = GoogleAnalytics.getInstance(mContext).newTracker(R.xml.app_tracker);
          tracker.enableAdvertisingIdCollection(true);
          break;
        default:
          throw new IllegalArgumentException("Unhandled analytics target " + target);
      }
      mTrackers.put(target, tracker);
    }

    return mTrackers.get(target);
  }
}

我不知道如何初始化它,如果我每次在设备旋转时在onCreate()的MainActivity中执行它我会得到一个异常“额外调用初始化分析跟踪器”

我该如何解决问题?

1 个答案:

答案 0 :(得分:0)

您可以在Application类上初始化它,只有在应用程序启动时才会调用它。

只需创建一个类,扩展应用程序并在onCreate()上初始化您的分析。

public class VivaApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        AnalyticsTrackers.initialize(this);
    }
}

http://developer.android.com/intl/pt-br/reference/android/app/Application.html

或者将GoogleAnalytics实例保留在Application类本身上,例如v4 docs示例:

/**
 * 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;
     }
 }

https://developers.google.com/analytics/devguides/collection/android/v4/#application