Skip to content

Commit ce1140c

Browse files
authored
chore: manual metric tracking (#174)
1 parent 0fadd4d commit ce1140c

File tree

6 files changed

+35
-32
lines changed

6 files changed

+35
-32
lines changed

android/src/main/kotlin/io/customer/customer_io/CustomerIoPlugin.kt

+15-12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import io.customer.sdk.core.di.SDKComponent
1616
import io.customer.sdk.core.util.CioLogLevel
1717
import io.customer.sdk.core.util.Logger
1818
import io.customer.sdk.data.model.Region
19+
import io.customer.sdk.events.Metric
20+
import io.customer.sdk.events.TrackMetric
1921
import io.flutter.embedding.engine.plugins.FlutterPlugin
2022
import io.flutter.embedding.engine.plugins.activity.ActivityAware
2123
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
@@ -191,22 +193,23 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
191193
}
192194

193195
private fun trackMetric(params: Map<String, Any>) {
194-
// TODO: Fix trackMetric implementation
195-
/*
196-
val deliveryId = params.getString(Keys.Tracking.DELIVERY_ID)
197-
val deliveryToken = params.getString(Keys.Tracking.DELIVERY_TOKEN)
198-
val eventName = params.getProperty<String>(Keys.Tracking.METRIC_EVENT)
199-
val event = MetricEvent.getEvent(eventName)
200-
201-
if (event == null) {
202-
logger.info("metric event type null. Possible issue with SDK? Given: $eventName")
203-
return
196+
val deliveryId = params.getAsTypeOrNull<String>(Keys.Tracking.DELIVERY_ID)
197+
val deliveryToken = params.getAsTypeOrNull<String>(Keys.Tracking.DELIVERY_TOKEN)
198+
val eventName = params.getAsTypeOrNull<String>(Keys.Tracking.METRIC_EVENT)
199+
200+
if (deliveryId == null || deliveryToken == null || eventName == null) {
201+
throw IllegalArgumentException("Missing required parameters")
204202
}
205203

204+
val event = Metric.valueOf(eventName)
205+
206206
CustomerIO.instance().trackMetric(
207-
deliveryID = deliveryId, deviceToken = deliveryToken, event = event
207+
event = TrackMetric.Push(
208+
deliveryId = deliveryId,
209+
deviceToken = deliveryToken,
210+
metric = event
211+
)
208212
)
209-
*/
210213
}
211214

212215
private fun setDeviceAttributes(params: Map<String, Any>) {

ios/Classes/SwiftCustomerIoPlugin.swift

+14-14
Original file line numberDiff line numberDiff line change
@@ -153,20 +153,20 @@ public class SwiftCustomerIoPlugin: NSObject, FlutterPlugin {
153153
}
154154

155155
private func trackMetric(params : Dictionary<String, AnyHashable>){
156-
// TODO: Fix trackMetric implementation
157-
/*
158-
guard let deliveryId = params[Keys.Tracking.deliveryId] as? String,
159-
let deviceToken = params[Keys.Tracking.deliveryToken] as? String,
160-
let metricEvent = params[Keys.Tracking.metricEvent] as? String,
161-
let event = Metric.getEvent(from: metricEvent)
162-
else {
163-
return
164-
}
165-
166-
CustomerIO.shared.trackMetric(deliveryID: deliveryId,
167-
event: event,
168-
deviceToken: deviceToken)
169-
*/
156+
157+
guard let deliveryId = params[Keys.Tracking.deliveryId] as? String,
158+
let deviceToken = params[Keys.Tracking.deliveryToken] as? String,
159+
let metricEvent = params[Keys.Tracking.metricEvent] as? String,
160+
let event = Metric.getEvent(from: metricEvent)
161+
else {
162+
logger.error("Missing required parameters in: \(params)")
163+
return
164+
}
165+
166+
CustomerIO.shared.trackMetric(deliveryID: deliveryId,
167+
event: event,
168+
deviceToken: deviceToken)
169+
170170
}
171171

172172
private func initialize(params : Dictionary<String, AnyHashable>){

lib/customer_io_const.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ class MethodConsts {
1717

1818
class TrackingConsts {
1919
static const String userId = "userId";
20-
static const String traits = "traits";
2120
static const String attributes = "attributes";
21+
static const String traits = "traits";
2222
static const String eventName = "eventName";
2323
static const String token = "token";
2424
static const String deliveryId = "deliveryId";

lib/customer_io_enums.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ enum CioLogLevel { none, error, info, debug }
88
enum Region { us, eu }
99

1010
/// Enum to specify the type of metric for tracking
11-
enum MetricEvent { delivered, opened, converted, clicked }
11+
enum MetricEvent { delivered, opened, converted }
1212

1313
/// Enum to specify the click behavior of push notification for Android
1414
enum PushClickBehaviorAndroid {

lib/customer_io_method_channel.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class CustomerIOMethodChannel extends CustomerIOPlatform {
182182
@override
183183
void setDeviceAttributes({required Map<String, dynamic> attributes}) {
184184
try {
185-
final payload = {TrackingConsts.attributes: attributes};
185+
final payload = {TrackingConsts.traits: attributes};
186186
methodChannel.invokeMethod(MethodConsts.setDeviceAttributes, payload);
187187
} on PlatformException catch (exception) {
188188
handleException(exception);

test/customer_io_method_channel_test.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void main() {
9191
final Map<String, dynamic> args = {
9292
'deliveryId': '123',
9393
'deliveryToken': 'asdf',
94-
'metricEvent': 'clicked'
94+
'metricEvent': 'opened'
9595
};
9696

9797
final customerIO = CustomerIOMethodChannel();
@@ -153,11 +153,11 @@ void main() {
153153
'setDeviceAttributes() should call platform method with correct arguments',
154154
() async {
155155
final Map<String, dynamic> args = {
156-
'attributes': {'os': 'Android'}
156+
'traits': {'os': 'Android'}
157157
};
158158

159159
final customerIO = CustomerIOMethodChannel();
160-
customerIO.setDeviceAttributes(attributes: args['attributes']);
160+
customerIO.setDeviceAttributes(attributes: args['traits']);
161161

162162
expectMethodInvocationArguments('setDeviceAttributes', args);
163163
});

0 commit comments

Comments
 (0)