在Google

时间:2015-05-22 13:04:48

标签: android android-studio google-tag-manager

我正在尝试按照本教程实施Google跟踪代码管理器:https://developers.google.com/tag-manager/android/v4/

当我运行应用程序时,我会在控制台中遇到这些错误:

    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

我在Google Analytics中没有实时数据,甚至没有任何数据证明我的应用程序已被跟踪。

如果有人遇到同样的问题,或者他已按照相同的步骤知道如何解决此问题,请在评论中留下解释。

我已经完成的步骤:

  • 我通过在builde.gradle(module:app)中添加以下部分代码来设置Google Play Services SDK: compile 'com.google.android.gms:play-services:7.3.0'
  • 我将权限添加到AndroidManifest.xml文件
  • 我下载并向应用程序添加默认容器二进制文件
  • 我创建了一个启动画面活动,我按照教程的模式初始化GTM
  • 我已将事件推送到dataLayer

我目前在启动画面活动中的内容如下:

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());
        }

    }

}

主要活动:

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

我也创建了这个类:

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;
    }
}

请注意我已隐藏Google跟踪代码管理器容器ID

如果有人想要更多信息,请告诉我

谢谢

1 个答案:

答案 0 :(得分:5)

替换线 compile 'com.google.android.gms:play-services:7.3.0'compile 'com.google.android.gms:play-services:7.0.0' 要么 compile 'com.google.android.gms:play-services-analytics:7.0.0'(这更好,因为它只包含ga和gtm相关的东西)

我很幸运,从7.0.0开始,看到gtm工作。然后升级到7.3.0,它就停止了工作。它可能用新的gms lib修复。

假设您已经配置了容器。如果没有在此处详细了解https://support.google.com/tagmanager/answer/6103657?hl=en&ref_topic=3441530