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/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..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; @@ -31,16 +32,26 @@ 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); 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); } @@ -181,7 +192,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 +351,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 +379,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); }