Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Analytics Tracker file in Android library module

I am trying to split my Google Analytics code into a separate module (package com.abc) from my main app (package com.xyz). I am facing these issues :

  1. Should I generate tracker for app module, or library module ?
  2. Where should I put this tracker - app or library module ?
  3. My goal is to have all Analytics creation and handling logic in the library module through some interfaces. I want to use this library for my other projects too; so I don't want to put tracker files in the library module itself. Is there a way I can initialize the tracker in the library module, but keep the XML file in app module ?

What I have tried (and failed) :

  1. I have generated tracker files for both com.abc and com.xyz
  2. I have tried all permutations of both files in both modules; but the library module is unable to resolve R.xml.global_tracker. Being new to Google Analytics I don't know if I have to copy/paste any tracker file. Before I made the library module, R.xml.global_tracker was automatically resolved in the main module.
like image 529
dev Avatar asked Feb 13 '16 09:02

dev


People also ask

How do I integrate Google Analytics into an android app?

Go to Analytics and select the Analytics account you registered the app with. If you're unsure, look for the Analytics tracking ID in the google-services. json file you added to your project earlier. Copy that ID to the Analytics account search in the report drop-down.

What is Android library module?

An Android library is structurally the same as an Android app module. It can include everything needed to build an app, including source code, resource files, and an Android manifest.

What is Google Analytics in Android?

Google Analytics collects usage and behavior data for your web app. The SDK logs two primary types of information: Events: What is happening in your app, such as user actions, system events, or errors.


1 Answers

Instead of using R.xml.global_tracker.You can use this code in your Application class.

/**
 * @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
        analytics.setLocalDispatchPeriod(1800);
        mTracker = analytics.newTracker("Tracker code");
        analytics.setLocalDispatchPeriod(1);
        mTracker.enableExceptionReporting(true);
        mTracker.enableAdvertisingIdCollection(true);
        mTracker.enableAutoActivityTracking(false);
    }
    return mTracker;
}

And use it in the activity/fragment like this:

    MyApplication application = (MyApplication ) context.getApplication();
    Tracker mTracker = application.getDefaultTracker();
    mTracker.setScreenName(name);
    mTracker.send(new HitBuilders.ScreenViewBuilder().build());

This might resolve your problem, not tested for your case though.

like image 171
Muhammad Umair Shafique Avatar answered Oct 12 '22 00:10

Muhammad Umair Shafique