Welcome to our Knowledge Center.

Looking to learn more about digital analytics? We have you covered on a wide variety of topics.

How to Integrate Google Tag Manager for Android with Firebase Analytics

 

As a developer, Google Tag Manager (GTM) offers you the ability to manage measurement tags and pixels in your mobile applications without rebuilding and resubmitting the app to marketplaces. If you’re already using Google Analytics for Firebase in your Android app, integrating GTM can streamline your event management and help you easily implement changes, even after your app is live.

 

 

In this guide, we’ll walk through the setup process for integrating Google Tag Manager into your Android app using Firebase Analytics, along with clear instructions and steps for logging events, setting variables, and previewing and publishing your GTM container.

 

Prerequisites

 

Before getting started, make sure you’ve completed the following steps:

 

  1. Set up Firebase: Add your Android app to your Firebase project in the Firebase Console.

  2. Enable Google Analytics: Ensure Google Analytics is enabled in your Firebase project, and add the Google Analytics for Firebase SDK to your app.

  3. Create a Google Tag Manager Account: Create an account if you haven’t already, and configure a new GTM container.

 

1. Add Google Tag Manager to Your Project

 

To enable GTM in your Android app, you’ll need to configure dependencies and set up the container. Follow these steps:

 

Step 1.1: Add GTM Dependency

 

In your app’s `build.gradle` file (usually located at `app/build.gradle`), add the following dependency for the GTM library:

 

```gradle
dependencies {
  // Add Google Tag Manager dependency
  implementation 'com.google.android.gms:play-services-tagmanager:18.1.1'
}
```

 

Step 1.2: Download and Add the GTM Container

 

  1. Sign in to your Google Tag Manager account

  2. Select your mobile container.

  3. Go to Versions in the navigation bar, select the version you’d like to use, and click Download.

  4. In your project, create a `containers` folder inside `app/src/main/assets` if it doesn’t exist.

  5. Place the downloaded container file in this folder.

 

 

2. Log Events and Set Variables

 

Once the GTM container is added, you can start logging events and setting variables to capture event parameters and user properties.

 

Step 2.1: Log Events in Firebase Analytics

 

Using the Firebase SDK, log important events in your app. Here’s an example of logging a custom event:

 

```java
Bundle params = new Bundle();
params.putString("image_name", name);
params.putString("full_text", text);
mFirebaseAnalytics.logEvent("share_image", params);
```

 

Step 2.2: Configure Variables in Google Tag Manager

 

To capture event parameters and user properties in GTM, set up variables in the GTM interface.

 

1. Event Parameter Variables: For the event above, configure event parameters for `image_name` and `full_text` in GTM.

 

  • Variable Name: `Image Name`

  • Variable Type: `Event Parameter`

  • Event Parameter Key Name: `image_name`

 

2. User Properties: You can set user properties similarly. For example, set a user property in your app:

 

   ```java
   mFirebaseAnalytics.setUserProperty("favorite_food", mFavoriteFood);
   ```

 

   Then, create a new Firebase User Property variable in GTM:

  • Variable Name: `Favorite Food`

  • Variable Type: `Firebase User Property`

  • Event Parameter Key Name: `favorite_food`

 

3. Modify and Block Events

 

Google Tag Manager allows you to modify or block events before they reach Firebase Analytics. 

 

Step 3.1: Modify Events

 

With GTM, you can adjust event parameters or change event names without app updates. For instance, if you want to add or remove a parameter from an event, configure it in GTM directly.

 

Step 3.2: Block Events

 

If you want to prevent certain events from being sent to Firebase, you can set up a block condition in GTM. However, note that automatically collected events and properties in Firebase can’t be blocked.

 

4. Send Events

 

Firebase Analytics events are automatically fired, but you can also use GTM triggers to control the firing of events and tags.

 

Step 4.1: Set up Triggers

 

In GTM, triggers are conditions that determine when a tag should fire. To set up triggers:

 

 

  1. Use Firebase event names and event parameter variables to define trigger conditions.

  2. These conditions will be evaluated whenever you log an event in Firebase, allowing GTM to send or block events as specified.

 

5. Preview, Debug, and Publish Your Container

 

Testing your GTM container before publishing is crucial to ensure all tags and triggers work as expected. GTM offers a preview mode that enables you to view and troubleshoot container configurations in your app.

 

Step 5.1: Preview Your Container

 

  1. Go to the **Google Tag Manager** web interface.

  2. Select the container version and click **Actions > Preview**.

  3. Save the preview URL or QR code generated by GTM.

 

 

For Android Studio Users:

 

1. Add the following preview activity to your `AndroidManifest.xml` file:

 

   ```xml
   <activity
     android:name="com.google.android.gms.tagmanager.TagManagerPreviewActivity"
     android:noHistory="true">
     <intent-filter>
       <data android:scheme="tagmanager.c.com.example.app" />
       <action android:name="android.intent.action.VIEW" />
       <category android:name="android.intent.category.DEFAULT" />
       <category android:name="android.intent.category.BROWSABLE"/>
     </intent-filter>
   </activity>
   ```

 

2. Replace `tagmanager.c.com.example.app` with your app’s package name.

 

Step 5.2: Debugging the Container

 

To enable verbose logging and monitor GTM behavior in real-time, use the following ADB command:

 

```bash
$ adb shell setprop log.tag.GoogleTagManager VERBOSE
```

 

Step 5.3: Publish the Container

 

After successfully testing your container, you can publish it:
 

  1. In the GTM web interface, go to your container version and click **Publish**.
  2. Your GTM configurations are now live and will sync with user devices upon internet connection.

 

Note: Mobile containers have a default container used initially for tag configurations until the first container download. After the initial download, the app checks for container updates periodically, typically every 12 hours.

 

Still have questions?

 

By following these steps, you can integrate Google Tag Manager into your Android app and manage your tags and tracking pixels effectively. This setup gives you flexibility and control over your app’s analytics, without needing to update and redeploy your app with every change.

 

Integrating tools like GA4, Google Tag Manager, Looker Studio, and BigQuery can transform how your business tracks and optimizes marketing performance. But leveraging these platforms to their full potential requires expertise and strategy. At Tagmetrix, we specialize in helping businesses like yours turn raw data into actionable insights that drive growth and increase ROI.

 

Whether you’re running a small campaign or managing large-scale data, our digital analytics services are tailored to meet your specific needs. Let us handle the complexities of setup, tracking, and reporting so you can focus on what you do best—growing your business.

 

If you still have any questions, please feel free to reach out using our contact form and we'll be more than happy to help.

Let's get in touch.

We'll get back to you as quickly as possible, usually within 1-2 business days.