While using Google Analytics for android, if i use
tracker.start("UA-YOUR-ACCOUNT-HERE", 20, this)
then every 20 seconds, events will be dispatched automatically even if i dont do it manually using
tracker.dispatch()
My Question is, what happens if the user quits my application within 20 secs? will it b开发者_开发问答e dispatched?
or do i have to dispatch all the pending events manually when the user is trying to exit?
You don't have to do anything- The events get stored and will be lumped together with the next dispatch that occurs in your app (presumably the next time the user fires up the application).
Note that Analytics servers timestamp the hit based on when they receive the data, not based on when the event actually occurred- So if your users use the app for a couple minutes a day, visits which occurred on the 10th might show up in Analytics on the 11th, etc.
Update: To clarify on the behavior when tracker.stop() is called, it does not dispatch pending events at that point. They stay in an internal sqlite database, and are the first to go out when a dispatch is called in the next run of your application. The reason they aren't fired when the tracker is stopped is that it would add time to the Activity being destroyed, making the app feel less "snappy" on exit. This is also the reason you should think carefully before dispatching in the onDestroy method.
tracker.stop() does not dispatch the data. My advice is to also put tracker.dispatch() in the onDestroy() method
@Override
protected void onDestroy() {
super.onDestroy();
tracker.dispatch();
// Stop the tracker when it is no longer needed.
tracker.stop();
}
source: http://www.google.com/support/forum/p/Google%20Analytics/thread?tid=70a919f5b097f5dc&hl=en
It is recommended that you stop the tracker when your app is destroyed using the following;
@Override
protected void onDestroy() {
super.onDestroy();
// Stop the tracker when it is no longer needed.
tracker.stop();
}
I would assume this would dispatch any waiting events.
This code help you.....
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();
}
}
精彩评论