Test Script

Monday, April 15, 2013

Google Analytics Integration



Hi, so as  promised I am  starting to write a few tutorials !

In this tutorial I am going to teach you how to integrate the latest version of Google Analytics (v2) into your Android app.

Before i begin, let me tell you why your app should have Google Analytics in it.

Google analytics is a great tool for generating reports on your app's performance after users have started using it. Some features of Google Analytics are :


  • Number of active users using their app
  • Demographics of users
  • Adoption and usage of specific features
  • In-app purchases and transactions
  • Number and type of application crashes




So let 's get started on integrating these features in your application .

I will be using two classes to show you how to get this working
a) Main.java
b) Second.java
================================================================

Pre Requisites needed  before we integrate Google Analytics :

1. Create a new Google Analytics Account -->  Create Account

2. Add a new mobile app in your account(This is the app which you want to track)

3. After you add a app in your account,google provides you a tracking Id unique for that app. Note down the tracking ID , it is usually in  UA-XXXX-Y format

4. Download the Google Analytics jar file --->Jar Download Link


==================================================================

Update Manifest File :


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

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



===================================================================

Create a new analytics.xml File :

Create a new xml file in the res/values directory.

XML File

Replace UA-XXXX-Y with the tracking Id you received.

Here we set ga_autoActivityTracking & ga_reportUncaughtExceptions true so that you can monitor your Activities & Exceptions.

Also i have added the following lines so as to give my screens/activities a desired name when it shows up in reports .

 name="com.example.testapp.Main">Main
 name="com.example.testapp.Second">Second


Here name is the complete name of the activity including the package. You have to add all the screens you want to monitor & name them as per desire.


==============================================================
Main.java
==============================================================



package com.example.testapp;

import com.google.analytics.tracking.android.EasyTracker;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
Button b1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1=(Button) findViewById(R.id.button1);
        b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);
}
});
    }


    @Override
    public void onStart() {
      super.onStart();
       // The rest of your onStart() code.
      EasyTracker.getInstance().activityStart(this); // Add this method.
    }
    
    @Override
    public void onStop() {
      super.onStop();
      // The rest of your onStop() code.
      EasyTracker.getInstance().activityStop(this); // Add this method.
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

=================================================================

So in the above class , i did the following things :

1) Imported Google Analytics package 
2) Added the method  EasyTracker.getInstance().activityStart(this) to start tracking this activity/screen.
3) Added the method  EasyTracker.getInstance().activityStop(this) to stop tracking this activity/screen.
4) Also there is a button which on click opens up Second.java


==============================================================
Second.java
==============================================================

package com.example.testapp;

import com.google.analytics.tracking.android.EasyTracker;

import android.app.Activity;
import android.os.Bundle;

public class Second extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sec);
}
  @Override
    public void onStart() {
      super.onStart();
      // The rest of your onStart() code.
      EasyTracker.getInstance().activityStart(this); // Add this method.
    }
   
    @Override
    public void onStop() {
      super.onStop();
      // The rest of your onStop() code.
      EasyTracker.getInstance().activityStop(this); // Add this method.
    }
}

=================================================================

In this class as you can see,i did the exact same things as in Main.java. So you will need to override the onStart & onStop methods of all activities you want to monitor .

That's it ! Pretty simple right? Now once you have done this you can go the dashboard of your Google Analytics account & track the reports.

Note : Google Analytics also provides a feature to measure and track events,i will try to make a new post about it soon. I will be showing how you can track button clicks and other events.

If you need any help or have any request feel free to drop me a comment below !

2 comments:

  1. Great tutorial, perfectly described and easy to understand. Please add the event tracking turorial very soon.

    Many Thanks
    Lucy
    x

    ReplyDelete
  2. Thank you. Simple and superb tutorial..

    ReplyDelete

UA-42774700-1 Twitter Bird Gadget