Skip to content

Instantly share code, notes, and snippets.

@ekzee
Created November 30, 2015 10:32
Show Gist options
  • Save ekzee/2f6c455fc23ee7a31410 to your computer and use it in GitHub Desktop.
Save ekzee/2f6c455fc23ee7a31410 to your computer and use it in GitHub Desktop.
public class GA {
private static volatile GA sInstance;
public static GA from(Context context) {
if (sInstance == null) {
synchronized (GA.class) {
if (sInstance == null) {
sInstance = new GA(context);
}
}
}
return sInstance;
}
private final Context context;
private GA(Context context) {
this.context = context.getApplicationContext();
}
public void screen(@StringRes int screenId) {
if (!App.GA_ENABLED) {
return;
}
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(context) != ConnectionResult.SUCCESS) {
return;
}
Tracker tracker = getTracker();
tracker.setScreenName(context.getString(screenId));
tracker.send(new HitBuilders.ScreenViewBuilder().build());
}
public void event(@StringRes int eventCategoryId, @StringRes int eventActionId) {
event(eventCategoryId, context.getString(eventActionId));
}
@SuppressWarnings("PointlessBooleanExpression")
public void event(@StringRes int eventCategoryId, String eventAction) {
if (!App.GA_ENABLED) {
return;
}
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(context) != ConnectionResult.SUCCESS) {
return;
}
getTracker().send(new HitBuilders.EventBuilder()
.setCategory(context.getString(eventCategoryId))
.setAction(eventAction)
.build());
}
@SuppressWarnings("PointlessBooleanExpression")
public void event(@StringRes int eventCategoryId, @StringRes int eventActionId, String label) {
if (!App.GA_ENABLED) {
return;
}
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(context) != ConnectionResult.SUCCESS) {
return;
}
getTracker().send(new HitBuilders.EventBuilder()
.setCategory(context.getString(eventCategoryId))
.setAction(context.getString(eventActionId))
.setLabel(label)
.build());
}
private Tracker getTracker() {
Tracker tracker = GoogleAnalytics.getInstance(context).newTracker(R.xml.global_tracker);
tracker.enableAdvertisingIdCollection(true);
return tracker;
}
public void event(@StringRes int eventCategoryId, @StringRes int eventActionId, int labelId) {
event(eventCategoryId, eventActionId, context.getString(labelId));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment