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: update event tracking parameters #164

Merged
merged 4 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -163,18 +163,16 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
}

private fun track(params: Map<String, Any>) {
// TODO: Fix track implementation
/*
val name = params.getString(Keys.Tracking.EVENT_NAME)
val attributes =
params.getProperty<Map<String, Any>>(Keys.Tracking.ATTRIBUTES) ?: emptyMap()
val name = requireNotNull(params.getAsTypeOrNull<String>(Keys.Tracking.NAME)) {
"Event name is required to track event"
}
val properties = params.getAsTypeOrNull<Map<String, Any>>(Keys.Tracking.PROPERTIES)

if (attributes.isEmpty()) {
if (properties.isNullOrEmpty()) {
CustomerIO.instance().track(name)
} else {
CustomerIO.instance().track(name, attributes)
CustomerIO.instance().track(name, properties)
}
*/
}

private fun registerDeviceToken(params: Map<String, Any>) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.customer.customer_io.constant

// TODO: Cleanup this file later when all commented methods are implemented
internal object Keys {

object Methods {
Expand All @@ -24,5 +25,8 @@ internal object Keys {
const val DELIVERY_ID = "deliveryId"
const val DELIVERY_TOKEN = "deliveryToken"
const val METRIC_EVENT = "metricEvent"

const val NAME = "name"
const val PROPERTIES = "properties"
}
}
2 changes: 1 addition & 1 deletion apps/amiapp_flutter/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void main() async {
onDidReceiveNotificationResponse: (NotificationResponse notificationResponse) async {
// Callback from `flutter_local_notifications` plugin for when a local notification is clicked.
// Unfortunately, we are only able to get the payload object for the local push, not anything else such as title or body.
CustomerIO.instance.track(name: "local push notification clicked", attributes: {"payload": notificationResponse.payload});
CustomerIO.instance.track(name: "local push notification clicked", properties: {"payload": notificationResponse.payload});
}
);

Expand Down
10 changes: 5 additions & 5 deletions apps/amiapp_flutter/lib/src/screens/dashboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,20 @@ class _DashboardScreenState extends State<DashboardScreen> {
// Setup 3rd party SDK, flutter-fire.
// We install this SDK into sample app to make sure the CIO SDK behaves as expected when there is another SDK installed that handles push notifications.
FirebaseMessaging.instance.getInitialMessage().then((initialMessage) {
CustomerIO.instance.track(name: "push clicked", attributes: {"push": initialMessage?.notification?.title, "app-state": "killed"});
CustomerIO.instance.track(name: "push clicked", properties: {"push": initialMessage?.notification?.title, "app-state": "killed"});
});

// ...while app was in the background (but not killed).
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
CustomerIO.instance.track(name: "push clicked", attributes: {"push": message.notification?.title, "app-state": "background"});
CustomerIO.instance.track(name: "push clicked", properties: {"push": message.notification?.title, "app-state": "background"});
});

// Important that a 3rd party SDK can receive callbacks when a push is received while app in background.
//
// Note: A push will not be shown on the device while app is in foreground. This is a FCM behavior, not a CIO SDK behavior.
// If you send a push using Customer.io with the FCM service setup in Customer.io, the push will be shown on the device.
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
CustomerIO.instance.track(name: "push received", attributes: {"push": message.notification?.title, "app-state": "foreground"});
CustomerIO.instance.track(name: "push received", properties: {"push": message.notification?.title, "app-state": "foreground"});
});

super.initState();
Expand Down Expand Up @@ -113,7 +113,7 @@ class _DashboardScreenState extends State<DashboardScreen> {

CustomerIO.instance.track(
name: 'In-App Event',
attributes: attributes,
properties: attributes,
);
}

Expand Down Expand Up @@ -176,7 +176,7 @@ class _ActionList extends StatelessWidget {
if (attributes == null) {
CustomerIO.instance.track(name: eventName);
} else {
CustomerIO.instance.track(name: eventName, attributes: attributes);
CustomerIO.instance.track(name: eventName, properties: attributes);
}
context.showSnackBar('Event sent successfully');
}
Expand Down
2 changes: 1 addition & 1 deletion apps/amiapp_flutter/lib/src/screens/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class _CustomEventScreenState extends State<CustomEventScreen> {
: {propertyName: _propertyValueController.text};
CustomerIO.instance.track(
name: _eventNameController.text,
attributes: attributes);
properties: attributes);
_onEventTracked();
}
},
Expand Down
4 changes: 4 additions & 0 deletions ios/Classes/Keys.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

// TODO: Cleanup this file later when all commented methods are implemented
struct Keys {

struct Methods{
Expand All @@ -23,6 +24,9 @@ struct Keys {
static let deliveryId = "deliveryId"
static let deliveryToken = "deliveryToken"
static let metricEvent = "metricEvent"

static let name = "name"
static let properties = "properties"
}

struct Environment{
Expand Down
12 changes: 4 additions & 8 deletions ios/Classes/SwiftCustomerIoPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,17 @@ 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 {
guard let name = params[Keys.Tracking.name] as? String else {
logger.error("Missing event name in: \(params) for key: \(Keys.Tracking.name)")
return
}

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

CustomerIO.shared.track(name: name, data: attributes)
*/

CustomerIO.shared.track(name: name, properties: properties)
}

func screen(params : Dictionary<String, AnyHashable>) {
Expand Down
6 changes: 3 additions & 3 deletions lib/customer_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ class CustomerIO {
/// You may also track events with additional yet optional data.
///
/// @param name event name to be tracked
/// @param attributes (Optional) params to be sent with event
/// @param properties (Optional) params to be sent with event
void track(
{required String name, Map<String, dynamic> attributes = const {}}) {
return _platform.track(name: name, attributes: attributes);
{required String name, Map<String, dynamic> properties = const {}}) {
return _platform.track(name: name, properties: properties);
}

/// Track a push metric
Expand Down
5 changes: 5 additions & 0 deletions lib/customer_io_const.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// TODO: Cleanup this file later when all commented methods are implemented

class MethodConsts {
static const String initialize = "initialize";
static const String identify = "identify";
Expand All @@ -22,4 +24,7 @@ class TrackingConsts {
static const String metricEvent = "metricEvent";
static const String message = "message";
static const String handleNotificationTrigger = "handleNotificationTrigger";

static const String name = "name";
static const String properties = "properties";
}
6 changes: 3 additions & 3 deletions lib/customer_io_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ class CustomerIOMethodChannel extends CustomerIOPlatform {
@override
void track(
{required String name,
Map<String, dynamic> attributes = const {}}) async {
Map<String, dynamic> properties = const {}}) async {
try {
final payload = {
TrackingConsts.eventName: name,
TrackingConsts.attributes: attributes
TrackingConsts.name: name,
TrackingConsts.properties: properties
};
methodChannel.invokeMethod(MethodConsts.track, payload);
} on PlatformException catch (exception) {
Expand Down
2 changes: 1 addition & 1 deletion lib/customer_io_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ abstract class CustomerIOPlatform extends PlatformInterface {
}

void track(
{required String name, Map<String, dynamic> attributes = const {}}) {
{required String name, Map<String, dynamic> properties = const {}}) {
throw UnimplementedError('track() has not been implemented.');
}

Expand Down
6 changes: 3 additions & 3 deletions test/customer_io_method_channel_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ void main() {

test('track() should call platform method with correct arguments', () async {
final Map<String, dynamic> args = {
'eventName': 'test_event',
'attributes': {'eventData': 2}
'name': 'test_event',
'properties': {'eventData': 2}
};

final customerIO = CustomerIOMethodChannel();
customerIO.track(name: args['eventName'], attributes: args['attributes']);
customerIO.track(name: args['name'], properties: args['properties']);

expectMethodInvocationArguments('track', args);
});
Expand Down
8 changes: 4 additions & 4 deletions test/customer_io_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,19 @@ void main() {
test('track() calls platform', () {
const name = 'itemAddedToCart';
final attributes = {'item': 'shoes'};
CustomerIO.instance.track(name: name, attributes: attributes);
verify(mockPlatform.track(name: name, attributes: attributes))
CustomerIO.instance.track(name: name, properties: attributes);
verify(mockPlatform.track(name: name, properties: attributes))
.called(1);
});

test('track() correct arguments are passed', () {
const name = 'itemAddedToCart';
final givenAttributes = {'name': 'John Doe'};
CustomerIO.instance.track(name: name, attributes: givenAttributes);
CustomerIO.instance.track(name: name, properties: givenAttributes);
expect(
verify(mockPlatform.track(
name: captureAnyNamed("name"),
attributes: captureAnyNamed("attributes"),
properties: captureAnyNamed("properties"),
)).captured,
[name, givenAttributes],
);
Expand Down
4 changes: 2 additions & 2 deletions test/customer_io_test.mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ class MockTestCustomerIoPlatform extends _i1.Mock
@override
void track({
required String? name,
Map<String, dynamic>? attributes = const {},
Map<String, dynamic>? properties = const {},
}) =>
super.noSuchMethod(
Invocation.method(
#track,
[],
{
#name: name,
#attributes: attributes,
#properties: properties,
},
),
returnValueForMissingStub: null,
Expand Down
Loading