随着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>
此配置似乎是为活动启用自动跟踪,但我认为这对Fragments没有任何魔力?在此示例中,我假设如果调用t.setScreenName(path);
并且路径为"com.google.android.gms.analytics.samples.mobileplayground.EcommerceFragment"
,则会发送“AnalyticsSampleApp ScreenView”屏幕事件?
答案 0 :(得分:5)
是的,您必须使用您发布的链接中描述的解决方案。造成这种情况的主要原因是因为片段的生命周期不像活动那么简单。 Android不提供片段生命周期的回调。
每当片段显示时,您应该将片段标识符设置为屏幕名称。在示例应用中,如果您查看MobilePlayground.java
,则会看到onTabSelected
。在示例应用程序中,只要屏幕发生变化,就会调用此函数。这将是设置屏幕并可能发送screenview
/ appview
点击的好地方。
如果您需要更详细的示例,请与我们联系。
答案 1 :(得分:0)
使用Google's docs中的此代码。
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());
}
});
}
}