使用Google跟踪代码管理器向Google Analytics发送无法使用的事件

时间:2017-07-30 07:40:30

标签: java android google-analytics google-tag-manager

我想实施Google跟踪代码管理器以跟踪用户与我的应用互动,我已按照Google Docs的指南进行操作,但我遇到了一些问题。

这是我的主要活动

public class MainActivity extends AppCompatActivity {
private static final String CONTAINER_ID = "GTM-TRFZVD5";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TagManager tagManager = TagManager.getInstance(this);
    tagManager.setVerboseLoggingEnabled(true);
    PendingResult<ContainerHolder> pendingResult =
            tagManager.loadContainerPreferNonDefault(CONTAINER_ID, R.raw.gtm_trfzvd5);

    pendingResult.setResultCallback(new ResultCallback<ContainerHolder>() {
        @Override
        public void onResult(@NonNull ContainerHolder containerHolder) {
            ContainerHolderSingleton.setContainerHolder(containerHolder);
            Container container = containerHolder.getContainer();
            if(!containerHolder.getStatus().isSuccess()){
                Log.e(MainActivity.class.getSimpleName(), "Failure loading container");
                return;
            }
            ContainerLoadedCallback.registerCallbackForContainer(container);
            containerHolder.setContainerAvailableListener(new ContainerLoadedCallback());
            startAnotherActivity();
        }
    }, 2, java.util.concurrent.TimeUnit.SECONDS);

}

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

这是用户将要互动的活动

public class AnotherActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_another);
    Button btn = (Button) findViewById(R.id.btn);

    GTMUtils.pushOpenScreenEvent(getApplicationContext(), AnotherActivity.class.getSimpleName());

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            GTMUtils.pushClickButtonEvent(getApplicationContext(),
                    "btn");

        }
    });
}

这是将事件推送到Google Analytics的两种方法

public static void pushOpenScreenEvent(Context context, String screenName){
    DataLayer dataLayer = TagManager.getInstance(context).getDataLayer();
    dataLayer.push(DataLayer.mapOf("event", "openScreen", "screenName", screenName));
}


public static void pushClickButtonEvent(Context context, String btnEventName){
    DataLayer dataLayer = TagManager.getInstance(context).getDataLayer();
    dataLayer.push(DataLayer.mapOf("event", "videoPlay", "videoName", btnEventName));

}

我完全从谷歌指南中复制,但是当我运行应用程序时,它只推送了第一个事件(&#34;打开屏幕&#34;)并且点击按钮没有发送任何内容。 那我做错了什么?

0 个答案:

没有答案