From 9d46f957aca8eb1d5e3410891596da9dbff9fbd3 Mon Sep 17 00:00:00 2001 From: Innovative-GauravKochar <117165746+Innovative-GauravKochar@users.noreply.github.com> Date: Tue, 3 Dec 2024 18:18:57 +0530 Subject: [PATCH] Added stats for listId to see the whether entire batch has same listId or not (#2616) Co-authored-by: Gaurav Kochar --- .../destinations/klaviyo/addProfileToList/index.ts | 12 +++++++++++- .../src/destinations/klaviyo/removeProfile/index.ts | 13 ++++++++++++- .../destinations/klaviyo/subscribeProfile/index.ts | 12 +++++++++++- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/packages/destination-actions/src/destinations/klaviyo/addProfileToList/index.ts b/packages/destination-actions/src/destinations/klaviyo/addProfileToList/index.ts index bdc5487de6..f8c7e1bdf2 100644 --- a/packages/destination-actions/src/destinations/klaviyo/addProfileToList/index.ts +++ b/packages/destination-actions/src/destinations/klaviyo/addProfileToList/index.ts @@ -64,7 +64,7 @@ const action: ActionDefinition = { const profileId = await createProfile(request, email, external_id, phone_number, additionalAttributes) return await addProfileToList(request, profileId, list_id) }, - performBatch: async (request, { payload }) => { + performBatch: async (request, { payload, statsContext, features }) => { // Filtering out profiles that do not contain either an email, external_id or valid phone number. payload = payload.filter((profile) => { // Validate and convert the phone number using the provided country code @@ -80,6 +80,16 @@ const action: ActionDefinition = { } return profile.email || profile.external_id || profile.phone_number }) + const statsClient = statsContext?.statsClient + const tags = statsContext?.tags + + if (features && features['klaviyo-list-id']) { + const set = new Set() + payload.forEach((profile) => { + set.add(profile.list_id) + }) + statsClient?.histogram(`klaviyo_list_id`, set.size, tags) + } const listId = payload[0]?.list_id const importJobPayload = createImportJobPayload(payload, listId) return sendImportJobRequest(request, importJobPayload) diff --git a/packages/destination-actions/src/destinations/klaviyo/removeProfile/index.ts b/packages/destination-actions/src/destinations/klaviyo/removeProfile/index.ts index 4dee411d14..16b3b3d4fa 100644 --- a/packages/destination-actions/src/destinations/klaviyo/removeProfile/index.ts +++ b/packages/destination-actions/src/destinations/klaviyo/removeProfile/index.ts @@ -65,7 +65,7 @@ const action: ActionDefinition = { ) return await removeProfileFromList(request, profileIds, list_id) }, - performBatch: async (request, { payload }) => { + performBatch: async (request, { payload, features, statsContext }) => { // Filtering out profiles that do not contain either an email, valid phone_number or external_id. const filteredPayload = payload.filter((profile) => { // Validate and convert the phone number using the provided country code @@ -81,6 +81,17 @@ const action: ActionDefinition = { } return profile.email || profile.external_id || profile.phone_number }) + const statsClient = statsContext?.statsClient + const tags = statsContext?.tags + + if (features && features['klaviyo-list-id']) { + const set = new Set() + payload.forEach((profile) => { + set.add(profile.list_id) + }) + statsClient?.histogram(`klaviyo_list_id`, set.size, tags) + } + const listId = filteredPayload[0]?.list_id const emails = filteredPayload.map((profile) => profile.email).filter((email) => email) as string[] const externalIds = filteredPayload diff --git a/packages/destination-actions/src/destinations/klaviyo/subscribeProfile/index.ts b/packages/destination-actions/src/destinations/klaviyo/subscribeProfile/index.ts index 62c5b19000..df9d130794 100644 --- a/packages/destination-actions/src/destinations/klaviyo/subscribeProfile/index.ts +++ b/packages/destination-actions/src/destinations/klaviyo/subscribeProfile/index.ts @@ -94,7 +94,7 @@ const action: ActionDefinition = { json: subData }) }, - performBatch: async (request, { payload }) => { + performBatch: async (request, { payload, statsContext, features }) => { // remove payloads that have niether email or phone_number const filteredPayload = payload.filter((profile) => { // Validate and convert the phone number using the provided country code @@ -137,6 +137,16 @@ const action: ActionDefinition = { 'Exceeded maximum allowed batches due to unique list_id and custom_source pairings' ) } + const statsClient = statsContext?.statsClient + const tags = statsContext?.tags + + if (features && features['klaviyo-list-id']) { + const set = new Set() + payload.forEach((profile) => { + set.add(profile.list_id) + }) + statsClient?.histogram(`klaviyo_list_id`, set.size, tags) + } const requests: Promise>[] = [] batches.forEach((key) => { const { list_id, custom_source, profiles } = sortedProfilesObject[key]