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.
- Number of users/active users from across the world
- Most viewed products, categories, brands, etc.
- 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/
- Google Play Integration
- Crash and Exception Reporting
- Custom Reports
- Event Tracking
- Flow Visualization
- 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
- 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
- Google Analytics SDK for Android v3, with
libGoogleAnalyticsServices.jar
file. Copy this file in your project’s /libs directory. - Codebase of an Android app that you like to implement the Google Analytics
- 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
- Login to your Google Analytics Account. Then press “Admin” button on the right side of your screen.
- Create a new Account, if you don’t have one and then create a new property. While creating a new property choose Mobile App.
- 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.
great its very simple and understandable ,,,,,,,Gracias…..
Nilanchala
Well I tried what you mentioned above, but I can’t see any users details on my Google Analytic Account. Am I missing anything?
are you using your correct tracking id? that matters.
Thanks mate, its now working…Thanks to you…
well Now its working. actually it reflects after some 24 hours…
Thanks! You literally saved my life from the disaster!!
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
EasyTracker
should list all activities. Sometimes it may take a while. Please check back and see if you still experience the issue.
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
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.
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 😉
what data set in easytracker.send method.Pls tell me
my app is not affected.
Can you verify wether the GA id is the right one ?
Fantastic tutorial. Thanks.
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!
Is the any tutorial (uniform way) to add it on IBM worklight apps
Sorry we don’t have any tutorial for now
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.