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

时间:2021-07-02 15:17:33

I have implemented Firebase Analytics. It is working fine. I wish to use the Google Analytics as well. Based on the discussion here I have included the below object in the google-services.json

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

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

For Google Analytics, I have the Tracker Class

对于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;
}

}

}

Usage

用法

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);

}

Error Scenario

错误场景

If I did not include the below google analytics object in google-services.json, then the Fire Base is working properly.

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

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

When I include google analytics object in google-services.json, then i am getting this below error

当我在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

Question 1. Do I need to remove the ecommerce_tracker.xml and global_tracker.xml ? Already PuzzleGamesTracker class is using it . How to overcome it ? 2. Please guide me so that I could have both FA and GA implementation successful

问题1.我是否需要删除ecommerce_tracker.xml和global_tracker.xml? PuzzleGamesTracker类已经在使用它了。怎么克服它? 2.请指导我,以便我可以同时实施FA和GA

2 个解决方案

#1


8  

Finally, I found an answer for adding both GA and FA

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

Step 1. Removed the xml files R.xml.global_tracker

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

Step 2. Replaced mTracker = analytics.newTracker(R.xml.global_tracker); with mTracker = analytics.newTracker(PROPERTY_ID);

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

Step 3. Add the following in json file "analytics_service": { "status": 2, "analytics_property": { "tracking_id": "" } },

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

#2


0  

Looks cool, but what about App id, Don't we need app id for tracking events on both Google and Firebase Analytics

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

#1


8  

Finally, I found an answer for adding both GA and FA

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

Step 1. Removed the xml files R.xml.global_tracker

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

Step 2. Replaced mTracker = analytics.newTracker(R.xml.global_tracker); with mTracker = analytics.newTracker(PROPERTY_ID);

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

Step 3. Add the following in json file "analytics_service": { "status": 2, "analytics_property": { "tracking_id": "" } },

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

#2


0  

Looks cool, but what about App id, Don't we need app id for tracking events on both Google and Firebase Analytics

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