Android Google跟踪代码管理器v4 ContainerLoadedCallback

时间:2015-04-09 08:59:30

标签: android google-tag-manager

我正在尝试按照此网页上的教程实施Google跟踪代码管理器:

https://developers.google.com/tag-manager/android/v4/#init

但是我似乎无法找到ContainerLoadedCallback应该做什么或者这个类的源位于何处。非常感谢任何帮助

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

        PendingResult<ContainerHolder> pending =
                App.getTagManager().loadContainerPreferNonDefault(C.Const.GTM_CONTAINER_ID,
                        R.raw.gtm_default_container);

        // The onResult method will be called as soon as one of the following happens:
//     1. a saved container is loaded
//     2. if there is no saved container, a network container is loaded
//     3. the request times out. The example below uses a constant to manage the timeout period.
        // The onResult method will be called as soon as one of the following happens:
        //     1. a saved container is loaded
        //     2. if there is no saved container, a network container is loaded
        //     3. the request times out. The example below uses a constant to manage the timeout period.
        pending.setResultCallback(new ResultCallback<ContainerHolder>() {
            @Override
            public void onResult(ContainerHolder containerHolder) {
                ContainerHolderSingleton.setContainerHolder(containerHolder);
                Container container = containerHolder.getContainer();
                if (!containerHolder.getStatus().isSuccess()) {
                    Log.e("CuteAnimals", "failure loading container");

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

1 个答案:

答案 0 :(得分:10)

它已在cuteanimals示例中实现,您可以在sdk文件夹中找到它(假设您已安装Google Play服务)

<SDK Dir>/extras/google/google_play_services/samples/tagmanager/cuteanimals/.../cuteanimals/SplashScreenActivity.java

段:

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 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.");
        }
    }
}

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.");
    }
}