Skip to content

Commit

Permalink
fix OneBusAway#105 - Google Analytics implementation.
Browse files Browse the repository at this point in the history
1- Event messages moved to values
2- Package names removed from screen names
3- GA active option added to preferences
4- Bug fix when custom region active
  • Loading branch information
cagryInside committed Jan 9, 2015
1 parent 6aa840c commit 53bfe1e
Show file tree
Hide file tree
Showing 18 changed files with 277 additions and 118 deletions.
11 changes: 0 additions & 11 deletions gradle/local.properties

This file was deleted.

11 changes: 0 additions & 11 deletions onebusaway-android/local.properties

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,20 @@ public synchronized Tracker getTracker(TrackerName trackerId) {
private void reportAnalytics() {
if (getCustomApiUrl() == null && getCurrentRegion() != null) {
ObaAnalytics.reportEventWithCategory(ObaAnalytics.ObaEventCategory.APP_SETTINGS.toString(),
"configured_region", "API Region: " + getCurrentRegion().getName());
getString(R.string.analytics_action_configured_region), getString(R.string.analytics_label_region)
+ getCurrentRegion().getName());
} else {
ObaAnalytics.reportEventWithCategory(ObaAnalytics.ObaEventCategory.APP_SETTINGS.toString(),
"configured_region", "API Region: " + getCustomApiUrl());
getString(R.string.analytics_action_configured_region), getString(R.string.analytics_label_region)
+ getCustomApiUrl());
}

if (getCurrentRegion() != null) {
Boolean showExperimentalRegions = getCurrentRegion().getExperimental();
ObaAnalytics.reportEventWithCategory(ObaAnalytics.ObaEventCategory.APP_SETTINGS.toString(),
getString(R.string.analytics_action_configured_region), getString(R.string.analytics_label_region)
+ (showExperimentalRegions ? "YES" : "NO"));
}
Boolean showExperimentalRegions = getCurrentRegion().getExperimental();
ObaAnalytics.reportEventWithCategory(ObaAnalytics.ObaEventCategory.APP_SETTINGS.toString(),
"configured_region", "API Region: " + (showExperimentalRegions ? "YES" : "NO"));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@
package org.onebusaway.android.io;

import android.app.Activity;
import android.content.SharedPreferences;
import android.location.Location;
import android.support.v4.app.Fragment;
import android.util.Log;

import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;

import org.onebusaway.android.R;
import org.onebusaway.android.app.Application;
import org.onebusaway.android.util.PreferenceHelp;

/**
* Analytics class for tracking the app
Expand All @@ -33,6 +37,11 @@

public class ObaAnalytics {

private static final float LOCATION_ACCURACY_THRESHOLD = 0.1f;

/**
* To measure the distance when the bus stop tapped.
*/
public enum ObaStopDistance {

CLOSE("CLOSE"), MEDIUM("MEDIUM"), FAR("FAR");
Expand All @@ -52,10 +61,14 @@ public String toString() {
}
}

/**
* Event categories for segmentation
* app_settings, ui_action, submit is similar with OBA IOS
*/
public enum ObaEventCategory {

APP_SETTINGS("app_settings"), UI_ACTION("ui_action"), ACCESSIBILITY("accessibility"),
SUBMIT("submit"), STOP_ACTION("stop_action");
APP_SETTINGS("app_settings"), UI_ACTION("ui_action"),
SUBMIT("`"), STOP_ACTION("stop_action_region[");

private final String stringValue;

Expand All @@ -68,57 +81,62 @@ public String toString() {
}
}

public static void reportEventWithCategory(String category, String action, String label){
Tracker tracker = Application.get().getTracker(Application.TrackerName.APP_TRACKER);
tracker.send(new HitBuilders.EventBuilder()
.setCategory(category)
.setAction(action)
.setLabel(label)
.build());
}

public static void trackBusStop(String stopName) {

Tracker tracker = Application.get().getTracker(Application.TrackerName.APP_TRACKER);
tracker.send(new HitBuilders.EventBuilder()
.setCategory("Bus Stop")
.setAction("Search")
.setLabel(stopName)
.setValue(1)
.build());
}

public static void trackBusStopByRegion(String region) {

Tracker tracker = Application.get().getTracker(Application.TrackerName.APP_TRACKER);
tracker.send(new HitBuilders.EventBuilder()
.setCategory("Bus Stop")
.setAction("Search")
.setLabel(region)
.setValue(1)
.build());
/**
* Reports events with categories. Helps segmentation in GA admin console.
*
* @param category category name
* @param action action name
* @param label label name
*/
public static void reportEventWithCategory(String category, String action, String label) {
if (isAnalyticsActive()) {
Tracker tracker = Application.get().getTracker(Application.TrackerName.APP_TRACKER);
tracker.send(new HitBuilders.EventBuilder()
.setCategory(category)
.setAction(action)
.setLabel(label)
.build());
}
}

public static void trackBusStopDistance(Location myLocation, Location stopLocation) {
float distanceInMeters = myLocation.distanceTo(stopLocation);
ObaStopDistance stopDistance = null;

if (distanceInMeters < ObaStopDistance.DISTANCE_CLOSE) {
stopDistance = ObaStopDistance.CLOSE;
} else if (distanceInMeters < ObaStopDistance.DISTANCE_MEDIUM) {
stopDistance = ObaStopDistance.MEDIUM;
} else {
stopDistance = ObaStopDistance.FAR;
/**
* Tracks stop tap distance between bus stop location and users current location
*
* @param regionName region name for category
* @param stopId for action
* @param myLocation the users location
* @param stopLocation tapped stop location
*/
public static void trackBusStopDistance(String regionName, String stopId, Location myLocation, Location stopLocation) {
if (isAnalyticsActive()) {
try {
if (myLocation.getAccuracy() > LOCATION_ACCURACY_THRESHOLD) {

float distanceInMeters = myLocation.distanceTo(stopLocation);
ObaStopDistance stopDistance = null;

if (distanceInMeters < ObaStopDistance.DISTANCE_CLOSE) {
stopDistance = ObaStopDistance.CLOSE;
} else if (distanceInMeters < ObaStopDistance.DISTANCE_MEDIUM) {
stopDistance = ObaStopDistance.MEDIUM;
} else {
stopDistance = ObaStopDistance.FAR;
}

Tracker tracker = Application.get().getTracker(Application.TrackerName.APP_TRACKER);
tracker.send(new HitBuilders.EventBuilder()
.setCategory(ObaEventCategory.STOP_ACTION.toString() + regionName + "]")
.setAction("Stop Id: " + stopId)
.setLabel("Search Distance: " + stopDistance.toString())
.setValue(1)
.build());
}

} catch (Exception e) {
//If location comes null
e.printStackTrace();
}
}

Log.v("stopDistance ----> ", stopDistance.toString());
Tracker tracker = Application.get().getTracker(Application.TrackerName.APP_TRACKER);
tracker.send(new HitBuilders.EventBuilder()
.setCategory("Bus Stop")
.setAction("Search")
.setLabel(stopDistance.toString())
.setValue(1)
.build());
}

/**
Expand All @@ -127,7 +145,9 @@ public static void trackBusStopDistance(Location myLocation, Location stopLocati
* @param activity
*/
public static void reportActivityStart(Activity activity) {
GoogleAnalytics.getInstance(activity).reportActivityStart(activity);
if (isAnalyticsActive()) {
GoogleAnalytics.getInstance(activity).reportActivityStart(activity);
}
}

/**
Expand All @@ -136,7 +156,30 @@ public static void reportActivityStart(Activity activity) {
* @param activity
*/
public static void reportActivityStop(Activity activity) {
GoogleAnalytics.getInstance(activity).reportActivityStop(activity);
if (isAnalyticsActive()) {
GoogleAnalytics.getInstance(activity).reportActivityStop(activity);
}
}

/**
* For reporting fragments on Start
*
* @param fragment
*/
public static void reportFragmentStart(Fragment fragment) {
if (isAnalyticsActive()) {
Tracker tracker = Application.get().getTracker(Application.TrackerName.APP_TRACKER);
tracker.setScreenName(fragment.getClass().getSimpleName());
tracker.send(new HitBuilders.ScreenViewBuilder().build());
}
}

/**
* @return is GA enabled or disabled from settings
*/
private static Boolean isAnalyticsActive() {
SharedPreferences settings = Application.getPrefs();
return settings.getBoolean(Application.get().getString(R.string.preferences_key_analytics), Boolean.TRUE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public ObaRegionsTask(Context context, ObaRegionsTask.Callback callback) {
* task, false if it should not
*/
public ObaRegionsTask(Context context, ObaRegionsTask.Callback callback, boolean force,
boolean showProgressDialog) {
boolean showProgressDialog) {
this.mContext = context;
this.mCallback = callback;
mForceReload = force;
Expand Down Expand Up @@ -164,7 +164,10 @@ protected void onPostExecute(ArrayList<ObaRegion> results) {
Log.d(TAG, "Detected closest region '" + closestRegion.getName() + "'");
}
//Analytics
ObaAnalytics.reportEventWithCategory(ObaAnalytics.ObaEventCategory.APP_SETTINGS.toString(), "configured_region_auto", "Set Region Automatically: " + closestRegion.getName());
ObaAnalytics.reportEventWithCategory(ObaAnalytics.ObaEventCategory.APP_SETTINGS.toString(),
mContext.getString(R.string.analytics_action_configured_region_auto),
mContext.getString(R.string.analytics_label_region_auto)
+ closestRegion.getName());
doCallback(true);
} else {
//No region has been set, and we couldn't find a usable region based on RegionUtil.isRegionUsable()
Expand All @@ -181,7 +184,10 @@ protected void onPostExecute(ArrayList<ObaRegion> results) {
}

//Analytics
ObaAnalytics.reportEventWithCategory(ObaAnalytics.ObaEventCategory.APP_SETTINGS.toString(), "configured_region_auto", "Set Region Automatically: " + closestRegion.getName());
ObaAnalytics.reportEventWithCategory(ObaAnalytics.ObaEventCategory.APP_SETTINGS.toString(),
mContext.getString(R.string.analytics_action_configured_region_auto)
, mContext.getString(R.string.analytics_label_region_auto)
+ closestRegion.getName());
doCallback(true);
} else {
doCallback(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,13 @@ public Loader<ObaArrivalInfoResponse> onCreateLoader(int id, Bundle args) {
return new ArrivalsListLoader(getActivity(), mStopId);
}


@Override
public void onStart() {
super.onStart();
ObaAnalytics.reportFragmentStart(this);
}

//
// This is where the bulk of the initialization takes place to create
// this screen.
Expand Down Expand Up @@ -773,7 +780,8 @@ public boolean setFavorite(boolean favorite) {

//Analytics
ObaAnalytics.reportEventWithCategory(ObaAnalytics.ObaEventCategory.UI_ACTION.toString(),
"edit_field", "Edited Bookmark");
getString(R.string.analytics_action_edit_field),
getString(R.string.analytics_label_edit_field));

return mFavorite;
}
Expand Down Expand Up @@ -974,7 +982,9 @@ private void loadMoreArrivals() {
refresh();

//Analytics
ObaAnalytics.reportEventWithCategory(ObaAnalytics.ObaEventCategory.UI_ACTION.toString(), "button_press", "Clicked load more arrivals button");
ObaAnalytics.reportEventWithCategory(ObaAnalytics.ObaEventCategory.UI_ACTION.toString(),
getActivity().getString(R.string.analytics_action_button_press),
getActivity().getString(R.string.analytics_label_button_press));
}

//
Expand Down
Loading

0 comments on commit 53bfe1e

Please sign in to comment.