Campaign Measurement - Android SDK

This document provides an overview of how to measure campaigns and traffic sources with the Google Analytics SDK for Android v3.

Overview

Measuring campaigns in Google Analytics enables the attribution of campaigns and traffic sources to user activity within your application. These options are available for campaign and traffic source attribution in the Google Analytics SDK for Android:

The following sections will describe when and how to implement each type of campaign measurement in your app.

Campaign parameters

Campaign parameters are used to pass information about the traffic sources and campaigns that are bringing users to your app.

The table below contains the available campaign parameters that can be used in Google Play or general campaign measurement:

Parameter Description Example(s)
utm_campaign Campaign name; used for keyword analysis to identify a specific product promotion or strategic campaign utm_campaign=spring_sale
utm_source Campaign source; used to identify a search engine, newsletter, or other source utm_source=google
utm_medium Campaign medium; used to identify a medium such as email or cost-per-click (cpc) utm_medium=cpc
utm_term Campaign term; used with paid search to supply the keywords for ads utm_term=running+shoes
utm_content 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
gclid Google Ads autotagging parameter; used to measure ads. This value is generated dynamically and should never be modified.

General Campaign & Traffic Source Attribution

After an app has been installed, it may be launched by referrals from ad campaigns, websites, or other apps. In this scenario, referring traffic sources or marketing campaigns can be attributed to user activity in subsequent sessions by setting the campaign fields on a tracker directly.

For example, the following implementation checks the intent that launched the app for Google Analytics campaign parameters:

package com.example.app;

import com.google.analytics.tracking.android.Fields;
import com.google.analytics.tracking.android.GoogleAnalytics;
import com.google.analytics.tracking.android.MapBuilder;
import com.google.analytics.tracking.android.Tracker;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import java.util.Map;

/*
 * An example of how to implement general campaign attribution in Android.
 *
 * If the intent that launched the Activity has a URI, parse it for campaign
 * parameters and send the referring data to Google Analytics.
 */
public class MainActivity extends Activity {

  private static final String GA_PROPERTY_ID = "UA-XXXX-Y";
  private static final String SCREEN_LABEL = "Home Screen";

  // This examples assumes the use of Google Analytics campaign
  // "utm" parameters, like "utm_source".
  private static final String CAMPAIGN_SOURCE_PARAM = "utm_source";

  Tracker mTracker;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mTracker = GoogleAnalytics.getInstance(this).getTracker(GA_PROPERTY_ID);
  }

  @Override
  public void onStart() {
    super.onStart();

    // Set screen name on tracker so that all subsequent hits will use this
    // value.
    mTracker.set(Fields.SCREEN_NAME, SCREEN_LABEL);

    // Get the intent that started this Activity.
    Intent intent = this.getIntent();
    Uri uri = intent.getData();

    // Send a screenview using any available campaign or referrer data.
    MapBuilder.createAppView().setAll(getReferrerMapFromUri(uri));
  }

  /*
   * Given a URI, returns a map of campaign data that can be sent with
   * any GA hit.
   *
   * @param uri A hierarchical URI that may or may not have campaign data
   *     stored in query parameters.
   *
   * @return A map that may contain campaign or referrer
   *     that may be sent with any Google Analytics hit.
   */
  Map<String,String> getReferrerMapFromUri(Uri uri) {

    MapBuilder paramMap = new MapBuilder();

    // If no URI, return an empty Map.
    if (uri == null) { return paramMap.build(); }

    // Source is the only required campaign field. No need to continue if not
    // present.
    if (uri.getQueryParameter(CAMPAIGN_SOURCE_PARAM) != null) {

      // MapBuilder.setCampaignParamsFromUrl parses Google Analytics campaign
      // ("UTM") parameters from a string URL into a Map that can be set on
      // the Tracker.
      paramMap.setCampaignParamsFromUrl(uri.toString());

     // If no source parameter, set authority to source and medium to
     // "referral".
     } else if (uri.getAuthority() != null) {

       paramMap.set(Fields.CAMPAIGN_MEDIUM, "referral");
       paramMap.set(Fields.CAMPAIGN_SOURCE, uri.getAuthority());

     }

     return paramMap.build();
  }
}

Alternatively, if you have campaign information in a form other than Google Analytics campaign parameters, you may set it on a Map and send it manually:

// May return null if EasyTracker has not yet been initialized with a property ID.
EasyTracker easyTracker = EasyTracker.getInstance(this);
easyTracker.set(Fields.SCREEN_NAME, "Home Screen");

// In this example, campaign information is set using a Map, rather than
// a url string with Google Analytics campaign parameters.
// Note that Fields.CAMPAIGN_KEYWORD is not necessary for this campaign.
HashMap<String, String> campaignData = new HashMap<String, String>();
campaignData.put(Fields.CAMPAIGN_SOURCE, "email");
campaignData.put(Fields.CAMPAIGN_MEDIUM, "email marketing");
campaignData.put(Fields.CAMPAIGN_NAME, "summer_campaign");
campaignData.put(Fields.CAMPAIGN_CONTENT, "email_variation_1");

MapBuilder paramMap = MapBuilder.createAppView();

// Campaign data sent with this hit.
// Note that the campaign data is set on the Map, not the tracker.
easyTracker.send(paramMap
    .setAll(campaignData).build()
);

Google Play Campaign Attribution

Google Play Campaign Measurement allows you to see which campaigns and traffic sources are sending users to download your app from the Google Play Store. It is recommended that all developers implement Google Play Store Campaign Measurement.

Implementing Google Play campaign attribution

When your app is downloaded from Google Play Store, the Play Store app broadcasts an INTENT_REFERRER to your app during installation. This intent contains the value of the referrer parameter of the link used to reach your app's Google Play Store page, if one was present.

To attribute an app download to a campaign, you must add a referrer parameter to any links that point to Google Play Store, and add a BroadcastReceiver to your app to receive and set the campaign information contained in the intent on your Google Analytics tracker.

It is recommended that most developers use the BroadcastReceiver provided with the SDK. To implement Google Play Store Campaign Measurement using the included receiver:

1. Add the Google Analytics receiver to your AndroidManifest.xml file. To add the Google Analytics receiver to the manifest, copy and paste the following markup:

<!-- Used for Google Play Store Campaign Measurement-->;
<service android:name="com.google.analytics.tracking.android.CampaignTrackingService" />
<receiver android:name="com.google.analytics.tracking.android.CampaignTrackingReceiver" android:exported="true">
  <intent-filter>
    <action android:name="com.android.vending.INSTALL_REFERRER" />
  </intent-filter>
</receiver>

2. Add Google Analytics Campaign Parameters to Google Play URLs

Next, add a referrer parameter to any URLs that will be linking directly to Google Play Store and set the value of that parameter to a string of Google Analytics campaign parameters that describe the source, as in this example:

https://play.google.com/store/apps/details?id=com.example.app
&referrer=utm_source%3Dgoogle
%26utm_medium%3Dcpc
%26utm_term%3Dpodcast%252Bapps
%26utm_content%3DdisplayAd1
%26utm_campaign%3Dpodcast%252Bgeneralkeywords

To learn how to build a campaign parameter strings, use the Google Play URL Builder, or consult the Campaign Parameters reference section.

Google Play URL Builder

Use the tool below to generate URLs for Google Play Campaign Measurement.