如何在从旧版Eclipse ADT导入的Android Studion中的项目上实施Google Analytics v4

时间:2015-05-12 05:45:24

标签: android google-analytics

我的遗留应用程序项目没有Application类。 因此,official document无济于事 我不知道下面的代码应该去哪里:

/**
 * Enum used to identify the tracker that needs to be used for tracking.
 *
 * A single tracker is usually enough for most purposes. In case you do need multiple trackers,
 * storing them all in Application object helps ensure that they are created only once per
 * application instance.
 */
public enum TrackerName {
  APP_TRACKER, // Tracker used only in this app.
  GLOBAL_TRACKER, // Tracker used by all the apps from a company. eg: roll-up tracking.
  ECOMMERCE_TRACKER, // Tracker used by all ecommerce transactions from a company.
}

HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();

这些代码:

synchronized Tracker getTracker(TrackerName trackerId) {
  if (!mTrackers.containsKey(trackerId)) {

    GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
    Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(PROPERTY_ID)
        : (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(R.xml.global_tracker)
            : analytics.newTracker(R.xml.ecommerce_tracker);
    mTrackers.put(trackerId, t);

  }
  return mTrackers.get(trackerId);
}

我已经导入了googleAnalytics的这些库。

enter image description here

但我仍然无法导入像this way这样的库:

import com.google.android.apps.analytics.GoogleAnalyticsTracker;

而且,因为我使用的是Android Studio 1.2 Beta3,the tutorial based on Eclipse也不适合我的情况。

有没有简单的方法在Android应用上实施Google Analytics?就像在iOS中初始化一样?

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [GAI sharedInstance].trackUncaughtExceptions = YES;
  [GAI sharedInstance].dispatchInterval = 20;
  [[[GAI sharedInstance] logger] setLogLevel:kGAILogLevelVerbose];
  [[GAI sharedInstance] trackerWithTrackingId:@"UA-XXXX-Y"];
}

1 个答案:

答案 0 :(得分:1)

在Android上简单完整地实施Google Analytics需要:

  1. 链接到Google Play服务(看起来您已经完成了这项工作)
  2. 在您的应用清单中添加required network permissions
  3. Extend Android Application classoverride onCreate并设置您的app class implementation in the <android name='you_app_class'> name属性。
  4. 在onCreate create new tracker中并将其保存在静态var tracker = GoogleAnalytics.getInstance(context).newTracker();启用screens, exceptions等的自动报告
  5. 在您的Application Manifest中注册AnalyticsReceiver & AnalyticsService
  6. Google Analytics有一个最简单的Analytics hello world all github项目。它显示了在Android上正确报告所需的最低限度。