在Android应用中添加Firebase Analytics和Google Analytics

时间:2016-06-21 05:45:08

标签: android google-analytics firebase google-analytics-api firebase-analytics

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

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

对于Google Analytics,我有跟踪器类

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 analytics对象,那么Fire Base工作正常。

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

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

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的实施

2 个答案:

答案 0 :(得分:8)

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

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

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

步骤3.在json文件中添加以下内容 &#34; analytics_service&#34;:{   &#34;状态&#34;:2,   &#34; analytics_property&#34;:{     &#34; tracking_id&#34;:&#34;&#34;   } },

答案 1 :(得分:0)

看起来很酷,但App ID,我们不需要用于跟踪Google和Firebase Analytics上的活动的应用ID