From 3db634f7183e7d1d630181ab021030c007adbe72 Mon Sep 17 00:00:00 2001 From: Rehan Date: Fri, 8 Nov 2024 16:30:48 +0500 Subject: [PATCH] chore: update screen tracking parameters --- .../customer/customer_io/CustomerIoPlugin.kt | 25 +++++++++++-------- .../io/customer/customer_io/constant/Keys.kt | 1 + apps/amiapp_flutter/lib/src/app.dart | 2 +- apps/amiapp_flutter/lib/src/data/config.dart | 13 +++++----- ios/Classes/Keys.swift | 1 + ios/Classes/SwiftCustomerIoPlugin.swift | 25 ++++++++----------- lib/customer_io.dart | 4 +-- lib/customer_io_const.dart | 1 + lib/customer_io_method_channel.dart | 8 +++--- lib/customer_io_platform_interface.dart | 2 +- test/customer_io_method_channel_test.dart | 6 ++--- test/customer_io_test.dart | 13 ++++++++++ test/customer_io_test.mocks.dart | 8 +++--- 13 files changed, 63 insertions(+), 46 deletions(-) diff --git a/android/src/main/kotlin/io/customer/customer_io/CustomerIoPlugin.kt b/android/src/main/kotlin/io/customer/customer_io/CustomerIoPlugin.kt index 8dbc1cc..1faea0b 100644 --- a/android/src/main/kotlin/io/customer/customer_io/CustomerIoPlugin.kt +++ b/android/src/main/kotlin/io/customer/customer_io/CustomerIoPlugin.kt @@ -22,6 +22,7 @@ import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.MethodChannel.MethodCallHandler +import io.flutter.plugin.common.MethodChannel.Result import java.lang.ref.WeakReference /** @@ -171,9 +172,11 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { private fun track(params: Map) { val name = requireNotNull(params.getAsTypeOrNull(Keys.Tracking.NAME)) { - "Event name is required to track event" + "Event name is missing in params: $params" } - val properties = params.getAsTypeOrNull>(Keys.Tracking.PROPERTIES) + val properties = params + .getAsTypeOrNull>(Keys.Tracking.PROPERTIES) + ?.filterValues { it != null } if (properties.isNullOrEmpty()) { CustomerIO.instance().track(name) @@ -225,18 +228,18 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { } private fun screen(params: Map) { - // TODO: Fix screen implementation - /* - val name = params.getString(Keys.Tracking.EVENT_NAME) - val attributes = - params.getProperty>(Keys.Tracking.ATTRIBUTES) ?: emptyMap() + val title = requireNotNull(params.getAsTypeOrNull(Keys.Tracking.TITLE)) { + "Screen title is missing in params: $params" + } + val properties = params + .getAsTypeOrNull>(Keys.Tracking.PROPERTIES) + ?.filterValues { it != null } - if (attributes.isEmpty()) { - CustomerIO.instance().screen(name) + if (properties.isNullOrEmpty()) { + CustomerIO.instance().screen(title) } else { - CustomerIO.instance().screen(name, attributes) + CustomerIO.instance().screen(title, properties) } - */ } private fun initialize(args: Map): kotlin.Result = runCatching { diff --git a/android/src/main/kotlin/io/customer/customer_io/constant/Keys.kt b/android/src/main/kotlin/io/customer/customer_io/constant/Keys.kt index c2cab20..68d7773 100644 --- a/android/src/main/kotlin/io/customer/customer_io/constant/Keys.kt +++ b/android/src/main/kotlin/io/customer/customer_io/constant/Keys.kt @@ -28,5 +28,6 @@ internal object Keys { const val NAME = "name" const val PROPERTIES = "properties" + const val TITLE = "title" } } diff --git a/apps/amiapp_flutter/lib/src/app.dart b/apps/amiapp_flutter/lib/src/app.dart index 4744fce..5ac08e5 100644 --- a/apps/amiapp_flutter/lib/src/app.dart +++ b/apps/amiapp_flutter/lib/src/app.dart @@ -216,7 +216,7 @@ class _AmiAppState extends State { if (_customerIOSDK.sdkConfig?.screenTrackingEnabled == true) { final Screen? screen = _router.currentLocation().toAppScreen(); if (screen != null) { - CustomerIO.instance.screen(name: screen.name); + CustomerIO.instance.screen(title: screen.name); } } } diff --git a/apps/amiapp_flutter/lib/src/data/config.dart b/apps/amiapp_flutter/lib/src/data/config.dart index d08b74b..7b14cfb 100644 --- a/apps/amiapp_flutter/lib/src/data/config.dart +++ b/apps/amiapp_flutter/lib/src/data/config.dart @@ -8,8 +8,8 @@ class CustomerIOSDKConfig { final String cdpApiKey; final String? migrationSiteId; final Region? region; - final bool? debugModeEnabled; - final bool? screenTrackingEnabled; + final bool debugModeEnabled; + final bool screenTrackingEnabled; final bool? autoTrackDeviceAttributes; final String? apiHost; final String? cdnHost; @@ -22,8 +22,8 @@ class CustomerIOSDKConfig { required this.cdpApiKey, this.migrationSiteId, this.region, - this.debugModeEnabled, - this.screenTrackingEnabled, + this.debugModeEnabled = true, + this.screenTrackingEnabled = true, this.autoTrackDeviceAttributes, this.apiHost, this.cdnHost, @@ -53,9 +53,10 @@ class CustomerIOSDKConfig { cdpApiKey: cdpApiKey, migrationSiteId: prefs.getString(_PreferencesKey.migrationSiteId), region: region, - debugModeEnabled: prefs.getBool(_PreferencesKey.debugModeEnabled), + debugModeEnabled: + prefs.getBool(_PreferencesKey.debugModeEnabled) != false, screenTrackingEnabled: - prefs.getBool(_PreferencesKey.screenTrackingEnabled), + prefs.getBool(_PreferencesKey.screenTrackingEnabled) != false, autoTrackDeviceAttributes: prefs.getBool(_PreferencesKey.autoTrackDeviceAttributes), apiHost: prefs.getString(_PreferencesKey.apiHost), diff --git a/ios/Classes/Keys.swift b/ios/Classes/Keys.swift index de7738b..087ac63 100644 --- a/ios/Classes/Keys.swift +++ b/ios/Classes/Keys.swift @@ -27,6 +27,7 @@ struct Keys { static let name = "name" static let properties = "properties" + static let title = "title" } struct Environment{ diff --git a/ios/Classes/SwiftCustomerIoPlugin.swift b/ios/Classes/SwiftCustomerIoPlugin.swift index c86bab5..8f85f0a 100644 --- a/ios/Classes/SwiftCustomerIoPlugin.swift +++ b/ios/Classes/SwiftCustomerIoPlugin.swift @@ -106,20 +106,17 @@ public class SwiftCustomerIoPlugin: NSObject, FlutterPlugin { } func screen(params : Dictionary) { - // TODO: Fix screen implementation - /* - guard let name = params[Keys.Tracking.eventName] as? String - else { - return - } - - guard let attributes = params[Keys.Tracking.attributes] as? Dictionary else{ - CustomerIO.shared.screen(name: name) - return - } - - CustomerIO.shared.screen(name: name, data: attributes) - */ + guard let title = params[Keys.Tracking.title] as? String else { + logger.error("Missing screen title in: \(params) for key: \(Keys.Tracking.title)") + return + } + + guard let properties = params[Keys.Tracking.properties] as? Dictionary else { + CustomerIO.shared.screen(title: title) + return + } + + CustomerIO.shared.screen(title: title, properties: properties) } diff --git a/lib/customer_io.dart b/lib/customer_io.dart index 76cb517..9de445d 100644 --- a/lib/customer_io.dart +++ b/lib/customer_io.dart @@ -131,8 +131,8 @@ class CustomerIO { /// @param name name of the screen user visited /// @param attributes (Optional) params to be sent with event void screen( - {required String name, Map attributes = const {}}) { - return _platform.screen(name: name, attributes: attributes); + {required String title, Map properties = const {}}) { + return _platform.screen(title: title, properties: properties); } /// Use this function to send custom device attributes diff --git a/lib/customer_io_const.dart b/lib/customer_io_const.dart index 8478dc5..e0d0d2f 100644 --- a/lib/customer_io_const.dart +++ b/lib/customer_io_const.dart @@ -28,4 +28,5 @@ class TrackingConsts { static const String name = "name"; static const String properties = "properties"; + static const String title = "title"; } diff --git a/lib/customer_io_method_channel.dart b/lib/customer_io_method_channel.dart index 4089c00..490dac2 100644 --- a/lib/customer_io_method_channel.dart +++ b/lib/customer_io_method_channel.dart @@ -128,12 +128,12 @@ class CustomerIOMethodChannel extends CustomerIOPlatform { /// Track screen events to record the screens a user visits @override void screen( - {required String name, - Map attributes = const {}}) async { + {required String title, + Map properties = const {}}) async { try { final payload = { - TrackingConsts.eventName: name, - TrackingConsts.attributes: attributes + TrackingConsts.title: title, + TrackingConsts.properties: properties }; methodChannel.invokeMethod(MethodConsts.screen, payload); } on PlatformException catch (exception) { diff --git a/lib/customer_io_platform_interface.dart b/lib/customer_io_platform_interface.dart index 1e44480..2109468 100644 --- a/lib/customer_io_platform_interface.dart +++ b/lib/customer_io_platform_interface.dart @@ -61,7 +61,7 @@ abstract class CustomerIOPlatform extends PlatformInterface { } void screen( - {required String name, Map attributes = const {}}) { + {required String title, Map properties = const {}}) { throw UnimplementedError('screen() has not been implemented.'); } diff --git a/test/customer_io_method_channel_test.dart b/test/customer_io_method_channel_test.dart index cd92143..315318c 100644 --- a/test/customer_io_method_channel_test.dart +++ b/test/customer_io_method_channel_test.dart @@ -105,12 +105,12 @@ void main() { test('screen() should call platform method with correct arguments', () async { final Map args = { - 'eventName': 'screen_event', - 'attributes': {'screenName': '你好'} + 'title': 'screen_event', + 'properties': {'screenName': '你好'} }; final customerIO = CustomerIOMethodChannel(); - customerIO.screen(name: args['eventName'], attributes: args['attributes']); + customerIO.screen(title: args['title'], properties: args['properties']); expectMethodInvocationArguments('screen', args); }); diff --git a/test/customer_io_test.dart b/test/customer_io_test.dart index 1d7b2ab..c76d2e7 100644 --- a/test/customer_io_test.dart +++ b/test/customer_io_test.dart @@ -149,6 +149,19 @@ void main() { ); }); + test('screen() correct arguments are passed', () { + const title = 'checkout'; + final givenProperties = {'source': 'push'}; + CustomerIO.instance.screen(title: title, properties: givenProperties); + expect( + verify(mockPlatform.screen( + title: captureAnyNamed("title"), + properties: captureAnyNamed("properties"), + )).captured, + [title, givenProperties], + ); + }); + test('trackMetric() calls platform', () { const deliveryID = '123'; const deviceToken = 'abc'; diff --git a/test/customer_io_test.mocks.dart b/test/customer_io_test.mocks.dart index 768a9cd..c49c303 100644 --- a/test/customer_io_test.mocks.dart +++ b/test/customer_io_test.mocks.dart @@ -124,16 +124,16 @@ class MockTestCustomerIoPlatform extends _i1.Mock ); @override void screen({ - required String? name, - Map? attributes = const {}, + required String? title, + Map? properties = const {}, }) => super.noSuchMethod( Invocation.method( #screen, [], { - #name: name, - #attributes: attributes, + #title: title, + #properties: properties, }, ), returnValueForMissingStub: null,