Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to integrate google analytics in Android Application

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 301
    Comment on it

    Are you interested in tracking your Android app activities? If yes, Google Analytics provides a service for which real time tracking of Activities, fragments, events, exceptions and crash reports can be done easily.

    Steps to integrate Google Analytics:-

    Step1:- Sign-in to Google Analytics Account
    Select tracking to mobile app, add account name, App name, category of app, country wise time zone and select Get tracking id. You will get a tracking-id and save it as seperate.

    Step2:- Apply plugin and under build.gradel add google analytics dependency and synchronize the app

    dependencies {
    
      compile 'com.google.android.gms:play-services-analytics:8.4.0'
    }

    Step3:- Permissions needed:

     <uses-permission android:name="android.permission.INTERNET" />
      <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    Step4:- Create folder xml inside res directory, now create a file analytics_tracker.xml and put below code inside it. The tracking id we get after sign-in to the google analytics account have to place inside the tag ga_trackingId.

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
      <!-- End current session if app sleeps for_image a period of time -->
      <integer name="ga_sessionTimeout">300</integer>
    
      <!-- Enable automatic Activity measurement -->
      <bool name="ga_autoActivityTracking">true</bool>
    
      <!-- The property id associated with this analytics tracker -->
      <string name="ga_trackingId">UA-XXXXXXXX-X</string>
    
      <string name="ga_sampleFrequency">100.0</string>
    
      <bool name="ga_reportUncaughtExceptions">true</bool>
    
    </resources>

    Step5:- Create AnalyticsTracker class in your utility package, initialize the tacker before getInstance().

    public final class AnalyticsTracker {
    
      public enum Target {
        APP
      }
    
      private static AnalyticsTracker sInstance;
    
      public static synchronized void initialize(Context context) {
        if (sInstance != null) {
          throw new IllegalStateException("Extra call to initialize analytics trackers");
        }
    
        sInstance = new AnalyticsTracker(context);
      }
    
      public static synchronized AnalyticsTracker getInstance() {
        if (sInstance == null) {
          throw new IllegalStateException("Call initialize() before getInstance()");
        }
    
        return sInstance;
      }
    
      private final Map<Target, Tracker> mTrackers = new HashMap<Target, Tracker>();
      private final Context mContext;
    
      // creating the instance of class
      private AnalyticsTracker(Context context) {
        mContext = context.getApplicationContext();
      }
    
      public synchronized Tracker get(Target target) {
        if (!mTrackers.containsKey(target)) {
          Tracker tracker;
          switch (target) {
            case APP:
              tracker = GoogleAnalytics.getInstance(mContext).newTracker(R.xml.analytics_tracker);
              break;
            default:
              throw new IllegalArgumentException("Unhandled analytics target " + target);
          }
          mTrackers.put(target, tracker);
        }
    
        return mTrackers.get(target);
      }
    }


    Step6:- Call below methods to initialize the tracker in your application class.

    AnalyticsTrackers.initialize(this);
    AnalyticsTrackers.getInstance().get(AnalyticsTrackers.Target.APP);

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: