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: manual metric tracking #174

Merged
merged 9 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
45 changes: 27 additions & 18 deletions android/src/main/kotlin/io/customer/customer_io/CustomerIoPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
import io.customer.sdk.core.util.CioLogLevel
import io.customer.sdk.core.util.Logger
import io.customer.sdk.data.model.Region
import io.customer.sdk.events.Metric
import io.customer.sdk.events.TrackMetric
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
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 +78,7 @@
}

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

Check failure on line 81 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 81 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 +89,7 @@
}
}

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

Check failure on line 92 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 92 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 @@ -191,35 +192,43 @@
}

private fun trackMetric(params: Map<String, Any>) {
// TODO: Fix trackMetric implementation
/*
val deliveryId = params.getString(Keys.Tracking.DELIVERY_ID)
val deliveryToken = params.getString(Keys.Tracking.DELIVERY_TOKEN)
val eventName = params.getProperty<String>(Keys.Tracking.METRIC_EVENT)
val event = MetricEvent.getEvent(eventName)

if (event == null) {
logger.info("metric event type null. Possible issue with SDK? Given: $eventName")
return
val deliveryId = params.getAsTypeOrNull<String>(Keys.Tracking.DELIVERY_ID)
val deliveryToken = params.getAsTypeOrNull<String>(Keys.Tracking.DELIVERY_TOKEN)
val eventName = params.getAsTypeOrNull<String>(Keys.Tracking.METRIC_EVENT)

if (deliveryId == null || deliveryToken == null || eventName == null) {
throw IllegalArgumentException("Missing required parameters")
}

val event = Metric.valueOf(eventName)

CustomerIO.instance().trackMetric(
deliveryID = deliveryId, deviceToken = deliveryToken, event = event
event = TrackMetric.Push(
deliveryId = deliveryId,
deviceToken = deliveryToken,
metric = event
)
)
*/
}

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

if (attributes.isNullOrEmpty()) {
logger.error("Device attributes are missing in params: $params")
return
}

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

private fun setProfileAttributes(params: Map<String, Any>) {
val attributes = params.getAsTypeOrNull<Map<String, Any>>(Keys.Tracking.TRAITS) ?: return
val attributes = params.getAsTypeOrNull<Map<String, Any>>(Keys.Tracking.TRAITS)

if (attributes.isNullOrEmpty()) {
logger.error("Profile attributes are missing in params: $params")
return
}

CustomerIO.instance().profileAttributes = attributes
}
Expand Down
45 changes: 22 additions & 23 deletions ios/Classes/SwiftCustomerIoPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,19 @@ public class SwiftCustomerIoPlugin: NSObject, FlutterPlugin {


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.traits] as? Dictionary<String, AnyHashable>
else {
logger.error("Missing device attributes in: \(params) for key: \(Keys.Tracking.traits)")
return
}

CustomerIO.shared.deviceAttributes = attributes
}

private func setProfileAttributes(params : Dictionary<String, AnyHashable>){
guard let attributes = params[Keys.Tracking.traits] as? Dictionary<String, AnyHashable>
else {
logger.error("Missing attributes in: \(params) for key: \(Keys.Tracking.traits)")
logger.error("Missing profile attributes in: \(params) for key: \(Keys.Tracking.traits)")
return
}

Expand All @@ -154,20 +153,20 @@ public class SwiftCustomerIoPlugin: NSObject, FlutterPlugin {
}

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 {
logger.error("Missing required parameters in: \(params)")
return
}

CustomerIO.shared.trackMetric(deliveryID: deliveryId,
event: event,
deviceToken: deviceToken)

}

private func initialize(params : Dictionary<String, AnyHashable>){
Expand Down
1 change: 0 additions & 1 deletion lib/customer_io_const.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class MethodConsts {
class TrackingConsts {
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";
static const String deliveryId = "deliveryId";
Expand Down
2 changes: 1 addition & 1 deletion lib/customer_io_enums.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ enum CioLogLevel { none, error, info, debug }
enum Region { us, eu }

/// Enum to specify the type of metric for tracking
enum MetricEvent { delivered, opened, converted, clicked }
enum MetricEvent { delivered, opened, converted }
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

iOS SDK doesn't have clicked in its enum.


/// Enum to specify the click behavior of push notification for Android
enum PushClickBehaviorAndroid {
Expand Down
2 changes: 1 addition & 1 deletion lib/customer_io_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class CustomerIOMethodChannel extends CustomerIOPlatform {
@override
void setDeviceAttributes({required Map<String, dynamic> attributes}) {
try {
final payload = {TrackingConsts.attributes: attributes};
final payload = {TrackingConsts.traits: attributes};
methodChannel.invokeMethod(MethodConsts.setDeviceAttributes, payload);
} on PlatformException catch (exception) {
handleException(exception);
Expand Down
4 changes: 2 additions & 2 deletions test/customer_io_method_channel_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ void main() {
'setDeviceAttributes() should call platform method with correct arguments',
() async {
final Map<String, dynamic> args = {
'attributes': {'os': 'Android'}
'traits': {'os': 'Android'}
};

final customerIO = CustomerIOMethodChannel();
customerIO.setDeviceAttributes(attributes: args['attributes']);
customerIO.setDeviceAttributes(attributes: args['traits']);

expectMethodInvocationArguments('setDeviceAttributes', args);
});
Expand Down
Loading