Skip to content

Commit

Permalink
Analytics - Update Methods , Clean Code
Browse files Browse the repository at this point in the history
  • Loading branch information
fjnoyp committed Aug 11, 2020
1 parent 668b55f commit 1e96e03
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,70 +29,66 @@ class AmplifyAnalyticsBridge {
try {
val argumentsMap = arguments as HashMap<*, *>
val name = argumentsMap["name"] as String
val properties = argumentsMap["propertiesMap"] as HashMap<String, Any>;
val properties = argumentsMap["propertiesMap"] as HashMap<String, Any>
Amplify.Analytics.recordEvent(
AmplifyAnalyticsBuilder.createAnalyticsEvent(name, properties));
result.success(true);
AmplifyAnalyticsBuilder.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();
result.success(true);
Amplify.Analytics.flushEvents()
result.success(true)
} catch (e: Exception) {
result.error("AmplifyException", "Error", e.message)
}
}

fun registerGlobalProperties(@NonNull arguments: Any, @NonNull result: MethodChannel.Result) {
try {
val globalProperties = arguments as HashMap<String, Any>;
val globalProperties = arguments as HashMap<String, Any>
Amplify.Analytics.registerGlobalProperties(
AmplifyAnalyticsBuilder.createAnalyticsProperties(globalProperties));
result.success(true);
AmplifyAnalyticsBuilder.createAnalyticsProperties(globalProperties))
result.success(true)
} catch (e: Exception) {
result.error("AmplifyException", "Error", e.message)
}
}

fun unregisterGlobalProperties(@NonNull arguments: Any, @NonNull result: MethodChannel.Result) {
try {
val propertyNames = (arguments as ArrayList<String>).toSet<String>();
val propertyNames = (arguments as ArrayList<String>).toSet<String>()

for (name in propertyNames) {
Amplify.Analytics.unregisterGlobalProperties(name);
if(propertyNames.size == 0){
Amplify.Analytics.unregisterGlobalProperties()
}
result.success(true);
} catch (e: Exception) {
result.error("AmplifyException", "Error", e.message)
}
}

fun unregisterAllGlobalProperties(@NonNull result: MethodChannel.Result) {
try {
Amplify.Analytics.unregisterGlobalProperties()
result.success(true);
else{
for (name in propertyNames) {
Amplify.Analytics.unregisterGlobalProperties(name)
}
}
result.success(true)
} catch (e: Exception) {
result.error("AmplifyException", "Error", e.message)
}
}

fun enable(@NonNull result: MethodChannel.Result) {
try {
Amplify.Analytics.enable();
result.success(true);
Amplify.Analytics.enable()
result.success(true)
} catch (e: Exception) {
result.error("AmplifyException", "Error", e.message)
}
}

fun disable(@NonNull result: MethodChannel.Result) {
try {
Amplify.Analytics.disable();
result.success(true);
Amplify.Analytics.disable()
result.success(true)
} catch (e: Exception) {
result.error("AmplifyException", "Error", e.message)
}
Expand All @@ -102,11 +98,11 @@ class AmplifyAnalyticsBridge {
try {
val argumentsMap = arguments as HashMap<*, *>
val userId = argumentsMap["userId"] as String
val userProfileMap = argumentsMap["userProfileMap"] as HashMap<String, Object>;
val userProfileMap = argumentsMap["userProfileMap"] as HashMap<String, Object>

Amplify.Analytics.identifyUser(userId,
AmplifyAnalyticsBuilder.createUserProfile(userProfileMap));
result.success(true);
AmplifyAnalyticsBuilder.createUserProfile(userProfileMap))
result.success(true)
} catch (e: Exception) {
result.error("AmplifyException", "Error", e.message)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,83 +30,83 @@ class AmplifyAnalyticsBuilder {

when (value) {
is String -> {
propertiesBuilder.add(key, value);
propertiesBuilder.add(key, value)
}
is Double -> {
propertiesBuilder.add(key, value);
propertiesBuilder.add(key, value)
}
is Boolean -> {
propertiesBuilder.add(key, value);
propertiesBuilder.add(key, value)
}
is Int -> {
propertiesBuilder.add(key, value);
propertiesBuilder.add(key, value)
}
else -> {
throw IllegalArgumentException("Warning unrecognized object type sent via MethodChannel-AnalyticsProperties");
throw IllegalArgumentException("Warning unrecognized object type sent via MethodChannel-AnalyticsProperties")
}
}

}

return propertiesBuilder.build();
return propertiesBuilder.build()
}

fun createAnalyticsEvent(name: String, propertiesMap: HashMap<String, Any>): AnalyticsEvent {

val eventBuilder: AnalyticsEvent.Builder = AnalyticsEvent.builder()
.name(name);
.name(name)

for ((key, value) in propertiesMap) {

when (value) {
is String -> {
eventBuilder.addProperty(key, value);
eventBuilder.addProperty(key, value)
}
is Double -> {
eventBuilder.addProperty(key, value);
eventBuilder.addProperty(key, value)
}
is Boolean -> {
eventBuilder.addProperty(key, value);
eventBuilder.addProperty(key, value)
}
is Int -> {
eventBuilder.addProperty(key, value);
eventBuilder.addProperty(key, value)
}
else -> {
throw IllegalArgumentException("Warning unrecognized object type sent via MethodChannel-AnalyticsProperties");
throw IllegalArgumentException("Warning unrecognized object type sent via MethodChannel-AnalyticsProperties")
}
}
}

return eventBuilder.build();
return eventBuilder.build()
}

fun createUserProfile(userProfileMap: HashMap<String, *>): UserProfile {

val userProfileBuilder = UserProfile.builder();
val userProfileBuilder = UserProfile.builder()

for (item in userProfileMap) {
when (item.key) {
"name" ->
userProfileBuilder.name(item.value as String);
userProfileBuilder.name(item.value as String)
"email" ->
userProfileBuilder.email(item.value as String);
userProfileBuilder.email(item.value as String)
"plan" ->
userProfileBuilder.plan(item.value as String);
userProfileBuilder.plan(item.value as String)
"location" -> {
val locationMap = item.value as HashMap<String, String>;
userProfileBuilder.location(createUserLocation(locationMap));
val locationMap = item.value as HashMap<String, String>
userProfileBuilder.location(createUserLocation(locationMap))
}
"properties" -> {
val propertiesMap = item.value as HashMap<String, Any>;
userProfileBuilder.customProperties(createAnalyticsProperties(propertiesMap));
val propertiesMap = item.value as HashMap<String, Any>
userProfileBuilder.customProperties(createAnalyticsProperties(propertiesMap))
}
else -> {
throw IllegalArgumentException("Warning unrecognized object type sent via MethodChannel-AnalyticsProperties");
throw IllegalArgumentException("Warning unrecognized object type sent via MethodChannel-AnalyticsProperties")
}
}
}

return userProfileBuilder.build();
return userProfileBuilder.build()
}

fun createUserLocation(userLocationMap: HashMap<String, *>): UserProfile.Location {
Expand All @@ -116,24 +116,24 @@ class AmplifyAnalyticsBuilder {
for (item in userLocationMap) {
when (item.key) {
"latitude" ->
locationBuilder.latitude(item.value as Double);
locationBuilder.latitude(item.value as Double)
"longitude" ->
locationBuilder.longitude(item.value as Double);
locationBuilder.longitude(item.value as Double)
"postalCode" ->
locationBuilder.postalCode(item.value as String);
locationBuilder.postalCode(item.value as String)
"city" ->
locationBuilder.city(item.value as String);
locationBuilder.city(item.value as String)
"region" ->
locationBuilder.region(item.value as String);
locationBuilder.region(item.value as String)
"country" ->
locationBuilder.country(item.value as String);
locationBuilder.country(item.value as String)
else -> {
throw IllegalArgumentException("Warning unrecognized object type sent via MethodChannel-AnalyticsProperties");
throw IllegalArgumentException("Warning unrecognized object type sent via MethodChannel-AnalyticsProperties")
}
}
}

return locationBuilder.build();
return locationBuilder.build()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package com.amazonaws.amplify.amplify_analytics_pinpoint
import android.app.Activity
import android.app.Application
import android.util.Log
import androidx.annotation.NonNull;
import androidx.annotation.NonNull

import com.amplifyframework.core.Amplify
import com.amplifyframework.analytics.pinpoint.AWSPinpointAnalyticsPlugin
Expand All @@ -41,15 +41,15 @@ public class AmplifyAnalyticsPinpointPlugin : FlutterPlugin, ActivityAware, Meth
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {

channel = MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "com.amazonaws.amplify/analytics_pinpoint")
channel.setMethodCallHandler(this);
channel.setMethodCallHandler(this)

// Edge case for getting Application for AWSPinpointAnalyticsPlugin initialization
// https://github.com/flutter/flutter/issues/47048
var context = flutterPluginBinding.applicationContext

while (context != null) {
if (context as Application != null) {
Amplify.addPlugin(AWSPinpointAnalyticsPlugin(context as Application));
Amplify.addPlugin(AWSPinpointAnalyticsPlugin(context as Application))
break
} else {
context = context.applicationContext
Expand All @@ -65,7 +65,7 @@ public class AmplifyAnalyticsPinpointPlugin : FlutterPlugin, ActivityAware, Meth
// This static function is optional and equivalent to onAttachedToEngine. It supports the old
// pre-Flutter-1.12 Android projects.
companion object {
const val TAG = "AmplifyAnalyticsPinpointPlugin";
const val TAG = "AmplifyAnalyticsPinpointPlugin"

@JvmStatic
fun registerWith(registrar: Registrar) {
Expand All @@ -80,21 +80,19 @@ public class AmplifyAnalyticsPinpointPlugin : FlutterPlugin, ActivityAware, Meth

when (call.method) {
"recordEvent" ->
AmplifyAnalyticsBridge.recordEvent(call.arguments, result);
AmplifyAnalyticsBridge.recordEvent(call.arguments, result)
"flushEvents" ->
AmplifyAnalyticsBridge.flushEvents(result);
AmplifyAnalyticsBridge.flushEvents(result)
"registerGlobalProperties" ->
AmplifyAnalyticsBridge.registerGlobalProperties(call.arguments, result);
AmplifyAnalyticsBridge.registerGlobalProperties(call.arguments, result)
"unregisterGlobalProperties" ->
AmplifyAnalyticsBridge.unregisterGlobalProperties(call.arguments, result);
"unregisterAllGlobalProperties" ->
AmplifyAnalyticsBridge.unregisterAllGlobalProperties(result);
AmplifyAnalyticsBridge.unregisterGlobalProperties(call.arguments, result)
"enable" ->
AmplifyAnalyticsBridge.enable(result);
AmplifyAnalyticsBridge.enable(result)
"disable" ->
AmplifyAnalyticsBridge.disable(result);
AmplifyAnalyticsBridge.disable(result)
"identifyUser" ->
AmplifyAnalyticsBridge.identifyUser(call.arguments, result);
AmplifyAnalyticsBridge.identifyUser(call.arguments, result)

else -> result.notImplemented()
}
Expand Down
15 changes: 9 additions & 6 deletions packages/amplify_analytics_pinpoint/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,14 @@ class _MyAppState extends State<MyApp> {
amplifyInstance.addPlugin(
authPlugins: [authPlugin], analyticsPlugins: [analyticsPlugin]);

await amplifyInstance.configure(amplifyconfig);
setState(() {
_amplifyConfigured = true;
});
try {
await amplifyInstance.configure(amplifyconfig);
setState(() {
_amplifyConfigured = true;
});
} catch (e) {
print(e);
}
}

void _recordEvent() async {
Expand Down Expand Up @@ -81,11 +85,10 @@ class _MyAppState extends State<MyApp> {
print("unregister global properties: " + _globalProp);

Amplify.Analytics.unregisterGlobalProperties(propertyNames: [_globalProp]);
;
}

void _unregisterAllGlobalProperties() async {
Amplify.Analytics.unregisterAllGlobalProperties();
Amplify.Analytics.unregisterGlobalProperties();
}

void _enable() async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ public class AmplifyAnalyticsBridge {

public static func unregisterGlobalProperties(arguments: Any?, result: @escaping FlutterResult){
let propertyNames = Set<String>(arguments as! Array<String>)
Amplify.Analytics.unregisterGlobalProperties(propertyNames)
result(true);
}
public static func unregisterAllGlobalProperties(result: @escaping FlutterResult){
Amplify.Analytics.unregisterGlobalProperties()

if(propertyNames.count == 0){
Amplify.Analytics.unregisterGlobalProperties()
}
else{
Amplify.Analytics.unregisterGlobalProperties(propertyNames)
}

result(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ public class SwiftAmplifyAnalyticsPinpointPlugin: NSObject, FlutterPlugin {
AmplifyAnalyticsBridge.registerGlobalProperties(arguments: call.arguments, result: result)
case "unregisterGlobalProperties":
AmplifyAnalyticsBridge.unregisterGlobalProperties(arguments: call.arguments, result: result)
case "unregisterAllGlobalProperties":
AmplifyAnalyticsBridge.unregisterAllGlobalProperties(result: result)
case "enable":
AmplifyAnalyticsBridge.enable(result: result)
case "disable":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ class AmplifyAnalyticsPinpointPlugin extends AnalyticsPluginInterface {
return _instance.unregisterGlobalProperties(propertyNames: propertyNames);
}

Future<void> unregisterAllGlobalProperties() async {
await _instance.unregisterAllGlobalProperties();
}

Future<void> enable() async {
await _instance.enable();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,6 @@ class AmplifyAnalyticsPinpointMethodChannel
'unregisterGlobalProperties', propertyNames);
}

@override
Future<void> unregisterAllGlobalProperties() async {
await _channel.invokeMethod<bool>(
'unregisterAllGlobalProperties',
);
}

@override
Future<void> enable() async {
await _channel.invokeMethod<bool>(
Expand Down

0 comments on commit 1e96e03

Please sign in to comment.