Tutorials Android Integrating Google Analytics in Android Example

Integrating Google Analytics in Android Example

1. Why Analytics in Mobile app ?

Analytics 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.

The above information is required in order to improve the product and make the service is better.

2. Introduction to Google Analytics

The Google Analytics Platform lets you measure user interactions with your business across various devices and environments. The platform provides all the computing resources to collect, store, process, and report on these user-interactions.

3. Supported Features

Below are the currently supported features. Read more

http://www.google.co.in/analytics/mobile/

  1. Google Play Integration
  2. Crash and Exception Reporting
  3. Custom Reports
  4. Event Tracking
  5. Flow Visualization
  6. Real-Time Reporting

4. How to use Google Analytics in android

This section of tutorial we will see detailed steps to install Google Analytics for your android application. Before begin with Analytics integration we need the following items

  1. Android developer SDK and required configurations. I assume you have it already, if not you can follow Android official documentation http://developer.android.com/sdk/index.html
  2. Google Analytics SDK for Android v3, with libGoogleAnalyticsServices.jar file. Copy this file in your project’s /libs directory.
  3. Codebase of an Android app that you like to implement the Google Analytics
  4. Create a Google Analytics account (Requires Gmail), and then create a new app property and view in the Analytics console.

5. Using Google Analytics Detailed Steps

Before starting with section, I assume you already have Codebase of an Android app that you like to implement the Google Analytics. But to simplify my example, I am creating an simple Android App and integrating with Google Analytics.

5.1. Creating Google Analytics Account and App Property

  1. Login to your Google Analytics Account. Then press “Admin” button on the right side of your screen.
  2. Create a new Account, if you don’t have one and then create a new property. While creating a new property choose Mobile App.
  3. After property is created, you will get the tracking Id. This tracking id will be used as configuration in android app.

5.2. Add analytics.xml configuration file

Add analytics.xml configuration file in your application values folder

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!-- Replace placeholder ID with your tracking ID -->
    <string name="ga_trackingId">UA-xxxxxxxxxx</string>

    <!-- Enable automatic activity tracking -->
    <bool name="ga_autoActivityTracking">true</bool>

    <!-- Enable automatic exception tracking -->
    <bool name="ga_reportUncaughtExceptions">true</bool>    

</resources>

 

5.3. Android Activity Layout (activity_main.xml)

For this example, I have created an sample Activity with two buttons for tracking event and exceptions.

<LinearLayout 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:orientation="vertical"
    android:padding="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView        
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/info" />

    <Button
        android:id="@+id/trackEvent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Track Event" />

    <Button
        android:id="@+id/trackCrash"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Track Crash" />

</LinearLayout>

5.4. Android Activity (MainActivity.java)

Now, you need to write the tracking code in your activity class. For that simply add the activiyStart() and activityStop() methods to the onStart() and onStop() methods of each of your Activities as in the following example. This two method enables basic activity tracking.

package com.javatechig.ganalytics;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.MapBuilder;
import com.google.analytics.tracking.android.StandardExceptionParser;

public class MainActivity extends Activity {

	private EasyTracker easyTracker = null;

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

		easyTracker = EasyTracker.getInstance(MainActivity.this);

		findViewById(R.id.trackEvent).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				easyTracker.send(MapBuilder.createEvent("your_action",
						"envet_name", "button_name/id", null).build());
			}
		});

		findViewById(R.id.trackCrash).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

				try {
					int a[] = new int[2];
					int num = a[4];
				} catch (ArrayIndexOutOfBoundsException e) {
					easyTracker.send(MapBuilder.createException(
									new StandardExceptionParser(MainActivity.this, null)
											.getDescription(Thread.currentThread().getName(), e), false).build());
				}

			}
		});
	}

	@Override
	public void onStart() {
		super.onStart();
		EasyTracker.getInstance(this).activityStart(this);
	}

	@Override
	public void onStop() {
		super.onStop();
		EasyTracker.getInstance(this).activityStop(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

In this above activity code sample, we are tracking event an crashes on two button clicks. Just for demonstation purpose I am creating sample exception and tracking it.

6. Google Analytics Output

Google Analytics Report

7. Download Complete Example

[download url=”https://github.com/javatechig/javatechig-android-advanced/tree/master/GoogleAnalyticsDemo”]
Total
0
Shares
guest
19 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Deepak
Deepak
8 years ago

great its very simple and understandable ,,,,,,,Gracias…..
Nilanchala

Pravinsingh_Waghela
Pravinsingh_Waghela
8 years ago

Well I tried what you mentioned above, but I can’t see any users details on my Google Analytic Account. Am I missing anything?

Nilanchala Panigrahy
Nilanchala Panigrahy
8 years ago

are you using your correct tracking id? that matters.

Pravinsingh_Waghela
Pravinsingh_Waghela
8 years ago

Thanks mate, its now working…Thanks to you…

Pravinsingh_Waghela
Pravinsingh_Waghela
8 years ago

well Now its working. actually it reflects after some 24 hours…

Alberto
Alberto
8 years ago

Thanks! You literally saved my life from the disaster!!

Sravani Ch
Sravani Ch
8 years ago

I have added start and stop methods to all of my activities in my application but it is tracking only one activity. why?
easy tracker tracks only one activity per application . Any one please reply

Nilanchala Panigrahy
Nilanchala Panigrahy
8 years ago
Reply to  Sravani Ch

EasyTracker

should list all activities. Sometimes it may take a while. Please check back and see if you still experience the issue.

Sravani Ch
Sravani Ch
8 years ago

Ok. I have checked multiple times if i am in one activity which is tracking it is showing Right Now value is “1” and it is showing complete class name of that activity under the “Top activity Screens” but if i stay in other activities for 15 min also its not tracking

Ramesh
Ramesh
8 years ago

Hi all..
I need to know the origin of request to download my app from https://play.google.com

like my app was advertised in so many apps, just like as ads,
when the user click on that add and redirected to https://play.google.com and download will started.

Now i would like get that source(app) name from where the user find my app.

SK_DROID
SK_DROID
8 years ago

Great Tutorial ..!!! +1 For your clean steps. 🙂 The Analytics Works well, Can you Please tell us how exactly Tracker-id works for EasyTracker.?? Where can we get detailed Info. about different properties and classes for Google Analytics. Any link for detailed study ????

Thanks and continue your great contribution to Droid 😉

May Mg
May Mg
8 years ago

what data set in easytracker.send method.Pls tell me

May Mg
May Mg
8 years ago

my app is not affected.

javatechig
javatechig
8 years ago
Reply to  May Mg

Can you verify wether the GA id is the right one ?

Hasamm
Hasamm
8 years ago

Fantastic tutorial. Thanks.

Rajiv
Rajiv
7 years ago

Is there any possibility to implement the same tracking to Android APK which is not Placed in google play store..? If yes please guide how to…

Thanks in Advance!

باسل
باسل
7 years ago

Is the any tutorial (uniform way) to add it on IBM worklight apps

Nilanchala Panigrahy
Nilanchala Panigrahy
7 years ago
Reply to  باسل

Sorry we don’t have any tutorial for now

Jp Kansara
Jp Kansara
7 years ago

Hey I didn’t find your code work. I didn’t found any user in the console as well. Can you please tell me when you are using tracking_id? I don’t think without it the analytic works.

Poor tutorial I didn’t find any helpful thing in it.

Previous Post
Android Tutorials

Android Handler Example

Next Post
Android Tutorials

Android ImageView Example

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
19
0
Would love your thoughts, please comment.x
()
x