Skip to content
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

Add superProperties param in initializer for First App Open #651

Merged
merged 2 commits into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void runDecideCheck(String token, RemoteService poster) throws RemoteServ
}
};

mCleanMixpanelAPI = new MixpanelAPI(getContext(), mMockReferrerPreferences, TOKEN, false) {
mCleanMixpanelAPI = new MixpanelAPI(getContext(), mMockReferrerPreferences, TOKEN, false, null) {

@Override
/* package */ PersistentIdentity getPersistentIdentity(final Context context, final Future<SharedPreferences> referrerPreferences, final String token) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public void testDecideChecksOnConstruction() {
"}"
);

MixpanelAPI api = new MixpanelAPI(getContext(), mMockPreferences, useToken, false) {
MixpanelAPI api = new MixpanelAPI(getContext(), mMockPreferences, useToken, false, null) {
@Override
AnalyticsMessages getAnalyticsMessages() {
return mMockMessages;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ private MPConfig mpConfig(final Bundle metaData) {
}

private MixpanelAPI mixpanelApi(final MPConfig config) {
return new MixpanelAPI(getContext(), new TestUtils.EmptyPreferences(getContext()), TOKEN, config, false);
return new MixpanelAPI(getContext(), new TestUtils.EmptyPreferences(getContext()), TOKEN, config, false, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ public void peopleMessage(PeopleDescription heard) {

class TestMixpanelAPI extends MixpanelAPI {
public TestMixpanelAPI(Context c, Future<SharedPreferences> prefs, String token) {
super(c, prefs, token, false);
super(c, prefs, token, false, null);
}

@Override
Expand Down Expand Up @@ -943,7 +943,7 @@ public void peopleMessage(PeopleDescription heard) {

class TestMixpanelAPI extends MixpanelAPI {
public TestMixpanelAPI(Context c, Future<SharedPreferences> prefs, String token) {
super(c, prefs, token, false);
super(c, prefs, token, false, null);
}

@Override
Expand Down Expand Up @@ -1134,7 +1134,7 @@ protected AnalyticsMessages getAnalyticsMessages() {
}

public void testPersistence() {
MixpanelAPI metricsOne = new MixpanelAPI(getContext(), mMockPreferences, "SAME TOKEN", false);
MixpanelAPI metricsOne = new MixpanelAPI(getContext(), mMockPreferences, "SAME TOKEN", false, null);
metricsOne.reset();

JSONObject props;
Expand Down Expand Up @@ -1169,7 +1169,7 @@ public void peopleMessage(PeopleDescription heard) {

class ListeningAPI extends MixpanelAPI {
public ListeningAPI(Context c, Future<SharedPreferences> prefs, String token) {
super(c, prefs, token, false);
super(c, prefs, token, false, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@SuppressWarnings("deprecation")
class MockMixpanel extends MixpanelAPI {
public MockMixpanel(Context context, Future<SharedPreferences> prefsFuture, String testToken) {
super(context, prefsFuture, testToken, false);
super(context, prefsFuture, testToken, false, null);
}

// Not complete- you may need to override track(), registerSuperProperties, etc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected void tearDown() throws Exception {
*/
public void testOptOutDefaultFlag() throws InterruptedException {
mCleanUpCalls = new CountDownLatch(2); // optOutTrack calls
mMixpanelAPI = new MixpanelAPI(getContext(), mMockReferrerPreferences, TOKEN, true) {
mMixpanelAPI = new MixpanelAPI(getContext(), mMockReferrerPreferences, TOKEN, true, null) {
@Override
PersistentIdentity getPersistentIdentity(Context context, Future<SharedPreferences> referrerPreferences, String token) {
mPersistentIdentity = super.getPersistentIdentity(context, referrerPreferences, token);
Expand Down Expand Up @@ -136,7 +136,7 @@ AnalyticsMessages getAnalyticsMessages() {
*/
public void testHasOptOutTrackingOrNot() throws InterruptedException {
mCleanUpCalls = new CountDownLatch(4); // optOutTrack calls
mMixpanelAPI = new MixpanelAPI(getContext(), mMockReferrerPreferences, TOKEN, true) {
mMixpanelAPI = new MixpanelAPI(getContext(), mMockReferrerPreferences, TOKEN, true, null) {
@Override
PersistentIdentity getPersistentIdentity(Context context, Future<SharedPreferences> referrerPreferences, String token) {
mPersistentIdentity = super.getPersistentIdentity(context, referrerPreferences, token);
Expand Down Expand Up @@ -168,7 +168,7 @@ AnalyticsMessages getAnalyticsMessages() {
*/
public void testPeopleUpdates() throws InterruptedException, JSONException {
mCleanUpCalls = new CountDownLatch(2);
mMixpanelAPI = new MixpanelAPI(getContext(), mMockReferrerPreferences, TOKEN,false) {
mMixpanelAPI = new MixpanelAPI(getContext(), mMockReferrerPreferences, TOKEN,false, null) {
@Override
PersistentIdentity getPersistentIdentity(Context context, Future<SharedPreferences> referrerPreferences, String token) {
mPersistentIdentity = super.getPersistentIdentity(context, referrerPreferences, token);
Expand Down Expand Up @@ -285,7 +285,7 @@ AnalyticsMessages getAnalyticsMessages() {
* Track calls before and after opting out
*/
public void testTrackCalls() throws InterruptedException, JSONException {
mMixpanelAPI = new MixpanelAPI(getContext(), mMockReferrerPreferences, TOKEN,false) {
mMixpanelAPI = new MixpanelAPI(getContext(), mMockReferrerPreferences, TOKEN, false, null) {
@Override
PersistentIdentity getPersistentIdentity(Context context, Future<SharedPreferences> referrerPreferences, String token) {
mPersistentIdentity = super.getPersistentIdentity(context, referrerPreferences, token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static byte[] bytes(String s) {

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

@Override
Expand Down
49 changes: 42 additions & 7 deletions src/main/java/com/mixpanel/android/mpmetrics/MixpanelAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,15 @@ public static Tweak<Byte> byteTweak(String tweakName, byte defaultValue) {
* You shouldn't instantiate MixpanelAPI objects directly.
* Use MixpanelAPI.getInstance to get an instance.
*/
MixpanelAPI(Context context, Future<SharedPreferences> referrerPreferences, String token, boolean optOutTrackingDefault) {
this(context, referrerPreferences, token, MPConfig.getInstance(context), optOutTrackingDefault);
MixpanelAPI(Context context, Future<SharedPreferences> referrerPreferences, String token, boolean optOutTrackingDefault, JSONObject superProperties) {
this(context, referrerPreferences, token, MPConfig.getInstance(context), optOutTrackingDefault, superProperties);
}

/**
* You shouldn't instantiate MixpanelAPI objects directly.
* Use MixpanelAPI.getInstance to get an instance.
*/
MixpanelAPI(Context context, Future<SharedPreferences> referrerPreferences, String token, MPConfig config, boolean optOutTrackingDefault) {
MixpanelAPI(Context context, Future<SharedPreferences> referrerPreferences, String token, MPConfig config, boolean optOutTrackingDefault, JSONObject superProperties) {
mContext = context;
mToken = token;
mPeople = new PeopleImpl();
Expand Down Expand Up @@ -272,6 +272,9 @@ public static Tweak<Byte> byteTweak(String tweakName, byte defaultValue) {
optOutTracking();
}

if (superProperties != null) {
registerSuperProperties(superProperties);
}
mUpdatesListener = constructUpdatesListener();
mDecideMessages = constructDecideUpdates(token, mUpdatesListener, mUpdatesFromMixpanel);
mConnectIntegrations = new ConnectIntegrations(this, mContext);
Expand Down Expand Up @@ -366,7 +369,38 @@ public static Tweak<Byte> byteTweak(String tweakName, byte defaultValue) {
* @return an instance of MixpanelAPI associated with your project
*/
public static MixpanelAPI getInstance(Context context, String token) {
return getInstance(context, token, false);
return getInstance(context, token, false, null);
}

/**
* Get the instance of MixpanelAPI associated with your Mixpanel project token.
*
* <p>Use getInstance to get a reference to a shared
* instance of MixpanelAPI you can use to send events
* and People Analytics updates to Mixpanel.</p>
* <p>getInstance is thread safe, but the returned instance is not,
* and may be shared with other callers of getInstance.
* The best practice is to call getInstance, and use the returned MixpanelAPI,
* object from a single thread (probably the main UI thread of your application).</p>
* <p>If you do choose to track events from multiple threads in your application,
* you should synchronize your calls on the instance itself, like so:</p>
* <pre>
* {@code
* MixpanelAPI instance = MixpanelAPI.getInstance(context, token);
* synchronized(instance) { // Only necessary if the instance will be used in multiple threads.
* instance.track(...)
* }
* }
* </pre>
*
* @param context The application context you are tracking
* @param token Your Mixpanel project token. You can get your project token on the Mixpanel web site,
* in the settings dialog.
* @param superProperties A JSONObject containing super properties to register.
* @return an instance of MixpanelAPI associated with your project
*/
public static MixpanelAPI getInstance(Context context, String token, JSONObject superProperties) {
return getInstance(context, token, false, superProperties);
}

/**
Expand Down Expand Up @@ -395,9 +429,10 @@ public static MixpanelAPI getInstance(Context context, String token) {
* in the settings dialog.
* @param optOutTrackingDefault Whether or not Mixpanel can start tracking by default. See
* {@link #optOutTracking()}.
* @param superProperties A JSONObject containing super properties to register.
* @return an instance of MixpanelAPI associated with your project
*/
public static MixpanelAPI getInstance(Context context, String token, boolean optOutTrackingDefault) {
public static MixpanelAPI getInstance(Context context, String token, boolean optOutTrackingDefault, JSONObject superProperties) {
if (null == token || null == context) {
return null;
}
Expand All @@ -416,7 +451,7 @@ public static MixpanelAPI getInstance(Context context, String token, boolean opt

MixpanelAPI instance = instances.get(appContext);
if (null == instance && ConfigurationChecker.checkBasicConfiguration(appContext)) {
instance = new MixpanelAPI(appContext, sReferrerPrefs, token, optOutTrackingDefault);
instance = new MixpanelAPI(appContext, sReferrerPrefs, token, optOutTrackingDefault, superProperties);
registerAppLinksListeners(context, instance);
instances.put(appContext, instance);
if (ConfigurationChecker.checkPushNotificationConfiguration(appContext)) {
Expand Down Expand Up @@ -1102,7 +1137,7 @@ public void optInTracking(String distinctId, JSONObject properties) {
}
/**
* Will return true if the user has opted out from tracking. See {@link #optOutTracking()} and
* {@link MixpanelAPI#getInstance(Context, String, boolean)} for more information.
* {@link MixpanelAPI#getInstance(Context, String, boolean, JSONObject)} for more information.
*
* @return true if user has opted out from tracking. Defaults to false.
*/
Expand Down