-
Notifications
You must be signed in to change notification settings - Fork 363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
prefix device-specific distinct IDs with '$device:' #810
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
import com.mixpanel.android.util.HttpService; | ||
import com.mixpanel.android.util.RemoteService; | ||
|
||
import org.hamcrest.CoreMatchers; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: redundant |
||
import org.json.JSONArray; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
@@ -36,11 +37,14 @@ | |
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNotSame; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
import static org.hamcrest.CoreMatchers.*; | ||
|
||
@RunWith(AndroidJUnit4.class) | ||
@LargeTest | ||
|
@@ -79,12 +83,14 @@ public void testGeneratedDistinctId() { | |
String fakeToken = UUID.randomUUID().toString(); | ||
MixpanelAPI mixpanel = new TestUtils.CleanMixpanelAPI(InstrumentationRegistry.getInstrumentation().getContext(), mMockPreferences, fakeToken); | ||
String generatedId1 = mixpanel.getDistinctId(); | ||
assertTrue(generatedId1 != null); | ||
assertThat(generatedId1, startsWith("$device:")); | ||
assertEquals(generatedId1, "$device:" + mixpanel.getAnonymousId()); | ||
|
||
mixpanel.reset(); | ||
String generatedId2 = mixpanel.getDistinctId(); | ||
assertTrue(generatedId2 != null); | ||
assertTrue(!generatedId1.equals(generatedId2)); | ||
assertThat(generatedId2, startsWith("$device:")); | ||
assertEquals(generatedId2, "$device:" + mixpanel.getAnonymousId()); | ||
assertNotEquals(generatedId1, generatedId2); | ||
} | ||
|
||
@Test | ||
|
@@ -512,6 +518,7 @@ protected AnalyticsMessages getAnalyticsMessages() { | |
assertNull(anonymousUpdates.poll(POLL_WAIT_SECONDS, TimeUnit.SECONDS)); | ||
assertNull(peopleUpdates.poll(POLL_WAIT_SECONDS, TimeUnit.SECONDS)); | ||
|
||
String deviceId = mixpanel.getAnonymousId(); | ||
mixpanel.identify("Personal Identity"); | ||
people.set("the prop identified", "prop value identified"); | ||
|
||
|
@@ -528,6 +535,88 @@ protected AnalyticsMessages getAnalyticsMessages() { | |
for (int i=0; i < data.length(); i++) { | ||
JSONObject j = data.getJSONObject(i); | ||
assertEquals("Personal Identity", j.getString("$distinct_id")); | ||
assertEquals(deviceId, j.getString("$device_id")); | ||
} | ||
} | ||
|
||
@Test | ||
public void testIdentifyAfterSetToAnonymousId() throws InterruptedException, JSONException { | ||
String token = "TEST TOKEN testIdentifyAfterSet"; | ||
final List<AnalyticsMessages.MixpanelDescription> messages = new ArrayList<AnalyticsMessages.MixpanelDescription>(); | ||
final BlockingQueue<JSONObject> anonymousUpdates = new LinkedBlockingQueue(); | ||
final BlockingQueue<JSONObject> peopleUpdates = new LinkedBlockingQueue(); | ||
|
||
final MPDbAdapter mockAdapter = new MPDbAdapter(InstrumentationRegistry.getInstrumentation().getContext()) { | ||
@Override | ||
public int addJSON(JSONObject j, String token, Table table) { | ||
if (table == Table.ANONYMOUS_PEOPLE) { | ||
anonymousUpdates.add(j); | ||
} else if (table == Table.PEOPLE) { | ||
peopleUpdates.add(j); | ||
} | ||
return super.addJSON(j, token, table); | ||
} | ||
}; | ||
final AnalyticsMessages listener = new AnalyticsMessages(InstrumentationRegistry.getInstrumentation().getContext()) { | ||
@Override | ||
public void peopleMessage(PeopleDescription heard) { | ||
messages.add(heard); | ||
super.peopleMessage(heard); | ||
} | ||
|
||
@Override | ||
public void pushAnonymousPeopleMessage(PushAnonymousPeopleDescription pushAnonymousPeopleDescription) { | ||
messages.add(pushAnonymousPeopleDescription); | ||
super.pushAnonymousPeopleMessage(pushAnonymousPeopleDescription); | ||
} | ||
|
||
@Override | ||
protected MPDbAdapter makeDbAdapter(Context context) { | ||
return mockAdapter; | ||
} | ||
}; | ||
|
||
MixpanelAPI mixpanel = new TestUtils.CleanMixpanelAPI(InstrumentationRegistry.getInstrumentation().getContext(), mMockPreferences, token) { | ||
@Override | ||
protected AnalyticsMessages getAnalyticsMessages() { | ||
return listener; | ||
} | ||
}; | ||
|
||
MixpanelAPI.People people = mixpanel.getPeople(); | ||
people.increment("the prop", 0L); | ||
people.append("the prop", 1); | ||
people.set("the prop", 2); | ||
people.increment("the prop", 3L); | ||
people.append("the prop", 5); | ||
|
||
assertEquals(0L, anonymousUpdates.poll(POLL_WAIT_SECONDS, TimeUnit.SECONDS).getJSONObject("$add").getLong("the prop")); | ||
assertEquals(1, anonymousUpdates.poll(POLL_WAIT_SECONDS, TimeUnit.SECONDS).getJSONObject("$append").get("the prop")); | ||
assertEquals(2, anonymousUpdates.poll(POLL_WAIT_SECONDS, TimeUnit.SECONDS).getJSONObject("$set").get("the prop")); | ||
assertEquals(3L, anonymousUpdates.poll(POLL_WAIT_SECONDS, TimeUnit.SECONDS).getJSONObject("$add").getLong("the prop")); | ||
assertEquals(5, anonymousUpdates.poll(POLL_WAIT_SECONDS, TimeUnit.SECONDS).getJSONObject("$append").get("the prop")); | ||
assertNull(anonymousUpdates.poll(POLL_WAIT_SECONDS, TimeUnit.SECONDS)); | ||
assertNull(peopleUpdates.poll(POLL_WAIT_SECONDS, TimeUnit.SECONDS)); | ||
|
||
String deviceId = mixpanel.getAnonymousId(); | ||
mixpanel.identify(mixpanel.getDistinctId()); | ||
people.set("the prop identified", "prop value identified"); | ||
assertNull(mixpanel.getUserId()); | ||
|
||
assertEquals("prop value identified", peopleUpdates.poll(POLL_WAIT_SECONDS, TimeUnit.SECONDS).getJSONObject("$set").getString("the prop identified")); | ||
assertNull(peopleUpdates.poll(POLL_WAIT_SECONDS, TimeUnit.SECONDS)); | ||
assertNull(anonymousUpdates.poll(POLL_WAIT_SECONDS, TimeUnit.SECONDS)); | ||
|
||
String[] storedAnonymous = mockAdapter.generateDataString(MPDbAdapter.Table.ANONYMOUS_PEOPLE, token); | ||
assertNull(storedAnonymous); | ||
|
||
String[] storedPeople = mockAdapter.generateDataString(MPDbAdapter.Table.PEOPLE, token); | ||
assertEquals(6, Integer.valueOf(storedPeople[2]).intValue()); | ||
JSONArray data = new JSONArray(storedPeople[1]); | ||
for (int i=0; i < data.length(); i++) { | ||
JSONObject j = data.getJSONObject(i); | ||
assertEquals("$device:" + deviceId, j.getString("$distinct_id")); | ||
assertEquals(deviceId, j.getString("$device_id")); | ||
} | ||
} | ||
|
||
|
@@ -536,57 +625,58 @@ public void testIdentifyAndGetDistinctId() { | |
MixpanelAPI metrics = new TestUtils.CleanMixpanelAPI(InstrumentationRegistry.getInstrumentation().getContext(), mMockPreferences, "Identify Test Token"); | ||
|
||
String generatedId = metrics.getDistinctId(); | ||
assertNotNull(generatedId); | ||
assertThat(generatedId, startsWith("$device:")); | ||
assertEquals(generatedId, "$device:" + metrics.getAnonymousId()); | ||
|
||
String emptyId = metrics.getPeople().getDistinctId(); | ||
assertNull(emptyId); | ||
assertNull(metrics.getUserId()); | ||
assertNull(metrics.getPeople().getDistinctId()); | ||
|
||
metrics.identify("Events Id"); | ||
String setId = metrics.getDistinctId(); | ||
assertEquals("Events Id", setId); | ||
assertEquals("Events Id", metrics.getDistinctId()); | ||
assertEquals("Events Id", metrics.getUserId()); | ||
assertEquals("Events Id", metrics.getPeople().getDistinctId()); | ||
} | ||
|
||
String userId = metrics.getUserId(); | ||
assertEquals("Events Id", userId); | ||
@Test | ||
public void testIdentifyToCurrentAnonymousDistinctId() { | ||
MixpanelAPI metrics = new TestUtils.CleanMixpanelAPI(InstrumentationRegistry.getInstrumentation().getContext(), mMockPreferences, "Identify Test Token"); | ||
|
||
String generatedId = metrics.getDistinctId(); | ||
assertThat(generatedId, startsWith("$device:")); | ||
assertEquals(generatedId, "$device:" + metrics.getAnonymousId()); | ||
|
||
String unchangedId = metrics.getDistinctId(); | ||
assertEquals("Events Id", unchangedId); | ||
assertNull(metrics.getUserId()); | ||
assertNull(metrics.getPeople().getDistinctId()); | ||
|
||
String setPeopleId = metrics.getPeople().getDistinctId(); | ||
assertEquals("Events Id", setPeopleId); | ||
metrics.identify(metrics.getDistinctId()); | ||
assertEquals(generatedId, metrics.getDistinctId()); | ||
assertNull(metrics.getUserId()); | ||
assertEquals(generatedId, metrics.getPeople().getDistinctId()); | ||
} | ||
|
||
@Test | ||
public void testIdentifyAndCheckUserIDAndDeviceID() { | ||
MixpanelAPI metrics = new TestUtils.CleanMixpanelAPI(InstrumentationRegistry.getInstrumentation().getContext(), mMockPreferences, "Identify Test Token"); | ||
|
||
String generatedId = metrics.getAnonymousId(); | ||
assertNotNull(generatedId); | ||
assertNotNull(metrics.getAnonymousId()); | ||
String eventsDistinctId = metrics.getDistinctId(); | ||
assertNotNull(eventsDistinctId); | ||
assertEquals(eventsDistinctId, generatedId); | ||
assertEquals("$device:" + generatedId, eventsDistinctId); | ||
assertNull(metrics.getUserId()); | ||
|
||
String emptyId = metrics.getPeople().getDistinctId(); | ||
assertNull(emptyId); | ||
assertNull(metrics.getPeople().getDistinctId()); | ||
|
||
metrics.identify("Distinct Id"); | ||
String setId = metrics.getDistinctId(); | ||
assertEquals("Distinct Id", setId); | ||
String anonymousIdAfterIdentify = metrics.getAnonymousId(); | ||
assertEquals(anonymousIdAfterIdentify, generatedId); | ||
|
||
String unchangedId = metrics.getDistinctId(); | ||
assertEquals("Distinct Id", unchangedId); | ||
|
||
String setPeopleId = metrics.getPeople().getDistinctId(); | ||
assertEquals("Distinct Id", setPeopleId); | ||
assertEquals("Distinct Id", metrics.getDistinctId()); | ||
assertEquals(generatedId, metrics.getAnonymousId()); | ||
assertEquals("Distinct Id", metrics.getPeople().getDistinctId()); | ||
|
||
// once its reset we will only have generated id but user id should be null | ||
metrics.reset(); | ||
String generatedId2 = metrics.getAnonymousId(); | ||
assertNotNull(generatedId2); | ||
assertNotSame(generatedId, generatedId2); | ||
assertNotNull(metrics.getDistinctId()); | ||
assertEquals("$device:" + generatedId2, metrics.getDistinctId()); | ||
assertNull(metrics.getUserId()); | ||
} | ||
|
||
@Test | ||
|
@@ -1121,10 +1211,13 @@ protected AnalyticsMessages getAnalyticsMessages() { | |
oldDistinctIds.add(metrics.getDistinctId()); | ||
metrics.identify(newDistinctId + "0"); | ||
metrics.reset(); | ||
|
||
assertThat(oldDistinctIds, not(hasItem(metrics.getDistinctId()))); | ||
oldDistinctIds.add(metrics.getDistinctId()); | ||
metrics.identify(newDistinctId + "1"); | ||
metrics.reset(); | ||
|
||
assertThat(oldDistinctIds, not(hasItem(metrics.getDistinctId()))); | ||
oldDistinctIds.add(metrics.getDistinctId()); | ||
metrics.identify(newDistinctId + "2"); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -627,7 +627,7 @@ public void alias(String alias, String distinct_id) { | |
* value is globally unique for each individual user you intend to track. | ||
*/ | ||
public void identify(String distinctId) { | ||
identify(distinctId, true, true); | ||
identify(distinctId, true); | ||
} | ||
|
||
/** | ||
|
@@ -652,24 +652,22 @@ public void identify(String distinctId) { | |
* | ||
*/ | ||
public void identify(String distinctId, boolean usePeople) { | ||
identify(distinctId, true, usePeople); | ||
} | ||
|
||
private void identify(String distinctId, boolean markAsUserId, boolean usePeople) { | ||
if (hasOptedOutTracking()) return; | ||
if (distinctId == null) { | ||
MPLog.e(LOGTAG, "Can't identify with null distinct_id."); | ||
return; | ||
} | ||
synchronized (mPersistentIdentity) { | ||
String currentEventsDistinctId = mPersistentIdentity.getEventsDistinctId(); | ||
mPersistentIdentity.setAnonymousIdIfAbsent(currentEventsDistinctId); | ||
mPersistentIdentity.setEventsDistinctId(distinctId); | ||
if(markAsUserId) { | ||
mPersistentIdentity.markEventsUserIdPresent(); | ||
} | ||
|
||
if (!distinctId.equals(currentEventsDistinctId)) { | ||
if (distinctId.startsWith("$device:")) { | ||
MPLog.e(LOGTAG, "Can't identify with '$device:' distinct_id."); | ||
return; | ||
} | ||
|
||
mPersistentIdentity.setEventsDistinctId(distinctId); | ||
mPersistentIdentity.setAnonymousIdIfAbsent(currentEventsDistinctId); | ||
mPersistentIdentity.markEventsUserIdPresent(); | ||
try { | ||
JSONObject identifyPayload = new JSONObject(); | ||
identifyPayload.put("$anon_distinct_id", currentEventsDistinctId); | ||
|
@@ -1779,7 +1777,7 @@ public void identify(String distinctId) { | |
MPLog.e(LOGTAG, "Can't identify with null distinct_id."); | ||
return; | ||
} | ||
if (distinctId != mPersistentIdentity.getEventsDistinctId()) { | ||
if (!distinctId.equals(mPersistentIdentity.getEventsDistinctId())) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch! |
||
MPLog.w(LOGTAG, "Identifying with a distinct_id different from the one being set by MixpanelAPI.identify() is not supported."); | ||
return; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.