在Google Analytics v3中,要Activity
自动跟踪,我们需要
ga_autoActivityTracking
标志。onStart
和onStop
public class myTrackedActivity extends Activity {
@Override
public void onStart() {
super.onStart();
... // The rest of your onStart() code.
EasyTracker.getInstance(this).activityStart(this); // Add this method.
}
@Override
public void onStop() {
super.onStop();
... // The rest of your onStop() code.
EasyTracker.getInstance(this).activityStop(this); // Add this method.
}
}
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<!--Replace placeholder ID with your tracking ID-->
<string name="ga_trackingId">UA-XXXX-Y</string>
<!--Enable automatic activity tracking-->
<bool name="ga_autoActivityTracking">true</bool>
<!--Enable automatic exception tracking-->
<bool name="ga_reportUncaughtExceptions">true</bool>
</resources>
但是,当使用较新版本的Google Analytics v4(https://developers.google.com/analytics/devguides/collection/android/v4/)时,我看不到需要在Activity
中添加任何代码。从上面的文档中,我们似乎只需要
ga_autoActivityTracking
标志。<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="ga_sessionTimeout">300</integer>
<!-- Enable automatic Activity measurement -->
<bool name="ga_autoActivityTracking">true</bool>
<!-- The screen names that will appear in reports -->
<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>
但是,经过测试,似乎没有从Activity
自动发送跟踪信息。
Google Analytics V4的文档是否缺少某些内容?
答案 0 :(得分:11)
添加app_tracker.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- The following value should be replaced with correct property id. -->
<string name="ga_trackingId">UA-00000000-1</string>
<!-- catch and report uncaught exceptions from the app -->
<bool name="ga_reportUncaughtExceptions">true</bool>
<integer name="ga_sessionTimeout">300</integer>
<!-- Enable automatic Activity measurement -->
<bool name="ga_autoActivityTracking">true</bool>
<!-- The screen names that will appear in reports -->
<screenName name="com.mypackage.NameActivity">Name Activity</screenName>
</resources>
已添加getTracker
public static Tracker getTracker() {
if (false == isGooglePlayServicesAvailable()) {
return null;
}
if (tracker == null) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(MyApplication.instance());
tracker = analytics.newTracker(R.xml.app_tracker);
}
return tracker;
}
在活动启动期间启用GA
public class MyFragmentActivity extends SherlockFragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Utils.getTracker();
答案 1 :(得分:6)
对于运行API v14(Ice Cream Sandwich)或更高版本的设备,除了在跟踪器配置文件中将ga_autoActivityTracking
设置为true之外,还需要调用enableAutoActivityReports
。我在v14后的设备上确认了这项工作(即在我的Google Analytics控制台中报告了屏幕视图)。
如果您希望自己的应用支持在API 14之前运行的设备,则还必须在reportActivityStop
和onStart
中为onStop
和{{1}}添加对所有活动的调用你想跟踪。我已经确认这适用于pre v14设备。
我只尝试使用活动,而不是片段,并且从上面的一个链接看起来,自动屏幕跟踪看起来不适用于片段。
答案 2 :(得分:1)
您需要在活动/片段代码中添加以下链接的步骤4中提到的以下代码:
// Get tracker.
Tracker t = ((AnalyticsSampleApp) getActivity().getApplication()).getTracker(
TrackerName.APP_TRACKER);
// Set screen name.
// Where path is a String representing the screen name.
t.setScreenName(path);
// Send a screen view.
t.send(new HitBuilders.AppViewBuilder().build());
如果您查看链接:https://developers.google.com/analytics/devguides/collection/android/v4/screens#implementation,那里也会提供一个示例片段片段。
的修改:
抱歉,上述信息仅供人工跟踪
根据此链接:https://developers.google.com/analytics/devguides/collection/android/v4/screens#automatic,
如果在配置XML中打开自动屏幕视图跟踪,则只需执行两个步骤: