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 device attributes support #173

Merged
merged 5 commits into from
Nov 19, 2024
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
Expand Up @@ -210,16 +210,23 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
}

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.ATTRIBUTES)

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.ATTRIBUTES)

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

CustomerIO.instance().profileAttributes = attributes
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ internal object Keys {
object Tracking {
const val USER_ID = "userId"
const val TRAITS = "traits"
const val ATTRIBUTES = "attributes"
const val EVENT_NAME = "eventName"
const val TOKEN = "token"
const val DELIVERY_ID = "deliveryId"
Expand Down
1 change: 1 addition & 0 deletions ios/Classes/Keys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct Keys {
struct Tracking {
static let userId = "userId"
static let traits = "traits"
static let attributes = "attributes"
static let eventName = "eventName"
static let token = "token"
static let deliveryId = "deliveryId"
Expand Down
19 changes: 9 additions & 10 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.attributes] as? Dictionary<String, AnyHashable>
else {
logger.error("Missing device attributes in: \(params) for key: \(Keys.Tracking.attributes)")
return
}

CustomerIO.shared.deviceAttributes = attributes
}

private func setProfileAttributes(params : Dictionary<String, AnyHashable>){
guard let attributes = params[Keys.Tracking.traits] as? Dictionary<String, AnyHashable>
guard let attributes = params[Keys.Tracking.attributes] 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.attributes)")
return
}

Expand Down
2 changes: 1 addition & 1 deletion lib/customer_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class CustomerIO {
/// @param attributes additional attributes for a user profile
void setProfileAttributes({required Map<String, dynamic> attributes}) {
return _platform.setProfileAttributes(
traits: attributes.excludeNullValues());
attributes: attributes.excludeNullValues());
}

/// Subscribes to an in-app event listener.
Expand Down
4 changes: 2 additions & 2 deletions lib/customer_io_method_channel.dart
Original file line number Diff line number Diff line change
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> traits}) {
void setProfileAttributes({required Map<String, dynamic> attributes}) {
try {
final payload = {TrackingConsts.traits: traits};
final payload = {TrackingConsts.attributes: attributes};
methodChannel.invokeMethod(MethodConsts.setProfileAttributes, payload);
} on PlatformException catch (exception) {
handleException(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 @@ -69,7 +69,7 @@ abstract class CustomerIOPlatform extends PlatformInterface {
throw UnimplementedError('setDeviceAttributes() has not been implemented.');
}

void setProfileAttributes({required Map<String, dynamic> traits}) {
void setProfileAttributes({required Map<String, dynamic> attributes}) {
throw UnimplementedError(
'setProfileAttributes() has not been implemented.');
}
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 @@ -140,11 +140,11 @@ void main() {
'setProfileAttributes() should call platform method with correct arguments',
() async {
final Map<String, dynamic> args = {
'traits': {'age': 1}
'attributes': {'age': 1}
};

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

expectMethodInvocationArguments('setProfileAttributes', args);
});
Expand Down
2 changes: 1 addition & 1 deletion test/customer_io_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ void main() {
CustomerIO.instance.setProfileAttributes(attributes: givenAttributes);
expect(
verify(mockPlatform.setProfileAttributes(
traits: captureAnyNamed("traits"),
attributes: captureAnyNamed("attributes"),
)).captured.first,
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 @@ -149,12 +149,12 @@ class MockTestCustomerIoPlatform extends _i1.Mock
returnValueForMissingStub: null,
);
@override
void setProfileAttributes({required Map<String, dynamic>? traits}) =>
void setProfileAttributes({required Map<String, dynamic>? attributes}) =>
super.noSuchMethod(
Invocation.method(
#setProfileAttributes,
[],
{#traits: traits},
{#attributes: attributes},
),
returnValueForMissingStub: null,
);
Expand Down
Loading