GTM Android Setup
Steps for using Google Tag Manager with the Branch Android SDK.
Overview
You can use Google Tag Manager (GTM) to trigger specific actions related to Branch.
This guide walks you through how to do this for your Android app, in combination with the Branch Android SDK.
More specifically, you will learn how to create and log Branch Events.
Prerequisites
In order to use GTM and Branch together, you first need to:
- Create a GTM account that has a container with "Android" as the chosen platform.
- Integrate the Branch Android SDK into your Android app.
Setup
Import GTM
Update your build.gradle
(Groovy) or build.gradle.kts
(Kotlin) file to include the following line:
implementation 'com.google.android.gms:play-services-tagmanager:18.0.4'
implementation("com.google.android.gms:play-services-tagmanager:18.0.4")
Learn more about updating your build file in our Android SDK Basic Integration guide.
Create Custom Class
You'll now need to write a class that includes your Branch code. This is the code that GTM will be configured to call.
Create a custom class that extends com.google.android.gms.tagmanager.CustomTagProvider
.
In this example, the class will create and log a Branch PURCHASE
Event when triggered by GTM.
Note the path to this class in your code. You'll need it when you create your tag in GTM.
Implement Branch Features
Track Events
1. Create a Trigger
Create a new trigger that is associated with a Firebase event.
-
In the Workspace tab, click on Triggers.
-
Click New, and create a trigger with type Custom Event.
-
Provide an event name and configure when you want the trigger to fire.
-
Click Save.
2. Create a Tag
Create a new tag, which will describe what you want to have occur when the trigger fires.
-
In the Workspace tab, click on Tags.
-
Click New, and create a tag with type "Function Call".
-
For the Class Path field, add the path that leads to your custom class that calls Branch Event code.
-
In the Key and Value sections, add your relevant variables.
-
In the Triggering section, add the trigger you just created.
-
Click Save.
3. Deploy GTM Changes
Deploy the changes you have made on the GTM side. Once you do this, the Branch Event creation and logging code will run every time the GTM tag is fired.
4. Trigger Firebase Event
In your code, trigger the Firebase event associated with the GTM trigger you created.
FirebaseAnalytics mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
Bundle bundle = new Bundle();
bundle.putString("currency", "EUR");
bundle.putString("revenue", "100");
mFirebaseAnalytics.logEvent("Purchase", bundle);
val mFirebaseAnalytics = FirebaseAnalytics.getInstance(this)
val bundle = Bundle()
bundle.putString("currency", "EUR")
bundle.putString("revenue", "100")
mFirebaseAnalytics.logEvent("Purchase", bundle)
Testing
Liveview
To make sure you've successfully set up GTM to work with the Branch Android SDK:
- Trigger the event you are interested in, based on the trigger you set up in GTM.
- Go to the Liveview page of the Branch Dashboard.
- Filter the dropdown to the relevant event type that you are interested in, such as "custom event".
- Check to make sure that the event is being triggered with the expected details.
Enable Branch Android SDK Logging
Make sure you have logging enabled within the Branch Android SDK so you can see logs related to the Branch Event.
For steps on how to do this, visit our Branch Android SDK Testing guide.
FAQs
Visit our FAQs section to learn more about implementing Branch with GTM.
Updated 6 months ago