diff --git a/astrid/src/com/todoroo/astrid/service/abtesting/ABTestEventReportingService.java b/astrid/src/com/todoroo/astrid/service/abtesting/ABTestEventReportingService.java index ee1ba7d97..8697b54f6 100644 --- a/astrid/src/com/todoroo/astrid/service/abtesting/ABTestEventReportingService.java +++ b/astrid/src/com/todoroo/astrid/service/abtesting/ABTestEventReportingService.java @@ -21,10 +21,12 @@ import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.andlib.sql.Order; import com.todoroo.andlib.sql.Query; +import com.todoroo.andlib.utility.Preferences; import com.todoroo.astrid.dao.ABTestEventDao; import com.todoroo.astrid.data.ABTestEvent; import com.todoroo.astrid.service.StartupService; import com.todoroo.astrid.service.StatisticsService; +import com.todoroo.astrid.service.TaskService; /** * Service to manage the reporting of launch events for AB testing. @@ -44,6 +46,11 @@ public final class ABTestEventReportingService { private static final String KEY_DAYS = "days"; private static final String KEY_DATE = "date"; + private static final String KEY_INITIAL = "initial"; + private static final String KEY_ACTIVATION = "activation"; + + private static final String PREF_REPORTED_ACTIVATION = "p_reported_activation"; + @Autowired private ABTestEventDao abTestEventDao; @@ -53,6 +60,9 @@ public final class ABTestEventReportingService { @Autowired private ABTests abTests; + @Autowired + private TaskService taskService; + public ABTestEventReportingService() { DependencyInjectionService.getInstance().inject(this); } @@ -69,6 +79,7 @@ public final class ABTestEventReportingService { try { abTestEventDao.createRelativeDateEvents(); pushAllUnreportedABTestEvents(); + reportUserActivation(); } catch (SQLiteException e) { StartupService.handleSQLiteError(context, e); } @@ -86,7 +97,7 @@ public final class ABTestEventReportingService { if (unreported.getCount() > 0) { try { JSONArray payload = jsonArrayFromABTestEvents(unreported); - abTestInvoker.post(payload); + abTestInvoker.post(ABTestInvoker.AB_RETENTION_METHOD, payload); ABTestEvent model = new ABTestEvent(); for (unreported.moveToFirst(); !unreported.isAfterLast(); unreported.moveToNext()) { model.readFromCursor(unreported); @@ -104,6 +115,29 @@ public final class ABTestEventReportingService { } } + private void reportUserActivation() { + synchronized (ABTestEventReportingService.class) { + if (StatisticsService.dontCollectStatistics()) + return; + if (Preferences.getBoolean(PREF_REPORTED_ACTIVATION, false) || !taskService.getUserActivationStatus()) + return; + + final TodorooCursor variants = abTestEventDao.query(Query.select(ABTestEvent.PROPERTIES) + .groupBy(ABTestEvent.TEST_NAME)); + try { + JSONArray payload = jsonArrayForActivationAnalytics(variants); + abTestInvoker.post(ABTestInvoker.AB_ACTIVATION_METHOD, payload); + Preferences.setBoolean(PREF_REPORTED_ACTIVATION, true); + } catch (JSONException e) { + handleException(e); + } catch (IOException e) { + handleException(e); + } finally { + variants.close(); + } + } + } + public JSONArray getTestsWithVariantsArray() { JSONArray array = new JSONArray(); Set tests = abTests.getAllTestKeys(); @@ -159,4 +193,21 @@ public final class ABTestEventReportingService { return result; } + private static JSONArray jsonArrayForActivationAnalytics(TodorooCursor events) throws JSONException { + JSONArray result = new JSONArray(); + + ABTestEvent model = new ABTestEvent(); + for (events.moveToFirst(); !events.isAfterLast(); events.moveToNext()) { + JSONObject event = new JSONObject(); + event.put(KEY_TEST, model.getValue(ABTestEvent.TEST_NAME)); + event.put(KEY_VARIANT, model.getValue(ABTestEvent.TEST_VARIANT)); + if (model.getValue(ABTestEvent.ACTIVATED_USER) > 0) + event.put(KEY_INITIAL, true); + else + event.put(KEY_ACTIVATION, true); + result.put(event); + } + return result; + } + } diff --git a/astrid/src/com/todoroo/astrid/service/abtesting/ABTestInvoker.java b/astrid/src/com/todoroo/astrid/service/abtesting/ABTestInvoker.java index 379f2080c..c4b93cc65 100644 --- a/astrid/src/com/todoroo/astrid/service/abtesting/ABTestInvoker.java +++ b/astrid/src/com/todoroo/astrid/service/abtesting/ABTestInvoker.java @@ -35,8 +35,10 @@ import com.todoroo.astrid.service.StatisticsService; public class ABTestInvoker { /** NOTE: these values are development values and will not work on production */ - private static final String URL = "http://analytics.astrid.com/api/2/ab_retention"; - private static final String ACQ_URL = "http://analytics.astrid.com/api/2/acquisition"; + private static final String URL = "http://analytics.astrid.com/api/2/"; + public static final String AB_RETENTION_METHOD = "ab_retention"; + public static final String AB_ACTIVATION_METHOD = "ab_activation"; + private static final String ACQUISITION_METHOD = "acquisition"; private static final String API_KEY = "ryyubd"; private static final String API_SECRET = "q9ef3i"; @@ -56,7 +58,7 @@ public class ABTestInvoker { public void run() { try { HttpEntity postData = createPostData(null); - restClient.post(ACQ_URL, postData); + restClient.post(URL + ACQUISITION_METHOD, postData); Preferences.setBoolean(PREF_REPORTED_ACQUISITION, true); } catch (IOException e) { // Ignored @@ -73,10 +75,10 @@ public class ABTestInvoker { * @return * @throws IOException */ - public JSONObject post(JSONArray payload) throws IOException { + public JSONObject post(String method, JSONArray payload) throws IOException { try { HttpEntity postData = createPostData(payload); - String response = restClient.post(URL, postData); + String response = restClient.post(URL + method, postData); JSONObject object = new JSONObject(response); if (object.getString("status").equals("error")) { throw new IOException("Error reporting ABTestEvent: " +