Skip to content

Commit

Permalink
feat: add userId to web pixel events from redis if available
Browse files Browse the repository at this point in the history
  • Loading branch information
yashasvibajpai committed Feb 11, 2025
1 parent 72978c6 commit d63487b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/v1/sources/shopify/transformV1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion src/v1/sources/shopify/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
13 changes: 10 additions & 3 deletions src/v1/sources/shopify/webpixelTransformations/pixelTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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;

Check warning on line 169 in src/v1/sources/shopify/webpixelTransformations/pixelTransform.js

View check run for this annotation

Codecov / codecov/patch

src/v1/sources/shopify/webpixelTransformations/pixelTransform.js#L169

Added line #L169 was not covered by tests
}
message = removeUndefinedAndNullValues(message);
return message;
}

const processPixelWebEvents = (event) => {
const pixelEvent = processPixelEvent(event);
const processPixelWebEvents = async (event) => {
const pixelEvent = await processPixelEvent(event);
return removeUndefinedAndNullValues(pixelEvent);
};

Expand Down
7 changes: 7 additions & 0 deletions test/integrations/sources/shopify/mocks.ts
Original file line number Diff line number Diff line change
@@ -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({});
});
};

0 comments on commit d63487b

Please sign in to comment.