Tutorialsee Integrating Google Analytics in Android

nalytics is one of the major parts in applications that drive revenue. Mobile App Analytics help you set and track the goal conversions you want most: purchases, clicks, or simply time spent on your app. It is required to make business inclined towards user’s interest. For example, if you developing an application for Mobile Shopper app, it is important to track the below things.

1. Number of users/active users from across the world

2. Most viewed products, categories, brands, etc.

3. Number of users placing request, etc.

 

Integrating GoogleAnalytics setup

step: 1

 

step: 2

 

step: 3

 

step: 4

 

step: 5

1. GoogleAnalyticsApp.java

package com.tutorialsee;

import java.util.HashMap;
import android.app.Application;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
import com.tutorialsee.R;

public class GoogleAnalyticsApp extends Application {

// change the following line
private static final String PROPERTY_ID = "UA-74851064-1";

public static int GENERAL_TRACKER = 0;

public enum TrackerName {
APP_TRACKER, GLOBAL_TRACKER, ECOMMERCE_TRACKER,
}

public HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();

public GoogleAnalyticsApp() {
super();
}

public synchronized Tracker getTracker(TrackerName appTracker) {
if (!mTrackers.containsKey(appTracker)) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
Tracker t = (appTracker == TrackerName.APP_TRACKER) ? analytics.newTracker(PROPERTY_ID) : (appTracker == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(R.xml.global_tracker) : analytics.newTracker(R.xml.ecommerce_tracker);
mTrackers.put(appTracker, t);
}
return mTrackers.get(appTracker);
}
}

2. GoogleAnalyticsExample.java

package com.tutorialsee;

import android.app.Activity;
import android.os.Bundle;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.tutorialsee.R;
import com.tutorialsee.GoogleAnalyticsApp.TrackerName;

public class GoogleAnalyticsExample extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Tracker t = ((GoogleAnalyticsApp) getApplication()).getTracker(TrackerName.APP_TRACKER);
t.setScreenName("Home");
t.send(new HitBuilders.AppViewBuilder().build());
}

@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
GoogleAnalytics.getInstance(GoogleAnalyticsExample.this).reportActivityStart(this);
}


@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
GoogleAnalytics.getInstance(GoogleAnalyticsExample.this).reportActivityStop(this);
}
}

3. activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.tutorialsee.GoogleAnalyticsExample" />

4. fragment_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#ffffff"
  
    tools:context="com.tutorialsee.GoogleAnalyticsExample$PlaceholderFragment" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_world"
        android:textColor="#000000"
           android:gravity="center"
        android:textSize="20sp"
       />

</RelativeLayout>

5. AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tutorialsee"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="20" />

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

    <application
        android:name="com.tutorialsee.GoogleAnalyticsApp"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="com.tutorialsee.GoogleAnalyticsExample"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

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

</manifest>