Google 在 android 中实施时跟踪代码管理器 v4 出错

Google Tag manager v4 giving error while implementing in android

实施 GTM 时出现以下错误

未能从资源文件中提取容器。资源是 UTF-8 编码的字符串,但不包含 JSON 容器

我已经完成编码如下

TagManager tagManager = TagManager.getInstance(this);

    tagManager.setVerboseLoggingEnabled(true);
    Log.v("tag", "before loading binary file ");
    PendingResult<ContainerHolder> pending = tagManager
            .loadContainerPreferNonDefault(CONTAINER_ID,
                    R.raw.gtm_default_container);
    Log.v("tag", "after loading binary file ");
     pending.setResultCallback(new ResultCallback<ContainerHolder>() {
            @Override
            public void onResult(ContainerHolder containerHolder) {
                Container container = containerHolder.getContainer();
                if (!containerHolder.getStatus().isSuccess()) {
                   // Log.e("CuteAnimals", "failure loading container");
                   // displayErrorToUser("error");

                    return;
                }
                ContainerHolderSingleton.setContainerHolder(containerHolder);
                ContainerLoadedCallback.registerCallbacksForContainer(container);
                containerHolder.setContainerAvailableListener(new ContainerLoadedCallback());
                startMainActivity();
            }
        }, 2, TimeUnit.SECONDS);





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

 private static class CustomTagCallback implements FunctionCallTagCallback {
        @Override
        public void execute(String tagName, Map<String, Object> parameters) {
            // The code for firing this custom tag.
            Log.i("CuteAnimals", "Custom function call tag :" + tagName + " is fired.");
        }
    }
private static class CustomMacroCallback implements FunctionCallMacroCallback {
    private int numCalls;

    @Override
    public Object getValue(String name, Map<String, Object> parameters) {
        if ("increment".equals(name)) {
            return ++numCalls;
        } else if ("mod".equals(name)) {
            return (Long) parameters.get("key1") % Integer.valueOf((String) parameters.get("key2"));
        } else {
            throw new IllegalArgumentException("Custom macro name: " + name + " is not supported.");
        }
    }
}

@Override
protected void onStart() {
    super.onStart();
    Utilities.pushOpenScreenEvent(this, "SplasScreen");
}
@Override
protected void onStop() {
    super.onStop();
    Utilities.pushCloseScreenEvent(this, "Splashscreen");
}

我们如何使用 GTM 跟踪页面浏览量并将其显示在 android

中的 GA 中

您是否已将容器二进制文件添加到您的项目中?解释如下: https://developers.google.com/tag-manager/android/v4/#add

Google Tag Manager uses a default container on the first run of your application. The default container will cease to be used as soon as the app is able to retrieve a fresh container over the network.

To download and add a default container binary to your application, follow these steps:

  1. Sign in to the Google Tag Manager web interface.
  2. Select the Version of the container you'd like to download.
  3. Click the Download button to retrieve the container binary.
  4. Add the downloaded container binary file to your project as a raw resource.
    • If the raw subfolder under /res/ does not exist, create it.
    • Rename the container binary file if necessary. It consists of only lowercase letters, digits, and underscores.
    • Copy the container binary file to the folder /res/raw.

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.

您可以获得二进制版本,但在创建变量、触发器和标记之后,然后发布您的容器。 返回版本面板,select 操作下拉菜单并选择下载。