Skip to content

Instantly share code, notes, and snippets.

Created September 27, 2011 00:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1243850 to your computer and use it in GitHub Desktop.
Save anonymous/1243850 to your computer and use it in GitHub Desktop.
Google analytics singleton wrapper, provides a second based split between application visits.
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment