Universal Analytics Tags

To enable run-time changes to your Google Analytics implementation that do not require you to rebuild and resubmit your app binary to marketplaces, implement Universal Analytics (UA) in your app with Google Tag Manager (GTM).

This guide shows you how to use Universal Analytics tags and the Google Tag Manager SDK for Android to:

Send screen views

To send a screen view each time a new screen is opened in your app to Google Analytics:

  1. Create a screen name variable
  2. Create an open screen event trigger
  3. Create a Universal Analytics screen view tag
  4. Push open screen event and screen name to data layer

1. Create a screen name variable

To create a screen name variable in GTM:

  1. Sign in to your Google Tag Manager account.
  2. Select a mobile container.
  3. On the left navigation bar, click Variables.
  4. Under User-Defined Variables, click New.
  5. Click Untitled Variable to enter the variable name screen name.
  6. Choose Data Layer Variable as the variable type.
  7. Enter screenName as the Data Layer Variable Name and set its default value unknown screen.
  8. Click Create Variable.

2. Create an open screen event trigger

To create a trigger in GTM to fire the Universal Analytics screen view tag:

  1. Sign in to your Google Tag Manager account.
  2. Select a mobile container.
  3. On the left navigation bar, click Triggers.
  4. Click New.
  5. Click Untitled Trigger to enter the trigger name OpenScreenEvent.
  6. Choose Custom event.
  7. Under Fire On, enter the following condition:

    A trigger that will be fired when the event equals openScreen

  8. Click Create Trigger.

3. Create a Universal Analytics screen view tag

To create a Universal Analytics screen view tag fired by the open screen event:

  1. Sign in to your Google Tag Manager account.
  2. Select a mobile container.
  3. On the left navigation bar, click Tags.
  4. Click New.
  5. Click Untitled Tag to enter the tag name Open Screen.
  6. Choose product Google Analytics.
  7. Enter the tracking ID.
  8. Select App View as the Track Type.
  9. Click More settings.
  10. Click Fields to Set.
  11. Repeatedly click + Add Field to add the following fields:

    add these fields: app name, app version, and screen name

  12. Click Continue.
  13. Under Fire On, click Custom.
  14. Select the custom trigger OpenScreenEvent.
  15. Click Save.
  16. Click Create Tag.

4. Push open screen event and screen name to data layer

Add code to your app to push an openScreen event and a screenName value to the data layer, for example:

import com.google.tagmanager.DataLayer;
import com.google.tagmanager.TagManager;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

  private static final String SCREEN_NAME = "Home Screen";
  private DataLayer mDataLayer;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate();
  }

  @Override
  public void onStart() {
    super.onStart();
    mDataLayer = TagManager.getInstance(this).getDataLayer();

    // Push an openScreen event and a screenName value to the data layer.
    // This call assumes the container has already been opened, otherwise events
    // pushed to the data layer will not fire tags in that container.
    mDataLayer.push(DataLayer.mapOf("event", "openScreen", "screenName", SCREEN_NAME));
  }
}

Send events

To send an event to Google Analytics when a user interacts with UI controls or content in your app:

  1. (Optional) Create event variables
  2. Create an event trigger
  3. Create a Universal Analytics event tag
  4. Push event and event variable value to data layer

The following steps show how an event is sent to Google Analytics when a user starts a video playback.

1. (Optional) Create event variables

If the event uses data layer variables to represent its category, action, label, or value fields, you must first create those variables. In this example, the video play event tag uses a data layer variable to get the name of the video.

To create a video name variable in GTM:

  1. Sign in to your Google Tag Manager account.
  2. Select a mobile container.
  3. On the left navigation bar, click Variables.
  4. Under User-Defined Variables, click New.
  5. Click Untitled Variable to enter the variable name video name.
  6. Choose Data Layer Variable as the variable type.
  7. Enter videoName as the Data Layer Variable Name and set its default value to unknown video.
  8. Click Create Variable.

2. Create an event trigger

To create an event trigger in GTM to fire the Universal Analytics event tag:

  1. Sign in to your Google Tag Manager account.
  2. Select a mobile container.
  3. On the left navigation bar, click Triggers.
  4. Click New.
  5. Click Untitled Trigger to enter the trigger name VideoPlayEvent.
  6. Choose Custom event.
  7. Under Fire On, enter the following condition:

    A trigger that will be fired when the event equals videoPlay.

  8. Click Create Trigger.

3. Create a Universal Analytics event tag

To create a Universal Analytics event tag fired by the video play event:

  1. Sign in to your Google Tag Manager account.
  2. Select a mobile container.
  3. On the left navigation bar, click Tags.
  4. Click New.
  5. Click Untitled Tag to enter the tag name Video Play Event.
  6. Choose product Google Analytics.
  7. Enter the tracking ID.
  8. Select Event as the Track Type.
  9. Enter the following event tracking parameters:

    enter these tracking parameters: Video for category, Play for action, and video name for label

  10. Click More settings.
  11. Click Fields to Set.
  12. Repeatedly click + Add Field to add the following fields:

    add these fields: app name, app version, and screen name

  13. Click Continue.
  14. Under Fire On, click Custom.
  15. Select the custom trigger VideoPlayEvent.
  16. Click Save.
  17. Click Create Tag.

4. Push event and event variable value to data layer

Write code to push a videoPlay event and a videoName value to the data layer, for example:

import com.google.tagmanager.DataLayer;
import com.google.tagmanager.TagManager;

public class VideoPlayer {

  // Called when a user starts a video playback.
  public void onPlay(Video v) {
    DataLayer dataLayer = TagManager.getInstance(this).getDataLayer();

    // Push a videoPlay event and a videoName value to the data layer.
    // This call assumes the container has already been opened, otherwise events
    // pushed to the data layer will not fire tags in that container.
    dataLayer.push(DataLayer.mapOf("event", "videoPlay", "videoName", v.getName());
    play(v);
  }

  // Rest of the implementation.
  // ...
}

Send ecommerce transactions

When a user completes an in-app purchase, to send the transaction and the associated items to Google Analytics:

  1. Create a transaction trigger
  2. Create a Universal Analytics transaction tag
  3. Push transaction event, transaction, and items to data layer

For the list of supported transaction and item variable names, see Supported ecommerce data layer variables.

1. Create a transaction trigger

To create transaction trigger in GTM to fire the Universal Analytics transaction tag:

  1. Sign in to your Google Tag Manager account.
  2. Select a mobile container.
  3. On the left navigation bar, click Triggers.
  4. Click New.
  5. Click Untitled Trigger to enter the trigger name TransactionCompleted.
  6. Choose Custom event.
  7. Under Fire On, enter the following condition:

    A trigger that will be fired when the event equals transaction.

  8. Click Create Trigger.

2. Create a Universal Analytics transaction tag

To create a Universal Analytics transaction tag fired by the transaction event:

  1. Sign in to your Google Tag Manager account.
  2. Select a mobile container.
  3. On the left navigation bar, click Tags.
  4. Click New.
  5. Click Untitled Tag to enter the tag name Transaction Completed.
  6. Choose product Google Analytics.
  7. Enter the tracking ID.
  8. Select Transaction as the Track Type.
  9. Click More settings.
  10. Click Fields to Set.
  11. Repeatedly click + Add Field to add the following fields:

    add these fields: app name, app version, and screen name

  12. Click Continue.
  13. Under Fire On, click Custom.
  14. Select the custom trigger TransactionCompleted.
  15. Click Save.
  16. Click Create Tag.

3. Push transaction event, transaction, and items to data layer

Write code to push the transaction event, transaction and the associated items to the data layer. You must use the supported transaction and item variable names to push transaction and item data to the data layer. For example:

// Called when a user completes a transaction.
public void onPurchaseCompleted(Purchase p) {

  DataLayer dataLayer = TagManager.getInstance(this).getDataLayer();

  // Put maps of item data into an array to be pushed to the data layer.
  ArrayList<Map<String, String>> purchasedItems = new ArrayList<Map<String, String>>();
  for (Item i : p.items) {
    HashMap<String, String> currentItem = new HashMap<String, String>();

    currentItem.put("name", i.getProductName());
    currentItem.put("sku", i.getProductSku());
    currentItem.put("category", i.getProductCategory());
    currentItem.put("price", i.getProductPrice());
    currentItem.put("currency", "USD");
    currentItem.put("quantity", i.getProductQty());

    purchasedItems.add(currentItem);
  }

  // This call assumes the container has already been opened, otherwise events
  // pushed to the [DataLayer] data layer will not fire tags in that container.
  dataLayer.push(DataLayer.mapOf("event", "transaction",
                                 "transactionId", p.getTransId(),
                                 "transactionTotal", p.getTotal(),
                                 "transactionAffiliation", "In-app Store",
                                 "transactionTax", p.getTax(),
                                 "transactionShipping", p.getShippingCost(),
                                 "transactionCurrency", "USD",
                                 "transactionProducts", purchasedItems));

  // Because the data layer is persistent, you should reset fields to null
  // after you are done pushing the transaction.
  dataLayer.push(DataLayer.mapOf("transactionId", null,
                                 "transactionTotal", null,
                                 "transactionAffiliation", null,
                                 "transactionTax", null,
                                 "transactionShipping", null,
                                 "transactionCurrency", null,
                                 "transactionProducts", null));
}

For more information on using the Google Tag Manager SDK for Android to implement Universal Analytics Enhanced Ecommerce features, see Enhanced Ecommerce.

Supported ecommerce data layer variables

Universal transaction tags support the following transaction and item variable names.

Transaction variables

Variable Name Description Type Required
transactionId Unique transaction identifier string Yes
transactionAffiliation Partner or store string No
transactionTotal Total value of the transaction string No
transactionTax Tax amount for the transaction string No
transactionShipping Shipping cost for the transaction string No
transactionCurrency Currency of the transaction string No
transactionProducts List of items purchased in the transaction an array of containing item variables. No

Item variables

Variable Name Description Type Required
name Product name string Yes
sku Product SKU string No
category Product category string No
price Product price string No
currency The currency type of price string No
quantity Quantity of items string No

Install referrer attribution

If you register the following service and receiver in your AndroidManifest.xml file, available install referrer data will be automatically included with any Google Analytics hits or AdWords Remarketing pixel requests:

<service android:name="com.google.tagmanager.InstallReferrerService" />
<receiver android:name="com.google.tagmanager.InstallReferrerReceiver" android:exported="true">
  <intent-filter>
    <action android:name="com.android.vending.INSTALL_REFERRER" />
  </intent-filter>
</receiver>