From a27219de643985f1313bd5b1075f2b2e9438d1c0 Mon Sep 17 00:00:00 2001 From: Kyle Date: Sat, 18 Jul 2020 08:51:38 -0700 Subject: [PATCH] Comments Updates Refactor native Kotlin + Dart code into separated functions and classes Switch to positional / required parameters for Dart Switch method channel to analytics_pinpoint Basic unit tests in amplify_analytics_pinpoint_test.dart --- .../AmplifyAnalyticsBridge.kt | 100 +++++++++++ .../AmplifyAnalyticsConstructor.kt | 125 ++++++++++++++ .../AmplifyAnalyticsPinpointPlugin.kt | 159 ++---------------- .../example/lib/main.dart | 13 +- .../ios/Classes/AmplifyAnalyticsBridge.swift | 59 +++++++ .../Classes/AmplifyAnalyticsConstructor.swift | 70 ++++++++ .../SwiftAmplifyAnalyticsPinpointPlugin.swift | 106 ++---------- .../lib/amplify_analytics_pinpoint.dart | 19 ++- .../lib/method_channel_amplify.dart | 20 +-- .../PinpointAnalyticsEvent.dart | 0 .../PinpointAnalyticsProperties.dart | 0 .../PinpointAnalyticsUserProfile.dart | 0 ...PinpointAnalyticsUserProfileLocation.dart} | 0 .../lib/src/types.dart | 4 + .../amplify_analytics_pinpoint/pubspec.yaml | 4 + .../test/amplify_analytics_pinpoint_test.dart | 114 ++++++++++++- .../lib/analytics_plugin_interface.dart | 25 +-- .../lib/src/types.dart | 4 + packages/amplify_core/lib/amplify_core.dart | 2 +- .../lib/amplify_analytics_category.dart | 16 +- 20 files changed, 530 insertions(+), 310 deletions(-) create mode 100644 packages/amplify_analytics_pinpoint/android/src/main/kotlin/com/amazonaws/amplify/amplify_analytics_pinpoint/AmplifyAnalyticsBridge.kt create mode 100644 packages/amplify_analytics_pinpoint/android/src/main/kotlin/com/amazonaws/amplify/amplify_analytics_pinpoint/AmplifyAnalyticsConstructor.kt create mode 100644 packages/amplify_analytics_pinpoint/ios/Classes/AmplifyAnalyticsBridge.swift create mode 100644 packages/amplify_analytics_pinpoint/ios/Classes/AmplifyAnalyticsConstructor.swift rename packages/amplify_analytics_pinpoint/lib/{ => src}/PinpointAnalytics/PinpointAnalyticsEvent.dart (100%) rename packages/amplify_analytics_pinpoint/lib/{ => src}/PinpointAnalytics/PinpointAnalyticsProperties.dart (100%) rename packages/amplify_analytics_pinpoint/lib/{ => src}/PinpointAnalytics/PinpointAnalyticsUserProfile.dart (100%) rename packages/amplify_analytics_pinpoint/lib/{PinpointAnalytics/PintpointAnalyticsUserProfileLocation.dart => src/PinpointAnalytics/PinpointAnalyticsUserProfileLocation.dart} (100%) create mode 100644 packages/amplify_analytics_pinpoint/lib/src/types.dart create mode 100644 packages/amplify_analytics_plugin_interface/lib/src/types.dart diff --git a/packages/amplify_analytics_pinpoint/android/src/main/kotlin/com/amazonaws/amplify/amplify_analytics_pinpoint/AmplifyAnalyticsBridge.kt b/packages/amplify_analytics_pinpoint/android/src/main/kotlin/com/amazonaws/amplify/amplify_analytics_pinpoint/AmplifyAnalyticsBridge.kt new file mode 100644 index 00000000000..b019682d34b --- /dev/null +++ b/packages/amplify_analytics_pinpoint/android/src/main/kotlin/com/amazonaws/amplify/amplify_analytics_pinpoint/AmplifyAnalyticsBridge.kt @@ -0,0 +1,100 @@ +package com.amazonaws.amplify.amplify_analytics_pinpoint + +import androidx.annotation.NonNull + +import com.amplifyframework.core.Amplify + +import io.flutter.plugin.common.MethodChannel + + +class AmplifyAnalyticsBridge { + companion object Bridge { + + fun recordEvent(@NonNull arguments: Any, @NonNull result: MethodChannel.Result){ + try { + val argumentsMap = arguments as HashMap<*,*> + val name = argumentsMap["name"] as String + val properties = argumentsMap["propertiesMap"] as HashMap; + Amplify.Analytics.recordEvent( + AmplifyAnalyticsConstructor.createAnalyticsEvent(name, properties) ); + result.success(true); + } + catch(e: Exception) { + result.error("AmplifyException", "Error", e.message ) + } + } + + fun flushEvents(@NonNull result: MethodChannel.Result){ + try { + Amplify.Analytics.flushEvents(); + } + catch(e: Exception){ + result.error("AmplifyException", "Error", e.message ) + } + } + + fun registerGlobalProperties(@NonNull arguments: Any, @NonNull result: MethodChannel.Result){ + try{ + val globalProperties = arguments as HashMap; + Amplify.Analytics.registerGlobalProperties( + AmplifyAnalyticsConstructor.createAnalyticsProperties(globalProperties) ); + } + catch(e: Exception){ + result.error("AmplifyException", "Error", e.message ) + } + } + + fun unregisterGlobalProperties(@NonNull arguments: Any, @NonNull result: MethodChannel.Result){ + try{ + val propertyNames = (arguments as ArrayList).toSet(); + + for(name in propertyNames){ + Amplify.Analytics.unregisterGlobalProperties( name ); + } + } + catch(e: Exception){ + result.error("AmplifyException", "Error", e.message ) + } + } + + fun unregisterAllGlobalProperties(@NonNull result: MethodChannel.Result){ + try { + Amplify.Analytics.unregisterGlobalProperties() + } + catch(e: Exception){ + result.error("AmplifyException", "Error", e.message ) + } + } + + fun enable(@NonNull result: MethodChannel.Result) { + try { + Amplify.Analytics.enable(); + } catch (e: Exception) { + result.error("AmplifyException", "Error", e.message) + } + } + + fun disable(@NonNull result: MethodChannel.Result){ + try { + Amplify.Analytics.disable(); + } catch (e: Exception) { + result.error("AmplifyException", "Error", e.message) + } + } + + fun identifyUser(@NonNull arguments: Any, @NonNull result: MethodChannel.Result){ + try { + val argumentsMap = arguments as HashMap<*, *> + val userId = argumentsMap["userId"] as String + val userProfileMap = argumentsMap["userProfileMap"] as HashMap; + + Amplify.Analytics.identifyUser(userId, + AmplifyAnalyticsConstructor.createUserProfile(userProfileMap)); + } + catch(e: Exception){ + result.error("AmplifyException", "Error", e.message ) + } + } + + } +} \ No newline at end of file diff --git a/packages/amplify_analytics_pinpoint/android/src/main/kotlin/com/amazonaws/amplify/amplify_analytics_pinpoint/AmplifyAnalyticsConstructor.kt b/packages/amplify_analytics_pinpoint/android/src/main/kotlin/com/amazonaws/amplify/amplify_analytics_pinpoint/AmplifyAnalyticsConstructor.kt new file mode 100644 index 00000000000..329a9132e5d --- /dev/null +++ b/packages/amplify_analytics_pinpoint/android/src/main/kotlin/com/amazonaws/amplify/amplify_analytics_pinpoint/AmplifyAnalyticsConstructor.kt @@ -0,0 +1,125 @@ +package com.amazonaws.amplify.amplify_analytics_pinpoint + +import com.amplifyframework.analytics.AnalyticsEvent +import com.amplifyframework.analytics.AnalyticsProperties +import com.amplifyframework.analytics.UserProfile + +class AmplifyAnalyticsConstructor { + companion object Constructor { + + fun createAnalyticsProperties(propertiesMap : HashMap) : AnalyticsProperties { + + val propertiesBuilder: AnalyticsProperties.Builder = AnalyticsProperties.builder() + + for( (key, value ) in propertiesMap){ + + when (value) { + is String -> { + propertiesBuilder.add(key, value); + } + is Double -> { + propertiesBuilder.add(key, value); + } + is Boolean -> { + propertiesBuilder.add(key, value); + } + is Int -> { + propertiesBuilder.add(key, value); + } + else -> { + throw IllegalArgumentException("Warning unrecognized object type sent via MethodChannel-AnalyticsProperties"); + } + } + + } + + return propertiesBuilder.build(); + } + + fun createAnalyticsEvent(name: String, propertiesMap: HashMap) : AnalyticsEvent{ + + val eventBuilder: AnalyticsEvent.Builder = AnalyticsEvent.builder() + .name( name ); + + for( (key, value ) in propertiesMap){ + + when (value) { + is String -> { + eventBuilder.addProperty(key, value); + } + is Double -> { + eventBuilder.addProperty(key, value); + } + is Boolean -> { + eventBuilder.addProperty(key, value); + } + is Int -> { + eventBuilder.addProperty(key, value); + } + else -> { + throw IllegalArgumentException("Warning unrecognized object type sent via MethodChannel-AnalyticsProperties"); + } + } + } + + return eventBuilder.build(); + } + + fun createUserProfile(userProfileMap: HashMap) : UserProfile { + + val userProfileBuilder = UserProfile.builder(); + + for (item in userProfileMap) { + when (item.key) { + "name" -> + userProfileBuilder.name(item.value as String); + "email" -> + userProfileBuilder.email(item.value as String); + "plan" -> + userProfileBuilder.plan(item.value as String); + "location" -> { + val locationMap = item.value as HashMap; + userProfileBuilder.location(createUserLocation(locationMap)); + } + "properties" -> { + val propertiesMap = item.value as HashMap; + userProfileBuilder.customProperties(createAnalyticsProperties(propertiesMap)); + } + else -> { + throw IllegalArgumentException("Warning unrecognized object type sent via MethodChannel-AnalyticsProperties"); + } + } + } + + return userProfileBuilder.build(); + } + + fun createUserLocation(userLocationMap: HashMap) : UserProfile.Location{ + + val locationBuilder = UserProfile.Location.builder() + + for(item in userLocationMap){ + when(item.key){ + "latitude" -> + locationBuilder.latitude(item.value as Double); + "longitude" -> + locationBuilder.longitude(item.value as Double); + "postalCode" -> + locationBuilder.postalCode(item.value as String); + "city" -> + locationBuilder.city(item.value as String); + "region" -> + locationBuilder.region(item.value as String); + "country" -> + locationBuilder.country(item.value as String); + else -> { + throw IllegalArgumentException("Warning unrecognized object type sent via MethodChannel-AnalyticsProperties"); + } + } + } + + return locationBuilder.build(); + } + } + +} \ No newline at end of file diff --git a/packages/amplify_analytics_pinpoint/android/src/main/kotlin/com/amazonaws/amplify/amplify_analytics_pinpoint/AmplifyAnalyticsPinpointPlugin.kt b/packages/amplify_analytics_pinpoint/android/src/main/kotlin/com/amazonaws/amplify/amplify_analytics_pinpoint/AmplifyAnalyticsPinpointPlugin.kt index c4beda61cd5..0240958b257 100644 --- a/packages/amplify_analytics_pinpoint/android/src/main/kotlin/com/amazonaws/amplify/amplify_analytics_pinpoint/AmplifyAnalyticsPinpointPlugin.kt +++ b/packages/amplify_analytics_pinpoint/android/src/main/kotlin/com/amazonaws/amplify/amplify_analytics_pinpoint/AmplifyAnalyticsPinpointPlugin.kt @@ -5,11 +5,6 @@ import android.app.Application import android.util.Log import androidx.annotation.NonNull; - -import com.amplifyframework.AmplifyException -import com.amplifyframework.analytics.AnalyticsEvent -import com.amplifyframework.analytics.AnalyticsProperties -import com.amplifyframework.analytics.UserProfile import com.amplifyframework.analytics.pinpoint.AWSPinpointAnalyticsPlugin import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin import com.amplifyframework.core.Amplify @@ -32,7 +27,7 @@ public class AmplifyAnalyticsPinpointPlugin: FlutterPlugin, ActivityAware, Metho override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { - channel = MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "com.amazonaws.amplify/analytics") + channel = MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "com.amazonaws.amplify/analytics_pinpoint") channel.setMethodCallHandler(this); Amplify.addPlugin(AWSCognitoAuthPlugin()) @@ -71,53 +66,22 @@ public class AmplifyAnalyticsPinpointPlugin: FlutterPlugin, ActivityAware, Metho when (call.method) { "recordEvent" -> - try { - val arguments = call.arguments as HashMap<*,*> - val name = arguments["name"] as String - val properties = arguments["propertiesMap"] as HashMap; - Amplify.Analytics.recordEvent( createAnalyticsEvent(name, properties) ); - } - catch (e: Exception) { - result.error("AmplifyException", "Error", e.message ) - } + AmplifyAnalyticsBridge.recordEvent(call.arguments, result); "flushEvents" -> - Amplify.Analytics.flushEvents(); + AmplifyAnalyticsBridge.flushEvents(result); "registerGlobalProperties" -> - try{ - val globalProperties = call.arguments as HashMap; - Amplify.Analytics.registerGlobalProperties( createAnalyticsProperties(globalProperties) ); - } - catch(e: Exception){ - result.error("AmplifyException", "Error", e.message ) - } + AmplifyAnalyticsBridge.registerGlobalProperties(call.arguments, result); "unregisterGlobalProperties" -> - try{ - val propertyNames = (call.arguments as ArrayList).toSet(); - - for(name in propertyNames){ - Amplify.Analytics.unregisterGlobalProperties( name ); - } - } - catch(e: Exception){ - result.error("AmplifyException", "Error", e.message ) - } + AmplifyAnalyticsBridge.unregisterGlobalProperties(call.arguments, result); "unregisterAllGlobalProperties" -> - Amplify.Analytics.unregisterGlobalProperties() + AmplifyAnalyticsBridge.unregisterAllGlobalProperties(result); "enable" -> - Amplify.Analytics.enable(); + AmplifyAnalyticsBridge.enable(result); "disable" -> - Amplify.Analytics.disable(); + AmplifyAnalyticsBridge.disable(result); "identifyUser" -> - try { - val arguments = call.arguments as HashMap<*, *> - val userId = arguments["userId"] as String - val userProfileMap = arguments["userProfileMap"] as HashMap; - - Amplify.Analytics.identifyUser(userId, createUserProfile(userProfileMap)); - } - catch(e: Exception){ - result.error("AmplifyException", "Error", e.message ) - } + AmplifyAnalyticsBridge.identifyUser(call.arguments, result); + else -> result.notImplemented() } } @@ -142,108 +106,5 @@ public class AmplifyAnalyticsPinpointPlugin: FlutterPlugin, ActivityAware, Metho channel.setMethodCallHandler(null) } - private fun createAnalyticsProperties(propertiesMap : HashMap) : AnalyticsProperties { - - val propertiesBuilder: AnalyticsProperties.Builder = AnalyticsProperties.builder() - - for( (key, value ) in propertiesMap){ - - if(value is String){ - propertiesBuilder.add(key, value); - } - else if(value is Double){ - propertiesBuilder.add(key, value); - } - else if(value is Boolean){ - propertiesBuilder.add(key, value); - } - else if(value is Int){ - propertiesBuilder.add(key, value); - } - else{ - Log.w(TAG,"Warning unrecognized object type sent via MethodChannel-AnalyticsProperties") - } - - } - - return propertiesBuilder.build(); - } - - private fun createAnalyticsEvent(name: String, propertiesMap: HashMap) : AnalyticsEvent{ - - val eventBuilder: AnalyticsEvent.Builder = AnalyticsEvent.builder() - .name( name ); - - for( (key, value ) in propertiesMap){ - - if(value is String){ - eventBuilder.addProperty(key, value); - } - else if(value is Double){ - eventBuilder.addProperty(key, value); - } - else if(value is Boolean){ - eventBuilder.addProperty(key, value); - } - else if(value is Int){ - eventBuilder.addProperty(key, value); - } - else{ - Log.w(TAG,"Warning unrecognized object type sent via MethodChannel-AnalyticsProperties") - } - } - - return eventBuilder.build(); - } - - private fun createUserProfile(userProfileMap: HashMap) : UserProfile { - - val userProfileBuilder = UserProfile.builder(); - - for (item in userProfileMap) { - when (item.key) { - "name" -> - userProfileBuilder.name(item.value as String); - "email" -> - userProfileBuilder.email(item.value as String); - "plan" -> - userProfileBuilder.plan(item.value as String); - "location" -> { - val locationMap = item.value as HashMap; - userProfileBuilder.location(createUserLocation(locationMap)); - } - "properties" -> { - val propertiesMap = item.value as HashMap; - userProfileBuilder.customProperties(createAnalyticsProperties(propertiesMap)); - } - } - } - - return userProfileBuilder.build(); - } - - private fun createUserLocation(userLocationMap: HashMap) : UserProfile.Location{ - - val locationBuilder = UserProfile.Location.builder() - - for(item in userLocationMap){ - when(item.key){ - "latitude" -> - locationBuilder.latitude(item.value as Double); - "longitude" -> - locationBuilder.longitude(item.value as Double); - "postalCode" -> - locationBuilder.postalCode(item.value as String); - "city" -> - locationBuilder.city(item.value as String); - "region" -> - locationBuilder.region(item.value as String); - "country" -> - locationBuilder.country(item.value as String); - } - } - - return locationBuilder.build(); - } } diff --git a/packages/amplify_analytics_pinpoint/example/lib/main.dart b/packages/amplify_analytics_pinpoint/example/lib/main.dart index d60648e41d5..88925a6056a 100644 --- a/packages/amplify_analytics_pinpoint/example/lib/main.dart +++ b/packages/amplify_analytics_pinpoint/example/lib/main.dart @@ -1,8 +1,5 @@ -import 'package:amplify_analytics_pinpoint/PinpointAnalytics/PinpointAnalyticsProperties.dart'; import 'package:flutter/material.dart'; -import 'dart:async'; -import 'package:flutter/services.dart'; import 'package:amplify_core/amplify_core.dart'; import 'package:amplify_analytics_pinpoint/amplify_analytics_pinpoint.dart'; import 'package:amplify_analytics_plugin_interface/analytics_plugin_interface.dart'; @@ -51,7 +48,7 @@ class _MyAppState extends State { } catch (e) { print(e); } - Amplify.Analytics.registerGlobalProperties(new PinpointAnalyticsProperties()); + } void _recordEvent() async { @@ -62,7 +59,7 @@ class _MyAppState extends State { event.properties.addIntProperty("intKey", 10); event.properties.addStringProperty("stringKey", "stringValue"); - Amplify.Analytics.recordEvent(event); + Amplify.Analytics.recordEvent(event: event); } void _flushEvents() async { @@ -78,14 +75,14 @@ class _MyAppState extends State { properties.addIntProperty(_globalProp + "_intKey", 10); properties.addStringProperty(_globalProp + "_stringKey", "stringValue"); - Amplify.Analytics.registerGlobalProperties(properties); + Amplify.Analytics.registerGlobalProperties(globalProperties: properties); } void _unregisterGlobalProperties() async{ print("unregister global properties: " + _globalProp); - Amplify.Analytics.unregisterGlobalProperties( [_globalProp] ); ; + Amplify.Analytics.unregisterGlobalProperties(propertyNames: [_globalProp] ); ; } void _unregisterAllGlobalProperties() async{ Amplify.Analytics.unregisterAllGlobalProperties(); @@ -118,7 +115,7 @@ class _MyAppState extends State { analyticsUserProfile.properties = properties; - Amplify.Analytics.identifyUser(_userId, analyticsUserProfile); + Amplify.Analytics.identifyUser(userId: _userId, userProfile: analyticsUserProfile); } @override diff --git a/packages/amplify_analytics_pinpoint/ios/Classes/AmplifyAnalyticsBridge.swift b/packages/amplify_analytics_pinpoint/ios/Classes/AmplifyAnalyticsBridge.swift new file mode 100644 index 00000000000..d5436d06abc --- /dev/null +++ b/packages/amplify_analytics_pinpoint/ios/Classes/AmplifyAnalyticsBridge.swift @@ -0,0 +1,59 @@ +import Amplify +import AmplifyPlugins + +public class AmplifyAnalyticsBridge { + + public static func recordEvent(arguments: Any?, result: @escaping FlutterResult){ + let argumentsMap = arguments as! Dictionary + + let name = argumentsMap["name"] as! String + let propertiesMap = argumentsMap["propertiesMap"] as! Dictionary + + let event = BasicAnalyticsEvent(name: name, properties: AmplifyAnalyticsConstructor.createAnalyticsProperties(propertiesMap: propertiesMap)) + Amplify.Analytics.record(event: event) + + result(true); + } + + + public static func flushEvents(result: @escaping FlutterResult){ + Amplify.Analytics.flushEvents() + result(true); + } + + public static func registerGlobalProperties(arguments: Any?, result: @escaping FlutterResult){ + let propertiesMap = arguments as! Dictionary + Amplify.Analytics.registerGlobalProperties(AmplifyAnalyticsConstructor.createAnalyticsProperties(propertiesMap: propertiesMap)) + result(true); + } + + public static func unregisterGlobalProperties(arguments: Any?, result: @escaping FlutterResult){ + let propertyNames = Set(arguments as! Array) + Amplify.Analytics.unregisterGlobalProperties(propertyNames) + result(true); + } + public static func unregisterAllGlobalProperties(result: @escaping FlutterResult){ + Amplify.Analytics.unregisterGlobalProperties() + result(true); + } + + public static func enable(result: @escaping FlutterResult){ + Amplify.Analytics.enable() + result(true); + } + + public static func disable(result: @escaping FlutterResult){ + Amplify.Analytics.disable() + result(true); + } + + public static func identifyUser(arguments: Any?, result: @escaping FlutterResult){ + let arguments = arguments as! Dictionary + + let userId = arguments["userId"] as! String + let userProfileMap = arguments["userProfileMap"] as! Dictionary + + Amplify.Analytics.identifyUser(userId, withProfile: AmplifyAnalyticsConstructor.createUserProfile(userProfileMap: userProfileMap)) + result(true); + } +} diff --git a/packages/amplify_analytics_pinpoint/ios/Classes/AmplifyAnalyticsConstructor.swift b/packages/amplify_analytics_pinpoint/ios/Classes/AmplifyAnalyticsConstructor.swift new file mode 100644 index 00000000000..df8bafcd358 --- /dev/null +++ b/packages/amplify_analytics_pinpoint/ios/Classes/AmplifyAnalyticsConstructor.swift @@ -0,0 +1,70 @@ +import Amplify +import AmplifyPlugins + +public class AmplifyAnalyticsConstructor { + + public static func createAnalyticsProperties(propertiesMap : Dictionary) -> AnalyticsProperties{ + + var analyticsProperties: AnalyticsProperties = AnalyticsProperties() + + for(key, value) in propertiesMap + { + analyticsProperties[key] = value as? AnalyticsPropertyValue + } + return analyticsProperties; + } + + public static func createUserProfile(userProfileMap : Dictionary) -> AnalyticsUserProfile{ + + var userProfile = AnalyticsUserProfile( location: nil ) // TODO double check if okay + + for(key, value) in userProfileMap{ + switch key{ + case "name": + userProfile.name = (value as! String); + case "email": + userProfile.email = (value as! String); + case "plan": + userProfile.plan = (value as! String); + case "location": + let locationMap = value as! Dictionary + userProfile.location = createUserLocation(userLocationMap: locationMap) + case "properties": + let propertiesMap = value as! Dictionary + userProfile.properties = createAnalyticsProperties(propertiesMap: propertiesMap) + default: + print("Unknown key for UserProfile") + + } + } + + return userProfile + } + + public static func createUserLocation(userLocationMap : Dictionary) -> AnalyticsUserProfile.Location{ + + var userLocation : AnalyticsUserProfile.Location = AnalyticsUserProfile.Location() + + for(key, value) in userLocationMap{ + switch key{ + case "latitude": + userLocation.latitude = (value as! Double) + case "longitude": + userLocation.longitude = (value as! Double) + case "postalCode": + userLocation.postalCode = (value as! String) + case "city": + userLocation.city = (value as! String) + case "region": + userLocation.region = (value as! String) + case "country": + userLocation.country = (value as! String) + default: + print("Unknown key for UserLocation") + } + } + + return userLocation + } + +} diff --git a/packages/amplify_analytics_pinpoint/ios/Classes/SwiftAmplifyAnalyticsPinpointPlugin.swift b/packages/amplify_analytics_pinpoint/ios/Classes/SwiftAmplifyAnalyticsPinpointPlugin.swift index 8f3be9e50d2..08ecdf39b2b 100644 --- a/packages/amplify_analytics_pinpoint/ios/Classes/SwiftAmplifyAnalyticsPinpointPlugin.swift +++ b/packages/amplify_analytics_pinpoint/ios/Classes/SwiftAmplifyAnalyticsPinpointPlugin.swift @@ -6,7 +6,7 @@ import AmplifyPlugins public class SwiftAmplifyAnalyticsPinpointPlugin: NSObject, FlutterPlugin { public static func register(with registrar: FlutterPluginRegistrar) { - let channel = FlutterMethodChannel(name: "com.amazonaws.amplify/analytics", binaryMessenger: registrar.messenger()) + let channel = FlutterMethodChannel(name: "com.amazonaws.amplify/analytics_pinpoint", binaryMessenger: registrar.messenger()) let instance = SwiftAmplifyAnalyticsPinpointPlugin() registrar.addMethodCallDelegate(instance, channel: channel) @@ -21,112 +21,26 @@ public class SwiftAmplifyAnalyticsPinpointPlugin: NSObject, FlutterPlugin { public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) { switch call.method{ - case "recordEvent": - let arguments = call.arguments as! Dictionary - - let name = arguments["name"] as! String - let propertiesMap = arguments["propertiesMap"] as! Dictionary - - let event = BasicAnalyticsEvent(name: name, properties: createAnalyticsProperties(propertiesMap: propertiesMap)) - - Amplify.Analytics.record(event: event) - + AmplifyAnalyticsBridge.recordEvent(arguments: call.arguments, result: result) case "flushEvents": - Amplify.Analytics.flushEvents() - + AmplifyAnalyticsBridge.flushEvents(result: result) case "registerGlobalProperties": - let propertiesMap = call.arguments as! Dictionary - Amplify.Analytics.registerGlobalProperties(createAnalyticsProperties(propertiesMap: propertiesMap)) - + AmplifyAnalyticsBridge.registerGlobalProperties(arguments: call.arguments, result: result) case "unregisterGlobalProperties": - let propertyNames = Set(call.arguments as! Array) - Amplify.Analytics.unregisterGlobalProperties(propertyNames) - + AmplifyAnalyticsBridge.unregisterGlobalProperties(arguments: call.arguments, result: result) case "unregisterAllGlobalProperties": - Amplify.Analytics.unregisterGlobalProperties() - + AmplifyAnalyticsBridge.unregisterAllGlobalProperties(result: result) case "enable": - Amplify.Analytics.enable() - + AmplifyAnalyticsBridge.enable(result: result) case "disable": - Amplify.Analytics.disable() - + AmplifyAnalyticsBridge.disable(result: result) case "identifyUser": - let arguments = call.arguments as! Dictionary - - let userId = arguments["userId"] as! String - let userProfileMap = arguments["userProfileMap"] as! Dictionary - - Amplify.Analytics.identifyUser(userId, withProfile: createUserProfile(userProfileMap: userProfileMap)) - + AmplifyAnalyticsBridge.identifyUser(arguments: call.arguments, result: result) default : print("unknown event") - } } - private func createAnalyticsProperties(propertiesMap : Dictionary) -> AnalyticsProperties{ - - var analyticsProperties: AnalyticsProperties = AnalyticsProperties() - - for(key, value) in propertiesMap - { - analyticsProperties[key] = value as? AnalyticsPropertyValue - } - return analyticsProperties; - } - - private func createUserProfile(userProfileMap : Dictionary) -> AnalyticsUserProfile{ - - var userProfile = AnalyticsUserProfile( location: nil ) // TODO double check if okay - - for(key, value) in userProfileMap{ - switch key{ - case "name": - userProfile.name = (value as! String); - case "email": - userProfile.email = (value as! String); - case "plan": - userProfile.plan = (value as! String); - case "location": - let locationMap = value as! Dictionary - userProfile.location = createUserLocation(userLocationMap: locationMap) - case "properties": - let propertiesMap = value as! Dictionary - userProfile.properties = createAnalyticsProperties(propertiesMap: propertiesMap) - default: - print("Unknown key for UserProfile") - - } - } - - return userProfile - } - - private func createUserLocation(userLocationMap : Dictionary) -> AnalyticsUserProfile.Location{ - - var userLocation : AnalyticsUserProfile.Location = AnalyticsUserProfile.Location() - - for(key, value) in userLocationMap{ - switch key{ - case "latitude": - userLocation.latitude = (value as! Double) - case "longitude": - userLocation.longitude = (value as! Double) - case "postalCode": - userLocation.postalCode = (value as! String) - case "city": - userLocation.city = (value as! String) - case "region": - userLocation.region = (value as! String) - case "country": - userLocation.country = (value as! String) - default: - print("Unknown key for UserLocation") - } - } - - return userLocation - } + } diff --git a/packages/amplify_analytics_pinpoint/lib/amplify_analytics_pinpoint.dart b/packages/amplify_analytics_pinpoint/lib/amplify_analytics_pinpoint.dart index 4170ce8fb4a..7426000c080 100644 --- a/packages/amplify_analytics_pinpoint/lib/amplify_analytics_pinpoint.dart +++ b/packages/amplify_analytics_pinpoint/lib/amplify_analytics_pinpoint.dart @@ -1,9 +1,12 @@ library amplify_analytics_pinpoint; import 'package:amplify_analytics_plugin_interface/analytics_plugin_interface.dart'; +import 'package:flutter/cupertino.dart'; import 'package:plugin_platform_interface/plugin_platform_interface.dart'; import './method_channel_amplify.dart'; +export './src/types.dart'; + class AmplifyAnalyticsPinpointPlugin extends AnalyticsPluginInterface { static final Object _token = Object(); @@ -29,20 +32,20 @@ class AmplifyAnalyticsPinpointPlugin extends AnalyticsPluginInterface { // Public facing methods - Future recordEvent(AnalyticsEvent event) async { - return _instance.recordEvent(event); + Future recordEvent({@required AnalyticsEvent event}) async { + return _instance.recordEvent(event: event); } Future flushEvents() async { return _instance.flushEvents(); } - Future registerGlobalProperties(AnalyticsProperties globalProperties) async { - return _instance.registerGlobalProperties(globalProperties); + Future registerGlobalProperties({@required AnalyticsProperties globalProperties}) async { + return _instance.registerGlobalProperties(globalProperties: globalProperties); } - Future unregisterGlobalProperties(List propertyNames) async { - return _instance.unregisterGlobalProperties(propertyNames); + Future unregisterGlobalProperties({@required List propertyNames}) async { + return _instance.unregisterGlobalProperties(propertyNames: propertyNames); } Future unregisterAllGlobalProperties() async { @@ -57,7 +60,7 @@ class AmplifyAnalyticsPinpointPlugin extends AnalyticsPluginInterface { return _instance.disable(); } - Future identifyUser(String userId, AnalyticsUserProfile userProfile) async { - return _instance.identifyUser(userId, userProfile); + Future identifyUser({@required String userId, @required AnalyticsUserProfile userProfile}) async { + return _instance.identifyUser(userId: userId, userProfile: userProfile); } } diff --git a/packages/amplify_analytics_pinpoint/lib/method_channel_amplify.dart b/packages/amplify_analytics_pinpoint/lib/method_channel_amplify.dart index 4424c18e215..a8faf456203 100644 --- a/packages/amplify_analytics_pinpoint/lib/method_channel_amplify.dart +++ b/packages/amplify_analytics_pinpoint/lib/method_channel_amplify.dart @@ -3,7 +3,7 @@ import 'package:amplify_analytics_plugin_interface/analytics_plugin_interface.da import 'amplify_analytics_pinpoint.dart'; -const MethodChannel _channel = MethodChannel('com.amazonaws.amplify/analytics'); +const MethodChannel _channel = MethodChannel('com.amazonaws.amplify/analytics_pinpoint'); class AmplifyAnalyticsPinpointMethodChannel extends AmplifyAnalyticsPinpointPlugin { @@ -12,17 +12,7 @@ class AmplifyAnalyticsPinpointMethodChannel extends AmplifyAnalyticsPinpointPlug // This code sends Dart commands to the method channel @override - Future configure(String configuration) { - return _channel.invokeMethod( - 'configure', - { - 'configuration': configuration, - }, - ); - } - - @override - Future recordEvent(AnalyticsEvent event) async { + Future recordEvent({AnalyticsEvent event}) async { var name = event.name; var eventProperties = event.properties; @@ -44,7 +34,7 @@ class AmplifyAnalyticsPinpointMethodChannel extends AmplifyAnalyticsPinpointPlug } @override - Future registerGlobalProperties(AnalyticsProperties globalProperties) async { + Future registerGlobalProperties({AnalyticsProperties globalProperties}) async { return _channel.invokeMethod( 'registerGlobalProperties', globalProperties.getAllProperties() @@ -52,7 +42,7 @@ class AmplifyAnalyticsPinpointMethodChannel extends AmplifyAnalyticsPinpointPlug } @override - Future unregisterGlobalProperties(List propertyNames) async { + Future unregisterGlobalProperties({List propertyNames}) async { return _channel.invokeMethod( 'unregisterGlobalProperties', propertyNames @@ -81,7 +71,7 @@ class AmplifyAnalyticsPinpointMethodChannel extends AmplifyAnalyticsPinpointPlug } @override - Future identifyUser(String userId, AnalyticsUserProfile userProfile) async { + Future identifyUser({String userId, AnalyticsUserProfile userProfile}) async { return _channel.invokeMethod( 'identifyUser', { diff --git a/packages/amplify_analytics_pinpoint/lib/PinpointAnalytics/PinpointAnalyticsEvent.dart b/packages/amplify_analytics_pinpoint/lib/src/PinpointAnalytics/PinpointAnalyticsEvent.dart similarity index 100% rename from packages/amplify_analytics_pinpoint/lib/PinpointAnalytics/PinpointAnalyticsEvent.dart rename to packages/amplify_analytics_pinpoint/lib/src/PinpointAnalytics/PinpointAnalyticsEvent.dart diff --git a/packages/amplify_analytics_pinpoint/lib/PinpointAnalytics/PinpointAnalyticsProperties.dart b/packages/amplify_analytics_pinpoint/lib/src/PinpointAnalytics/PinpointAnalyticsProperties.dart similarity index 100% rename from packages/amplify_analytics_pinpoint/lib/PinpointAnalytics/PinpointAnalyticsProperties.dart rename to packages/amplify_analytics_pinpoint/lib/src/PinpointAnalytics/PinpointAnalyticsProperties.dart diff --git a/packages/amplify_analytics_pinpoint/lib/PinpointAnalytics/PinpointAnalyticsUserProfile.dart b/packages/amplify_analytics_pinpoint/lib/src/PinpointAnalytics/PinpointAnalyticsUserProfile.dart similarity index 100% rename from packages/amplify_analytics_pinpoint/lib/PinpointAnalytics/PinpointAnalyticsUserProfile.dart rename to packages/amplify_analytics_pinpoint/lib/src/PinpointAnalytics/PinpointAnalyticsUserProfile.dart diff --git a/packages/amplify_analytics_pinpoint/lib/PinpointAnalytics/PintpointAnalyticsUserProfileLocation.dart b/packages/amplify_analytics_pinpoint/lib/src/PinpointAnalytics/PinpointAnalyticsUserProfileLocation.dart similarity index 100% rename from packages/amplify_analytics_pinpoint/lib/PinpointAnalytics/PintpointAnalyticsUserProfileLocation.dart rename to packages/amplify_analytics_pinpoint/lib/src/PinpointAnalytics/PinpointAnalyticsUserProfileLocation.dart diff --git a/packages/amplify_analytics_pinpoint/lib/src/types.dart b/packages/amplify_analytics_pinpoint/lib/src/types.dart new file mode 100644 index 00000000000..5008a10bd4f --- /dev/null +++ b/packages/amplify_analytics_pinpoint/lib/src/types.dart @@ -0,0 +1,4 @@ +export 'PinpointAnalytics/PinpointAnalyticsEvent.dart'; +export 'PinpointAnalytics/PinpointAnalyticsProperties.dart'; +export 'PinpointAnalytics/PinpointAnalyticsUserProfile.dart'; +export 'PinpointAnalytics/PinpointAnalyticsUserProfileLocation.dart'; \ No newline at end of file diff --git a/packages/amplify_analytics_pinpoint/pubspec.yaml b/packages/amplify_analytics_pinpoint/pubspec.yaml index 23e41573ce1..e2dcb878dc7 100644 --- a/packages/amplify_analytics_pinpoint/pubspec.yaml +++ b/packages/amplify_analytics_pinpoint/pubspec.yaml @@ -13,6 +13,10 @@ dependencies: sdk: flutter amplify_analytics_plugin_interface: path: ../amplify_analytics_plugin_interface + plugin_platform_interface: ^1.0.1 + # amplify_core included only for testing: should be removed after pub.dev up and running + amplify_core: + path: ../amplify_core dev_dependencies: diff --git a/packages/amplify_analytics_pinpoint/test/amplify_analytics_pinpoint_test.dart b/packages/amplify_analytics_pinpoint/test/amplify_analytics_pinpoint_test.dart index 491f15e97f5..2998c7411f5 100644 --- a/packages/amplify_analytics_pinpoint/test/amplify_analytics_pinpoint_test.dart +++ b/packages/amplify_analytics_pinpoint/test/amplify_analytics_pinpoint_test.dart @@ -1,23 +1,125 @@ import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:amplify_analytics_plugin_interface/analytics_plugin_interface.dart'; import 'package:amplify_analytics_pinpoint/amplify_analytics_pinpoint.dart'; +import 'package:amplify_core/amplify_core.dart'; void main() { - const MethodChannel channel = MethodChannel('amplify_analytics_pinpoint'); + const MethodChannel analyticsChannel = + MethodChannel('com.amazonaws.amplify/analytics_pinpoint'); + const MethodChannel coreChannel = MethodChannel('com.amazonaws.amplify/core'); + + Amplify amplify = new Amplify(); + AmplifyAnalyticsPinpointPlugin analytics = + new AmplifyAnalyticsPinpointPlugin(); TestWidgetsFlutterBinding.ensureInitialized(); setUp(() { - channel.setMockMethodCallHandler((MethodCall methodCall) async { - return '42'; + analyticsChannel.setMockMethodCallHandler((MethodCall methodCall) async { + return true; + }); + coreChannel.setMockMethodCallHandler((MethodCall methodCall) async { + return true; }); }); tearDown(() { - channel.setMockMethodCallHandler(null); + analyticsChannel.setMockMethodCallHandler(null); + coreChannel.setMockMethodCallHandler(null); + }); + + test('amplify_analytics_pinpoint plugin can be added to Amplify instance', + () async { + expect(amplify.addPlugin(analytics), true); + }); + + test('configure success after plugin is added', () async { + expect(await amplify.configure("{}"), true); + }); + + // test sending basic events + test('recordEvent results in true', () async { + var event = AnalyticsEvent("test"); + + expect(await Amplify.Analytics.recordEvent(event: event), true); + }); + + test('recordEvent results in true', () async { + var event = AnalyticsEvent("test"); + + event.properties.addBoolProperty("boolKey", true); + event.properties.addDoubleProperty("doubleKey", 10.0); + event.properties.addIntProperty("intKey", 10); + event.properties.addStringProperty("stringKey", "stringValue"); + + expect(await Amplify.Analytics.recordEvent(event: event), true); + }); + + test('registerGlobalProperties results in true', () async { + var globalProperties = AnalyticsProperties(); + + expect(await Amplify.Analytics.registerGlobalProperties( + globalProperties: globalProperties), true); + }); + + test('registerGlobalProperties results in true', () async { + var globalProperties = AnalyticsProperties(); + + globalProperties.addBoolProperty("boolKey", true); + globalProperties.addDoubleProperty("doubleKey", 10.0); + globalProperties.addIntProperty("intKey", 10); + globalProperties.addStringProperty("stringKey", "stringValue"); + + expect(await Amplify.Analytics.registerGlobalProperties( + globalProperties: globalProperties), true); + }); + + test('identify user results in true', () async { + var userProfile = AnalyticsUserProfile(); + expect(await Amplify.Analytics.identifyUser( + userId: "testUser", userProfile: userProfile), true); }); - test('getPlatformVersion', () async { - //expect(await AmplifyAnalyticsPinpoint.platformVersion, '42'); + test('identify user results in true', () async { + var userProfile = AnalyticsUserProfile(); + userProfile.name = "name"; + userProfile.email = "email"; + userProfile.plan = "plan"; + + var analyticsUserLocation = new AnalyticsUserProfileLocation(); + userProfile.location = analyticsUserLocation; + + var analyticsProperties = new AnalyticsProperties(); + userProfile.properties = analyticsProperties; + + expect(await Amplify.Analytics.identifyUser( + userId: "testUser", userProfile: userProfile), true); + }); + + test('identify user results in true', () async { + var userProfile = AnalyticsUserProfile(); + userProfile.name = "name"; + userProfile.email = "email"; + userProfile.plan = "plan"; + + var analyticsUserLocation = new AnalyticsUserProfileLocation(); + analyticsUserLocation.latitude = 5; + analyticsUserLocation.longitude = 5; + analyticsUserLocation.postalCode = "94070"; + analyticsUserLocation.city = "SanFrancisco"; + analyticsUserLocation.region = "California"; + analyticsUserLocation.country = "USA"; + userProfile.location = analyticsUserLocation; + + var analyticsProperties = new AnalyticsProperties(); + analyticsProperties.addBoolProperty("boolKey", true); + analyticsProperties.addDoubleProperty("doubleKey", 10.0); + analyticsProperties.addIntProperty("intKey", 10); + analyticsProperties.addStringProperty("stringKey", "stringValue"); + userProfile.properties = analyticsProperties; + + expect(await Amplify.Analytics.identifyUser( + userId: "testUser", userProfile: userProfile), true); }); } diff --git a/packages/amplify_analytics_plugin_interface/lib/analytics_plugin_interface.dart b/packages/amplify_analytics_plugin_interface/lib/analytics_plugin_interface.dart index aae1420085d..661b36d2c63 100644 --- a/packages/amplify_analytics_plugin_interface/lib/analytics_plugin_interface.dart +++ b/packages/amplify_analytics_plugin_interface/lib/analytics_plugin_interface.dart @@ -4,18 +4,10 @@ import 'dart:async'; import 'package:meta/meta.dart'; - import 'package:plugin_platform_interface/plugin_platform_interface.dart'; -import './src/Analytics/AnalyticsEvent.dart'; -import './src/Analytics/AnalyticsProperties.dart'; -import './src/Analytics/AnalyticsUserProfile.dart'; -import './src/Analytics/AnalyticsUserProfileLocation.dart'; - -export './src/Analytics/AnalyticsEvent.dart'; -export './src/Analytics/AnalyticsProperties.dart'; -export './src/Analytics/AnalyticsUserProfile.dart'; -export './src/Analytics/AnalyticsUserProfileLocation.dart'; +import 'src/types.dart'; +export 'src/types.dart'; abstract class AnalyticsPluginInterface extends PlatformInterface { @@ -23,12 +15,7 @@ abstract class AnalyticsPluginInterface extends PlatformInterface { /// Constructs a AmplifyPlatform. AnalyticsPluginInterface({@required Object token}) : super(token: token); - /// Adds the configuration and return true if it was successful. - Future configure(String configuration) { - throw UnimplementedError('configure() has not been implemented.'); - } - - Future recordEvent(AnalyticsEvent event) async { + Future recordEvent({@required AnalyticsEvent event}) async { throw UnimplementedError('recordEvent() has not been implemented.'); } @@ -36,11 +23,11 @@ abstract class AnalyticsPluginInterface extends PlatformInterface { throw UnimplementedError('flushEvents() has not been implemented.'); } - Future registerGlobalProperties(AnalyticsProperties globalProperties) async { + Future registerGlobalProperties({@required AnalyticsProperties globalProperties}) async { throw UnimplementedError('registerGlobalProperties() has not been implemented.'); } - Future unregisterGlobalProperties(List propertyNames) async { + Future unregisterGlobalProperties({@required List propertyNames}) async { throw UnimplementedError('unregisterGlobalProperties() has not been implemented.'); } @@ -56,7 +43,7 @@ abstract class AnalyticsPluginInterface extends PlatformInterface { throw UnimplementedError('disable() has not been implemented.'); } - Future identifyUser(String userId, AnalyticsUserProfile userProfile) async { + Future identifyUser({@required String userId, @required AnalyticsUserProfile userProfile}) async { throw UnimplementedError('identifyUser() has not been implemented.'); } diff --git a/packages/amplify_analytics_plugin_interface/lib/src/types.dart b/packages/amplify_analytics_plugin_interface/lib/src/types.dart new file mode 100644 index 00000000000..5843e7ff72a --- /dev/null +++ b/packages/amplify_analytics_plugin_interface/lib/src/types.dart @@ -0,0 +1,4 @@ +export 'Analytics/AnalyticsEvent.dart'; +export 'Analytics/AnalyticsProperties.dart'; +export 'Analytics/AnalyticsUserProfile.dart'; +export 'Analytics/AnalyticsUserProfileLocation.dart'; \ No newline at end of file diff --git a/packages/amplify_core/lib/amplify_core.dart b/packages/amplify_core/lib/amplify_core.dart index 1862f2066fb..88b62054fc1 100644 --- a/packages/amplify_core/lib/amplify_core.dart +++ b/packages/amplify_core/lib/amplify_core.dart @@ -9,7 +9,7 @@ class Amplify { static const AnalyticsCategory Analytics = const AnalyticsCategory(); bool addPlugin(AnalyticsPluginInterface analyticsPlugin){ - Analytics.addPlugin(analyticsPlugin); + return Analytics.addPlugin(analyticsPlugin); } Future configure(String configuration) async { diff --git a/packages/amplify_core_plugin_interface/lib/amplify_analytics_category.dart b/packages/amplify_core_plugin_interface/lib/amplify_analytics_category.dart index 183c2f243dd..1833c6fbb10 100644 --- a/packages/amplify_core_plugin_interface/lib/amplify_analytics_category.dart +++ b/packages/amplify_core_plugin_interface/lib/amplify_analytics_category.dart @@ -14,20 +14,20 @@ class AnalyticsCategory{ return true; } - Future recordEvent(AnalyticsEvent event) async { - return plugins.length == 1 ? plugins[0].recordEvent(event) : null; + Future recordEvent({AnalyticsEvent event}) async { + return plugins.length == 1 ? plugins[0].recordEvent(event: event) : null; } Future flushEvents() async { return plugins.length == 1 ? plugins[0].flushEvents() : null; } - Future registerGlobalProperties(AnalyticsProperties globalProperties) async { - return plugins.length == 1 ? plugins[0].registerGlobalProperties(globalProperties) : null; + Future registerGlobalProperties({AnalyticsProperties globalProperties}) async { + return plugins.length == 1 ? plugins[0].registerGlobalProperties(globalProperties: globalProperties) : null; } - Future unregisterGlobalProperties(List propertyNames) async { - return plugins.length == 1 ? plugins[0].unregisterGlobalProperties(propertyNames) : null; + Future unregisterGlobalProperties({List propertyNames}) async { + return plugins.length == 1 ? plugins[0].unregisterGlobalProperties(propertyNames : propertyNames) : null; } Future unregisterAllGlobalProperties() async { @@ -42,8 +42,8 @@ class AnalyticsCategory{ return plugins.length == 1 ? plugins[0].disable() : null; } - Future identifyUser(String userId, AnalyticsUserProfile userProfile) async { - return plugins.length == 1 ? plugins[0].identifyUser(userId, userProfile) : null; + Future identifyUser({String userId, AnalyticsUserProfile userProfile}) async { + return plugins.length == 1 ? plugins[0].identifyUser(userId: userId, userProfile: userProfile) : null; } }