9

我已经实施了 Firebase 分析。它工作正常。我也希望使用 Google Analytics。根据这里的讨论,我在 google-services.json 中包含了以下对象

"analytics_service": {
      "status": 2,
      "analytics_property": {
        "tracking_id": "<your tracking id>"
      }
    },

对于 Google Analytics,我有 Tracker 类

public class PuzzleGamesTracker extends Application {

// The following line should be changed to include the correct property id.
private static final String PROPERTY_ID = "<your tracking id>";

private Tracker mTracker;


@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}

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

}

用法

public void sendToGoogleAnalytics(String arg) {
    // TODO Auto-generated method stub
    sendToFireBase();
    PuzzleGamesTracker application = (PuzzleGamesTracker) getApplication();
    Tracker mTracker = application.getDefaultTracker();

    mTracker.setScreenName(arg);
    mTracker.send(new HitBuilders.AppViewBuilder().build());

}

public void sendToFireBase( ) {
    Bundle bundle = new Bundle();
    bundle.putString(FirebaseAnalytics.Param.ITEM_ID, "Test_Item 1A");
    bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, "Test_Name 1A");
    bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, "Test_Image 1");
    mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);

    bundle.putString(FirebaseAnalytics.Param.ITEM_ID, "Test_Item B");
    bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, "Test_Name B");
    bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, "Test_Image 1");
    mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);

    bundle.putString(FirebaseAnalytics.Param.ITEM_ID, "Test_Item 2A");
    bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, "Test_Name 2A");
    bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, "Test_Image 2");
    mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);

}

错误场景

如果我没有在 google-services.json 中包含以下 google 分析对象,则 Fire Base 工作正常。

"analytics_service": {
  "status": 2,
  "analytics_property": {
    "tracking_id": "<your tracking id>"
  }
},

当我在 google-services.json 中包含 google 分析对象时,我收到以下错误

Error:Execution failed for task ':android:mergeReleaseResources'.
> [xml/global_tracker] C:\Users\android\build\generated\res\google-   services\release\xml\global_tracker.xml   [xml/global_tracker] C:\Users\android\res\xml\global_tracker.xml: Error: Duplicate resources

问题 1. 我需要删除 ecommerce_tracker.xml 和 global_tracker.xml 吗?PuzzleGamesTracker 类已经在使用它。如何克服它?2.请指导我,以便我可以成功实现 FA 和 GA

4

2 回答 2

8

最后,我找到了添加 GA 和 FA 的答案

步骤 1. 删除 xml 文件 R.xml.global_tracker

步骤 2. 替换 mTracker = analytics.newTracker(R.xml.global_tracker); 使用 mTracker = analytics.newTracker(PROPERTY_ID);

步骤 3. 在 json 文件 "analytics_service" 中添加以下内容:{ "status": 2, "analytics_property": { "tracking_id": "" } },

于 2016-06-22T01:49:21.423 回答
0

看起来很酷,但是 App id 呢,我们不需要应用 id 来跟踪 Google 和 Firebase Analytics 上的事件吗

于 2016-06-29T09:23:35.627 回答