My favorites | English | Sign in

Faster apps faster - GWT 2.0 with Speed Tracer New!

Google Analytics (Labs)

Analytics Tracking for Mobile Apps

The Google Analytics Tracking for Mobile Apps module makes it easy for you to implement Google Analytics in your mobile app. The SDK can be downloaded and used on iPhone, iPod Touch and Android application platforms. This document describes how to integrate the SDK with your application.


  1. Why Use Mobile Apps Tracking?
  2. Supported Development Environments
  3. How Does the SDK Work?
    1. Starting the Tracker
    2. Tracking Pageviews and Events
    3. Batching Hits
    4. Tracking Referrals
    5. Examples
    6. Known Issues
  4. Versioning

Why Use Mobile Apps Tracking?

The Google Analytics for Mobile Apps SDKs provide an interface for tracking activity within mobile apps and reporting that activity to the standard Google Analytics dashboard. Tracking mobile applications has some structural variations from tracking website pages. For that reason, you should be familiar with Analytics tracking in order to understand how this SDK works.

We currently provide SDKs for iPhone and Android development platforms. Use the SDK to track two basic types of user interaction:

  • Pageviews. This is the standard unit of measure for a traditional web site, and is used to calculate visits, session length, and bounce rate. We recommend that you trigger at least one pageview at application load to track unique visitors. Because mobile apps don't contain HTML pages, you must decide when (and how often) to trigger a pageview request, and choose descriptive names for reporting purposes. The names you choose will be populated in your Analytics reports as page paths in the Content reports, even though they are not actually HTML pages.
  • Events. You can define additional events to be reported in the Event Tracking section of Google Analytics. Events are  grouped using categories and may also use per-event labels, which provides flexibility in reporting. For example, a multimedia app could could have play/stop/pause actions for its video category and assign a label for each video name. The Google Analytics reports would then aggregate events for all events tagged with the video category. For more information on Event Tracking, see the  Event Tracking Guide.

Supported Development Environments

You can develop Analytics Tracking for Mobile Apps using either the Android or iPhone developer SDKs. Download the Analytics SDK for Android or iPhone.

  • Add libGoogleAnalytics.jar to the project's /libs directory
  • Add the following permissions to permissions to AndroidManifest.xml:
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

iPhone/iPod Touch Instructions (requires Mac OS X v10.5.3+, XCode 3.1+, iPhone OS 2.0+)

  • Drag the GANTracker.h and libGoogleAnalytics.a files into your XCode project.
  • Include the CFNetwork framework in your project and link against libsqlite3.0.dylib.
Back to Top

How Does the SDK Work?

To use the SDK, you must create a free account at www.google.com/analytics, and create a new website profile in that account using a fake but descriptive website URL (e.g. http://mymobileapp.mywebsite.com). Once you create the profile, write down or keep a copy of the web property ID that is generated for the newly-created profile.

A Web property ID is also known as the UA number of your tracking code and looks like UA-xxxxx-yy, where the x's and y's indicate the unique numbers for your profile. You must indicate the web property ID you'd like to use when instantiating the tracking object. See Web Property for more information.

You must indicate to your users, either in the application itself or in your terms of service, that you reserve the right to anonymously track and report a user's activity inside of your app. Your use of the Google Analytics SDK is additionally governed by the Google Analytics Terms of Service, which you must agree to when signing up for an account.

Starting the Tracker

iPhone/iPod

Start the tracker by calling the startTrackerWithAccountID() method on the tracker singleton obtained via [GANTracker sharedTracker]. Pass the web property ID, required dispatch period, and optional delegate. For example:

#import "BasicExampleAppDelegate.h"

#import "GANTracker.h"

// Dispatch period in seconds
 static const NSInteger kGANDispatchPeriodSec = 10;

 @implementation BasicExampleAppDelegate
 
 @synthesize window = window_;
 
 - (void)applicationDidFinishLaunching:(UIApplication *)application {
   // **************************************************************************
   // PLEASE REPLACE WITH YOUR ACCOUNT DETAILS.
   // **************************************************************************
    [[GANTracker sharedTracker] startTrackerWithAccountID:@"UA-0000000-1"
                                          dispatchPeriod:kGANDispatchPeriodSec
                                                delegate:nil];
   NSError *error;
   if (![[GANTracker sharedTracker] trackEvent:@"my_category"
                                        action:@"my_action"
                                         label:@"my_label"
                                         value:-1
                                     withError:&error]) {
     // Handle error here
   }
 
   if (![[GANTracker sharedTracker] trackPageview:@"/app_entry_point"
                                        withError:&error]) {
     // Handle error here
   }
 
   [window_ makeKeyAndVisible];
 }
 
 - (void)dealloc {
   [[GANTracker sharedTracker] stopTracker];
   [window_ release];
   [super dealloc];
 }
 
 @end

Android

Obtain the tracker singleton by calling GoogleAnalyticsTracker.getInstance(). Then call its start() method, passing the web property ID and activity being tracked. For example:

 package com.google.android.apps.analytics.sample;

 import com.google.android.apps.analytics.GoogleAnalyticsTracker;
 
 import android.app.Activity;
 import android.os.Bundle;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 
 public class TestActivity extends Activity {
 
   GoogleAnalyticsTracker tracker;
 
   @Override
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
 
     tracker = GoogleAnalyticsTracker.getInstance();
 
     // Start the tracker in manual dispatch mode...
     tracker.start("UA-YOUR-ACCOUNT-HERE", this);
 
     // ...alternatively, the tracker can be started with a dispatch interval (in seconds).
     //tracker.start("UA-YOUR-ACCOUNT-HERE", 20, this);
 
     setContentView(R.layout.main);
     Button createEventButton = (Button)findViewById(R.id.NewEventButton);
     createEventButton.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {
         tracker.trackEvent(
             "Clicks",  // Category
             "Button",  // Action
             "clicked", // Label
             77);       // Value
       }
     });
 
     Button createPageButton = (Button)findViewById(R.id.NewPageButton);
     createPageButton.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {
 
         // Track a page view. This is probably the best way to track which parts of your application
         // are being used.
         // E.g.
         // tracker.trackPageView("/help"); to track someone looking at the help screen.
         // tracker.trackPageView("/level2"); to track someone reaching level 2 in a game.
         // tracker.trackPageView("/uploadScreen"); to track someone using an upload screen.
         tracker.trackPageView("/testApplicationHomeScreen");
       }
     });
 
     Button quitButton = (Button)findViewById(R.id.QuitButton);
     quitButton.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {
         finish();
       }
     });
 
     Button dispatchButton = (Button)findViewById(R.id.DispatchButton);
     dispatchButton.setOnClickListener(new OnClickListener() {
       @Override
        public void onClick(View v) {
         // Manually start a dispatch, not needed if the tracker was started with a dispatch
         // interval.
         tracker.dispatch();
       }
     });
   }
 
   @Override
   protected void onDestroy() {
     super.onDestroy();
     // Stop the tracker when it is no longer needed.
     tracker.stop();
   }
 }

Tracking Pageviews and Events

Tracking pageviews and events is straightforward: simply call the trackPageView() method of the tracker object each time you wish to trigger a pageview. Call the trackEvent() method to record an event.

Batching Hits

To save on connection and battery overhead, you can dispatch hits manually, or at specifi time intervals by using the dispatch() method of the tracker object. We recommend that you reduce overhead by bundling tracker dispatches with other HTTP requests made by the application.

Tracking Referrals

The Android 1.6 OS release supports the use of a referrer URL parameter in download links to the Android Market. The Google Analytics SDK for Android uses this parameter to automatically populate referral/campaign information in Google Analytics for your application. This enables the source of the application install to be recorded and associated with future pageviews and events.

This referral operates differently than conventional visit-level referrals, because the referral remains valid for the life of a given application install. To set up referrals on Android, see Android Market Referral Tracking.

In order for referral tracking to work, you must add the following code snippet to your AndroidManifest.xml file:

<!-- Used for install referrer tracking -->
<receiver android:name="com.google.android.apps.analytics.AnalyticsReceiver" android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>

Examples

Example applications are contained within each SDK and will supply you with a full reference for implementation.

Known Issues

  • Possibly inaccurate timestamps. Timestamps are recorded at the time the application dispatches to Google Analytics, so if a user experiences long periods of offline use, the timestamps may not be 100% accurate.
  • Referrals/Traffic Sources. It is not currently possible to trace the campaign/referral source of an app download on the iPhone.
Back to Top

Versioning

You can download the latest version of the tracking components as a ZIP file for Android or for iPhone. Each download contains all tracking components along with related documentation. The file name for the download file will display the version number of the code it contains.

Back to Top