/** * Tracks this application being launched (and if this happened as the result of the user opening * a push notification, this method sends along information to correlate this open with that * push). * * @param intent The {@code Intent} that started an {@code Activity}, if any. Can be null. * @param callback callback.done(e) is called when the event has been tracked by Parse. */ public static void trackAppOpenedInBackground(Intent intent, SaveCallback callback) { ParseTaskUtils.callbackOnMainThreadAsync(trackAppOpenedInBackground(intent), callback); }
/** * Tracks the occurrence of a custom event with additional dimensions. Parse will store a data * point at the time of invocation with the given event name. Dimensions will allow segmentation * of the occurrences of this custom event. * <p> * To track a user signup along with additional metadata, consider the following: * <pre> * Map<String, String> dimensions = new HashMap<String, String>(); * dimensions.put("gender", "m"); * dimensions.put("source", "web"); * dimensions.put("dayType", "weekend"); * ParseAnalytics.trackEvent("signup", dimensions); * </pre> * There is a default limit of 8 dimensions per event tracked. * * @param name The name of the custom event to report to Parse as having happened. * @return A Task that is resolved when the event has been tracked by Parse. */ public static Task<Void> trackEventInBackground(String name) { return trackEventInBackground(name, (Map<String, String>) null); }
@Override public Task<Void> then(Task<String> task) { String sessionToken = task.getResult(); return getAnalyticsController().trackEventInBackground(name, dimensionsCopy, sessionToken); } });
@Test public void testGetPushHashFromIntentEmptyIntent() throws Exception { Intent intent = new Intent(); Bundle bundle = new Bundle(); JSONObject json = new JSONObject(); json.put("push_hash_wrong_key", "test"); bundle.putString("data_wrong_key", json.toString()); intent.putExtras(bundle); String pushHash = ParseAnalytics.getPushHashFromIntent(intent); assertEquals(null, pushHash); }
@After public void tearDown() { ParseCorePlugins.getInstance().reset(); // Clear LRU cache in ParseAnalytics ParseAnalytics.clear(); controller = null; }
@Test public void testGetPushHashFromIntentEmptyPushHashIntent() throws Exception { Intent intent = new Intent(); Bundle bundle = new Bundle(); JSONObject json = new JSONObject(); json.put("push_hash_wrong_key", "test"); bundle.putString(ParsePushBroadcastReceiver.KEY_PUSH_DATA, json.toString()); intent.putExtras(bundle); String pushHash = ParseAnalytics.getPushHashFromIntent(intent); assertEquals("", pushHash); }
ParseAnalytics.trackAppOpenedInBackground(intent);
/** * Tracks the occurrence of a custom event. Parse will store a data point at the time of * invocation with the given event name. * * @param name The name of the custom event to report to Parse as having happened. * @param callback callback.done(e) is called when the event has been tracked by Parse. */ public static void trackEventInBackground(String name, SaveCallback callback) { ParseTaskUtils.callbackOnMainThreadAsync(trackEventInBackground(name), callback); }
@Test public void testGetPushHashFromIntentWrongPushHashIntent() { Intent intent = new Intent(); Bundle bundle = new Bundle(); bundle.putString(ParsePushBroadcastReceiver.KEY_PUSH_DATA, "error_data"); intent.putExtras(bundle); String pushHash = ParseAnalytics.getPushHashFromIntent(intent); assertEquals(null, pushHash); }
@Override public Task<Void> then(Task<String> task) { String sessionToken = task.getResult(); return getAnalyticsController().trackAppOpenedInBackground(pushHash.get(), sessionToken); } });
@Test public void testTrackAppOpenedInBackgroundNullCallback() throws Exception { Intent intent = makeIntentWithParseData("test"); ParseAnalytics.trackAppOpenedInBackground(intent, null); verify(controller, times(1)).trackAppOpenedInBackground(eq("test"), isNull(String.class)); }
/** * Tracks the occurrence of a custom event with additional dimensions. Parse will store a data * point at the time of invocation with the given event name. Dimensions will allow segmentation * of the occurrences of this custom event. * <p> * To track a user signup along with additional metadata, consider the following: * <pre> * Map<String, String> dimensions = new HashMap<String, String>(); * dimensions.put("gender", "m"); * dimensions.put("source", "web"); * dimensions.put("dayType", "weekend"); * ParseAnalytics.trackEvent("signup", dimensions); * </pre> * There is a default limit of 8 dimensions per event tracked. * * @param name The name of the custom event to report to Parse as having happened. * @param dimensions The dictionary of information by which to segment this event. * @param callback callback.done(e) is called when the event has been tracked by Parse. */ public static void trackEventInBackground(String name, Map<String, String> dimensions, SaveCallback callback) { ParseTaskUtils.callbackOnMainThreadAsync(trackEventInBackground(name, dimensions), callback); }
/** * Tracks this application being launched (and if this happened as the result of the user opening * a push notification, this method sends along information to correlate this open with that * push). * * @param intent The {@code Intent} that started an {@code Activity}, if any. Can be null. * @return A Task that is resolved when the event has been tracked by Parse. */ public static Task<Void> trackAppOpenedInBackground(Intent intent) { String pushHashStr = getPushHashFromIntent(intent); final Capture<String> pushHash = new Capture<>(); if (pushHashStr != null && pushHashStr.length() > 0) { synchronized (lruSeenPushes) { if (lruSeenPushes.containsKey(pushHashStr)) { return Task.forResult(null); } else { lruSeenPushes.put(pushHashStr, true); pushHash.set(pushHashStr); } } } return ParseUser.getCurrentSessionTokenAsync().onSuccessTask(new Continuation<String, Task<Void>>() { @Override public Task<Void> then(Task<String> task) { String sessionToken = task.getResult(); return getAnalyticsController().trackAppOpenedInBackground(pushHash.get(), sessionToken); } }); }
@Test public void testGetAnalyticsController() { assertSame(controller, ParseAnalytics.getAnalyticsController()); }
@Test public void testTrackAppOpenedInBackgroundDuplicatedIntent() throws Exception { Intent intent = makeIntentWithParseData("test"); ParseTaskUtils.wait(ParseAnalytics.trackAppOpenedInBackground(intent)); verify(controller, times(1)).trackAppOpenedInBackground(eq("test"), isNull(String.class)); ParseTaskUtils.wait(ParseAnalytics.trackAppOpenedInBackground(intent)); verify(controller, times(1)).trackAppOpenedInBackground(eq("test"), isNull(String.class)); }
@Test public void testTrackEventInBackgroundNullCallback() { Map<String, String> dimensions = new HashMap<>(); ParseAnalytics.trackEventInBackground("test", dimensions, null); verify(controller, times(1)).trackEventInBackground( eq("test"), eq(dimensions), isNull(String.class)); }
@Test public void testGetPushHashFromIntentNullIntent() { String pushHash = ParseAnalytics.getPushHashFromIntent(null); assertEquals(null, pushHash); }
@Test public void testTrackAppOpenedInBackgroundNormalCallback() throws Exception { Intent intent = makeIntentWithParseData("test"); final Semaphore done = new Semaphore(0); ParseAnalytics.trackAppOpenedInBackground(intent, new SaveCallback() { @Override public void done(ParseException e) { assertNull(e); done.release(); } }); // Make sure the callback is called assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS)); verify(controller, times(1)).trackAppOpenedInBackground(eq("test"), isNull(String.class)); }
@Test public void testTrackEventInBackgroundNormalParameters() throws Exception { Map<String, String> dimensions = new HashMap<>(); dimensions.put("key", "value"); ParseTaskUtils.wait(ParseAnalytics.trackEventInBackground("test", dimensions)); verify(controller, times(1)).trackEventInBackground( eq("test"), eq(dimensions), isNull(String.class)); }
@Test public void testGetPushHashFromIntentNormalIntent() throws Exception { Intent intent = makeIntentWithParseData("test"); String pushHash = ParseAnalytics.getPushHashFromIntent(intent); assertEquals("test", pushHash); }