Reputation: 1433
I am trying to track how much time user spend on my application in a day and send this data to my server.I want to know is google analytic can do this work for me, i have search about this and i came to know google analytic can measuring screen views allows you to see which content is being viewed most by your users, and how they are navigating between different pieces of content. I want to is google analytic do session tracking?(I didn't find this yet). If google analytic not doing this work can you suggest me how did i do it manually, i just need a rough sketch.
Upvotes: 5
Views: 2269
Reputation: 56
I don't know about Google analitics but without that you could try to employ a system based on onResume() and onPause() methods of the Activity.
Implement BaseActivity as this and extend it with your other activities.
public class BaseActivity extends Activity {
private long start;
@Override
protected void onResume() {
super.onResume();
start = System.currentTimeMillis();
}
@Override
protected void onPause() {
super.onPause();
long elapsedTime = System.currentTimeMillis() - start;
saveSomewhere(elapsedTime); // some storing procedure
}
}
Every time the Activity gets focus onResume is called and start time is acquired. When the activity goes out of focus the onPause is called and elapsedTime is calculated. You just then need to decide how to use and store that time.
Upvotes: 1
Reputation: 1085
Create your own CustomApplication
class and make it extend Application
, then override the onCreate
method, this method will be called when your application is opened, and in there you can send the starttime of the session to your server. Then override the onDestroy
method, which will be called when the application is closed, and in there send the endtime of the session to your server.
Use ID's to match the right starttime to the right endtime. Then on your server you can easily calcute the difference between the endtime and the starttime to determine how long a user has spent on your app. Then you can just add up all the session times from a single day and you'll have the total time a user has spent on your app on a specific day.
Upvotes: 2