From 11fd1f7d0b39349a5bbae64d9ecc52736bb2f503 Mon Sep 17 00:00:00 2001 From: Santi G Date: Sat, 18 Oct 2025 11:27:56 -0400 Subject: [PATCH 1/5] Fix: Use fresh super properties from SDK instead of stale static cache Super properties passed to init() could not be updated after initialization in React Native with useNative=true. Modified appendLibraryProperties() to fetch fresh values from MixpanelAPI instance instead of using a stale static cache. --- .../reactnative/AutomaticProperties.java | 24 +++++++++++++------ .../MixpanelReactNativeModule.java | 7 +++--- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/android/src/main/java/com/mixpanel/reactnative/AutomaticProperties.java b/android/src/main/java/com/mixpanel/reactnative/AutomaticProperties.java index 9ac48f24..07909ceb 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 library properties and fresh super properties to the given properties. + * 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 + * @throws JSONException If there's an error merging properties */ - public static void appendLibraryProperties(JSONObject properties) throws JSONException { + public static void appendLibraryProperties(MixpanelAPI instance, 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)); + // Get fresh super properties from the Android SDK (not from stale static cache) + if (instance != null) { + 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); } From 5363da14442f46733b25b21b4361b2d93ceb7fc8 Mon Sep 17 00:00:00 2001 From: Santi Gracia Date: Tue, 21 Oct 2025 15:26:36 +0200 Subject: [PATCH 2/5] fixing function documentation (from copilot) The documentation mentions 'library properties' but the implementation only appends super properties from the SDK. --- .../main/java/com/mixpanel/reactnative/AutomaticProperties.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/src/main/java/com/mixpanel/reactnative/AutomaticProperties.java b/android/src/main/java/com/mixpanel/reactnative/AutomaticProperties.java index 07909ceb..f259153b 100644 --- a/android/src/main/java/com/mixpanel/reactnative/AutomaticProperties.java +++ b/android/src/main/java/com/mixpanel/reactnative/AutomaticProperties.java @@ -17,7 +17,7 @@ public static void setAutomaticProperties(JSONObject properties) { } /** - * This method will append library properties and fresh super properties to the given properties. + * This method will append fresh super properties to the given properties. * Instead of using a stale static cache, it fetches super properties directly from the Android SDK * to ensure updated values are used. * From d3bb9697ab1cf4b8846df4b38c42d0139160da8d Mon Sep 17 00:00:00 2001 From: Santi Gracia Date: Tue, 21 Oct 2025 15:34:43 +0200 Subject: [PATCH 3/5] Fix - Creating a new JSONObject when properties is null has no effect since the parameter is passed by value fixing Creating a new JSONObject when properties is null has no effect since the parameter is passed by value https://github.com/mixpanel/mixpanel-react-native/pull/328#discussion_r2445522767 --- .../com/mixpanel/reactnative/AutomaticProperties.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/android/src/main/java/com/mixpanel/reactnative/AutomaticProperties.java b/android/src/main/java/com/mixpanel/reactnative/AutomaticProperties.java index f259153b..76e9057f 100644 --- a/android/src/main/java/com/mixpanel/reactnative/AutomaticProperties.java +++ b/android/src/main/java/com/mixpanel/reactnative/AutomaticProperties.java @@ -22,16 +22,12 @@ public static void setAutomaticProperties(JSONObject properties) { * 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 + * @param properties The properties object to append to (must not be null) * @throws JSONException If there's an error merging properties */ public static void appendLibraryProperties(MixpanelAPI instance, JSONObject properties) throws JSONException { - if (properties == null) { - properties = new JSONObject(); - } - // Get fresh super properties from the Android SDK (not from stale static cache) - if (instance != null) { + if (instance != null && properties != null)) { JSONObject superProperties = instance.getSuperProperties(); if (superProperties != null) { for (Iterator keys = superProperties.keys(); keys.hasNext();) { From ca4e98d3ec315f07e42568d7bf9153a47f2f4d33 Mon Sep 17 00:00:00 2001 From: Santi Gracia Date: Tue, 21 Oct 2025 16:00:31 +0200 Subject: [PATCH 4/5] fixing extra closing parenthesis fixing extra closing parenthesis --- .../main/java/com/mixpanel/reactnative/AutomaticProperties.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/src/main/java/com/mixpanel/reactnative/AutomaticProperties.java b/android/src/main/java/com/mixpanel/reactnative/AutomaticProperties.java index 76e9057f..d0c78c11 100644 --- a/android/src/main/java/com/mixpanel/reactnative/AutomaticProperties.java +++ b/android/src/main/java/com/mixpanel/reactnative/AutomaticProperties.java @@ -27,7 +27,7 @@ public static void setAutomaticProperties(JSONObject properties) { */ 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 && properties != null)) { + if (instance != null && properties != null) { JSONObject superProperties = instance.getSuperProperties(); if (superProperties != null) { for (Iterator keys = superProperties.keys(); keys.hasNext();) { From 5b4d10af491ffa3752549432b14d14e9e7bb1137 Mon Sep 17 00:00:00 2001 From: Santi G Date: Sat, 25 Oct 2025 22:49:57 +0200 Subject: [PATCH 5/5] Fix null-safety in appendLibraryProperties method Initialize properties to new JSONObject if null to prevent silent failures and add clarifying documentation. --- .../mixpanel/reactnative/AutomaticProperties.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/android/src/main/java/com/mixpanel/reactnative/AutomaticProperties.java b/android/src/main/java/com/mixpanel/reactnative/AutomaticProperties.java index 07909ceb..641506df 100644 --- a/android/src/main/java/com/mixpanel/reactnative/AutomaticProperties.java +++ b/android/src/main/java/com/mixpanel/reactnative/AutomaticProperties.java @@ -17,21 +17,21 @@ public static void setAutomaticProperties(JSONObject properties) { } /** - * This method will append library properties and fresh super properties to the given 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 + * @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(MixpanelAPI instance, JSONObject properties) throws JSONException { - if (properties == null) { - properties = new JSONObject(); - } - // 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();) {