Skip to content

Commit

Permalink
support defining multiple Mixpanel instances with specifying 'instanc…
Browse files Browse the repository at this point in the history
…eName'
  • Loading branch information
zihejia committed Jun 29, 2022
1 parent 7aec383 commit e5c3d32
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -153,20 +152,21 @@ public void runDecideCheck(String token, RemoteService poster) throws RemoteServ
mCleanMixpanelAPI = new MixpanelAPI(InstrumentationRegistry.getInstrumentation().getContext(), mMockReferrerPreferences, TOKEN, false, null) {

@Override
/* package */ PersistentIdentity getPersistentIdentity(final Context context, final Future<SharedPreferences> referrerPreferences, final String token) {
final String prefsName = "com.mixpanel.android.mpmetrics.MixpanelAPI_" + token;
/* package */ PersistentIdentity getPersistentIdentity(final Context context, final Future<SharedPreferences> referrerPreferences, final String token, final String instanceName) {
String instanceKey = instanceName != null ? instanceName : token;
final String prefsName = "com.mixpanel.android.mpmetrics.MixpanelAPI_" + instanceKey;
final SharedPreferences ret = context.getSharedPreferences(prefsName, Context.MODE_PRIVATE);
ret.edit().clear().commit();

final String timeEventsPrefsName = "com.mixpanel.android.mpmetrics.MixpanelAPI.TimeEvents_" + token;
final String timeEventsPrefsName = "com.mixpanel.android.mpmetrics.MixpanelAPI.TimeEvents_" + instanceKey;
final SharedPreferences timeSharedPrefs = context.getSharedPreferences(timeEventsPrefsName, Context.MODE_PRIVATE);
timeSharedPrefs.edit().clear().commit();

final String mixpanelPrefsName = "com.mixpanel.android.mpmetrics.Mixpanel";
final SharedPreferences mpSharedPrefs = context.getSharedPreferences(mixpanelPrefsName, Context.MODE_PRIVATE);
mpSharedPrefs.edit().clear().putInt("latest_version_code", -2).commit(); // -1 is the default value

return super.getPersistentIdentity(context, referrerPreferences, token);
return super.getPersistentIdentity(context, referrerPreferences, token, instanceName);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,6 @@ private MPConfig mpConfig(final Bundle metaData) {
}

private MixpanelAPI mixpanelApi(final MPConfig config) {
return new MixpanelAPI(InstrumentationRegistry.getInstrumentation().getContext(), new TestUtils.EmptyPreferences(InstrumentationRegistry.getInstrumentation().getContext()), TOKEN, config, false, null);
return new MixpanelAPI(InstrumentationRegistry.getInstrumentation().getContext(), new TestUtils.EmptyPreferences(InstrumentationRegistry.getInstrumentation().getContext()), TOKEN, config, false, null,null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;

Expand All @@ -30,7 +28,6 @@
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -890,14 +887,15 @@ public TestMixpanelAPI(Context c, Future<SharedPreferences> prefs, String token)
}

@Override
/* package */ PersistentIdentity getPersistentIdentity(final Context context, final Future<SharedPreferences> referrerPreferences, final String token) {
/* package */ PersistentIdentity getPersistentIdentity(final Context context, final Future<SharedPreferences> referrerPreferences, final String token, final String instanceName) {
String instanceKey = instanceName != null ? instanceName : token;
final String mixpanelPrefsName = "com.mixpanel.android.mpmetrics.Mixpanel";
final SharedPreferences mpSharedPrefs = context.getSharedPreferences(mixpanelPrefsName, Context.MODE_PRIVATE);
mpSharedPrefs.edit().clear().putBoolean(token, true).putBoolean("has_launched", true).commit();
final String prefsName = "com.mixpanel.android.mpmetrics.MixpanelAPI_" + token;
final String prefsName = "com.mixpanel.android.mpmetrics.MixpanelAPI_" + instanceKey;
final SharedPreferences loadstorePrefs = context.getSharedPreferences(prefsName, Context.MODE_PRIVATE);
loadstorePrefs.edit().clear().putString("events_distinct_id", savedDistinctID).putString("people_distinct_id", savedDistinctID).commit();
return super.getPersistentIdentity(context, referrerPreferences, token);
return super.getPersistentIdentity(context, referrerPreferences, token, instanceName);
}

@Override
Expand Down Expand Up @@ -1196,12 +1194,13 @@ public ListeningAPI(Context c, Future<SharedPreferences> prefs, String token) {
}

@Override
/* package */ PersistentIdentity getPersistentIdentity(final Context context, final Future<SharedPreferences> referrerPreferences, final String token) {
final String mixpanelPrefsName = "com.mixpanel.android.mpmetrics.Mixpanel";
/* package */ PersistentIdentity getPersistentIdentity(final Context context, final Future<SharedPreferences> referrerPreferences, final String token, final String instanceName) {
String instanceKey = instanceName != null ? instanceName : token;
final String mixpanelPrefsName = "com.mixpanel.android.mpmetrics.Mixpanel";
final SharedPreferences mpSharedPrefs = context.getSharedPreferences(mixpanelPrefsName, Context.MODE_PRIVATE);
mpSharedPrefs.edit().clear().putBoolean(token, true).putBoolean("has_launched", true).commit();
mpSharedPrefs.edit().clear().putBoolean(instanceKey, true).putBoolean("has_launched", true).commit();

return super.getPersistentIdentity(context, referrerPreferences, token);
return super.getPersistentIdentity(context, referrerPreferences, token, instanceName);
}

@Override
Expand Down Expand Up @@ -1363,6 +1362,63 @@ protected AnalyticsMessages getAnalyticsMessages() {
metrics.alias("new id", "old id");
}

@Test
public void testMultiInstancesWithInstanceName() throws InterruptedException, JSONException {
final BlockingQueue<JSONObject> anonymousUpdates = new LinkedBlockingQueue<JSONObject>();
final BlockingQueue<JSONObject> identifiedUpdates = new LinkedBlockingQueue<JSONObject>();

final MPDbAdapter mockAdapter = new MPDbAdapter(InstrumentationRegistry.getInstrumentation().getContext()) {
@Override
public int addJSON(JSONObject j, String token, Table table, boolean isAutomaticRecord) {
if (table == Table.ANONYMOUS_PEOPLE) {
anonymousUpdates.add(j);
} else if (table == Table.PEOPLE) {
identifiedUpdates.add(j);
}
return super.addJSON(j, token, table, isAutomaticRecord);
}
};

final AnalyticsMessages listener = new AnalyticsMessages(InstrumentationRegistry.getInstrumentation().getContext()) {
@Override
protected MPDbAdapter makeDbAdapter(Context context) {
return mockAdapter;
}
};

MixpanelAPI mixpanel1 = new TestUtils.CleanMixpanelAPI(InstrumentationRegistry.getInstrumentation().getContext(), mMockPreferences, "testAnonymousPeopleUpdates", "instance1") {
@Override
AnalyticsMessages getAnalyticsMessages() {
return listener;
}
};
MixpanelAPI mixpanel2 = new TestUtils.CleanMixpanelAPI(InstrumentationRegistry.getInstrumentation().getContext(), mMockPreferences, "testAnonymousPeopleUpdates", "instance2") {
@Override
AnalyticsMessages getAnalyticsMessages() {
return listener;
}
};
// mixpanel1 and mixpanel2 are treated as different instances because of the their instance names are different
assertTrue(!mixpanel1.getDistinctId().equals(mixpanel2.getDistinctId()));
mixpanel1.getPeople().set("firstProperty", "firstValue");
assertEquals("firstValue", anonymousUpdates.poll(POLL_WAIT_SECONDS, TimeUnit.SECONDS).getJSONObject("$set").getString("firstProperty"));
mixpanel1.identify("mixpanel_distinct_id");
mixpanel1.getPeople().set("firstPropertyIdentified", "firstValue");
assertEquals("firstValue", identifiedUpdates.poll(POLL_WAIT_SECONDS, TimeUnit.SECONDS).getJSONObject("$set").getString("firstPropertyIdentified"));
assertEquals(0, anonymousUpdates.size());
assertTrue(mixpanel1.getDistinctId().equals("mixpanel_distinct_id"));


mixpanel2.getPeople().set("firstProperty2", "firstValue2");
assertEquals("firstValue2", anonymousUpdates.poll(POLL_WAIT_SECONDS, TimeUnit.SECONDS).getJSONObject("$set").getString("firstProperty2"));
mixpanel2.identify("mixpanel_distinct_id2");
mixpanel2.getPeople().set("firstPropertyIdentified2", "firstValue2");
assertEquals("firstValue2", identifiedUpdates.poll(POLL_WAIT_SECONDS, TimeUnit.SECONDS).getJSONObject("$set").getString("firstPropertyIdentified2"));
assertEquals(0, anonymousUpdates.size());
assertTrue(!mixpanel1.getDistinctId().equals(mixpanel2.getDistinctId()));
assertTrue(mixpanel2.getDistinctId().equals("mixpanel_distinct_id2"));
}

@Test
public void testAnonymousPeopleUpdates() throws InterruptedException, JSONException {
final BlockingQueue<JSONObject> anonymousUpdates = new LinkedBlockingQueue<JSONObject>();
Expand Down Expand Up @@ -1424,8 +1480,8 @@ public void testEventTiming() throws InterruptedException {
mMockReferrerPreferences = new TestUtils.EmptyPreferences(InstrumentationRegistry.getInstrumentation().getContext());
MixpanelAPI mMixpanelAPI = new MixpanelAPI(InstrumentationRegistry.getInstrumentation().getContext(), mMockReferrerPreferences, "TESTTOKEN", false, null) {
@Override
PersistentIdentity getPersistentIdentity(Context context, Future<SharedPreferences> referrerPreferences, String token) {
mPersistentIdentity = super.getPersistentIdentity(context, referrerPreferences, token);
PersistentIdentity getPersistentIdentity(Context context, Future<SharedPreferences> referrerPreferences, String token, String instanceName) {
mPersistentIdentity = super.getPersistentIdentity(context, referrerPreferences, token, instanceName);
return mPersistentIdentity;
}

Expand Down
21 changes: 10 additions & 11 deletions src/androidTest/java/com/mixpanel/android/mpmetrics/OptOutTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
Expand Down Expand Up @@ -119,8 +118,8 @@ public void testOptOutDefaultFlag() throws InterruptedException {
mCleanUpCalls = new CountDownLatch(2); // optOutTrack calls
mMixpanelAPI = new MixpanelAPI(InstrumentationRegistry.getInstrumentation().getContext(), mMockReferrerPreferences, TOKEN, true, null) {
@Override
PersistentIdentity getPersistentIdentity(Context context, Future<SharedPreferences> referrerPreferences, String token) {
mPersistentIdentity = super.getPersistentIdentity(context, referrerPreferences, token);
PersistentIdentity getPersistentIdentity(Context context, Future<SharedPreferences> referrerPreferences, String token, String instanceName) {
mPersistentIdentity = super.getPersistentIdentity(context, referrerPreferences, token, instanceName);
return mPersistentIdentity;
}

Expand Down Expand Up @@ -149,8 +148,8 @@ public void testHasOptOutTrackingOrNot() throws InterruptedException {
mCleanUpCalls = new CountDownLatch(4); // optOutTrack calls
MixpanelAPI mixpanel = new MixpanelAPI(InstrumentationRegistry.getInstrumentation().getContext(), mMockReferrerPreferences, "TOKEN", true, null) {
@Override
PersistentIdentity getPersistentIdentity(Context context, Future<SharedPreferences> referrerPreferences, String token) {
mPersistentIdentity = super.getPersistentIdentity(context, referrerPreferences, token);
PersistentIdentity getPersistentIdentity(Context context, Future<SharedPreferences> referrerPreferences, String token, String instanceName) {
mPersistentIdentity = super.getPersistentIdentity(context, referrerPreferences, token, instanceName);
return mPersistentIdentity;
}

Expand Down Expand Up @@ -180,8 +179,8 @@ public void testPeopleUpdates() throws InterruptedException, JSONException {
mCleanUpCalls = new CountDownLatch(2);
mMixpanelAPI = new MixpanelAPI(InstrumentationRegistry.getInstrumentation().getContext(), mMockReferrerPreferences, TOKEN,false, null) {
@Override
PersistentIdentity getPersistentIdentity(Context context, Future<SharedPreferences> referrerPreferences, String token) {
mPersistentIdentity = super.getPersistentIdentity(context, referrerPreferences, token);
PersistentIdentity getPersistentIdentity(Context context, Future<SharedPreferences> referrerPreferences, String token, String instanceName) {
mPersistentIdentity = super.getPersistentIdentity(context, referrerPreferences, token, instanceName);
return mPersistentIdentity;
}

Expand Down Expand Up @@ -259,8 +258,8 @@ AnalyticsMessages getAnalyticsMessages() {
public void testDropEventsAndOptInEvent() throws InterruptedException {
mMixpanelAPI = new TestUtils.CleanMixpanelAPI(InstrumentationRegistry.getInstrumentation().getContext(), mMockReferrerPreferences, TOKEN) {
@Override
PersistentIdentity getPersistentIdentity(Context context, Future<SharedPreferences> referrerPreferences, String token) {
mPersistentIdentity = super.getPersistentIdentity(context, referrerPreferences, token);
PersistentIdentity getPersistentIdentity(Context context, Future<SharedPreferences> referrerPreferences, String token, String instanceName) {
mPersistentIdentity = super.getPersistentIdentity(context, referrerPreferences, token, instanceName);
return mPersistentIdentity;
}

Expand Down Expand Up @@ -297,8 +296,8 @@ AnalyticsMessages getAnalyticsMessages() {
public void testTrackCalls() throws InterruptedException, JSONException {
mMixpanelAPI = new MixpanelAPI(InstrumentationRegistry.getInstrumentation().getContext(), mMockReferrerPreferences, TOKEN, false, null) {
@Override
PersistentIdentity getPersistentIdentity(Context context, Future<SharedPreferences> referrerPreferences, String token) {
mPersistentIdentity = super.getPersistentIdentity(context, referrerPreferences, token);
PersistentIdentity getPersistentIdentity(Context context, Future<SharedPreferences> referrerPreferences, String token, String instanceName) {
mPersistentIdentity = super.getPersistentIdentity(context, referrerPreferences, token, instanceName);
return mPersistentIdentity;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class TestUtils {
public static byte[] bytes(String s) {
Expand All @@ -26,21 +24,26 @@ public CleanMixpanelAPI(final Context context, final Future<SharedPreferences> r
super(context, referrerPreferences, token, false, null);
}

public CleanMixpanelAPI(final Context context, final Future<SharedPreferences> referrerPreferences, final String token, final String instanceName) {
super(context, referrerPreferences, token, false, null, instanceName);
}

@Override
/* package */ PersistentIdentity getPersistentIdentity(final Context context, final Future<SharedPreferences> referrerPreferences, final String token) {
final String prefsName = "com.mixpanel.android.mpmetrics.MixpanelAPI_" + token;
/* package */ PersistentIdentity getPersistentIdentity(final Context context, final Future<SharedPreferences> referrerPreferences, final String token, final String instanceName) {
String instanceKey = instanceName != null ? instanceName : token;
final String prefsName = "com.mixpanel.android.mpmetrics.MixpanelAPI_" + instanceKey;
final SharedPreferences ret = context.getSharedPreferences(prefsName, Context.MODE_PRIVATE);
ret.edit().clear().commit();

final String timeEventsPrefsName = "com.mixpanel.android.mpmetrics.MixpanelAPI.TimeEvents_" + token;
final String timeEventsPrefsName = "com.mixpanel.android.mpmetrics.MixpanelAPI.TimeEvents_" + instanceKey;
final SharedPreferences timeSharedPrefs = context.getSharedPreferences(timeEventsPrefsName, Context.MODE_PRIVATE);
timeSharedPrefs.edit().clear().commit();

final String mixpanelPrefsName = "com.mixpanel.android.mpmetrics.Mixpanel";
final SharedPreferences mpSharedPrefs = context.getSharedPreferences(mixpanelPrefsName, Context.MODE_PRIVATE);
mpSharedPrefs.edit().clear().putBoolean(token, true).putBoolean("has_launched", true).apply();

return super.getPersistentIdentity(context, referrerPreferences, token);
return super.getPersistentIdentity(context, referrerPreferences, token, instanceName);
}

@Override
Expand Down
Loading

0 comments on commit e5c3d32

Please sign in to comment.