Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Firebase Analytics and Google Analytics in Android App

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

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

For Google Analytics, I have the Tracker Class

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.

"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

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

like image 413
iappmaker Avatar asked Jun 21 '16 05:06

iappmaker


1 Answers

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

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

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

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

like image 131
iappmaker Avatar answered Oct 01 '22 17:10

iappmaker