From 11fd1f7d0b39349a5bbae64d9ecc52736bb2f503 Mon Sep 17 00:00:00 2001 From: Santi G Date: Sat, 18 Oct 2025 11:27:56 -0400 Subject: [PATCH 1/6] 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/6] 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/6] 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/6] 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/6] 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();) { From c3869da79e221d6c0b70499c7ca13ac61bfc97b0 Mon Sep 17 00:00:00 2001 From: Santi G Date: Mon, 2 Feb 2026 14:48:54 +0100 Subject: [PATCH 6/6] Honor serverURL and bump Android SDK to 8.2.7 Configure Mixpanel on Android via MixpanelOptions to honor the serverURL init parameter and properly apply gzip settings. Update MixpanelAPI initialization to use MixpanelOptions (preserving opt-out and super properties), keep explicit serverURL handling, and bump the native Android SDK dependency to v8.2.7. Add an Unreleased changelog entry describing the enhancement. --- CHANGELOG.md | 8 ++++++++ android/build.gradle | 2 +- .../MixpanelReactNativeModule.java | 20 +++++++++++++++---- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c1ca8680..cea52e8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # +## [Unreleased] + +### Enhancements + +- Honor the serverURL init parameter on Android by configuring it through MixpanelOptions and bumping the native SDK to v8.2.7. + +# + ## [v3.1.2](https://github.com/mixpanel/mixpanel-react-native/tree/v3.1.2) (2025-06-05) ### Fixes diff --git a/android/build.gradle b/android/build.gradle index 3bb316ac..4dc67a18 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -41,5 +41,5 @@ repositories { dependencies { implementation 'com.facebook.react:react-native:+' - implementation 'com.mixpanel.android:mixpanel-android:8.2.0' + implementation 'com.mixpanel.android:mixpanel-android:8.2.7' } diff --git a/android/src/main/java/com/mixpanel/reactnative/MixpanelReactNativeModule.java b/android/src/main/java/com/mixpanel/reactnative/MixpanelReactNativeModule.java index 41c2185c..21656c5c 100644 --- a/android/src/main/java/com/mixpanel/reactnative/MixpanelReactNativeModule.java +++ b/android/src/main/java/com/mixpanel/reactnative/MixpanelReactNativeModule.java @@ -1,6 +1,7 @@ package com.mixpanel.reactnative; import com.mixpanel.android.mpmetrics.MixpanelAPI; +import com.mixpanel.android.mpmetrics.MixpanelOptions; import com.facebook.react.bridge.Promise; import com.facebook.react.bridge.ReactApplicationContext; @@ -35,11 +36,22 @@ public String getName() { public void initialize(String token, boolean trackAutomaticEvents, boolean optOutTrackingDefault, ReadableMap metadata, String serverURL, boolean useGzipCompression, Promise promise) throws JSONException { JSONObject mixpanelProperties = ReactNativeHelper.reactToJSON(metadata); AutomaticProperties.setAutomaticProperties(mixpanelProperties); - MixpanelAPI instance = MixpanelAPI.getInstance(this.mReactContext, token, optOutTrackingDefault, mixpanelProperties, null, trackAutomaticEvents); - instance.setServerURL(serverURL); - if (useGzipCompression) { - instance.setShouldGzipRequestPayload(true); + + MixpanelOptions.Builder optionsBuilder = new MixpanelOptions.Builder() + .optOutTrackingDefault(optOutTrackingDefault) + .superProperties(mixpanelProperties); + + if (serverURL != null && !serverURL.isEmpty()) { + optionsBuilder.serverURL(serverURL); } + + MixpanelAPI instance = MixpanelAPI.getInstance(this.mReactContext, token, trackAutomaticEvents, optionsBuilder.build()); + + if (serverURL != null && !serverURL.isEmpty()) { + instance.setServerURL(serverURL); + } + + instance.setShouldGzipRequestPayload(useGzipCompression); promise.resolve(null); }