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
3 changes: 3 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ android {
implementation 'com.segment.analytics.android:analytics:4.10.4'
implementation 'com.segment.analytics.android.integrations:amplitude:3.1.0'
implementation 'com.appsflyer:segment-android-integration:6.5.2'
implementation ("com.moengage:moengage-segment-integration:5.5.01") {
transitive = true
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ public class FlutterSegmentOptions {
private final Boolean appsflyerIntegrationEnabled;
private final Boolean debug;

public FlutterSegmentOptions(
public FlutterSegmentOptions(
String writeKey,
Boolean trackApplicationLifecycleEvents,
Boolean amplitudeIntegrationEnabled,
Boolean appsflyerIntegrationEnabled,
Boolean debug
) {
Boolean moengageIntegrationEnabled,
Boolean debug) {
this.writeKey = writeKey;
this.trackApplicationLifecycleEvents = trackApplicationLifecycleEvents;
this.amplitudeIntegrationEnabled = amplitudeIntegrationEnabled;
this.appsflyerIntegrationEnabled = appsflyerIntegrationEnabled;
this.moengageIntegrationEnabled = moengageIntegrationEnabled;
this.debug = debug;
}

Expand All @@ -41,26 +42,39 @@ public Boolean isAppsflyerIntegrationEnabled() {
return appsflyerIntegrationEnabled;
}

public Boolean isMoengageIntegrationEnabled() {
return moengageIntegrationEnabled;
}

public Boolean getDebug() {
return debug;
}

static FlutterSegmentOptions create(Bundle bundle) {
String writeKey = bundle.getString("com.claimsforce.segment.WRITE_KEY");
Boolean trackApplicationLifecycleEvents = bundle.getBoolean("com.claimsforce.segment.TRACK_APPLICATION_LIFECYCLE_EVENTS");
Boolean isAmplitudeIntegrationEnabled = bundle.getBoolean("com.claimsforce.segment.ENABLE_AMPLITUDE_INTEGRATION", false);
Boolean isAppsflyerIntegrationEnabled = bundle.getBoolean("com.claimsforce.segment.ENABLE_APPSFLYER_INTEGRATION", false);
Boolean trackApplicationLifecycleEvents = bundle
.getBoolean("com.claimsforce.segment.TRACK_APPLICATION_LIFECYCLE_EVENTS");
Boolean isAmplitudeIntegrationEnabled = bundle
.getBoolean("com.claimsforce.segment.ENABLE_AMPLITUDE_INTEGRATION", false);
Boolean isAppsflyerIntegrationEnabled = bundle
.getBoolean("com.claimsforce.segment.ENABLE_APPSFLYER_INTEGRATION", false);
Boolean isMoengageIntegrationEnabled = bundle.getBoolean("com.claimsforce.segment.ENABLE_MOENGAGE_INTEGRATION",
false);
Boolean debug = bundle.getBoolean("com.claimsforce.segment.DEBUG", false);
return new FlutterSegmentOptions(writeKey, trackApplicationLifecycleEvents, isAmplitudeIntegrationEnabled, isAppsflyerIntegrationEnabled, debug);

return new FlutterSegmentOptions(writeKey, trackApplicationLifecycleEvents, isAmplitudeIntegrationEnabled,
isAppsflyerIntegrationEnabled, isMoengageIntegrationEnabled, debug);
}

static FlutterSegmentOptions create(HashMap<String, Object> options) {
String writeKey = (String) options.get("writeKey");
Boolean trackApplicationLifecycleEvents = (Boolean) options.get("trackApplicationLifecycleEvents");
Boolean isAmplitudeIntegrationEnabled = orFalse((Boolean) options.get("amplitudeIntegrationEnabled"));
Boolean isAppsflyerIntegrationEnabled = orFalse((Boolean) options.get("appsflyerIntegrationEnabled"));
Boolean isMoengageIntegrationEnabled = orFalse((Boolean) options.get("moengageIntegrationEnabled"));
Boolean debug = orFalse((Boolean) options.get("debug"));
return new FlutterSegmentOptions(writeKey, trackApplicationLifecycleEvents, isAmplitudeIntegrationEnabled, isAppsflyerIntegrationEnabled, debug);
return new FlutterSegmentOptions(writeKey, trackApplicationLifecycleEvents, isAmplitudeIntegrationEnabled,
isAppsflyerIntegrationEnabled, isMoengageIntegrationEnabled, debug);
}

private static Boolean orFalse(Boolean value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.segment.analytics.integrations.BasePayload;
import com.segment.analytics.android.integrations.amplitude.AmplitudeIntegration;
import com.segment.analytics.android.integrations.appsflyer.AppsflyerIntegration;
import com.segment.analytics.android.integrations.moengage.MoEngageIntegration;
import static com.segment.analytics.Analytics.LogLevel;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -51,7 +52,7 @@ private void setupChannels(Context applicationContext, BinaryMessenger messenger

try {
ApplicationInfo ai = applicationContext.getPackageManager()
.getApplicationInfo(applicationContext.getPackageName(), PackageManager.GET_META_DATA);
.getApplicationInfo(applicationContext.getPackageName(), PackageManager.GET_META_DATA);

Bundle bundle = ai.metaData;

Expand Down Expand Up @@ -84,42 +85,45 @@ private void setupChannels(FlutterSegmentOptions options) {
if (options.isAppsflyerIntegrationEnabled()) {
analyticsBuilder.use(AppsflyerIntegration.FACTORY);
}
if (options.isMoengageIntegrationEnabled()) {
analyticsBuilder.use(MoEngageIntegration.FACTORY);
}

// Here we build a middleware that just appends data to the current context
// using the [deepMerge] strategy.
analyticsBuilder.useSourceMiddleware(
new Middleware() {
@Override
public void intercept(Chain chain) {
try {
if (appendToContextMiddleware == null) {
chain.proceed(chain.payload());
return;
}

BasePayload payload = chain.payload();
Map<String, Object> originalContext = new LinkedHashMap<>(payload.context());
Map<String, Object> mergedContext = FlutterSegmentPlugin.deepMerge(
originalContext,
appendToContextMiddleware
);

BasePayload newPayload = payload.toBuilder()
.context(mergedContext)
.build();

chain.proceed(newPayload);
} catch (Exception e) {
Log.e("FlutterSegment", e.getMessage());
chain.proceed(chain.payload());
}
new Middleware() {
@Override
public void intercept(Chain chain) {
try {
if (appendToContextMiddleware == null) {
chain.proceed(chain.payload());
return;
}

BasePayload payload = chain.payload();
Map<String, Object> originalContext = new LinkedHashMap<>(payload.context());
Map<String, Object> mergedContext = FlutterSegmentPlugin.deepMerge(
originalContext,
appendToContextMiddleware);

BasePayload newPayload = payload.toBuilder()
.context(mergedContext)
.build();

chain.proceed(newPayload);
} catch (Exception e) {
Log.e("FlutterSegment", e.getMessage());
chain.proceed(chain.payload());
}
);
}
});

// Set the initialized instance as globally accessible.
// It may throw an exception if we are trying to re-register a singleton Analytics instance.
// This state may happen after the app is popped (back button until the app closes)
// It may throw an exception if we are trying to re-register a singleton
// Analytics instance.
// This state may happen after the app is popped (back button until the app
// closes)
// and opened again from the TaskManager.
try {
Analytics.setSingletonInstance(analyticsBuilder.build());
Expand Down Expand Up @@ -205,14 +209,13 @@ private void identify(MethodCall call, Result result) {
}

private void callIdentify(
String userId,
HashMap<String, Object> traitsData,
HashMap<String, Object> optionsData
) {
String userId,
HashMap<String, Object> traitsData,
HashMap<String, Object> optionsData) {
Traits traits = new Traits();
Options options = this.buildOptions(optionsData);

for(Map.Entry<String, Object> trait : traitsData.entrySet()) {
for (Map.Entry<String, Object> trait : traitsData.entrySet()) {
String key = trait.getKey();
Object value = trait.getValue();
traits.putValue(key, value);
Expand All @@ -234,10 +237,9 @@ private void track(MethodCall call, Result result) {
}

private void callTrack(
String eventName,
HashMap<String, Object> propertiesData,
HashMap<String, Object> optionsData
) {
String eventName,
HashMap<String, Object> propertiesData,
HashMap<String, Object> optionsData) {
Properties properties = propertiesMapper.buildProperties(propertiesData);
Options options = this.buildOptions(optionsData);

Expand All @@ -257,10 +259,9 @@ private void screen(MethodCall call, Result result) {
}

private void callScreen(
String screenName,
HashMap<String, Object> propertiesData,
HashMap<String, Object> optionsData
) {
String screenName,
HashMap<String, Object> propertiesData,
HashMap<String, Object> optionsData) {
Properties properties = propertiesMapper.buildProperties(propertiesData);
Options options = this.buildOptions(optionsData);

Expand All @@ -280,14 +281,13 @@ private void group(MethodCall call, Result result) {
}

private void callGroup(
String groupId,
HashMap<String, Object> traitsData,
HashMap<String, Object> optionsData
) {
String groupId,
HashMap<String, Object> traitsData,
HashMap<String, Object> optionsData) {
Traits traits = new Traits();
Options options = this.buildOptions(optionsData);

for(Map.Entry<String, Object> trait : traitsData.entrySet()) {
for (Map.Entry<String, Object> trait : traitsData.entrySet()) {
String key = trait.getKey();
Object value = trait.getValue();
traits.putValue(key, value);
Expand Down Expand Up @@ -367,8 +367,10 @@ private void disable(MethodCall call, Result result) {
}

/**
* Enables / disables / sets custom integration properties so Segment can properly
* interact with 3rd parties, such as Amplitude.
* Enables / disables / sets custom integration properties so Segment can
* properly
* interact with 3rd parties, such as Amplitude, MoEngage.
*
* @see https://segment.com/docs/connections/sources/catalog/libraries/mobile/android/#selecting-destinations
* @see https://github.com/segmentio/analytics-android/blob/master/analytics/src/main/java/com/segment/analytics/Options.java
*/
Expand All @@ -377,16 +379,17 @@ private Options buildOptions(HashMap<String, Object> optionsData) {
Options options = new Options();

if (optionsData != null &&
optionsData.containsKey("integrations") &&
(optionsData.get("integrations") instanceof HashMap)) {
for (Map.Entry<String, Object> integration : ((HashMap<String,Object>)optionsData.get("integrations")).entrySet()) {
optionsData.containsKey("integrations") &&
(optionsData.get("integrations") instanceof HashMap)) {
for (Map.Entry<String, Object> integration : ((HashMap<String, Object>) optionsData.get("integrations"))
.entrySet()) {
String key = integration.getKey();

if (integration.getValue() instanceof HashMap) {
HashMap<String, Object> values = ((HashMap<String, Object>)integration.getValue());
HashMap<String, Object> values = ((HashMap<String, Object>) integration.getValue());
options.setIntegrationOptions(key, values);
} else if (integration.getValue() instanceof Boolean) {
Boolean value = ((Boolean)integration.getValue());
Boolean value = ((Boolean) integration.getValue());
options.setIntegration(key, value);
}
}
Expand Down
13 changes: 13 additions & 0 deletions example/ios/Flutter/flutter_export_environment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh
# This is a generated file; do not edit or check into version control.
export "FLUTTER_ROOT=/Users/rajajain/development/flutter"
export "FLUTTER_APPLICATION_PATH=/Users/rajajain/work/flutter-segment/example"
export "COCOAPODS_PARALLEL_CODE_SIGN=true"
export "FLUTTER_TARGET=lib/main.dart"
export "FLUTTER_BUILD_DIR=build"
export "FLUTTER_BUILD_NAME=0.0.1"
export "FLUTTER_BUILD_NUMBER=1"
export "DART_OBFUSCATION=false"
export "TRACK_WIDGET_CREATION=true"
export "TREE_SHAKE_ICONS=false"
export "PACKAGE_CONFIG=.dart_tool/package_config.json"
Loading