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

chore: added identify and attributes #163

Merged
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: 16 additions & 12 deletions android/src/main/kotlin/io/customer/customer_io/CustomerIoPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
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

/**
Expand Down Expand Up @@ -77,7 +76,7 @@
}

private fun MethodCall.toNativeMethodCall(
result: Result, performAction: (params: Map<String, Any>) -> Unit

Check failure on line 79 in android/src/main/kotlin/io/customer/customer_io/CustomerIoPlugin.kt

View workflow job for this annotation

GitHub Actions / Building sample app amiapp_flutter

One type argument expected for class Result<out T>

Check failure on line 79 in android/src/main/kotlin/io/customer/customer_io/CustomerIoPlugin.kt

View workflow job for this annotation

GitHub Actions / Building sample app amiapp_flutter

One type argument expected for class Result<out T>
) {
try {
val params = this.arguments as? Map<String, Any> ?: emptyMap()
Expand All @@ -88,7 +87,7 @@
}
}

override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {

Check failure on line 90 in android/src/main/kotlin/io/customer/customer_io/CustomerIoPlugin.kt

View workflow job for this annotation

GitHub Actions / Building sample app amiapp_flutter

One type argument expected for class Result<out T>

Check failure on line 90 in android/src/main/kotlin/io/customer/customer_io/CustomerIoPlugin.kt

View workflow job for this annotation

GitHub Actions / Building sample app amiapp_flutter

One type argument expected for class Result<out T>
when (call.method) {
Keys.Methods.INITIALIZE -> {
call.toNativeMethodCall(result) {
Expand Down Expand Up @@ -153,13 +152,21 @@
}

private fun identify(params: Map<String, Any>) {
// TODO: Fix identify implementation
/*
val identifier = params.getString(Keys.Tracking.IDENTIFIER)
val attributes =
params.getProperty<Map<String, Any>>(Keys.Tracking.ATTRIBUTES) ?: emptyMap()
CustomerIO.instance().identify(identifier, attributes)
*/
val userId = params.getAsTypeOrNull<String>(Keys.Tracking.USER_ID)
val traits = params.getAsTypeOrNull<Map<String, Any>>(Keys.Tracking.TRAITS) ?: emptyMap()

if (userId == null && traits.isEmpty()) {
logger.error("Please provide either an ID or traits to identify.")
return
}

if (userId != null && traits.isNotEmpty()) {
CustomerIO.instance().identify(userId, traits)
} else if (userId != null) {
CustomerIO.instance().identify(userId)
} else {
CustomerIO.instance().profileAttributes = traits
}
}

private fun track(params: Map<String, Any>) {
Expand Down Expand Up @@ -214,12 +221,9 @@
}

private fun setProfileAttributes(params: Map<String, Any>) {
// TODO: Fix setProfileAttributes implementation
/*
val attributes = params.getProperty<Map<String, Any>>(Keys.Tracking.ATTRIBUTES) ?: return
val attributes = params.getAsTypeOrNull<Map<String, Any>>(Keys.Tracking.TRAITS) ?: return

CustomerIO.instance().profileAttributes = attributes
*/
}

private fun screen(params: Map<String, Any>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ internal object Keys {
}

object Tracking {
const val IDENTIFIER = "identifier"
const val ATTRIBUTES = "attributes"
const val USER_ID = "userId"
const val TRAITS = "traits"
const val EVENT_NAME = "eventName"
const val TOKEN = "token"
const val DELIVERY_ID = "deliveryId"
Expand Down
2 changes: 1 addition & 1 deletion apps/amiapp_flutter/lib/src/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class _AmiAppState extends State<AmiApp> {
onLogin: (user) {
_auth.login(user).then((signedIn) {
if (signedIn) {
CustomerIO.instance.identify(identifier: user.email, attributes: {
CustomerIO.instance.identify(userId: user.email, traits: {
"first_name": user.displayName,
"email": user.email,
"is_guest": user.isGuest,
Expand Down
4 changes: 2 additions & 2 deletions ios/Classes/Keys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ struct Keys {
}

struct Tracking {
static let identifier = "identifier"
static let attributes = "attributes"
static let userId = "userId"
static let traits = "traits"
static let eventName = "eventName"
static let token = "token"
static let deliveryId = "deliveryId"
Expand Down
134 changes: 65 additions & 69 deletions ios/Classes/SwiftCustomerIoPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public class SwiftCustomerIoPlugin: NSObject, FlutterPlugin {

private var methodChannel: FlutterMethodChannel!
private var inAppMessagingChannelHandler: CusomterIOInAppMessaging!

private let logger: CioInternalCommon.Logger = DIGraphShared.shared.logger

public static func register(with registrar: FlutterPluginRegistrar) {
Expand Down Expand Up @@ -70,20 +69,22 @@ public class SwiftCustomerIoPlugin: NSObject, FlutterPlugin {
}

private func identify(params : Dictionary<String, AnyHashable>){
// TODO: Fix identify implementation
/*
guard let identifier = params[Keys.Tracking.identifier] as? String
else {
return
}

guard let attributes = params[Keys.Tracking.attributes] as? Dictionary<String, AnyHashable> else{
CustomerIO.shared.identify(identifier: identifier)
let userId = params[Keys.Tracking.userId] as? String
let traits = params[Keys.Tracking.traits] as? Dictionary<String, AnyHashable> ?? [:]

if userId == nil && traits.isEmpty {
logger.error("Please provide either an ID or traits to identify.")
return
}

CustomerIO.shared.identify(identifier: identifier, body: attributes)
*/
if let userId = userId, !traits.isEmpty {
CustomerIO.shared.identify(userId: userId, traits: traits)
} else if let userId = userId {
CustomerIO.shared.identify(userId: userId)
} else {
CustomerIO.shared.profileAttributes = traits
}
}

private func clearIdentify() {
Expand All @@ -93,87 +94,82 @@ public class SwiftCustomerIoPlugin: NSObject, FlutterPlugin {
private func track(params : Dictionary<String, AnyHashable>) {
// TODO: Fix track implementation
/*
guard let name = params[Keys.Tracking.eventName] as? String
else {
return
}

guard let attributes = params[Keys.Tracking.attributes] as? Dictionary<String, AnyHashable> else{
CustomerIO.shared.track(name: name)
return
}

CustomerIO.shared.track(name: name, data: attributes)
guard let name = params[Keys.Tracking.eventName] as? String
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to revert these formatting changes for commented code? As it could be a bit challenging when merging as I'm working in same file in other ticket.

else {
return
}
guard let attributes = params[Keys.Tracking.attributes] as? Dictionary<String, AnyHashable> else{
CustomerIO.shared.track(name: name)
return
}
CustomerIO.shared.track(name: name, data: attributes)
*/

}

func screen(params : Dictionary<String, AnyHashable>) {
// TODO: Fix screen implementation
/*
guard let name = params[Keys.Tracking.eventName] as? String
else {
return
}

guard let attributes = params[Keys.Tracking.attributes] as? Dictionary<String, AnyHashable> else{
CustomerIO.shared.screen(name: name)
return
}

CustomerIO.shared.screen(name: name, data: attributes)
guard let name = params[Keys.Tracking.eventName] as? String
else {
return
}
guard let attributes = params[Keys.Tracking.attributes] as? Dictionary<String, AnyHashable> else{
CustomerIO.shared.screen(name: name)
return
}
CustomerIO.shared.screen(name: name, data: attributes)
*/
}


private func setDeviceAttributes(params : Dictionary<String, AnyHashable>){
// TODO: Fix setDeviceAttributes implementation
/*
guard let attributes = params[Keys.Tracking.attributes] as? Dictionary<String, AnyHashable>
else {
return
}
CustomerIO.shared.deviceAttributes = attributes
guard let attributes = params[Keys.Tracking.attributes] as? Dictionary<String, AnyHashable>
else {
return
}
CustomerIO.shared.deviceAttributes = attributes
*/
}

private func setProfileAttributes(params : Dictionary<String, AnyHashable>){
// TODO: Fix setProfileAttributes implementation
/*
guard let attributes = params[Keys.Tracking.attributes] as? Dictionary<String, AnyHashable>
else {
return
}
guard let attributes = params[Keys.Tracking.traits] as? Dictionary<String, AnyHashable>
else { return }
CustomerIO.shared.profileAttributes = attributes
*/
}

private func registerDeviceToken(params : Dictionary<String, AnyHashable>){
// TODO: Fix registerDeviceToken implementation
/*
guard let token = params[Keys.Tracking.token] as? String
else {
return
}

CustomerIO.shared.registerDeviceToken(token)
guard let token = params[Keys.Tracking.token] as? String
else {
return
}
CustomerIO.shared.registerDeviceToken(token)
*/
}

private func trackMetric(params : Dictionary<String, AnyHashable>){
// TODO: Fix trackMetric implementation
/*
guard let deliveryId = params[Keys.Tracking.deliveryId] as? String,
let deviceToken = params[Keys.Tracking.deliveryToken] as? String,
let metricEvent = params[Keys.Tracking.metricEvent] as? String,
let event = Metric.getEvent(from: metricEvent)
else {
return
}

CustomerIO.shared.trackMetric(deliveryID: deliveryId,
event: event,
deviceToken: deviceToken)
guard let deliveryId = params[Keys.Tracking.deliveryId] as? String,
let deviceToken = params[Keys.Tracking.deliveryToken] as? String,
let metricEvent = params[Keys.Tracking.metricEvent] as? String,
let event = Metric.getEvent(from: metricEvent)
else {
return
}
CustomerIO.shared.trackMetric(deliveryID: deliveryId,
event: event,
deviceToken: deviceToken)
*/
}

Expand All @@ -195,13 +191,13 @@ public class SwiftCustomerIoPlugin: NSObject, FlutterPlugin {
private func initializeInApp(){
// TODO: Fix initializeInApp implementation
/*
DispatchQueue.main.async {
MessagingInApp.shared.initialize(eventListener: CustomerIOInAppEventListener(
invokeMethod: {method,args in
self.invokeMethod(method, args)
})
)
}
DispatchQueue.main.async {
MessagingInApp.shared.initialize(eventListener: CustomerIOInAppEventListener(
invokeMethod: {method,args in
self.invokeMethod(method, args)
})
)
}
*/
}

Expand Down
14 changes: 7 additions & 7 deletions lib/customer_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,17 @@ class CustomerIO {
}
}

/// Identify a person using a unique identifier, eg. email id.
/// Identify a person using a unique userId, eg. email id.
/// Note that you can identify only 1 profile at a time. In case, multiple
/// identifiers are attempted to be identified, then the last identified profile
/// will be removed automatically.
///
/// @param identifier unique identifier for a profile
/// @param attributes (Optional) params to set profile attributes
/// @param userId unique identifier for a profile
/// @param traits (Optional) params to set profile attributes
void identify(
{required String identifier,
Map<String, dynamic> attributes = const {}}) {
return _platform.identify(identifier: identifier, attributes: attributes);
{required String userId,
Map<String, dynamic> traits = const {}}) {
return _platform.identify(userId: userId, traits: traits);
}

/// Call this function to stop identifying a person.
Expand Down Expand Up @@ -148,7 +148,7 @@ class CustomerIO {
///
/// @param attributes additional attributes for a user profile
void setProfileAttributes({required Map<String, dynamic> attributes}) {
return _platform.setProfileAttributes(attributes: attributes);
return _platform.setProfileAttributes(traits: attributes);
}

/// Subscribes to an in-app event listener.
Expand Down
3 changes: 2 additions & 1 deletion lib/customer_io_const.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class MethodConsts {
}

class TrackingConsts {
static const String identifier = "identifier";
static const String userId = "userId";
static const String traits = "traits";
static const String attributes = "attributes";
static const String eventName = "eventName";
static const String token = "token";
Expand Down
14 changes: 7 additions & 7 deletions lib/customer_io_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,18 @@ class CustomerIOMethodChannel extends CustomerIOPlatform {
}
}

/// Identify a person using a unique identifier, eg. email id.
/// Identify a person using a unique userId, eg. email id.
/// Note that you can identify only 1 profile at a time. In case, multiple
/// identifiers are attempted to be identified, then the last identified profile
/// will be removed automatically.
@override
void identify(
{required String identifier,
Map<String, dynamic> attributes = const {}}) async {
{required String userId,
Map<String, dynamic> traits = const {}}) async {
try {
final payload = {
TrackingConsts.identifier: identifier,
TrackingConsts.attributes: attributes
TrackingConsts.userId: userId,
TrackingConsts.traits: traits
};
methodChannel.invokeMethod(MethodConsts.identify, payload);
} on PlatformException catch (exception) {
Expand Down Expand Up @@ -168,9 +168,9 @@ class CustomerIOMethodChannel extends CustomerIOPlatform {
/// Set custom user profile information such as user preference, specific
/// user actions etc
@override
void setProfileAttributes({required Map<String, dynamic> attributes}) {
void setProfileAttributes({required Map<String, dynamic> traits}) {
try {
final payload = {TrackingConsts.attributes: attributes};
final payload = {TrackingConsts.traits: traits};
methodChannel.invokeMethod(MethodConsts.setProfileAttributes, payload);
} on PlatformException catch (exception) {
handleException(exception);
Expand Down
6 changes: 3 additions & 3 deletions lib/customer_io_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ abstract class CustomerIOPlatform extends PlatformInterface {
}

void identify(
{required String identifier,
Map<String, dynamic> attributes = const {}}) {
{required String userId,
Map<String, dynamic> traits = const {}}) {
throw UnimplementedError('identify() has not been implemented.');
}

Expand Down Expand Up @@ -69,7 +69,7 @@ abstract class CustomerIOPlatform extends PlatformInterface {
throw UnimplementedError('setDeviceAttributes() has not been implemented.');
}

void setProfileAttributes({required Map<String, dynamic> attributes}) {
void setProfileAttributes({required Map<String, dynamic> traits}) {
throw UnimplementedError(
'setProfileAttributes() has not been implemented.');
}
Expand Down
Loading
Loading