Skip to content

[STRATCONN-5489] - Add batch_keys to Klaviyo actions #2880

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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 @@ -31,6 +31,20 @@ Object {
}
`;

exports[`Testing snapshot for actions-klaviyo destination: addProfileToList action - batch keys 1`] = `
Object {
"default": Array [
"list_id",
],
"description": "The keys to use for batching the events.",
"label": "Batch Keys",
"multiple": true,
"required": false,
"type": "string",
"unsafe_hidden": true,
}
`;

exports[`Testing snapshot for actions-klaviyo destination: orderCompleted action - all fields 1`] = `
Object {
"data": Object {
Expand Down Expand Up @@ -68,8 +82,36 @@ Object {

exports[`Testing snapshot for actions-klaviyo destination: removeProfile action - all fields 1`] = `""`;

exports[`Testing snapshot for actions-klaviyo destination: removeProfile action - batch keys 1`] = `
Object {
"default": Array [
"list_id",
],
"description": "The keys to use for batching the events.",
"label": "Batch Keys",
"multiple": true,
"required": false,
"type": "string",
"unsafe_hidden": true,
}
`;

exports[`Testing snapshot for actions-klaviyo destination: removeProfileFromList action - all fields 1`] = `""`;

exports[`Testing snapshot for actions-klaviyo destination: removeProfileFromList action - batch keys 1`] = `
Object {
"default": Array [
"list_id",
],
"description": "The keys to use for batching the events.",
"label": "Batch Keys",
"multiple": true,
"required": false,
"type": "string",
"unsafe_hidden": true,
}
`;

exports[`Testing snapshot for actions-klaviyo destination: subscribeProfile action - all fields 1`] = `
Object {
"data": Object {
Expand Down Expand Up @@ -114,6 +156,21 @@ Object {
}
`;

exports[`Testing snapshot for actions-klaviyo destination: subscribeProfile action - batch keys 1`] = `
Object {
"default": Array [
"list_id",
"custom_source",
],
"description": "The keys to use for batching the events.",
"label": "Batch Keys",
"multiple": true,
"required": false,
"type": "string",
"unsafe_hidden": true,
}
`;

exports[`Testing snapshot for actions-klaviyo destination: trackEvent action - all fields 1`] = `
Object {
"data": Object {
Expand Down Expand Up @@ -193,6 +250,20 @@ Object {
}
`;

exports[`Testing snapshot for actions-klaviyo destination: unsubscribeProfile action - batch keys 1`] = `
Object {
"default": Array [
"list_id",
],
"description": "The keys to use for batching the events.",
"label": "Batch Keys",
"multiple": true,
"required": false,
"type": "string",
"unsafe_hidden": true,
}
`;

exports[`Testing snapshot for actions-klaviyo destination: upsertProfile action - all fields 1`] = `
Object {
"data": Object {
Expand Down Expand Up @@ -223,3 +294,18 @@ Object {
},
}
`;

exports[`Testing snapshot for actions-klaviyo destination: upsertProfile action - batch keys 1`] = `
Object {
"default": Array [
"list_id",
"override_list_id",
],
"description": "The keys to use for batching the events.",
"label": "Batch Keys",
"multiple": true,
"required": false,
"type": "string",
"unsafe_hidden": true,
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,12 @@ describe(`Testing snapshot for ${destinationSlug} destination:`, () => {
expect(rawBody).toMatchSnapshot()
}
})

const action = destination.actions[actionSlug]
if (action.fields.batch_keys) {
it(`${actionSlug} action - batch keys`, async () => {
expect(action.fields.batch_keys).toMatchSnapshot()
})
}
}
})

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,16 @@ const action: ActionDefinition<Settings, Payload> = {
organization: { ...organization },
location: { ...location },
properties: { ...properties },
country_code: { ...country_code }
country_code: { ...country_code },
batch_keys: {
label: 'Batch Keys',
description: 'The keys to use for batching the events.',
type: 'string',
unsafe_hidden: true,
required: false,
multiple: true,
default: ['list_id']
}
},
perform: async (request, { payload }) => {
const {
Expand All @@ -48,6 +57,7 @@ const action: ActionDefinition<Settings, Payload> = {
enable_batching,
batch_size,
country_code,
batch_keys,
...additionalAttributes
} = payload
const phone_number = processPhoneNumber(initialPhoneNumber, country_code)
Expand All @@ -57,8 +67,8 @@ const action: ActionDefinition<Settings, Payload> = {
const profileId = await createProfile(request, email, external_id, phone_number, additionalAttributes)
return await addProfileToList(request, profileId, list_id)
},
performBatch: async (request, { payload }) => {
return sendBatchedProfileImportJobRequest(request, payload)
performBatch: async (request, { payload, statsContext }) => {
return sendBatchedProfileImportJobRequest(request, payload, statsContext)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
JSONLikeObject,
HTTPError,
MultiStatusResponse,
ErrorCodes
ErrorCodes,
StatsContext
} from '@segment/actions-core'
import { API_URL, REVISION_DATE } from './config'
import { Settings } from './generated-types'
Expand Down Expand Up @@ -141,7 +142,7 @@ export const createImportJobPayload = (profiles: Payload[], listId?: string): {
attributes: {
profiles: {
data: profiles.map(
({ list_id, enable_batching, batch_size, override_list_id, country_code, ...attributes }) => ({
({ list_id, enable_batching, batch_size, override_list_id, country_code, batch_keys, ...attributes }) => ({
type: 'profile',
attributes
})
Expand Down Expand Up @@ -519,7 +520,11 @@ async function updateMultiStatusWithKlaviyoErrors(
})
}

export async function removeBulkProfilesFromList(request: RequestClient, payloads: RemoveProfilePayload[]) {
export async function removeBulkProfilesFromList(
request: RequestClient,
payloads: RemoveProfilePayload[],
statsContext?: StatsContext
) {
const multiStatusResponse = new MultiStatusResponse()

const { filteredPayloads, validPayloadIndicesBitmap } = validateAndPrepareRemoveBulkProfilePayloads(
Expand All @@ -535,6 +540,13 @@ export async function removeBulkProfilesFromList(request: RequestClient, payload
const externalIds = extractField(filteredPayloads, 'external_id')
const phoneNumbers = extractField(filteredPayloads, 'phone_number')

if (statsContext) {
const { tags, statsClient } = statsContext
const set = new Set()
filteredPayloads.forEach((x) => set.add(x.list_id))
statsClient?.histogram('actions-klaviyo.remove_profile_from_list.unique_list_id', set.size, tags)
}

const listId = filteredPayloads[0]?.list_id as string

try {
Expand Down Expand Up @@ -640,7 +652,7 @@ function validateAndConstructProfilePayload(payload: AddProfileToListPayload): {
delete payload.country_code
}

const { list_id, enable_batching, batch_size, country_code, ...attributes } = payload
const { list_id, enable_batching, batch_size, country_code, batch_keys, ...attributes } = payload

response.validPayload = { type: 'profile', attributes: attributes as JSONLikeObject }
return response
Expand All @@ -666,7 +678,11 @@ function validateAndPrepareBatchedProfileImportPayloads(
return { filteredPayloads, validPayloadIndicesBitmap }
}

export async function sendBatchedProfileImportJobRequest(request: RequestClient, payloads: AddProfileToListPayload[]) {
export async function sendBatchedProfileImportJobRequest(
request: RequestClient,
payloads: AddProfileToListPayload[],
statsContext?: StatsContext
) {
const multiStatusResponse = new MultiStatusResponse()
const { filteredPayloads, validPayloadIndicesBitmap } = validateAndPrepareBatchedProfileImportPayloads(
payloads,
Expand All @@ -676,6 +692,13 @@ export async function sendBatchedProfileImportJobRequest(request: RequestClient,
if (!filteredPayloads.length) {
return multiStatusResponse
}

if (statsContext) {
const { tags, statsClient } = statsContext
const set = new Set()
filteredPayloads.forEach((x) => set.add(x.list_id))
statsClient?.histogram('actions-klaviyo.add_profile_to_list.unique_list_id', set.size, tags)
}
const importJobPayload = constructBulkProfileImportPayload(
filteredPayloads as unknown as KlaviyoProfile[],
payloads[0]?.list_id
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ const action: ActionDefinition<Settings, Payload> = {
},
country_code: {
...country_code
},
batch_keys: {
label: 'Batch Keys',
description: 'The keys to use for batching the events.',
type: 'string',
unsafe_hidden: true,
required: false,
multiple: true,
default: ['list_id']
}
},
dynamicFields: {
Expand All @@ -66,8 +75,8 @@ const action: ActionDefinition<Settings, Payload> = {
)
return await removeProfileFromList(request, profileIds, list_id)
},
performBatch: async (request, { payload }) => {
return await removeBulkProfilesFromList(request, payload)
performBatch: async (request, { payload, statsContext }) => {
return await removeBulkProfilesFromList(request, payload, statsContext)
}
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ const action: ActionDefinition<Settings, Payload> = {
phone_number: { ...phone_number },
enable_batching: { ...enable_batching },
country_code: { ...country_code },
batch_size: { ...batch_size, default: 1000 }
batch_size: { ...batch_size, default: 1000 },
batch_keys: {
label: 'Batch Keys',
description: 'The keys to use for batching the events.',
type: 'string',
unsafe_hidden: true,
required: false,
multiple: true,
default: ['list_id']
}
},
perform: async (request, { payload }) => {
const { email, list_id, external_id, phone_number: initialPhoneNumber, country_code } = payload
Expand All @@ -32,8 +41,8 @@ const action: ActionDefinition<Settings, Payload> = {
)
return await removeProfileFromList(request, profileIds, list_id)
},
performBatch: async (request, { payload }) => {
return await removeBulkProfilesFromList(request, payload)
performBatch: async (request, { payload, statsContext }) => {
return await removeBulkProfilesFromList(request, payload, statsContext)
}
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading