Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add webengage integration flow #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
Binary file removed .DS_Store
Binary file not shown.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -594,3 +594,9 @@ healthchecksdb
MigrationBackup/

# End of https://www.gitignore.io/api/git,dart,flutter,intellij,webstorm,visualstudio

# Not needed macOS generated files
.DS_Store
example/.DS_Store
example/android/.DS_Store
example/ios/.DS_Store
3 changes: 3 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ android {
}

dependencies {
implementation 'com.webengage:android-segment:2.+'
implementation 'com.google.firebase:firebase-messaging:22.0.0'
implementation 'com.segment.analytics.android:analytics:4.10.0'
implementation 'com.clevertap.android:clevertap-segment-android:+'
implementation 'com.segment.analytics.android.integrations:amplitude:3.0.3'
}
}
Expand Down
45 changes: 42 additions & 3 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.flutter_segment">
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
xmlns:tools="http://schemas.android.com/tools"
package="com.example.flutter_segment">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".MyApplication"
android:icon="@drawable/ic_launcher">
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service
android:name="com.clevertap.android.sdk.FcmTokenListenerService"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>

<service
android:name="com.clevertap.android.sdk.FcmMessageListenerService"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<receiver
android:name=".WebEngageFirebaseMessagingReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</receiver>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ public class FlutterSegmentOptions {
private final String writeKey;
private final Boolean trackApplicationLifecycleEvents;
private final Boolean amplitudeIntegrationEnabled;
private final Boolean webEngageIntegrationEnabled;
private final Boolean cleverTapIntegrationEnabled;
private final Boolean debug;

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

Expand All @@ -29,6 +33,14 @@ public Boolean isAmplitudeIntegrationEnabled() {
return amplitudeIntegrationEnabled;
}

public Boolean isWebEngageIntegrationEnabled() {
return webEngageIntegrationEnabled;
}

public Boolean isCleverTapIntegrationEnabled() {
return cleverTapIntegrationEnabled;
}

public Boolean getDebug() {
return debug;
}
Expand All @@ -37,16 +49,20 @@ 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 isWebEngageIntegrationEnabled = bundle.getBoolean("com.claimsforce.segment.ENABLE_WEBENGAGE_INTEGRATION", false);
Boolean isCleverTapIntegrationEnabled = bundle.getBoolean("com.claimsforce.segment.ENABLE_CLEVERTAP_INTEGRATION", false);
Boolean debug = bundle.getBoolean("com.claimsforce.segment.DEBUG", false);
return new FlutterSegmentOptions(writeKey, trackApplicationLifecycleEvents, isAmplitudeIntegrationEnabled, debug);
return new FlutterSegmentOptions(writeKey, trackApplicationLifecycleEvents, isAmplitudeIntegrationEnabled, isWebEngageIntegrationEnabled, isCleverTapIntegrationEnabled, 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 isWebEngageIntegrationEnabled = orFalse((Boolean) options.get("webEngageIntegrationEnabled"));
Boolean isCleverTapIntegrationEnabled = orFalse((Boolean) options.get("cleverTapIntegrationEnabled"));
Boolean debug = orFalse((Boolean) options.get("debug"));
return new FlutterSegmentOptions(writeKey, trackApplicationLifecycleEvents, isAmplitudeIntegrationEnabled, debug);
return new FlutterSegmentOptions(writeKey, trackApplicationLifecycleEvents, isAmplitudeIntegrationEnabled, isWebEngageIntegrationEnabled, isCleverTapIntegrationEnabled, debug);
}

private static Boolean orFalse(Boolean value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import com.segment.analytics.integrations.BasePayload;
import com.segment.analytics.android.integrations.amplitude.AmplitudeIntegration;
import static com.segment.analytics.Analytics.LogLevel;
import com.segment.analytics.android.integrations.clevertap.CleverTapIntegration;
import com.webengage.sdk.android.integrations.segment.WebEngageIntegration;
import com.webengage.sdk.android.WebEngageConfig;

import java.util.LinkedHashMap;
import java.util.HashMap;
Expand Down Expand Up @@ -87,6 +90,17 @@ private void setupChannels(FlutterSegmentOptions options) {
analyticsBuilder.use(AmplitudeIntegration.FACTORY);
}

if (options.isWebEngageIntegrationEnabled()) {
WebEngageConfig webEngageConfig = new WebEngageConfig.Builder()
.setDebugMode(true)
.build();
analyticsBuilder.use(WebEngageIntegration.FACTORY.withWebEngageConfig(webEngageConfig));
}

if (options.isCleverTapIntegrationEnabled()) {
analyticsBuilder.use(CleverTapIntegration.FACTORY);
}

// Here we build a middleware that just appends data to the current context
// using the [deepMerge] strategy.
analyticsBuilder.middleware(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.flutter_segment;
import android.app.Application;

import androidx.annotation.NonNull;

import com.webengage.sdk.android.WebEngage;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.messaging.FirebaseMessaging;

public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();

FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {
@Override
public void onComplete(@NonNull Task<String> task) {
try {
String token = task.getResult();
WebEngage.get().setRegistrationID(token);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.flutter_segment;

import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import com.webengage.sdk.android.WebEngage;

import java.util.Map;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onNewToken(String s) {
super.onNewToken(s);
WebEngage.get().setRegistrationID(s);
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Not needed already handling in MessagingReceiver
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.example.flutter_segment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.google.firebase.messaging.RemoteMessage;
import com.webengage.sdk.android.WebEngage;

import java.util.HashMap;
import java.util.Map;

public class WebEngageFirebaseMessagingReceiver extends BroadcastReceiver {
private static final String TAG = "Tournafest-WebEngage";
static HashMap<String, RemoteMessage> notifications = new HashMap<>();

@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Webengage broadcast received for message");

if (intent.getExtras() == null) {
Log.d(
TAG,
"broadcast received but intent contained no extras to process RemoteMessage. Operation cancelled.");
return;
}

RemoteMessage remoteMessage = new RemoteMessage(intent.getExtras());
Map<String, String> data = remoteMessage.getData();
if(data != null) {
if(data.containsKey("source") && "webengage".equals(data.get("source"))) {
WebEngage.get().receive(data);
}
}
}
}
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ packages:
path: ".."
relative: true
source: path
version: "3.5.0"
version: "3.7.0"
flutter_test:
dependency: "direct dev"
description: flutter
Expand Down
11 changes: 11 additions & 0 deletions ios/Classes/FlutterSegmentPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#import <Segment/SEGAnalytics.h>
#import <Segment/SEGContext.h>
#import <Segment/SEGMiddleware.h>
#import <Segment_WebEngage/WEGSegmentIntegrationFactory.h>
#import <Segment_Amplitude/SEGAmplitudeIntegrationFactory.h>

@implementation FlutterSegmentPlugin
Expand Down Expand Up @@ -347,6 +348,7 @@ + (SEGAnalyticsConfiguration*)createConfigFromFile {
NSString *writeKey = [dict objectForKey: @"com.claimsforce.segment.WRITE_KEY"];
BOOL trackApplicationLifecycleEvents = [[dict objectForKey: @"com.claimsforce.segment.TRACK_APPLICATION_LIFECYCLE_EVENTS"] boolValue];
BOOL isAmplitudeIntegrationEnabled = [[dict objectForKey: @"com.claimsforce.segment.ENABLE_AMPLITUDE_INTEGRATION"] boolValue];
BOOL isWebEngageIntegrationEnabled = [[dict objectForKey: @"com.claimsforce.segment.ENABLE_WEBENGAGE_INTEGRATION"] boolValue];
if(!writeKey) {
return nil;
}
Expand All @@ -357,20 +359,29 @@ + (SEGAnalyticsConfiguration*)createConfigFromFile {
[configuration use:[SEGAmplitudeIntegrationFactory instance]];
}

if (isWebEngageIntegrationEnabled) {
[configuration use:[WEGSegmentIntegrationFactory instanceWithApplication:UIApplication.sharedApplication launchOptions:nil]];
}

return configuration;
}

+ (SEGAnalyticsConfiguration*)createConfigFromDict:(NSDictionary*) dict {
NSString *writeKey = [dict objectForKey: @"writeKey"];
BOOL trackApplicationLifecycleEvents = [[dict objectForKey: @"trackApplicationLifecycleEvents"] boolValue];
BOOL isAmplitudeIntegrationEnabled = [[dict objectForKey: @"amplitudeIntegrationEnabled"] boolValue];
BOOL isWebEngageIntegrationEnabled = [[dict objectForKey: @"webEngageIntegrationEnabled"] boolValue];
SEGAnalyticsConfiguration *configuration = [SEGAnalyticsConfiguration configurationWithWriteKey:writeKey];
configuration.trackApplicationLifecycleEvents = trackApplicationLifecycleEvents;

if (isAmplitudeIntegrationEnabled) {
[configuration use:[SEGAmplitudeIntegrationFactory instance]];
}

if (isWebEngageIntegrationEnabled) {
[configuration use:[WEGSegmentIntegrationFactory instanceWithApplication:UIApplication.sharedApplication launchOptions:nil]];
}

return configuration;
}

Expand Down
1 change: 1 addition & 0 deletions ios/flutter_segment.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ A new flutter plugin project.
s.dependency 'Flutter'
s.dependency 'Analytics', '4.1.6'
s.dependency 'Segment-Amplitude', '3.3.2'
s.dependency 'Segment-WebEngage'
s.ios.deployment_target = '11.0'

# Added because Segment-Amplitude dependencies on iOS cause this error:
Expand Down
6 changes: 6 additions & 0 deletions lib/src/segment_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@ class SegmentConfig {
required this.writeKey,
this.trackApplicationLifecycleEvents = false,
this.amplitudeIntegrationEnabled = false,
this.webEngageIntegrationEnabled = false,
this.cleverTapIntegrationEnabled = false,
this.debug = false,
});

final String writeKey;
final bool trackApplicationLifecycleEvents;
final bool amplitudeIntegrationEnabled;
final bool webEngageIntegrationEnabled;
final bool cleverTapIntegrationEnabled;
final bool debug;

Map<String, dynamic> toMap() {
return {
'writeKey': writeKey,
'trackApplicationLifecycleEvents': trackApplicationLifecycleEvents,
'amplitudeIntegrationEnabled': amplitudeIntegrationEnabled,
'webEngageIntegrationEnabled': webEngageIntegrationEnabled,
'cleverTapIntegrationEnabled': cleverTapIntegrationEnabled,
'debug': debug,
};
}
Expand Down