Using Google Tag Manager as a key value store

When i first read about the Google Tag Manager my first impression was more analytics and marketing driven but you can use it for more than that! One of these things is to store and receive simple key value pairs inside your app!

In my use case i only want to store some static settings data available in the www and receive them inside my Android app. It is really easy!

Prerequisite: A GTM account and a new created tag container.

The first thing you need to understand is the versioning: You can add, remove and edit everything you want and will get live when you publish this version. The version number will automatically increase afterwards.

In this example i want to implement a force update dialog where the user have to update to a newer app version.

Create the key and put some data in it:

  • Go to your new created container. You will see the container id at the top right corner. It should look like this GTM-ASD123
  • Click on Variables
  • Click on new
  • The default variable name is "Untitled Variable". The name must be unique since you receive this variable later with this name
  • Chose the Type: Constant
  • Write the value you want to receive later. (f.e. {"from":12, "to": 13}
Click on Publish, go to the Version screen and download the new published version. Why should you download the file? You have to provide a base version in your app. This version contains the current version data. If you modify and publish the version later then the app will immediately update to the new available data. 

Lets write code!

Get a TagManager Instance and insert the Container id with the previous downloaded file. Add a callback method to receive a new Tag Manager package if available. The Container will be automatically updated every 12 hours but you can add the .refresh() method to check for a new version container programmatically. 

TagManager tagManager = TagManager.getInstance(context);
PendingResult<ContainerHolder> pending = tagManager
    .loadContainerPreferNonDefault("GTM-ASD123", R.raw.gtm_default);

pending.setResultCallback(new ResultCallback<ContainerHolder>() {
    public void onResult(ContainerHolder containerHolder) {

        // if not success then just log the error.
        if (!containerHolder.getStatus().isSuccess()) {
            Ln.e("TAG MANAGER: failure loading container");
        // handle the current package
            containerHolder, "default");

        // and set a new available listener if a new package is available
            new ContainerLoadedCallback(context));

        // refresh programmatically. This will call the previous listener
}, 2, TimeUnit.SECONDS);

Use .getString(...) to get the defined value.

public static class ContainerLoadedCallback 
    implements ContainerHolder.ContainerAvailableListener {
    private Context mContext;

    public ContainerLoadedCallback(Context context) {
        this.mContext = context;

    public void onContainerAvailable(ContainerHolder containerHolder, String s)     {
        handleContainer(mContext, containerHolder, s);

    public static void handleContainer(Context context,
        ContainerHolder containerHolder, String s) {

        // Receive the data
        String update_force = containerHolder.getContainer()
        // use the data

Thats it. You now have an easy to use key value store. It is really simple and you can implement it in your Android and iOS app at the same time.

Using Google Tag Manager as a key value store Using Google Tag Manager as a key value store Reviewed by Unknown on 10:00 Rating: 5

1 comment:

  1. Your article is brilliant. I like this post, enjoyed this one appreciate it for putting up. Thank You
    Analytics tag manager


Powered by Blogger.