如何在Google Analytics(分析)v4中跟踪片段?

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

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

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> 

这个configuration看起来是启用自动跟踪的活动,但我会认为这不工作任何魔术的碎片? 在这个例子中,我假设“AnalyticsSampleApp ScreenView”屏幕事件将发送如果t.setScreenName(path); 被称为和path是"com.google.android.gms.analytics.samples.mobileplayground.EcommerceFragment"

Solutions Collecting From Web of "如何在Google Analytics(分析)v4中跟踪片段?"

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

你应该做的是设置片段标识符作为屏幕名称,无论何时显示片段。 在示例应用程序中,如果您查看MobilePlayground.java ,则会看到onTabSelected 。 在示例应用程序中,只要屏幕更改,就会调用此函数。 这将是一个很好的地方设置屏幕,并可能发送screenview / appview命中。

让我知道你是否想要更详细的例子。

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