diff --git a/android/src/main/java/com/mixpanel/reactnative/AutomaticProperties.java b/android/src/main/java/com/mixpanel/reactnative/AutomaticProperties.java index 9ac48f24..641506df 100644 --- a/android/src/main/java/com/mixpanel/reactnative/AutomaticProperties.java +++ b/android/src/main/java/com/mixpanel/reactnative/AutomaticProperties.java @@ -1,5 +1,6 @@ package com.mixpanel.reactnative; +import com.mixpanel.android.mpmetrics.MixpanelAPI; import org.json.JSONException; import org.json.JSONObject; @@ -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 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 keys = superProperties.keys(); keys.hasNext();) { + String key = keys.next(); + properties.put(key, superProperties.get(key)); + } } } } diff --git a/android/src/main/java/com/mixpanel/reactnative/MixpanelReactNativeModule.java b/android/src/main/java/com/mixpanel/reactnative/MixpanelReactNativeModule.java index b800c9b3..41c2185c 100644 --- a/android/src/main/java/com/mixpanel/reactnative/MixpanelReactNativeModule.java +++ b/android/src/main/java/com/mixpanel/reactnative/MixpanelReactNativeModule.java @@ -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); @@ -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); } @@ -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); } @@ -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); }