http://code.google.com/intl/zh-CN/mobile/analytics/docs/android/

http://wodingdong.com/archives/203.html

Google Analytics SDK for Android

The Google Analytics for Mobile Apps SDK for Android makes it easy to implement Google Analytics in an Android-based applications. This document describes how to integrate the SDK with your apps.

  1. SDK Overview
  2. Getting Started
  3. Using the SDK
    1. Starting the Tracker
    2. Tracking Pageviews and Events
    3. Using Custom Variables
    4. Batching Hits
    5. Known Issues
  4. Tracking Referrals
    1. Android Market Referral Tracking

SDK Overview

The Google Analytics for Mobile Apps SDKs provide an interface for tracking activity within mobile apps and reporting that activity to Google Analytics. For example you can use this SDK to calculate visits, session length, bounce rate and unique visitors. Tracking mobile applications has some structural variations from tracking website pages.

This SDK uses a tracking model that is designed to track visitors to traditional websites and interaction with widgets in traditional web pages. For this reason, the terms used below reflect the conventional website tracking model and are being mapped over to tracking mobile applications. You should be familiar with Analytics tracking in order to understand how this SDK works.

Use the mobile tracking SDK to track your phone applications with the following Analytics interaction types:

Pageview Tracking
A pageview is a standard means to measure traffic volume to a traditional website. Because mobile apps don't contain HTML pages, you must decide when (and how often) to trigger a pageview request. Also, since pageview requests are designed to report on directory structures, you should provide descriptive names for the requests to take advantage of page path naming in the Content reports in Analytics. The names you choose will be populated in your Analytics reports as page paths even though they are not actually HTML pages, but you can use this to your advantage by structuring paths to provide additional groupings for your calls.
Event Tracking
In Analytics, events are designed to track user interaction to web page elements distinctly from pageview requests. You can use the Event Tracking feature of Google Analytics to make additional calls that will be reported in the Event Tracking section of the Analytics report interface. 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
Custom Variables
Custom variables are name-value pair tags that you can insert in your tracking code in order to refine Google Analytics tracking. For more information on how you can use custom variables, read the Custom Variable Guide.

Getting Started

Requirements

To integrate Google Analytics' tracking capabilities with your Android app, you will need the following:

Setup

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

An example application is included with the SDK that demonstrates what your project should look like if set up successfully. Feel free to use it as a template for your own Analytics-integrated apps.

Using the SDK

Before you begin using the SDK, you must first 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

Obtain the tracker singleton by calling GoogleAnalyticsTracker.getInstance(). Then call its start method, passing the web property ID and activity being tracked. It is often convenient to call this method directly in the onCreate method of your Activity. 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) {          // Add a Custom Variable to this pageview, with name of "Medium" and value "MobileApp" and          // scope of session-level.          tracker.setCustomVar(1, "Navigation Type", "Button click", 2);          // 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 trackPageView of the tracker object each time you wish to trigger a pageview. Call trackEvent to record an event. For more on pageviews and events, see SDK Overview above.

Using Custom Variables

Adding a custom variable is also straightforward: just use the setCustomVar method provided by the mobile SDK. You'll want to plan out ahead of time which indexes each custom variable maps to, so you don't overwrite any previously existing variable. For more information on custom variables, see the Custom Variable Guide. Note that the method setCustomVar does not directly send data on its own. Rather the data is sent with the next tracked pageview or event. You have to call setCustomVar before you track a pageview or event.

Batching Hits

To save on connection and battery overhead, we recommend batching your tracking requests. You can call dispatch on the tracking object any time you want to make a batch request, and you can do this either manually or at specific time intervals.

Tip: Bundle your tracker dispatches with other HTTP requests made by your application to reduce overhead.

Known Issues

  • Possible 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.

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, which can be useful for gauging the effectiveness of a particular advertisement for your app for example.

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

In order for referral tracking to work, you must add the following code snippet to your project's AndroidManifest.xml manifest 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>

Android Market Referral Tracking

To set up Google Analytics referral tracking through the Android market, use the URL builder below to generate a referral link. Use the link to refer users to your application. The Analytics SDK will automatically parse and record the referral information and populate it in your Analytics report.

To generate the referral link, fill in the fields below. Package Name, Campaign Source, Campaign Medium, and Campaign Name are required. For a detailed description of each parameter, see the table below.

Package Name: *
(Java package, e.g. com.example.application)
Campaign Source: *
(original referrer, e.g. google, citysearch, newsletter4)
Campaign Medium: *
(marketing medium, e.g. cpc, banner, email)
Campaign Term:
(paid keywords, e.g. running+shoes)
Campaign Content:
(ad-specific content used to differentiate ads)
Campaign Name: *
(product, promotion code, or slogan)
ParameterRequiredDescriptionExample(s)
utm_source Yes Campaign source; used to identify a search engine, newsletter, or other source utm_source=google
utm_medium Yes Campaign medium; used to identify a medium such as email or cost-per-click (cpc) utm_medium=cpc
utm_term No Campaign term; used with paid search to supply the keywords for ads utm_term=running+shoes
utm_content No Campaign content; used for A/B testing and content-targeted ads to differentiate ads or links that point to the same URL utm_content=logolink
utm_content=textlink
utm_campaign Yes Campaign name; used for keyword analysis to identify a specific product promotion or strategic campaign utm_campaign=spring_sale

Change Log

Version 1.1

This release contains:

  • Bug Fixes:
    • Fixed improper encoding of spaces
    • SQLiteExceptions no longer cause apps to crash
  • New Features:
    • Added support for custom variables
posted on 2010-12-29 16:34  stay  阅读(3993)  评论(0编辑  收藏  举报