16

随着 Google Analytics v4 (Android) 的发布,跟踪片段视图的推荐方法是什么?这个解决方案仍然是推荐的方式吗 - https://stackoverflow.com/a/19284014/413254

文档中的示例(https://developers.google.com/analytics/devguides/collection/android/v4/#analytics-xml)具有以下配置:

global_tracker.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="ga_sessionTimeout">300</integer>
    <bool name="ga_autoActivityTracking">true</bool>
    <screenName name="com.google.android.gms.analytics.samples.mobileplayground.ScreenviewFragment">
        AnalyticsSampleApp ScreenView
    </screenName>
    <screenName name="com.google.android.gms.analytics.samples.mobileplayground.EcommerceFragment">
        AnalyticsSampleApp EcommerceView
    </screenName>
    <!--  The following value should be replaced with correct property id. -->
    <string name="ga_trackingId">UA-XXXXXXX-Y</string>
</resources>

此配置似乎启用了活动的自动跟踪,但我认为这对片段没有任何魔力?在这个例子中,我假设“AnalyticsSampleApp ScreenView”屏幕事件如果 t.setScreenName(path);被调用并且路径是"com.google.android.gms.analytics.samples.mobileplayground.EcommerceFragment"

4

2 回答 2

5

是的,您必须使用您发布的链接中描述的解决方案。主要原因是片段的生命周期不像活动的生命周期那么简单。Android 不提供片段生命周期的回调。

您应该做的是在显示片段时将片段标识符设置为屏幕名称。在示例应用程序中,如果您查看MobilePlayground.java,您会看到onTabSelected。在示例应用程序中,只要屏幕发生变化,就会调用此函数。那将是设置屏幕并可能发送screenview/appview点击的好地方。

如果您需要更详细的示例,请告诉我。

于 2014-04-02T23:31:24.873 回答
0

使用Google 文档中的此代码。

package com.google.android.apps.mobileplayground;

import com.google.android.apps.mobileplayground.AnalyticsSampleApp.TrackerName;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

/**
 * Class to exercise Event hits.
 */
public class EventFragment extends Fragment {

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    View view = inflater.inflate(R.layout.event, container, false);

    setupEvent(view, R.id.video1Play, R.string.videoCategory, R.string.videoPlay, R.string.video1);
    setupEvent(view, R.id.video1Pause, R.string.videoCategory, R.string.videoPause,
        R.string.video1);
    setupEvent(view, R.id.video2Play, R.string.videoCategory, R.string.videoPlay, R.string.video2);
    setupEvent(view, R.id.video2Pause, R.string.videoCategory, R.string.videoPause,
        R.string.video2);

    setupEvent(view, R.id.book1View, R.string.bookCategory, R.string.bookView, R.string.book1);
    setupEvent(view, R.id.book1Share, R.string.bookCategory, R.string.bookShare, R.string.book1);

    final Button dispatchButton = (Button) view.findViewById(R.id.eventDispatch);
    dispatchButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        // Manually start a dispatch (Unnecessary if the tracker has a dispatch interval)
        GoogleAnalytics.getInstance(getActivity().getApplicationContext()).dispatchLocalHits();
      }
    });
    return view;
  }

  private void setupEvent(View v, int buttonId, final int categoryId, final int actionId,
      final int labelId) {
    final Button pageviewButton = (Button) v.findViewById(buttonId);
    pageviewButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        // Get tracker.
        Tracker t = ((AnalyticsSampleApp) getActivity().getApplication()).getTracker(
            TrackerName.APP_TRACKER);
        // Build and send an Event.
        t.send(new HitBuilders.EventBuilder()
            .setCategory(getString(categoryId))
            .setAction(getString(actionId))
            .setLabel(getString(labelId))
            .build());
      }
    });
  }
}
于 2014-07-15T21:41:30.300 回答