Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing Google Tag Manager into Android

I am trying to implement Google Tag Manager by following this tutorial: https://developers.google.com/tag-manager/android/v4/

When I run the application, I get in the console these errors :

    05-22 12:27:44.490    V/GoogleTagManager﹕ Attempting to load a container from the resource ID 2131099650 (testconverteo.ismail.converteotag:raw/gtm_default_container)
    05-22 12:27:44.520    E/GoogleTagManager﹕ Invalid macro: _gtm.loadEventEnabled
    05-22 12:27:44.520    V/GoogleTagManager﹕ loadAfterDelay: containerId=GTM-XXXXX delay=29397190
    05-22 12:27:44.530    E/GoogleTagManager﹕ Invalid macro: _gtm.loadEventEnabled
    05-22 12:27:44.490    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

And I have no data in Google Analytics in real time, even more nothing which proves that my application is tracked.

If anybody has had the same problem , or he had followed the same steps and knows how to resolve this issue, please leave an explanation in comment.

The steps I've done:

  • I set up the Google Play Services SDK by adding in the builde.gradle (module: app) this part of code: compile 'com.google.android.gms:play-services:7.3.0'
  • I add the permissions to the AndroidManifest.xml file
  • I download and add a default container binary to the application
  • I Create a splash screen activity where I initialize GTM by following the tutorial’s pattern
  • I’ve pushed an event into a dataLayer

What I currently have in my splash screen activity is the following:

public class SplashScreenActivity extends Activity {

    private static final String CONTAINER_ID = "GTM-XXXX";

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splashscreen);

        TagManager tagManager = TagManager.getInstance(this);
        tagManager.setVerboseLoggingEnabled(true);

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

        pending.setResultCallback(new ResultCallback<ContainerHolder>() {
            @Override
            public void onResult(ContainerHolder containerHolder) {
                ContainerHolderSingleton.setContainerHolder(containerHolder);
                Container container = containerHolder.getContainer();
                if (!containerHolder.getStatus().isSuccess()) {
                    Log.e("IL", "failure loading container");
                    return;
                }
                ContainerHolderSingleton.setContainerHolder(containerHolder);
            ContainerLoadedCallback.registerCallbacksForContainer(container);
            containerHolder.setContainerAvailableListener(new ContainerLoadedCallback());
                startMainActivity();
            }
        }, 2, TimeUnit.SECONDS);
    }

    private void startMainActivity() {
        Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
        startActivity(intent);
    }

    private static class ContainerLoadedCallback implements ContainerHolder.ContainerAvailableListener {
        @Override
        public void onContainerAvailable(ContainerHolder containerHolder, String containerVersion) {
            // We load each container when it becomes available.
            Container container = containerHolder.getContainer();
            registerCallbacksForContainer(container);
        }

        public static void registerCallbacksForContainer(Container container) {
            // Register two custom function call macros to the container.
            container.registerFunctionCallMacroCallback("increment", new CustomMacroCallback());
            container.registerFunctionCallMacroCallback("mod", new CustomMacroCallback());
            // Register a custom function call tag to the container.
            container.registerFunctionCallTagCallback("custom_tag", new CustomTagCallback());
        }

    }

}

And in the main Activity:

public void onStart() {
    super.onStart();
    DataLayer dataLayer = TagManager.getInstance(this).getDataLayer();
    dataLayer.pushEvent("Application ouverte", DataLayer.mapOf("login page","un de plus"));
}

And I’ve also created this class:

public class ContainerHolderSingleton {

    private static ContainerHolder containerHolder;

    /**
     * Utility class; don't instantiate.
     */
    private ContainerHolderSingleton() {
    }

    public static ContainerHolder getContainerHolder() {
        return containerHolder;
    }

    public static void setContainerHolder(ContainerHolder c) {
        containerHolder = c;
    }
}

Please note I've hidden the Google Tag Manager container ID

Let me know if anybody wants more information

Thank you

like image 840
Mallasse Avatar asked May 22 '15 13:05

Mallasse


1 Answers

Replace line compile 'com.google.android.gms:play-services:7.3.0' with compile 'com.google.android.gms:play-services:7.0.0' or compile 'com.google.android.gms:play-services-analytics:7.0.0' (that's even better as it just includes ga and gtm related stuff)

I was lucky enough to start with 7.0.0 and saw gtm working. Then upgraded to 7.3.0 and it just stopped working. It might be fixed with new gms lib.

All that assuming you've configured your container. If not read more about that here https://support.google.com/tagmanager/answer/6103657?hl=en&ref_topic=3441530

like image 66
fada21 Avatar answered Sep 20 '22 09:09

fada21