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

CHNL-6305: Added set profile attributes bridge method #135

Merged
merged 5 commits into from
Mar 15, 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
2 changes: 1 addition & 1 deletion example/src/AppViewInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const appViews: AppViewInterface[] = [
onPress: setProfile,
},
{
title: 'Click to set custom profile attribute',
title: 'Click to set some profile attributes',
color: '#841584',
onPress: setProfileAttribute,
},
Expand Down
8 changes: 5 additions & 3 deletions example/src/KlaviyoReactWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
type Event,
Klaviyo,
type Location,
type Profile,
ProfileProperty,
type Event,
type Location,
} from 'klaviyo-react-native-sdk';

import {
Expand Down Expand Up @@ -97,7 +97,9 @@ export const setPushToken = async () => {

export const setProfileAttribute = async () => {
try {
Klaviyo.setProfileAttribute('CUSTOM', generateRandomName(12));
Klaviyo.setProfileAttribute(ProfileProperty.CITY, generateRandomName(5));
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added a couple properties outside of the custom one so that manual testing is easier.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice.
Yeah I'd love to update this sample app someday to have an input form here instead of just generating random data.

Klaviyo.setProfileAttribute(ProfileProperty.IMAGE, generateRandomName(5));
Klaviyo.setProfileAttribute('MY_CUSTOM_PROPERTY', generateRandomName(5));
} catch (e: any) {
console.log(e.message, e.code);
}
Expand Down
38 changes: 38 additions & 0 deletions ios/KlaviyoBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public class KlaviyoBridge: NSObject {
KlaviyoSDK().set(profile: profile)
}

@objc
public static func setProfileAttribute(_ key: String, value: String) {
KlaviyoSDK().set(profileAttribute: getProfileKey(key), value: value)
Copy link
Contributor

@evan-masseau evan-masseau Mar 13, 2024

Choose a reason for hiding this comment

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

Doesn't really matter, but in the Android code we just used CUSTOM for all incoming keys 🤷


    @ReactMethod
    override fun setProfileAttribute(
      propertyKey: String,
      value: String,
    ) {
      Klaviyo.setProfileAttribute(ProfileKey.CUSTOM(propertyKey), value)
    }

}

@objc
public static func setExternalId(_ value: String) {
KlaviyoSDK().set(externalId: value)
Expand Down Expand Up @@ -154,6 +159,39 @@ public class KlaviyoBridge: NSObject {
return .CustomEvent(str)
}
}

static func getProfileKey(_ str: String) -> Profile.ProfileKey {
switch str {
case ProfileProperty.firstName.rawValue:
return Profile.ProfileKey.firstName
case ProfileProperty.lastName.rawValue:
return Profile.ProfileKey.lastName
case ProfileProperty.address1.rawValue:
return Profile.ProfileKey.address1
case ProfileProperty.address2.rawValue:
return Profile.ProfileKey.address2
case ProfileProperty.title.rawValue:
return Profile.ProfileKey.title
case ProfileProperty.organization.rawValue:
return Profile.ProfileKey.organization
case ProfileProperty.city.rawValue:
return Profile.ProfileKey.city
case ProfileProperty.region.rawValue:
return Profile.ProfileKey.region
case ProfileProperty.country.rawValue:
return Profile.ProfileKey.country
case ProfileProperty.zip.rawValue:
return Profile.ProfileKey.zip
case ProfileProperty.image.rawValue:
return Profile.ProfileKey.image
case ProfileProperty.latitude.rawValue:
return Profile.ProfileKey.latitude
case ProfileProperty.longitude.rawValue:
return Profile.ProfileKey.longitude
default:
return Profile.ProfileKey.custom(customKey: str)
}
}
}

extension Collection where Element: RawRepresentable, Element.RawValue == String {
Expand Down
5 changes: 5 additions & 0 deletions ios/KlaviyoReactNativeSdk.mm
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ - (NSDictionary *)constantsToExport {
[KlaviyoBridge initialize: apiKey];
}

RCT_EXPORT_METHOD(setProfileAttribute: (NSString *)key value:(NSString *)value)
{
[KlaviyoBridge setProfileAttribute:key value:value];
}

RCT_EXPORT_METHOD(setProfile: (NSDictionary *)profileDict)
{
[KlaviyoBridge setProfile:profileDict];
Expand Down
Loading