From d63487be28428466ba63b1a861802d68a2c2748e Mon Sep 17 00:00:00 2001 From: Yashasvi Bajpai <33063622+yashasvibajpai@users.noreply.github.com> Date: Tue, 11 Feb 2025 14:43:44 +0530 Subject: [PATCH] feat: add userId to web pixel events from redis if available --- src/v1/sources/shopify/transformV1.js | 2 +- src/v1/sources/shopify/utils.js | 3 ++- .../webpixelTransformations/pixelTransform.js | 13 ++++++++++--- test/integrations/sources/shopify/mocks.ts | 7 +++++++ 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/v1/sources/shopify/transformV1.js b/src/v1/sources/shopify/transformV1.js index b3e01d95c67..bf8f6d7241c 100644 --- a/src/v1/sources/shopify/transformV1.js +++ b/src/v1/sources/shopify/transformV1.js @@ -17,7 +17,7 @@ const processV1Events = async (event) => { return processIdentifierEvent(event); } // handle events from the app pixel. - const pixelWebEventResponse = processPixelWebEvents(event); + const pixelWebEventResponse = await processPixelWebEvents(event); return pixelWebEventResponse; } if (isServerSideEvent) { diff --git a/src/v1/sources/shopify/utils.js b/src/v1/sources/shopify/utils.js index 9502b2ee595..7b6227fc4f4 100644 --- a/src/v1/sources/shopify/utils.js +++ b/src/v1/sources/shopify/utils.js @@ -11,8 +11,9 @@ const NO_OPERATION_SUCCESS = { const isIdentifierEvent = (payload) => ['rudderIdentifier'].includes(payload?.event); const processIdentifierEvent = async (event) => { - const { cartToken, anonymousId } = event; + const { cartToken, anonymousId, userId } = event; await RedisDB.setVal(`pixel:${cartToken}`, ['anonymousId', anonymousId]); + await RedisDB.setVal(`pixel:${anonymousId}`, ['userId', userId]); return NO_OPERATION_SUCCESS; }; diff --git a/src/v1/sources/shopify/webpixelTransformations/pixelTransform.js b/src/v1/sources/shopify/webpixelTransformations/pixelTransform.js index 44928686f83..ac23e240448 100644 --- a/src/v1/sources/shopify/webpixelTransformations/pixelTransform.js +++ b/src/v1/sources/shopify/webpixelTransformations/pixelTransform.js @@ -85,7 +85,7 @@ const handleCartTokenRedisOperations = async (inputEvent, clientId) => { } }; -function processPixelEvent(inputEvent) { +async function processPixelEvent(inputEvent) { // eslint-disable-next-line @typescript-eslint/naming-convention const { name, query_parameters, context, clientId, data, id } = inputEvent; const shopifyDetails = { ...inputEvent }; @@ -161,12 +161,19 @@ function processPixelEvent(inputEvent) { message.context.campaign = campaignParams; } message.messageId = id; + + // attach userId to the message if anonymousId is present in Redis + // this allows stitching of events from the same user across multiple checkouts + const redisData = await RedisDB.getVal(`pixel:${message.anonymousId}`); + if (isDefinedNotNullNotEmpty(redisData)) { + message.userId = redisData.userId; + } message = removeUndefinedAndNullValues(message); return message; } -const processPixelWebEvents = (event) => { - const pixelEvent = processPixelEvent(event); +const processPixelWebEvents = async (event) => { + const pixelEvent = await processPixelEvent(event); return removeUndefinedAndNullValues(pixelEvent); }; diff --git a/test/integrations/sources/shopify/mocks.ts b/test/integrations/sources/shopify/mocks.ts index e1895e78124..2afcb92ff19 100644 --- a/test/integrations/sources/shopify/mocks.ts +++ b/test/integrations/sources/shopify/mocks.ts @@ -1,5 +1,12 @@ import utils from '../../../../src/v0/util'; +import { RedisDB } from '../../../../src/util/redis/redisConnector'; export const mockFns = (_) => { jest.spyOn(utils, 'generateUUID').mockReturnValue('5d3e2cb6-4011-5c9c-b7ee-11bc1e905097'); + jest.spyOn(RedisDB, 'getVal').mockImplementation((key) => { + if (key === 'pixel:c7b3f99b-4d34-463b-835f-c879482a7750') { + return Promise.resolve({ userId: 'test-user-id' }); + } + return Promise.resolve({}); + }); };