public
anonymous / Tracker.java
Created

Google analytics singleton wrapper, provides a second based split between application visits.

  • Download Gist
Tracker.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
import android.content.Context;
import com.application.R;
import com.google.android.apps.analytics.GoogleAnalyticsTracker;
 
public class Tracker {
private static Tracker singletonTracker;
public static final int VISIT_IDLE_LIMIT = 900; //in seconds ... 15 minuttes
public static Tracker getInstance(Context ctx) {
if(singletonTracker == null)
{
singletonTracker = new Tracker(ctx);
}
return singletonTracker;
}
private GoogleAnalyticsTracker analytics;
private long lastTime;
private Context applicationContext;
public Tracker(Context ctx) {
applicationContext = ctx;
newVisit();
}
private void newVisit()
{
lastTime = System.currentTimeMillis();
analytics = GoogleAnalyticsTracker.getInstance();
 
analytics.startNewSession(applicationContext.getString(R.string.analytics_id), 20, applicationContext);
}
public void trackPageView(String url) {
long now = System.currentTimeMillis();
if((now - lastTime)/1000 > VISIT_IDLE_LIMIT)
{
analytics.stopSession();
newVisit();
}
lastTime = now;
analytics.trackPageView(url);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.