Search This Blog

Tuesday, December 13, 2011

Google Analytics

To start with Google Analytic, We must have have an Google account & We need to get a unique key that will be used in code to trace events. Follow below mentioned process to achieve it.

At Web side (Google):

1. Create an account on http://www.google.com/analytics/

2. Select Setting button & add a new Account

3. Provide Account name, URL, Time Zone & Press "Create Account"

4. Now you will get a "Web Property ID"; this key we need to use at code level.

5. Now download the required lib from below path

http://code.google.com/apis/analytics/docs/mobile/download.html

At code side:

Set Environment (for Eclipse):

1. Get libGoogleAnalytics.jar

2. Go to Properties - > Java Build Path & select Add external jar.

3. Provide the path of libGoogleAnalytics.jar & add it.

Changes in Code:

1. Add permission in AndroidManifest.xml manifest file.

2. Start session in onCreate & End Session in onDestroy

public void onCreate()

{

super.onCreate(savedInstanceState);

tracker = AnalyticsTracker.createInstance();

tracker.start("<GOOGLE ANALYTICS ACCOUNT ID>", this);

}

public void onDestroy()

{

super.onDestroy();

// manually dispatch tracker

tracker.dispatch();

// Stop tracker.

tracker.stop();

}

Your application is linked to google account & it's ready to track page views and event. To do the same, use its API set. Basic use of them is shown below

To Trace Events:

tracker.trackEvent( Category, Action, Label, Value);

e.g. tracker.trackEvent("Category Name", "Action Name", "", 0);

To Trace Page Views:

tracker.trackPageView("");

Useful Link:

http://code.google.com/apis/analytics/docs/mobile/android.html