Created
September 27, 2011 00:13
-
-
Save anonymous/1243850 to your computer and use it in GitHub Desktop.
Google analytics singleton wrapper, provides a second based split between application visits.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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