Skip to content
Open
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
@@ -1,5 +1,6 @@
package com.mixpanel.reactnative;

import com.mixpanel.android.mpmetrics.MixpanelAPI;
import org.json.JSONException;
import org.json.JSONObject;

Expand All @@ -16,18 +17,27 @@ public static void setAutomaticProperties(JSONObject properties) {
}

/**
* This method will append library properties to the default properties.
* This method will append fresh super properties to the given properties object.
* Instead of using a stale static cache, it fetches super properties directly from the Android SDK
* to ensure updated values are used.
*
* @param instance The MixpanelAPI instance to get fresh super properties from
* @param properties The properties object to append to. If null, a new JSONObject will be created
* (note: the caller's reference will not be updated; use the return value if needed)
* @throws JSONException If there's an error merging properties
*/
public static void appendLibraryProperties(JSONObject properties) throws JSONException {
if (properties == null) {
properties = new JSONObject();
}

if (sAutomaticProperties != null) {
// merge automatic properties
for (Iterator<String> keys = sAutomaticProperties.keys(); keys.hasNext();) {
String key = keys.next();
properties.put(key, sAutomaticProperties.get(key));
public static void appendLibraryProperties(MixpanelAPI instance, JSONObject properties) throws JSONException {
// Get fresh super properties from the Android SDK (not from stale static cache)
if (instance != null) {
if (properties == null) {
properties = new JSONObject();
}
JSONObject superProperties = instance.getSuperProperties();
if (superProperties != null) {
for (Iterator<String> keys = superProperties.keys(); keys.hasNext();) {
String key = keys.next();
properties.put(key, superProperties.get(key));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public String getName() {
return "MixpanelReactNative";
}


@ReactMethod
public void initialize(String token, boolean trackAutomaticEvents, boolean optOutTrackingDefault, ReadableMap metadata, String serverURL, boolean useGzipCompression, Promise promise) throws JSONException {
JSONObject mixpanelProperties = ReactNativeHelper.reactToJSON(metadata);
Expand Down Expand Up @@ -181,7 +180,7 @@ public void track(final String token, final String eventName, ReadableMap proper
}
synchronized (instance) {
JSONObject eventProperties = ReactNativeHelper.reactToJSON(properties);
AutomaticProperties.appendLibraryProperties(eventProperties);
AutomaticProperties.appendLibraryProperties(instance, eventProperties);
instance.track(eventName, eventProperties);
promise.resolve(null);
}
Expand Down Expand Up @@ -340,7 +339,7 @@ public void set(final String token, ReadableMap properties, Promise promise) thr
}
synchronized (instance) {
JSONObject sendProperties = ReactNativeHelper.reactToJSON(properties);
AutomaticProperties.appendLibraryProperties(sendProperties);
AutomaticProperties.appendLibraryProperties(instance, sendProperties);
instance.getPeople().set(sendProperties);
promise.resolve(null);
}
Expand Down Expand Up @@ -368,7 +367,7 @@ public void setOnce(final String token, ReadableMap properties, Promise promise)
}
synchronized (instance) {
JSONObject sendProperties = ReactNativeHelper.reactToJSON(properties);
AutomaticProperties.appendLibraryProperties(sendProperties);
AutomaticProperties.appendLibraryProperties(instance, sendProperties);
instance.getPeople().setOnce(sendProperties);
promise.resolve(null);
}
Expand Down