Skip to content

Commit

Permalink
fix: patch to prevent set group from identify intercept (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
qingzhuozhen authored Feb 16, 2023
1 parent 3d69336 commit cc2064e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/main/java/com/amplitude/api/IdentifyInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public IdentifyInterceptor (
*/
public JSONObject intercept(String eventType, JSONObject event) {
if (eventType.equals(Constants.IDENTIFY_EVENT)) {
if (isSetOnly(event)) {
if (isSetOnly(event) && !isSetGroups(event)) {
// intercept and save user properties
lastIdentifyInterceptorId = saveIdentifyProperties(event);
scheduleTransfer();
Expand Down Expand Up @@ -186,6 +186,14 @@ private boolean isClearAll(JSONObject event) {
return isActionOnly(event, Constants.AMP_OP_CLEAR_ALL);
}

private boolean isSetGroups(JSONObject event) {
try {
return event.getJSONObject("groups").length() > 0;
} catch (JSONException e) {
return false;
}
}

private boolean isActionOnly(JSONObject event, String action) {
try {
JSONObject userProperties = event.getJSONObject("user_properties");
Expand Down
59 changes: 59 additions & 0 deletions src/test/java/com/amplitude/api/AmplitudeClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2434,4 +2434,63 @@ public void testUploadEventsSendInterceptedIdentify() throws JSONException {
assertEquals((long)dbHelper.getLastIdentifyInterceptorId(), -1L);
assertEquals((long)dbHelper.getLongValue(AmplitudeClient.LAST_IDENTIFY_ID_KEY), 1L);
}

@Test
public void testMultipleIdentifyWithSetActionAndSetGroup() throws JSONException {
long [] timestamps = {1000, 1001, 1002, 1003, 1004, 1005};
clock.setTimestamps(timestamps);
ShadowLooper looper = Shadows.shadowOf(amplitude.logThread.getLooper());
looper.runToEndOfTasks();
amplitude.identify(new Identify().set("key1", "key1-value1").set("key2", "key2-value1").set("key3", "key3-value1"));
amplitude.identify(new Identify().set("key1", "key1-value2").set("key4", "key4-value1"));
amplitude.identify(new Identify().set("key2", "key2-value2"));
amplitude.identify(new Identify().set("key3", "key3-value2").set("key4", "key4-value2"));
amplitude.setGroup("test-group-type", "test-group-value");
amplitude.identify(new Identify().set("key3", "key3-value3").set("key4", "key4-value3"));
looper.runToEndOfTasks();
DatabaseHelper dbHelper = DatabaseHelper.getDatabaseHelper(context);
assertEquals(1L, getUnsentIdentifyCount());
assertEquals(1L, getIdentifyInterceptorCount());
assertEquals((long)dbHelper.getLongValue(AmplitudeClient.LAST_IDENTIFY_ID_KEY), 1L);
assertEquals((long) dbHelper.getLongValue(AmplitudeClient.SEQUENCE_NUMBER_KEY), 6L);
assertEquals((long)dbHelper.getLongValue(AmplitudeClient.LAST_EVENT_TIME_KEY), timestamps[5]);

looper.runToEndOfTasks();
looper.runToEndOfTasks();
RecordedRequest request = runRequest(amplitude);
JSONArray events = getEventsFromRequest(request);
assertEquals(events.length(), 2);
JSONObject event = events.getJSONObject(0);
assertEquals(event.getString("event_type"), Constants.IDENTIFY_EVENT);
assertEquals(event.getLong("event_id"), 1);
assertEquals(event.getLong("timestamp"), timestamps[4]);
assertEquals(event.getLong("sequence_number"), 5);
JSONObject userProperties = event.getJSONObject("user_properties");
assertEquals(userProperties.length(), 1);
JSONObject expected = new JSONObject();
expected.put("key1", "key1-value2");
expected.put("key2", "key2-value2");
expected.put("key3", "key3-value2");
expected.put("key4", "key4-value2");
expected.put("test-group-type", "test-group-value");
JSONObject expectedGroups = new JSONObject();
expectedGroups.put("test-group-type", "test-group-value");
assertTrue(Utils.compareJSONObjects(userProperties.getJSONObject(Constants.AMP_OP_SET), expected));
assertTrue(Utils.compareJSONObjects(event.getJSONObject("groups"), expectedGroups));

JSONObject event2 = events.getJSONObject(1);
assertEquals(event2.getString("event_type"), Constants.IDENTIFY_EVENT);
assertEquals(event2.getLong("event_id"), 2);
assertEquals(event2.getLong("timestamp"), timestamps[5]);
assertEquals(event2.getLong("sequence_number"), 6);
JSONObject userProperties2 = event2.getJSONObject("user_properties");
assertEquals(userProperties2.length(), 1);
JSONObject expected2 = new JSONObject();
expected2.put("key3", "key3-value3");
expected2.put("key4", "key4-value3");
assertTrue(Utils.compareJSONObjects(userProperties2.getJSONObject(Constants.AMP_OP_SET), expected2));

assertEquals(0, getIdentifyInterceptorCount());
assertEquals((long)dbHelper.getLastIdentifyInterceptorId(), -1L);
}
}

0 comments on commit cc2064e

Please sign in to comment.