How to use Google Analytics API V4 in Android apps

With Google Analytic s  you can track information including the number of user use your Apps.

So you need to add “gloogle analytic traker id” in your apps. To accomplish these tasks, complete flowing working procedure:


Step-1: Create or signing Google Analytic and collect mobile Tracking ID:

You click this link “Google Analytics” ,show this windows:

9-23-2014 3-07-21 PM

Click “Access Google Analytics, show this windows:

9-23-2014 3-09-46 PM

After Sing Up,click on Mobile app

9-23-2014 3-11-12 PM

Fill-up necessary information  and click on “Get Trackig ID

9-23-2014 3-14-02 PM

Click on “I Accept” and show this windows:

9-23-2014 3-16-46 PM

step-2:Create a sample project:

Create a project using your IDE …………

step-3: Adding Google Play Services Library to Your Android App:


To develop an app using the Google Play services APIs, you must add Google Play services Library to your app. Google Play services can be downloaded from the SDK Manager. Once it is downloaded we will import it to our App.



( 1 ) Download Google Play Services SDK

  • Lunch Android SDK Manager
    • Click on icondownload or
    • Go to (ADT/Eclipse) the menu bar Window >> Android SDK Manager
  • In the SDK Manager window
    • Scroll down and expand Extras folder
    • Select Google Play services
    • Click Install Package

download (1)

( 2 ) Import the Library into Workspace

Now you need to import Google Play services library into your app current workspace.

File >> Import, select Android > Existing Android Code into Workspace, and browse to the copy of the library project to import it. The Google Play services SDK is saved in your Android SDK environment at <android-sdk>/extras/google/google_play_services/.

download (2)

Now, you should be able to see the google-play-servies-lib project imported into your workspace.

download (3)

( 3 ) Add Google Play services to Your App

  • Right-click your App project
  • Select Properties
  • In the Properties window select Android
  • Under the Library section click Add
  • Select google-play-services-lib
  • Click OK

download (4)

Step-4: Google Analytics API  V4 Add in Your App:

(1). Create a configuration XML file

Create the following app_tracker.xml,ecommerce_tracker.xml,global_tracker.xml config file and add it to your android application’s res/xml folder


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

<!-- Replace placeholder ID with your tracking ID -->
<string name="ga_trackingId">XX-XXXXXXXX-X</string>

<bool name="ga_autoActivityTracking">true</bool>
<bool name="ga_reportUncaughtExceptions">true</bool>

<string name="ga_appName">Serivce</string>
<string name="ga_appVersion">1.1.3</string>

<bool name="ga_debug">true</bool>

<item name="ga_dispatchPeriod" format="integer" type="integer">120</item>

<string name="ga_sampleFrequency">90</string>

<bool name="ga_anonymizeIp">true</bool>
<bool name="ga_dryRun">false</bool>

<!-- Percentage of events to include in reports -->
<string name="ga_sampleFrequency">100.0</string>

<!-- How long a session exists before giving up -->
<integer name="ga_sessionTimeout">-1</integer>

<string name="com.example.ui.MainActivity">MainActivity</string>


<?xml version="1.0" encoding="utf-8"?>
 <integer name="ga_sessionTimeout">60</integer>
 <!--  The following value should be replaced with correct property id. -->
 <string name="ga_trackingId">XX-XXXXXXX-1</string>


<?xml version="1.0" encoding="utf-8"?>
<!-- The following value should be replaced with correct property id. -->
<string name="ga_trackingId">XX-XXXXXXX-1</string>

<integer name="ga_sessionTimeout">300</integer>

<bool name="ga_autoActivityTracking">true</bool>

<!-- the Local LogLevel for Analytics -->
<string name="ga_logLevel">verbose</string>

<!-- how often the dispatcher should fire -->
<integer name="ga_dispatchPeriod">30</integer>

<!-- Treat events as test events and don't send to google -->
<bool name="ga_dryRun">false</bool>

<screenName name="com.example.ui.MainActivity">MainActivity</screenName>

(2).Update your AndroidManifest.xml file by adding the following permissions:

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




        android:value="@integer/google_play_services_version" />
        android:resource="@xml/global_tracker" />

(3)Using a Configuration File


create java file with and register this file in menifest like in application tag with android:name=”com.example.ui.AnalyticsSampleApp”

public class AnalyticsSampleApp extends Application {

// The following line should be changed to include the correct property id.
private static final String PROPERTY_ID = "XX-XXXXXX-1";

public static int GENERAL_TRACKER = 0;
public enum TrackerName {
    APP_TRACKER, // Tracker used only in this app.
    GLOBAL_TRACKER, // Tracker used by all the apps from a company. eg: roll-up tracking.
    ECOMMERCE_TRACKER, // Tracker used by all ecommerce transactions from a company.

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

public AnalyticsSampleApp() {

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

    return mTrackers.get(trackerId);


(4)Setting and Sending Data

Data is sent to Google Analytics using builders to set parameter-value pairs sending them via the tracker send method.

The following example shows how to send a screen view to Google Analytics by building an app view and calling the tracker send method:

 public class MainActivity extends Activity {

 protected void onCreate(Bundle savedInstanceState) {
 Tracker t = ((AnalyticsSampleApp)this.getApplication()).getTracker(TrackerName.APP_TRACKER);
 t.send(new HitBuilders.AppViewBuilder().build());

 protected void onStart() {
 // TODO Auto-generated method stub
 //Get an Analytics tracker to report app starts & uncaught exceptions etc.

 protected void onStop() {
 // TODO Auto-generated method stub
 //Stop the analytics tracking



If you now compile and install your app on your device and start it up, assuming you set ga_logLevel to verbose and ga_dryRun to false, in logCat you should see some of the following log lines confirming your hits being sent to Google Analytics.


com.mycompany.myapp V/GAV4? Thread[GAThread,5,main]: connecting to Analytics service
com.mycompany.myapp V/GAV4? Thread[GAThread,5,main]: connect: bindService returned false for Intent { (has extras) }
com.mycompany.myapp V/GAV4? Thread[GAThread,5,main]: Loaded clientId
com.mycompany.myapp I/GAV4? Thread[GAThread,5,main]: No campaign data found.
com.mycompany.myapp V/GAV4? Thread[GAThread,5,main]: Initialized GA Thread
com.mycompany.myapp V/GAV4? Thread[GAThread,5,main]: putHit called
com.mycompany.myapp V/GAV4? Thread[GAThread,5,main]: Dispatch running...
com.mycompany.myapp V/GAV4? Thread[GAThread,5,main]: sent 1 of 1 hits


Even better, if you’re logged into the Google Analytics console’s reporting dashboard, on the ‘Real Time – Overview’ page, you may even notice the following…

9-23-2014 3-33-27 PM



Working with Google Analytics API v4 for AndroidHMKCode , PankajAndroid(stackoverflow)  and  Java Code Geeks


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s