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. Google Tag Manager for mobile devices now makes the life of a developer much easier.

Google Tag Manager enables 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.

Background

You should be familiar with android and iOS development in order to understand and implement Google Tag Manager in them.

Steps to implement GTM in an Android device

    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-XXXX)
    4. Add libGoogleAnalyticsServices to project libraries (download library for Android)
    5. Add the following permissions in your AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
    1. Create a file named GTM-XXXX.json (where in XXXX stands for the container-ID) in your application in the assets/tagmanager folder. (in the end you must end with a directory structure as assets/tagmanager/GTM-XXXX.json)
      File Structure
    2. Now set default configuration values, in case when no saved container is available on the device or when an attempt to load the container from the disk times out.
    3. Following the best practices of programming, next you need to create a simple POJO named ContainerHolder.java (you can name as you like) in your app source code (optional).
      Optional
    4. The following code snippet will load a saved container from the disk or request a new one over the network:
private static final long TIMEOUT_FOR_CONTAINER_OPEN_MILLISECONDS = 2000;
private static final String CONTAINER_ID = "GTM-XXXX";

TagManager tagManager = TagManager.getInstance(this);

// The containerAvailable method will be called as soon as one of the following happens:

//   1. a saved container is loaded

//   2. if there is no saved container, a network container is loaded

//   3. the 2-second timeout occurs

ContainerOpener.openContainer(

tagManager, CONTAINER_ID, OpenType.PREFER_NON_DEFAULT,

TIMEOUT_FOR_CONTAINER_OPEN_MILLISECONDS, new ContainerOpener.Notifier() {

@Override

public void containerAvailable(Container container) {

// Save container for use by any other activities in the app.

ContainerHolder.setContainer(container);

startMainActivity();

}

});

}
    1. Now using the saved instance we can get the configuration values as follows:
private Container container;

container = ContainerHolder.getContainer();

adjective = container.getString(ADJECTIVE_KEY);

Various methods to read configuration values from the container are summarized in API reference guide.

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

Sudhir Mishra

Sudhir is a Technical Analyst at Tatvic. He likes to develop data-analysis applications and automating the analysis process. Google

One thought on “How to implement Google Tag Manager for Android

  1. kotesh

    Hi,

    I am trying to implement GTM for Android. I am unable to preview it neither i am unable to see any hits being sent. Do you mind sharing any samples or letting me know on how to resolve these issues please. Your response is highly appreciated.

    Thanks,
    Kotesh

    Reply

Leave a Reply

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