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:
- Create a screen name variable
- Create an open screen event trigger
- Create a Universal Analytics screen view tag
- Push open screen event and screen name to data layer
1. Create a screen name variable
To create a screen name variable in GTM:
- Sign in to your Google Tag Manager account.
- Select a mobile container.
- On the left navigation bar, click Variables.
- Under User-Defined Variables, click New.
- Click Untitled Variable to enter the variable name screen name.
- Choose Data Layer Variable as the variable type.
- Enter screenName as the Data Layer Variable Name and set its default value unknown screen.
- Click Create Variable.
2. Create an open screen event trigger
To create a trigger in GTM to fire the Universal Analytics screen view tag:
- Sign in to your Google Tag Manager account.
- Select a mobile container.
- On the left navigation bar, click Triggers.
- Click New.
- Click Untitled Trigger to enter the trigger name OpenScreenEvent.
- Choose Custom event.
- Under Fire On, enter the following condition:
- 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:
- Sign in to your Google Tag Manager account.
- Select a mobile container.
- On the left navigation bar, click Tags.
- Click New.
- Click Untitled Tag to enter the tag name Open Screen.
- Choose product Google Analytics.
- Enter the tracking ID.
- Select App View as the Track Type.
- Click More settings.
- Click Fields to Set.
- Repeatedly click + Add Field to add the following fields:
- Click Continue.
- Under Fire On, click Custom.
- Select the custom trigger OpenScreenEvent.
- Click Save.
- 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:
- (Optional) Create event variables
- Create an event trigger
- Create a Universal Analytics event tag
- 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:
- Sign in to your Google Tag Manager account.
- Select a mobile container.
- On the left navigation bar, click Variables.
- Under User-Defined Variables, click New.
- Click Untitled Variable to enter the variable name video name.
- Choose Data Layer Variable as the variable type.
- Enter videoName as the Data Layer Variable Name and set its default value to unknown video.
- Click Create Variable.
2. Create an event trigger
To create an event trigger in GTM to fire the Universal Analytics event tag:
- Sign in to your Google Tag Manager account.
- Select a mobile container.
- On the left navigation bar, click Triggers.
- Click New.
- Click Untitled Trigger to enter the trigger name VideoPlayEvent.
- Choose Custom event.
- Under Fire On, enter the following condition:
- Click Create Trigger.
3. Create a Universal Analytics event tag
To create a Universal Analytics event tag fired by the video play event:
- Sign in to your Google Tag Manager account.
- Select a mobile container.
- On the left navigation bar, click Tags.
- Click New.
- Click Untitled Tag to enter the tag name Video Play Event.
- Choose product Google Analytics.
- Enter the tracking ID.
- Select Event as the Track Type.
- Enter the following event tracking parameters:
- Click More settings.
- Click Fields to Set.
- Repeatedly click + Add Field to add the following fields:
- Click Continue.
- Under Fire On, click Custom.
- Select the custom trigger VideoPlayEvent.
- Click Save.
- 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:
- Create a transaction trigger
- Create a Universal Analytics transaction tag
- 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:
- Sign in to your Google Tag Manager account.
- Select a mobile container.
- On the left navigation bar, click Triggers.
- Click New.
- Click Untitled Trigger to enter the trigger name TransactionCompleted.
- Choose Custom event.
- Under Fire On, enter the following condition:
- Click Create Trigger.
2. Create a Universal Analytics transaction tag
To create a Universal Analytics transaction tag fired by the transaction event:
- Sign in to your Google Tag Manager account.
- Select a mobile container.
- On the left navigation bar, click Tags.
- Click New.
- Click Untitled Tag to enter the tag name Transaction Completed.
- Choose product Google Analytics.
- Enter the tracking ID.
- Select Transaction as the Track Type.
- Click More settings.
- Click Fields to Set.
- Repeatedly click + Add Field to add the following fields:
- Click Continue.
- Under Fire On, click Custom.
- Select the custom trigger TransactionCompleted.
- Click Save.
- 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>