使用Google Analytics for android时,如果我使用
tracker.start("UA-YOUR-ACCOUNT-HERE", 20, this)
然后每20秒,即使我不使用
手动执行,也会自动调度事件tracker.dispatch()
我的问题是,如果用户在20秒内退出我的应用程序会怎样? 它会被派遣吗?
或者我必须在用户尝试退出时手动调度所有待处理事件吗?
答案 0 :(得分:6)
您无需执行任何操作 - 事件将被存储,并将与应用程序中发生的下一次调度(可能是下次用户启动应用程序时)混为一谈。
请注意,Google Analytics服务器根据收到数据的时间戳记时间戳,而不是基于事件实际发生的时间 - 因此,如果您的用户每天使用该应用程序几分钟,那么10日发生的访问可能会显示在11日的分析等等。
更新: 为了澄清tracker.stop()被调用时的行为,不在那时派遣待处理事件。它们保留在内部sqlite数据库中,并且是第一个在下次运行应用程序时调用调度时出去的数据库。当跟踪器停止时它们没有被触发的原因是它会为被销毁的Activity增加时间,使得应用程序在退出时感觉不那么“活泼”。这也是您在调度onDestroy方法之前应该仔细考虑的原因。
答案 1 :(得分:4)
tracker.stop()不会调度数据。我的建议是将tracker.dispatch()放在onDestroy()方法
中 @Override
protected void onDestroy() {
super.onDestroy();
tracker.dispatch();
// Stop the tracker when it is no longer needed.
tracker.stop();
}
源: http://www.google.com/support/forum/p/Google%20Analytics/thread?tid=70a919f5b097f5dc&hl=en
答案 2 :(得分:2)
建议您在使用以下内容销毁应用时停止跟踪器;
@Override
protected void onDestroy() {
super.onDestroy();
// Stop the tracker when it is no longer needed.
tracker.stop();
}
我认为这会派遣任何等待事件。
答案 3 :(得分:1)
此代码可以帮助您.....
public class TestActivity extends Activity {
GoogleAnalyticsTracker tracker;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tracker = GoogleAnalyticsTracker.getInstance();
// Start the tracker in manual dispatch mode...
tracker.startNewSession("UA-33332745-1", this);
setContentView(R.layout.main);
Button createEventButton = (Button)findViewById(R.id.NewEventButton);
createEventButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tracker.trackEvent(
"Clicks", // Category
"Button", // Action
"clicked", // Label
77); // Value
}
});
Button createPageButton = (Button)findViewById(R.id.NewPageButton);
createPageButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Add a Custom Variable to this pageview, with name of "Medium" and value "MobileApp"
tracker.setCustomVar(1, "Medium", "Mobile App");
tracker.trackPageView("/testApplicationHomeScreen");
}
});
Button quitButton = (Button)findViewById(R.id.QuitButton);
quitButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
Button dispatchButton = (Button)findViewById(R.id.DispatchButton);
dispatchButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tracker.dispatch();
}
});
}
protected void onDestroy() {
super.onDestroy();
// Stop the tracker when it is no longer needed.
tracker.stopSession();
}
}