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

Amplify analytics #7

Merged
merged 1 commit into from
Aug 10, 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
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package com.amazonaws.amplify.amplify_analytics_pinpoint

import androidx.annotation.NonNull
Expand All @@ -10,89 +25,90 @@ import io.flutter.plugin.common.MethodChannel
class AmplifyAnalyticsBridge {
companion object Bridge {

fun recordEvent(@NonNull arguments: Any, @NonNull result: MethodChannel.Result){
fun recordEvent(@NonNull arguments: Any, @NonNull result: MethodChannel.Result) {
try {
val argumentsMap = arguments as HashMap<*,*>
val argumentsMap = arguments as HashMap<*, *>
val name = argumentsMap["name"] as String
val properties = argumentsMap["propertiesMap"] as HashMap<String, Any>;
Amplify.Analytics.recordEvent(
AmplifyAnalyticsConstructor.createAnalyticsEvent(name, properties) );
AmplifyAnalyticsBuilder.createAnalyticsEvent(name, properties));
result.success(true);
}
catch(e: Exception) {
result.error("AmplifyException", "Error", e.message )
} catch (e: Exception) {
result.error("AmplifyException", "Error", e.message)
}
}

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

fun registerGlobalProperties(@NonNull arguments: Any, @NonNull result: MethodChannel.Result){
try{
fun registerGlobalProperties(@NonNull arguments: Any, @NonNull result: MethodChannel.Result) {
try {
val globalProperties = arguments as HashMap<String, Any>;
Amplify.Analytics.registerGlobalProperties(
AmplifyAnalyticsConstructor.createAnalyticsProperties(globalProperties) );
}
catch(e: Exception){
result.error("AmplifyException", "Error", e.message )
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{
fun unregisterGlobalProperties(@NonNull arguments: Any, @NonNull result: MethodChannel.Result) {
try {
val propertyNames = (arguments as ArrayList<String>).toSet<String>();

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

fun unregisterAllGlobalProperties(@NonNull result: MethodChannel.Result){
fun unregisterAllGlobalProperties(@NonNull result: MethodChannel.Result) {
try {
Amplify.Analytics.unregisterGlobalProperties()
}
catch(e: Exception){
result.error("AmplifyException", "Error", e.message )
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);
} catch (e: Exception) {
result.error("AmplifyException", "Error", e.message)
}
}

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

fun identifyUser(@NonNull arguments: Any, @NonNull result: MethodChannel.Result){
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<String, Object>;

Amplify.Analytics.identifyUser(userId,
AmplifyAnalyticsConstructor.createUserProfile(userProfileMap));
}
catch(e: Exception){
result.error("AmplifyException", "Error", e.message )
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
@@ -1,17 +1,32 @@
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

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 {
class AmplifyAnalyticsBuilder {
companion object Builder {

fun createAnalyticsProperties(propertiesMap : HashMap<String, Any>) : AnalyticsProperties {
fun createAnalyticsProperties(propertiesMap: HashMap<String, Any>): AnalyticsProperties {

val propertiesBuilder: AnalyticsProperties.Builder = AnalyticsProperties.builder()

for( (key, value ) in propertiesMap){
for ((key, value) in propertiesMap) {

when (value) {
is String -> {
Expand All @@ -36,12 +51,12 @@ class AmplifyAnalyticsConstructor {
return propertiesBuilder.build();
}

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

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

for( (key, value ) in propertiesMap){
for ((key, value) in propertiesMap) {

when (value) {
is String -> {
Expand All @@ -65,7 +80,7 @@ class AmplifyAnalyticsConstructor {
return eventBuilder.build();
}

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

val userProfileBuilder = UserProfile.builder();

Expand Down Expand Up @@ -94,12 +109,12 @@ class AmplifyAnalyticsConstructor {
return userProfileBuilder.build();
}

fun createUserLocation(userLocationMap: HashMap<String, *>) : UserProfile.Location{
fun createUserLocation(userLocationMap: HashMap<String, *>): UserProfile.Location {

val locationBuilder = UserProfile.Location.builder()

for(item in userLocationMap){
when(item.key){
for (item in userLocationMap) {
when (item.key) {
"latitude" ->
locationBuilder.latitude(item.value as Double);
"longitude" ->
Expand Down
Loading