Skip to content

Commit 62b67cd

Browse files
Add stats to measure grouping
1 parent 2a77f6d commit 62b67cd

File tree

7 files changed

+59
-11
lines changed

7 files changed

+59
-11
lines changed

packages/destination-actions/src/destinations/klaviyo/addProfileToList/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ const action: ActionDefinition<Settings, Payload> = {
6666
const profileId = await createProfile(request, email, external_id, phone_number, additionalAttributes)
6767
return await addProfileToList(request, profileId, list_id)
6868
},
69-
performBatch: async (request, { payload }) => {
70-
return sendBatchedProfileImportJobRequest(request, payload)
69+
performBatch: async (request, { payload, statsContext }) => {
70+
return sendBatchedProfileImportJobRequest(request, payload, statsContext)
7171
}
7272
}
7373

packages/destination-actions/src/destinations/klaviyo/functions.ts

+26-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
JSONLikeObject,
88
HTTPError,
99
MultiStatusResponse,
10-
ErrorCodes
10+
ErrorCodes,
11+
StatsContext
1112
} from '@segment/actions-core'
1213
import { API_URL, REVISION_DATE } from './config'
1314
import { Settings } from './generated-types'
@@ -519,7 +520,11 @@ async function updateMultiStatusWithKlaviyoErrors(
519520
})
520521
}
521522

522-
export async function removeBulkProfilesFromList(request: RequestClient, payloads: RemoveProfilePayload[]) {
523+
export async function removeBulkProfilesFromList(
524+
request: RequestClient,
525+
payloads: RemoveProfilePayload[],
526+
statsContext?: StatsContext
527+
) {
523528
const multiStatusResponse = new MultiStatusResponse()
524529

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

543+
if (statsContext) {
544+
const { tags, statsClient } = statsContext
545+
const set = new Set()
546+
filteredPayloads.forEach((x) => set.add(x.list_id))
547+
statsClient.histogram('actions-klaviyo.remove_profile_from_list.unique_list_id', set.size, tags)
548+
}
549+
538550
const listId = filteredPayloads[0]?.list_id as string
539551

540552
try {
@@ -666,7 +678,11 @@ function validateAndPrepareBatchedProfileImportPayloads(
666678
return { filteredPayloads, validPayloadIndicesBitmap }
667679
}
668680

669-
export async function sendBatchedProfileImportJobRequest(request: RequestClient, payloads: AddProfileToListPayload[]) {
681+
export async function sendBatchedProfileImportJobRequest(
682+
request: RequestClient,
683+
payloads: AddProfileToListPayload[],
684+
statsContext?: StatsContext
685+
) {
670686
const multiStatusResponse = new MultiStatusResponse()
671687
const { filteredPayloads, validPayloadIndicesBitmap } = validateAndPrepareBatchedProfileImportPayloads(
672688
payloads,
@@ -676,6 +692,13 @@ export async function sendBatchedProfileImportJobRequest(request: RequestClient,
676692
if (!filteredPayloads.length) {
677693
return multiStatusResponse
678694
}
695+
696+
if (statsContext) {
697+
const { tags, statsClient } = statsContext
698+
const set = new Set()
699+
filteredPayloads.forEach((x) => set.add(x.list_id))
700+
statsClient.histogram('actions-klaviyo.add_profile_to_list.unique_list_id', set.size, tags)
701+
}
679702
const importJobPayload = constructBulkProfileImportPayload(
680703
filteredPayloads as unknown as KlaviyoProfile[],
681704
payloads[0]?.list_id

packages/destination-actions/src/destinations/klaviyo/removeProfile/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ const action: ActionDefinition<Settings, Payload> = {
7575
)
7676
return await removeProfileFromList(request, profileIds, list_id)
7777
},
78-
performBatch: async (request, { payload }) => {
79-
return await removeBulkProfilesFromList(request, payload)
78+
performBatch: async (request, { payload, statsContext }) => {
79+
return await removeBulkProfilesFromList(request, payload, statsContext)
8080
}
8181
}
8282

packages/destination-actions/src/destinations/klaviyo/removeProfileFromList/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ const action: ActionDefinition<Settings, Payload> = {
4141
)
4242
return await removeProfileFromList(request, profileIds, list_id)
4343
},
44-
performBatch: async (request, { payload }) => {
45-
return await removeBulkProfilesFromList(request, payload)
44+
performBatch: async (request, { payload, statsContext }) => {
45+
return await removeBulkProfilesFromList(request, payload, statsContext)
4646
}
4747
}
4848

packages/destination-actions/src/destinations/klaviyo/subscribeProfile/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ const action: ActionDefinition<Settings, Payload> = {
116116
return profile.email || profile.phone_number
117117
})
118118

119+
if (statsContext) {
120+
const { statsClient, tags } = statsContext
121+
const set = new Set()
122+
filteredPayload.forEach((profile) => {
123+
set.add(`${profile.list_id}-${profile.custom_source}`)
124+
})
125+
statsClient.histogram('actions-klaviyo.subscribe_profile.unique_list_id', set.size, tags)
126+
}
127+
119128
// if there are no payloads with phone or email throw error
120129
if (filteredPayload.length === 0) {
121130
throw new PayloadValidationError('Phone Number or Email is required.')

packages/destination-actions/src/destinations/klaviyo/unsubscribeProfile/index.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ const action: ActionDefinition<Settings, Payload> = {
8383
json: unSubData
8484
})
8585
},
86-
performBatch: async (request, { payload }) => {
86+
performBatch: async (request, { payload, statsContext }) => {
8787
// remove payloads that have niether email or phone_number
8888
const filteredPayload = payload.filter((profile) => {
8989
// Validate and convert the phone number using the provided country code
@@ -100,6 +100,15 @@ const action: ActionDefinition<Settings, Payload> = {
100100
return profile.email || profile.phone_number
101101
})
102102

103+
if (statsContext) {
104+
const { statsClient, tags } = statsContext
105+
const set = new Set()
106+
filteredPayload.forEach((profile) => {
107+
set.add(profile.list_id)
108+
})
109+
statsClient.histogram('actions-klaviyo.unsubscribe_profile.unique_list_id', set.size, tags)
110+
}
111+
103112
// if there are no payloads with phone or email throw error
104113
if (payload.length === 0) {
105114
throw new PayloadValidationError('Phone Number or Email is required.')

packages/destination-actions/src/destinations/klaviyo/upsertProfile/index.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ const action: ActionDefinition<Settings, Payload> = {
309309
}
310310
},
311311

312-
performBatch: async (request, { payload, hookOutputs }) => {
312+
performBatch: async (request, { payload, hookOutputs, statsContext }) => {
313313
payload = payload.filter((profile) => {
314314
// Validate and convert the phone number using the provided country code
315315
const validPhoneNumber = validateAndConvertPhoneNumber(profile.phone_number, profile.country_code)
@@ -325,6 +325,13 @@ const action: ActionDefinition<Settings, Payload> = {
325325
return profile.email || profile.phone_number || profile.external_id
326326
})
327327

328+
if (statsContext) {
329+
const { tags, statsClient } = statsContext
330+
const set = new Set()
331+
payload.forEach((x) => set.add(`${x.list_id}-${x.override_list_id}`))
332+
statsClient.histogram('actions-klaviyo.remove_profile_from_list.unique_list_id', set.size, tags)
333+
}
334+
328335
const profilesWithList: Payload[] = []
329336
const profilesWithoutList: Payload[] = []
330337

0 commit comments

Comments
 (0)