Skip to content

Commit

Permalink
fix: catch the case in intercept for identity update (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
qingzhuozhen authored Feb 17, 2023
1 parent 789d591 commit 85084f6
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/main/java/com/amplitude/api/IdentifyInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class IdentifyInterceptor {

private final AmplitudeClient client;

private String userId;
private String deviceId;
private final AtomicBoolean identitySet = new AtomicBoolean(false);

public IdentifyInterceptor (
DatabaseHelper dbHelper,
WorkerThread logThread,
Expand All @@ -50,6 +54,10 @@ public IdentifyInterceptor (
* @return event with potentially more information or null if intercepted
*/
public JSONObject intercept(String eventType, JSONObject event) {
if (isIdentityUpdated(event)) {
// if userId or deviceId is updated, send out the identify for older identity
transferInterceptedIdentify();
}
if (eventType.equals(Constants.IDENTIFY_EVENT)) {
if (isSetOnly(event) && !isSetGroups(event)) {
// intercept and save user properties
Expand Down Expand Up @@ -206,4 +214,36 @@ private boolean isActionOnly(JSONObject event, String action) {
private long saveIdentifyProperties(JSONObject event) {
return dbHelper.addIdentifyInterceptor(event.toString());
}

private boolean isIdentityUpdated(JSONObject event) {
try {
if (!identitySet.getAndSet(true)) {
userId = event.getString("user_id");
deviceId = event.getString("device_id");
return true;
}
boolean isUpdated = false;
if (isIdUpdated(userId, event.getString("user_id"))) {
userId = event.getString("user_id");
isUpdated = true;
}
if (isIdUpdated(deviceId, event.getString("device_id"))) {
deviceId = event.getString("device_id");
isUpdated = true;
}
return isUpdated;
} catch (JSONException e) {
return true;
}
}

private boolean isIdUpdated(String id, String updateId) {
if (id == null && updateId == null) {
return false;
}
if (id == null || updateId == null) {
return true;
}
return !id.equals(updateId);
}
}
57 changes: 57 additions & 0 deletions src/test/java/com/amplitude/api/AmplitudeClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2493,4 +2493,61 @@ public void testMultipleIdentifyWithSetActionAndSetGroup() throws JSONException
assertEquals(0, getIdentifyInterceptorCount());
assertEquals((long)dbHelper.getLastIdentifyInterceptorId(), -1L);
}

@Test
public void testMultipleIdentifyWithSetActionAndUserIdUpdated() throws JSONException {
long [] timestamps = {1000, 1001, 1002, 1003, 1004};
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.setUserId("identify-user-id");
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), 5L);
assertEquals((long)dbHelper.getLongValue(AmplitudeClient.LAST_EVENT_TIME_KEY), timestamps[4]);

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[0]);
assertEquals(event.getLong("sequence_number"), 1);
assertEquals(event.getString("user_id"), "null");
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");
assertTrue(Utils.compareJSONObjects(userProperties.getJSONObject(Constants.AMP_OP_SET), expected));

JSONObject event2 = events.getJSONObject(1);
assertEquals(event2.getString("event_type"), Constants.IDENTIFY_EVENT);
assertEquals(event2.getLong("event_id"), 2);
assertEquals(event2.getLong("timestamp"), timestamps[4]);
assertEquals(event2.getLong("sequence_number"), 5);
assertEquals(event2.getString("user_id"), "identify-user-id");
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 85084f6

Please sign in to comment.