How to implement Google Tag Manager for Android


At I/O 2013, Google added new features to the Google Tag Manager. They launched Google Tag Manager for Android and iOS devices. It is used in Mobile App along with Google Analytics to track various user behavior and help the marketers identify the trends, needs of users, app performance and other peculiar insights.

Google Tag Manager for mobile app developer devices also make their development easy and dynamic. It enables the developers to change configuration values in their mobile applications using the tag manager interface, without having to rebuild and resubmit application binaries to the app marketplaces.

In order to walk through the implementation you must be familiar with android and iOS development to understand and implement Google Tag Manager in them.

Steps to set up GTM in an Android Project

  1. Create a Google Tag Manager Account
  2. Create a Mobile App container
  3. Note down the Container ID (It would be of the form of GTM-XXXXX)
  4. Add Google Play Services library to your project
    NOTE: Check for latest updated version of Google Play services library when adding it in project.
    Google tag manager
  5. Add the following permissions in your AndroidManifest.xml
    GTM, Google Tag Manager for Android and iOS devices
  6. Also Add the Install Referrer Receiver in AndroidManifest.xml to track Campaign Data.
    GTM for android
  7. Now, Create a Trigger with the following configuration in Google Tag Manager Container.
    GTM for iOS devices
  8. Create a DataLayer Variable to fetch the value of screenName from the datalayer.
     tag manager, google analytics
  9. Create a Tag to fire the Screen View from the app.
    Google tag manager
  10. Add Google Tag Manager’s Default Container to your Project.
    • Within Google Tag Manager interface select VERSIONS -> ACTION -> DOWNLOAD this will download the container binary file.
      Google Tag Manager for Android and iOS devices
    • Within your Android project, create a raw folder in project-root / res path.
    • Copy the downloaded binary file to the path project-root / res / raw with changing the filename to gtm_defaultcontainer_gtm_id.
      GTM for android

Now, Let’s get started with initializing Tag Manager Instance and using DataLayer

  1. Create a ContainerHolderSingleton Class.
    ContainerHolderSingleton class is used to have access to a single instance of TagManager Object at a time.
    GTM for iOS devices
  2. Create a class ContainerLoadedCallback Class.
    ContainerLoadedCallback class is used to load the fresh GTM Container when it is available
    tag manager, google analytics
  3. Initialize Tag Manager in First Screen of the App (i.e. Splash Screen or Main Screen)
  4. Load latest container by specifying the CONTAINER_ID
  5. Use ResultCallback to return the ContainerHolder once it has finished loading or has timed out for fetching the latest container
  6. After Initializing GTM well, it’s time to add the tracking code, which is implemented using the DataLayer Object.
    DataLayer provides a layer of persistence that you use to store data in key-value pairs form and then send it to Google Analytics.
    Create one method in general util class to push the openScreen event and start tracking the screenviews from your app. You can call this method in onCreate()/onStart Method of your app activities with the screenName that you want to send.
    Google tag manager

With the above steps performed we have successfully implemented the Google Tag Manager in Android. To know that your implementation is working well, verify your data in Google Analytics Report.

Also please note that, to track any kind to action through GTM, an event must be pushed in dataLayer and for the very same event a Tag must be configured in GTM Interface.

So, that’s it. This is how you can integrate Google Tag Manager version 4 container in your Android app. Recently, Google rolled out version 5 container for the app

Now, the question is which version should you use in your app. Stay tuned for the benefits of the version 5 container over the version 4 container and steps to integrate it.

How to implement Google Tag Manager for Android by
The following two tabs change content below.
Vatsal Padhiyar

Vatsal Padhiyar

Vatsal is a Technical Analyst at Tatvic Analytics Pvt Ltd. He is working in Google Analytics and Google Tag Manager for a couple of years. He is Polyglot Programmer. Having a degree of Master of Computer Application, he has keen knowledge and interest about data science. He is ambitious and always eager to help others.
Vatsal Padhiyar

Latest posts by Vatsal Padhiyar (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *