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
Expand Up @@ -239,6 +239,7 @@ private void handleInitialize(MethodCall call, Result result) {

Boolean optOutTrackingDefault = call.<Boolean>argument("optOutTrackingDefault");
Boolean trackAutomaticEvents = call.<Boolean>argument("trackAutomaticEvents");
String serverURL = call.<String>argument("serverURL");

// Parse feature flags config if provided
Map<String, Object> featureFlagsMap = call.<HashMap<String, Object>>argument("featureFlags");
Expand Down Expand Up @@ -273,6 +274,10 @@ private void handleInitialize(MethodCall call, Result result) {
.optOutTrackingDefault(optOutTrackingDefault == null ? false : optOutTrackingDefault)
.superProperties(superAndMixpanelProperties);

if (serverURL != null && !serverURL.isEmpty()) {
optionsBuilder.serverURL(serverURL);
}

if (featureFlagsEnabled != null && featureFlagsEnabled) {
FeatureFlagOptions.Builder ffBuilder = new FeatureFlagOptions.Builder().enabled(true);
if (featureFlagsContext != null) {
Expand Down
9 changes: 8 additions & 1 deletion packages/mixpanel_flutter/lib/mixpanel_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -364,13 +364,17 @@ class Mixpanel {
/// * [superProperties] Optional super properties to register
/// * [config] Optional A dictionary of config options to override (WEB ONLY)
/// * [featureFlags] Optional Feature flags configuration
/// * [serverURL] Optional base URL used for Mixpanel API requests. Useful if you
/// need to proxy Mixpanel requests, or to route data to Mixpanel's EU servers
/// (`https://api-eu.mixpanel.com`). Defaults to `https://api.mixpanel.com`.
///
static Future<Mixpanel> init(String token,
{bool optOutTrackingDefault = false,
required bool trackAutomaticEvents,
Map<String, dynamic>? superProperties,
Map<String, dynamic>? config,
FeatureFlagsConfig? featureFlags}) async {
FeatureFlagsConfig? featureFlags,
String? serverURL}) async {
var allProperties = <String, dynamic>{'token': token};
allProperties['optOutTrackingDefault'] = optOutTrackingDefault;
allProperties['trackAutomaticEvents'] = trackAutomaticEvents;
Expand All @@ -380,6 +384,9 @@ class Mixpanel {
if (featureFlags != null) {
allProperties['featureFlags'] = featureFlags.toMap();
}
if (serverURL != null && _MixpanelHelper.isValidString(serverURL)) {
allProperties['serverURL'] = serverURL;
}
await _channel.invokeMethod<void>('initialize', allProperties);
return Mixpanel(token);
}
Expand Down
5 changes: 5 additions & 0 deletions packages/mixpanel_flutter/lib/mixpanel_flutter_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ class MixpanelFlutterPlugin {
dynamic config = args['config'];
Map<String, dynamic> initConfig = Map<String, dynamic>.from(config ?? {});

final serverURL = args['serverURL'];
if (serverURL is String && serverURL.isNotEmpty) {
initConfig['api_host'] = serverURL;
}

// Handle feature flags configuration
dynamic featureFlags = args['featureFlags'];
if (featureFlags != null && featureFlags is Map) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ public class SwiftMixpanelFlutterPlugin: NSObject, FlutterPlugin {
let superProperties = arguments["superProperties"] as? [String: Any]
self.token = token
let trackAutomaticEvents = arguments["trackAutomaticEvents"] as! Bool
let serverURL = (arguments["serverURL"] as? String).flatMap { $0.isEmpty ? nil : $0 }

// Check for feature flags configuration
var featureFlagOptions: FeatureFlagOptions? = nil
Expand All @@ -213,6 +214,7 @@ public class SwiftMixpanelFlutterPlugin: NSObject, FlutterPlugin {
trackAutomaticEvents: trackAutomaticEvents,
optOutTrackingByDefault: optOutTrackingDefault ?? false,
superProperties: MixpanelTypeHandler.mixpanelProperties(properties: superProperties, mixpanelProperties: mixpanelProperties),
serverURL: serverURL,
featureFlagOptions: featureFlagOptions
)
instance = Mixpanel.initialize(options: options)
Expand Down
79 changes: 79 additions & 0 deletions packages/mixpanel_flutter/test/mixpanel_flutter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,85 @@ void main() {
});


test('check initialize with serverURL forwards to native', () async {
_mixpanel = await Mixpanel.init(
"test token",
optOutTrackingDefault: false,
trackAutomaticEvents: true,
serverURL: 'https://api-eu.mixpanel.com',
);
expect(
methodCall,
isMethodCall(
'initialize',
arguments: <String, dynamic>{
'token': "test token",
'optOutTrackingDefault': false,
'trackAutomaticEvents': true,
'mixpanelProperties': {
'\$lib_version': sdkVersion,
'mp_lib': 'flutter',
},
'superProperties': null,
'config': null,
'serverURL': 'https://api-eu.mixpanel.com',
},
),
);
});

test('check initialize omits serverURL when blank', () async {
_mixpanel = await Mixpanel.init(
"test token",
optOutTrackingDefault: false,
trackAutomaticEvents: true,
serverURL: '',
);
expect(
methodCall,
isMethodCall(
'initialize',
arguments: <String, dynamic>{
'token': "test token",
'optOutTrackingDefault': false,
'trackAutomaticEvents': true,
'mixpanelProperties': {
'\$lib_version': sdkVersion,
'mp_lib': 'flutter',
},
'superProperties': null,
'config': null,
},
),
);
});

test('check initialize omits serverURL when null', () async {
_mixpanel = await Mixpanel.init(
"test token",
optOutTrackingDefault: false,
trackAutomaticEvents: true,
serverURL: null,
);
expect(
methodCall,
isMethodCall(
'initialize',
arguments: <String, dynamic>{
'token': "test token",
'optOutTrackingDefault': false,
'trackAutomaticEvents': true,
'mixpanelProperties': {
'\$lib_version': sdkVersion,
'mp_lib': 'flutter',
},
'superProperties': null,
'config': null,
},
),
);
});

test('check setServerURL', () async {
_mixpanel.setServerURL("https://api-eu.mixpanel.com");
expect(
Expand Down
Loading