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.
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:
You can develop Analytics Tracking for Mobile Apps using either the Android or iPhone developer SDKs. Download the Analytics SDK for Android or iPhone.
libGoogleAnalytics.jar
to the project's /libs directoryAndroidManifest.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+)
GANTracker.h and libGoogleAnalytics.a files into your XCode project. CFNetwork framework in your project and link against libsqlite3.0.dylib.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.
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
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 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.
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.
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>
Example applications are contained within each SDK and will supply you with a full reference for implementation.
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.