Importing the Copilot.cx Android SDK for Flutter
Setting up the android side for Copilot.cx
Please follow steps 1 through 5 in Importing the Android SDK
Creating CopilotModule to interface with Dart
- Create a CopilotModule class that will interact with the Dart code.
Inside the
setupMethodChannel
function, we'll "capture" function calls from the dart side(with or without parameters) and point them to our native side fuctions. In this following example we're using thestartSession
,endSession
andsendEvent
functions.
class CopilotModule(
private val flutterEngine: FlutterEngine,
private val application: Application,
private val context: Context,
) {
private val channel = "myProjectChannelName"
init {
setupMethodChannel()
}
private fun setupMethodChannel() {
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, channel).setMethodCallHandler { call, result ->
when (call.method) {
"startSession" -> {
val userId = call.argument<String>("userId")
val isConsentGiven = call.argument<Boolean>("isConsentGiven")
if (userId != null && isConsentGiven != null) {
startSession(userId, isConsentGiven)
}
}
"endSession" -> {
endSession()
}
"sendEvent" -> {
val eventName = call.argument<String>("eventName")
val eventParams = call.argument<Map<String, String>>("eventParams")
if (eventName != null && eventParams != null) {
sendEvent(eventName, eventParams)
}
}
else -> {
result.notImplemented()
}
}
}
}
private fun startSession(userId: String?, isConsentGiven: Boolean) {
Copilot.getInstance().Manage.YourOwn.sessionStarted(userId, isConsentGiven)
}
private fun endSession() {
Copilot.getInstance().Manage.YourOwn.sessionEnded()
}
private fun sendEvent(eventName: String, map: Map<String, String>) {
val customEvent = CustomAnalyticsEvent(eventName, map)
Copilot.getInstance().Report.logEvent(customEvent)
}
}
- Create a
FlutterApplication
class and init the Copilot.Cx Sdk inside theonCreate
function. Don't forget to replace the application class name inside theAndroidManifest.xml
file.
class MyApplication : FlutterApplication() {
override fun onCreate() {
super.onCreate()
Copilot.setup(this, listOf(FirebaseEventLogProvider(this)))
}
}
- inside your
MainActivity
add an instance forCopilotModule
and initialize it inside configureFlutterEngine and setup the Copilot.cx SDK with theEventLogProvider
class MainActivity: FlutterActivity() {
lateinit var copilotModule : CopilotModule
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
...
copilotModule = CopilotModule(flutterEngine,application, this)
...
}
}