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

Analytics - Update Methods , Clean Code #21

Merged
merged 2 commits into from
Aug 13, 2020
Merged
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
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
<img src="https://s3.amazonaws.com/aws-mobile-hub-images/aws-amplify-logo.png" alt="AWS Amplify" width="550" >

## AWS-Amplify-Flutter is a library for Flutter developers building cloud-enabled applications
<p>
<a href="https://discord.gg/jWVbPfC" target="_blank">
<img src="https://img.shields.io/discord/308323056592486420?logo=discord"" alt="Discord Chat" />
</a>
</p>

## AWS Amplify Flutter (Developer Preview) is a library for Flutter developers building cloud-enabled applications

AWS Amplify provides a declarative and easy-to-use interface across different categories of cloud operations. AWS Amplify goes well with any Flutter based frontend workflow, and React Native for mobile developers.

Our default implementation works with Amazon Web Services (AWS), but AWS Amplify is designed to be open and pluggable for any custom backend or service.

## Features / APIs

- [**Authentication**](https://docs.amplify.aws/lib/auth/getting-started/q/platform/flutter): APIs and building blocks for developers who want to create user authentication experiences.
- [**Analytics**](https://docs.amplify.aws/lib/analytics/getting-started/q/platform/flutter): Easily collect analytics data for your app. Analytics data includes user sessions and other custom events that you want to track in your app.
- [**Storage**](https://docs.amplify.aws/lib/storage/getting-started/q/platform/flutter): Provides a simple mechanism for managing user content for your app in public, protected or private storage buckets.
- [**Push Notifications**](https://docs.amplify.aws/lib/push-notifications/getting-started/q/platform/flutter): Allows you to integrate push notifications in your app with Amazon Pinpoint targeting and campaign management support.

### To Be Implmented

- API (REST/GraphQL)
- Predictions
- Datastore
- Hub Events (Listening to the Amplify events)
- [**FEEDBACK**](https://github.com/aws-amplify/amplify-flutter/issues/5)


#### Visit our [Web Site](https://docs.amplify.aws/) to learn more about AWS Amplify.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,59 +48,55 @@ class AmplifyAnalyticsBridge {

fun flushEvents(@NonNull result: MethodChannel.Result) {
try {
Amplify.Analytics.flushEvents();
result.success(true);
Amplify.Analytics.flushEvents()
result.success(true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have some lint tooling available that can enforce this as part of the build/PR process?

} 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){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (

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{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else {

for (name in propertyNames) {
fjnoyp marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -110,11 +106,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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the configuration fails, that probably means you won't be able to use Analytics, right? So, log-n-gobble may not be the best strategy here. Should you not have the exception bubble back up to the user, so they know not to proceed? (Or, can make the decision themselves, as to whether or not they want to, at least. Right now, you make that choice for them.)

}
}

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){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meh, yea -- these are driving me nuts though. I don't know any language where whitespace isn't used around control flow keywords. The Dart style guide does not contain any examples of this. https://dart.dev/guides/language/effective-dart/style

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
Loading