Implementing Google Analytics in your Android app – Track User Activities.

By | June 5, 2016

Implementing Google Analytics can help you find out how users are using your app. Which page is being visited regularly.You can also Know about the crashes happening in your app.
How many users are currently using your app in real time and which page is currently being used more.

Lets see how we can implement this.

To start

  1. Sign in to analytics.google.com
  2. Go to Admin tab.
  3. Click “Create new property”
  4. Select “Mobile App”.
  5. Give your AppName and other details.
  6. Click “Get Tracking ID”
  7. Save your Tracking ID to use in our app.

Check the Screenshot below

Google Analytics Android

Infolinks

UPDATE YOUR GRADLE

Add this to your Project build.gradle dependencies

	classpath 'com.google.gms:google-services:3.0.0'

Add this in your app build.gradle dependencies

 	compile 'com.google.android.gms:play-services-analytics:9.0.0'

UPDATE YOUR MANIFEST

Add this in your Manifest.xml

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

Add this inside the Application tag.


	<!--
	Optionally, register AnalyticsReceiver and AnalyticsService to support background
	dispatching on non-Google Play devices
	-->
	<receiver 
		android:name="com.google.android.gms.analytics.AnalyticsReceiver"
		android:enabled="true">
	        <intent-filter>
			<action 	android:name="com.google.android.gms.analytics.ANALYTICS_DISPATCH" />
		</intent-filter>
	</receiver>

	<service 
		android:name="com.google.android.gms.analytics.AnalyticsService"
		android:enabled="true"
		android:exported="false" />

	<!--
	 Optionally, register CampaignTrackingReceiver and CampaignTrackingService to enable
	 installation campaign reporting
	-->
	<receiver 
		android:name="com.google.android.gms.analytics.CampaignTrackingReceiver"
		android:exported="true">
		<intent-filter>
			<action android:name="com.android.vending.INSTALL_REFERRER" />
		</intent-filter>
	</receiver>

	<service android:name="com.google.android.gms.analytics.CampaignTrackingService" />

	<meta-data 
		android:name="com.google.android.gms.version"
		android:value="@integer/google_play_services_version" />

Implement the analytics

Now create a Class that extends the Application Class or if you have one, you can update it like this.


import android.app.Application;

import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;

public class AnalyticsApplication extends Application {

    public static GoogleAnalytics analytics;
    public static Tracker tracker;

    @Override
    public void onCreate() {
        super.onCreate();
        initializeAnalytics();
    }

    void initializeAnalytics(){
        analytics = GoogleAnalytics.getInstance(this);
        analytics.setLocalDispatchPeriod(100);
        tracker = analytics.newTracker(getString(R.string.google_analytics_id));
        tracker.enableExceptionReporting(true);
        tracker.enableAdvertisingIdCollection(true);
        tracker.enableAutoActivityTracking(true);
    }
    
     synchronized public Tracker getDefaultTracker() {
        if (tracker == null) {
            initializeAnalytics();
        }
        return tracker;
    }

}

Update the Manifest like this.

<application
        android:name=".AnalyticsApplication"
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
</application>

Infolinks

Track Activity in a Page

Now if you want to track the activity on a page..add this


	String screenName = "<REPLACE WITH YOUR SCREEN NAME>";
	AnalyticsApplication application = (AnalyticsApplication) getApplication();
	Tracker mTracker = application.getDefaultTracker(); 
	mTracker.setScreenName(screenName);
	mTracker.send(new HitBuilders.ScreenViewBuilder().build());


If you are using tabs, you could add this in your “onTabSelected” or “onPageSelected”.

ALL DONE.

Now you can run the app and check your Google Analytics console after 10 minutes.

Google Analytics Android

Leave a Reply

Your email address will not be published. Required fields are marked *