diff --git a/packages/destination-actions/src/destinations/klaviyo/__tests__/__snapshots__/snapshot.test.ts.snap b/packages/destination-actions/src/destinations/klaviyo/__tests__/__snapshots__/snapshot.test.ts.snap index b3f050ecaa..50c4f1340b 100644 --- a/packages/destination-actions/src/destinations/klaviyo/__tests__/__snapshots__/snapshot.test.ts.snap +++ b/packages/destination-actions/src/destinations/klaviyo/__tests__/__snapshots__/snapshot.test.ts.snap @@ -53,7 +53,7 @@ exports[`Testing snapshot for actions-klaviyo destination: subscribeProfile acti Object { "data": Object { "attributes": Object { - "custom_source": "Segment Klaviyo (Actions) Destination", + "custom_source": "TsKr#", "profiles": Object { "data": Array [ Object { diff --git a/packages/destination-actions/src/destinations/klaviyo/subscribeProfile/__tests__/index.test.ts b/packages/destination-actions/src/destinations/klaviyo/subscribeProfile/__tests__/index.test.ts index ff86f1ffd5..0f2fd37cfc 100644 --- a/packages/destination-actions/src/destinations/klaviyo/subscribeProfile/__tests__/index.test.ts +++ b/packages/destination-actions/src/destinations/klaviyo/subscribeProfile/__tests__/index.test.ts @@ -20,107 +20,10 @@ describe('Subscribe Profile', () => { ).rejects.toThrowError(PayloadValidationError) }) - it('should throw error if both subscribe_email and subscribe_sms are false', async () => { - const event = createTestEvent({ - type: 'track', - context: { - traits: { - email: 'segment@test.com', - phone_number: '+17065802344' - } - } - }) - // subscribe_email: false, subscribe_sms: false, - const mapping = { - klaviyo_id: '', - subscribe_email: false, - subscribe_sms: false, - list_id: '', - consented_at: { - '@path': '$.timestamp' - }, - email: { - '@path': '$.context.traits.email' - }, - phone_number: { - '@path': '$.context.traits.phone_number' - } - } - - await expect(testDestination.testAction('subscribeProfile', { event, settings, mapping })).rejects.toThrowError( - PayloadValidationError - ) - }) - - it('should throw error if (email = "", email_subscribed = true; phone = "+17065802344", subscribe_sms = false)', async () => { - const event = createTestEvent({ - type: 'track', - context: { - traits: { - email: '', - phone_number: '+17065802344' - } - } - }) - // subscribe_email: true, subscribe_sms: false, - const mapping = { - klaviyo_id: '', - subscribe_email: true, - subscribe_sms: false, - list_id: '', - consented_at: { - '@path': '$.timestamp' - }, - email: { - '@path': '$.context.traits.email' - }, - phone_number: { - '@path': '$.context.traits.phone_number' - } - } - - await expect(testDestination.testAction('subscribeProfile', { event, settings, mapping })).rejects.toThrowError( - PayloadValidationError - ) - }) - - it('should throw error if (email = "valid@email.com", email_subscribed = false; phone = "", subscribe_sms = true)', async () => { - const event = createTestEvent({ - type: 'track', - context: { - traits: { - email: 'valid@email.com', - phone_number: '' - } - } - }) - // subscribe_email: false, subscribe_sms: true, - const mapping = { - klaviyo_id: '', - subscribe_email: false, - subscribe_sms: true, - list_id: '', - consented_at: { - '@path': '$.timestamp' - }, - email: { - '@path': '$.context.traits.email' - }, - phone_number: { - '@path': '$.context.traits.phone_number' - } - } - await expect(testDestination.testAction('subscribeProfile', { event, settings, mapping })).rejects.toThrowError( - PayloadValidationError - ) - }) - it('formats the correct request body when list id is empty', async () => { const payload = { email: 'segment@email.com', phone_number: '+17067675219', - subscribe_email: true, - subscribe_sms: true, list_id: '', klaviyo_id: '6789', timestamp: '2024-04-01T18:37:06.558Z' @@ -176,8 +79,6 @@ describe('Subscribe Profile', () => { const mapping = { klaviyo_id: payload.klaviyo_id, - subscribe_email: payload.subscribe_email, - subscribe_sms: payload.subscribe_sms, list_id: payload.list_id, consented_at: { '@path': '$.timestamp' @@ -199,8 +100,6 @@ describe('Subscribe Profile', () => { const payload = { email: 'segment@email.com', phone_number: '+17067675219', - subscribe_email: true, - subscribe_sms: true, list_id: '12345', klaviyo_id: '6789', timestamp: '2024-04-01T18:37:06.558Z' @@ -264,8 +163,6 @@ describe('Subscribe Profile', () => { const mapping = { klaviyo_id: payload.klaviyo_id, - subscribe_email: payload.subscribe_email, - subscribe_sms: payload.subscribe_sms, list_id: payload.list_id, consented_at: { '@path': '$.timestamp' @@ -283,12 +180,9 @@ describe('Subscribe Profile', () => { ).resolves.not.toThrowError() }) - it('formats the correct request body when only email channel is subscribed', async () => { + it('formats the correct request body when only email is provided', async () => { const payload = { email: 'segment@email.com', - phone_number: '+17067675219', - subscribe_email: true, - subscribe_sms: false, list_id: '12345', klaviyo_id: '6789', timestamp: '2024-04-01T18:37:06.558Z' @@ -306,7 +200,6 @@ describe('Subscribe Profile', () => { attributes: { id: payload.klaviyo_id, email: payload.email, - phone_number: payload.phone_number, subscriptions: { email: { marketing: { @@ -338,16 +231,13 @@ describe('Subscribe Profile', () => { timestamp: payload.timestamp, context: { traits: { - email: payload.email, - phone_number: payload.phone_number + email: payload.email } } }) const mapping = { klaviyo_id: payload.klaviyo_id, - subscribe_email: payload.subscribe_email, - subscribe_sms: payload.subscribe_sms, list_id: payload.list_id, consented_at: { '@path': '$.timestamp' @@ -365,12 +255,9 @@ describe('Subscribe Profile', () => { ).resolves.not.toThrowError() }) - it('formats the correct request body when only sms channel is subscribed', async () => { + it('formats the correct request body when only phone number is provided', async () => { const payload = { - email: 'segment@email.com', phone_number: '+17067675219', - subscribe_email: false, - subscribe_sms: true, list_id: '12345', klaviyo_id: '6789', timestamp: '2024-04-01T18:37:06.558Z' @@ -387,7 +274,6 @@ describe('Subscribe Profile', () => { type: 'profile', attributes: { id: payload.klaviyo_id, - email: payload.email, phone_number: payload.phone_number, subscriptions: { sms: { @@ -420,7 +306,6 @@ describe('Subscribe Profile', () => { timestamp: payload.timestamp, context: { traits: { - email: payload.email, phone_number: payload.phone_number } } @@ -428,8 +313,6 @@ describe('Subscribe Profile', () => { const mapping = { klaviyo_id: payload.klaviyo_id, - subscribe_email: payload.subscribe_email, - subscribe_sms: payload.subscribe_sms, list_id: payload.list_id, consented_at: { '@path': '$.timestamp' diff --git a/packages/destination-actions/src/destinations/klaviyo/subscribeProfile/generated-types.ts b/packages/destination-actions/src/destinations/klaviyo/subscribeProfile/generated-types.ts index e50b58b84c..fd22077a34 100644 --- a/packages/destination-actions/src/destinations/klaviyo/subscribeProfile/generated-types.ts +++ b/packages/destination-actions/src/destinations/klaviyo/subscribeProfile/generated-types.ts @@ -6,25 +6,21 @@ export interface Payload { */ klaviyo_id?: string /** - * The email address to subscribe or to set on the profile if the email channel is omitted. + * The email address to subscribe. If provided, the associated profile will be subscribed to Email marketing. */ email?: string /** - * Controls the subscription status for email marketing. If set to "yes", the profile's consent preferences for email marketing are set to "SUBSCRIBED"; otherwise, the email channel is omitted. - */ - subscribe_email: boolean - /** - * The phone number to subscribe or to set on the profile if SMS channel is omitted. This must be in E.164 format. + * The phone number to subscribe. This must be in E.164 format. If provided, the associated profile will be subscribed to SMS marketing. */ phone_number?: string - /** - * Controls the subscription status for SMS marketing. If set to "yes", the profile's consent preferences for SMS marketing are set to "SUBSCRIBED"; otherwise, the SMS channel is omitted. - */ - subscribe_sms: boolean /** * The Klaviyo list to add the newly subscribed profiles to. If no List Id is present, the opt-in process used to subscribe the profile depends on the account's default opt-in settings. */ list_id?: string + /** + * A custom method or source to detail source of consent preferences (e.g., "Marketing Event"). + */ + custom_source?: string /** * The timestamp of when the profile's consent was gathered. */ diff --git a/packages/destination-actions/src/destinations/klaviyo/subscribeProfile/index.ts b/packages/destination-actions/src/destinations/klaviyo/subscribeProfile/index.ts index 33eddf9d3e..a04cf4f407 100644 --- a/packages/destination-actions/src/destinations/klaviyo/subscribeProfile/index.ts +++ b/packages/destination-actions/src/destinations/klaviyo/subscribeProfile/index.ts @@ -9,8 +9,7 @@ import { SubscribeProfile, SubscribeEventData } from '../types' const action: ActionDefinition = { title: 'Subscribe Profile', - description: 'Subscribe profiles to Email marketing, SMS marketing, or both.', - defaultSubscription: 'type = "track"', + description: 'Subscribe Klaviyo profiles to Email marketing, SMS marketing, or both.', fields: { klaviyo_id: { label: 'Klaviyo Id', @@ -19,37 +18,29 @@ const action: ActionDefinition = { }, email: { label: 'Email', - description: `The email address to subscribe or to set on the profile if the email channel is omitted.`, + description: `The email address to subscribe. If provided, the associated profile will be subscribed to Email marketing.`, type: 'string', format: 'email', default: { '@path': '$.context.traits.email' } }, - subscribe_email: { - label: 'Subscribe Profile to Email Marketing', - description: `Controls the subscription status for email marketing. If set to "yes", the profile's consent preferences for email marketing are set to "SUBSCRIBED"; otherwise, the email channel is omitted.`, - type: 'boolean', - required: true, - default: true - }, phone_number: { label: 'Phone Number', - description: `The phone number to subscribe or to set on the profile if SMS channel is omitted. This must be in E.164 format.`, + description: `The phone number to subscribe. This must be in E.164 format. If provided, the associated profile will be subscribed to SMS marketing.`, type: 'string', default: { '@path': '$.context.traits.phone' } }, - subscribe_sms: { - label: 'Subscribe Profile to SMS Marketing', - description: `Controls the subscription status for SMS marketing. If set to "yes", the profile's consent preferences for SMS marketing are set to "SUBSCRIBED"; otherwise, the SMS channel is omitted.`, - type: 'boolean', - required: true, - default: true - }, list_id: { label: 'List Id', description: `The Klaviyo list to add the newly subscribed profiles to. If no List Id is present, the opt-in process used to subscribe the profile depends on the account's default opt-in settings.`, type: 'string', dynamic: true }, + custom_source: { + label: 'Custom Source', + description: 'A custom method or source to detail source of consent preferences (e.g., "Marketing Event").', + type: 'string', + default: 'Segment Klaviyo (Actions) Destination' + }, consented_at: { label: 'Consented At', description: `The timestamp of when the profile's consent was gathered.`, @@ -65,30 +56,18 @@ const action: ActionDefinition = { } }, perform: async (request, { payload }) => { - const { email, klaviyo_id, phone_number, consented_at, list_id, subscribe_email, subscribe_sms } = payload + const { email, klaviyo_id, phone_number, consented_at, list_id, custom_source } = payload if (!email && !phone_number) { throw new PayloadValidationError('Phone Number or Email is required.') } - // Ensure the properties for at least one marketing channel subscription (Email or SMS) are available - if ((!email || subscribe_email !== true) && (!phone_number || subscribe_sms !== true)) { - throw new PayloadValidationError('At least one marketing channel (Email or SMS) needs to be subscribed to.') - } - - const profileToSubscribe = formatSubscribeProfile( - email, - phone_number, - klaviyo_id, - consented_at, - subscribe_sms, - subscribe_email - ) + const profileToSubscribe = formatSubscribeProfile(email, phone_number, klaviyo_id, consented_at) const eventData: SubscribeEventData = { data: { type: 'profile-subscription-bulk-create-job', attributes: { - custom_source: 'Segment Klaviyo (Actions) Destination', + custom_source: custom_source || 'Segment Klaviyo (Actions) Destination', // Use custom_source if populated, otherwise fallback to default value profiles: { data: [profileToSubscribe] } @@ -122,9 +101,7 @@ function formatSubscribeProfile( email: string | undefined, phone_number: string | undefined, klaviyo_id: string | undefined, - consented_at: string | number | undefined, - subscribe_sms: boolean, - subscribe_email: boolean + consented_at: string | number | undefined ) { const profileToSubscribe: SubscribeProfile = { type: 'profile', @@ -136,7 +113,7 @@ function formatSubscribeProfile( } } - if (email && subscribe_email) { + if (email) { profileToSubscribe.attributes.subscriptions.email = { marketing: { consent: 'SUBSCRIBED', @@ -145,7 +122,7 @@ function formatSubscribeProfile( } } - if (phone_number && subscribe_sms) { + if (phone_number) { profileToSubscribe.attributes.subscriptions.sms = { marketing: { consent: 'SUBSCRIBED',