Home » Android » android – Tag Manager: How to load binary default container?

android – Tag Manager: How to load binary default container?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m trying to set up Google Tag Manager as per the Getting Started guide.

When adding a default container, the docs make it clear that using binary default container file (as opposed to JSON) is recommended.

Although using the binary file is recommended, if your container does
not contain rules or tags, you may choose to use a simple JSON file
instead.

I’ve downloaded and added the binary default container under res/raw. But initialising Tag Manager with loadContainerPreferNonDefault() seems to fail.

PendingResult<ContainerHolder> pending =
    tagManager.loadContainerPreferNonDefault(CONTAINER_ID,
    R.raw.gtm_default_container);

I get this error in logs:

 W/GoogleTagManager﹕ Failed to extract the container from the resource file. 
 Resource is a UTF-8 encoded string but doesn't contain a JSON container

TagManager javadocs (the example at the very top) hint that you can pass a boolean param to indicate whether the resource is JSON or binary:

PendingResult pending = tagManager.loadContainerPreferNonDefault(
         myContainerId,    // container ID of the form "GTM-XXXX"
         R.raw.GTM-XXXX,   // the resource ID of the default container
         true);            // the default container is in JSON format (as opposed to binary)

Thing is, there’s no such parameter in loadContainerPreferNonDefault()!
You can optionally pass a Handler but no boolean. Dumbfoundingly, the TagManager javadoc conflicts itself here.

So anyway, is it even possible to do what the Getting Started guide recommends, and use a binary default container?

Using 'com.google.android.gms:play-services-analytics:7.0.0'

How to&Answers:

Sorry for the late answer. Had some struggles with Tag Manager and managed to solve mine. Hope this helps:

Yes, it is possible to use a binary default container. The documentation is conflicting with a previous version of Tag Manager. I believe it’s conflicting with v3. (The current version as of writing this answer is v4).

To load the binary container you need to use a different method.

You need to use TagManager.loadContainerDefaultOnly() this method is intended specifically for testing purposes so that you could test if a container is sending hits even with no container published.

On the other hand TagManager.loadContainerPreferNonDefault() will load it’s binary container and then check the network for a version that is currently published. If it finds a version that is published it will use that version no matter what binary container you have. Example:

You add binary container version15. However you have version14 published. This method will default to 14, since that version is published.

If you wanted to test version 15 you would have to use TagManager.loadContainerDefaultOnly().