Skip to content

feat: add redis support in shopify pixel for id stitching #3957

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

Merged
merged 16 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from 8 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
1 change: 1 addition & 0 deletions src/v0/sources/shopify/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ module.exports = {
createPropertiesForEcomEvent,
extractEmailFromPayload,
getAnonymousIdAndSessionId,
getCartToken,
checkAndUpdateCartItems,
getHashLineItems,
getDataFromRedis,
Expand Down
3 changes: 3 additions & 0 deletions src/v1/sources/shopify/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ const RUDDER_ECOM_MAP = {
orders_create: 'Order Created',
};

const SERVERSIDE_STITCHED_EVENTS = Object.values(RUDDER_ECOM_MAP);

const contextualFieldMappingJSON = JSON.parse(
fs.readFileSync(path.resolve(__dirname, 'pixelEventsMappings', 'contextualFieldMapping.json')),
);
Expand Down Expand Up @@ -95,6 +97,7 @@ module.exports = {
PIXEL_EVENT_TOPICS,
PIXEL_EVENT_MAPPING,
RUDDER_ECOM_MAP,
SERVERSIDE_STITCHED_EVENTS,
contextualFieldMappingJSON,
cartViewedEventMappingJSON,
productListViewedEventMappingJSON,
Expand Down
4 changes: 4 additions & 0 deletions src/v1/sources/shopify/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ const { process: processWebhookEvents } = require('../../../v0/sources/shopify/t
const {
process: processPixelWebhookEvents,
} = require('./webhookTransformations/serverSideTransform');
const { processIdentifierEvent, isIdentifierEvent } = require('./utils');

const process = async (inputEvent) => {
const { event } = inputEvent;
const { query_parameters } = event;
if (isIdentifierEvent(event)) {
return processIdentifierEvent(event);
}
// check identify the event is from the web pixel based on the pixelEventLabel property.
const { pixelEventLabel: pixelClientEventLabel } = event;
if (pixelClientEventLabel) {
Expand Down
22 changes: 22 additions & 0 deletions src/v1/sources/shopify/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { RedisDB } = require('../../../util/redis/redisConnector');

const NO_OPERATION_SUCCESS = {
outputToSource: {
body: Buffer.from('OK').toString('base64'),
contentType: 'text/plain',
},
statusCode: 200,
};

const isIdentifierEvent = (payload) => ['rudderIdentifier'].includes(payload?.event);

const processIdentifierEvent = async (event) => {
const { cartToken, anonymousId } = event;
await RedisDB.setVal(`${cartToken}`, ['anonymousId', anonymousId]);
return NO_OPERATION_SUCCESS;
};

module.exports = {
processIdentifierEvent,
isIdentifierEvent,
};
45 changes: 45 additions & 0 deletions src/v1/sources/shopify/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const { isIdentifierEvent, processIdentifierEvent } = require('./utils');
const { RedisDB } = require('../../../util/redis/redisConnector');

describe('Identifier Utils Tests', () => {
describe('test isIdentifierEvent', () => {
it('should return true if the event is rudderIdentifier', () => {
const event = { event: 'rudderIdentifier' };
expect(isIdentifierEvent(event)).toBe(true);
});

it('should return false if the event is not rudderIdentifier', () => {
const event = { event: 'checkout started' };
expect(isIdentifierEvent(event)).toBe(false);
});
});

describe('test processIdentifierEvent', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should set the anonymousId in redis and return NO_OPERATION_SUCCESS', async () => {
const setValSpy = jest.spyOn(RedisDB, 'setVal').mockResolvedValue('OK');
const event = { cartToken: 'cartTokenTest1', anonymousId: 'anonymousIdTest1' };

const response = await processIdentifierEvent(event);

expect(setValSpy).toHaveBeenCalledWith('cartTokenTest1', ['anonymousId', 'anonymousIdTest1']);
expect(response).toEqual({
outputToSource: {
body: Buffer.from('OK').toString('base64'),
contentType: 'text/plain',
},
statusCode: 200,
});
});

it('should handle redis errors', async () => {
jest.spyOn(RedisDB, 'setVal').mockRejectedValue(new Error('Redis connection failed'));
const event = { cartToken: 'cartTokenTest1', anonymousId: 'anonymousIdTest1' };

await expect(processIdentifierEvent(event)).rejects.toThrow('Redis connection failed');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const get = require('get-value');
const stats = require('../../../../util/stats');
const { getShopifyTopic, extractEmailFromPayload } = require('../../../../v0/sources/shopify/util');
const { removeUndefinedAndNullValues, isDefinedAndNotNull } = require('../../../../v0/util');
const { RedisDB } = require('../../../../util/redis/redisConnector');
const Message = require('../../../../v0/sources/message');
const { EventType } = require('../../../../constants');
const {
Expand All @@ -21,6 +22,7 @@ const {
getProductsFromLineItems,
getAnonymousIdFromAttributes,
} = require('./serverSideUtlis');
const { getCartToken } = require('./serverSideUtlis');

const NO_OPERATION_SUCCESS = {
outputToSource: {
Expand Down Expand Up @@ -125,6 +127,13 @@ const processEvent = async (inputEvent, metricMetadata) => {
const anonymousId = getAnonymousIdFromAttributes(event);
if (isDefinedAndNotNull(anonymousId)) {
message.setProperty('anonymousId', anonymousId);
} else {
// if anonymousId is not present in note_attributes or note_attributes is not present, query redis for anonymousId
const cartToken = getCartToken(event, message);
const redisData = await RedisDB.getVal(cartToken);
if (redisData?.anonymousId) {
message.setProperty('anonymousId', redisData.anonymousId);
}
}
}
message.setProperty(`integrations.${INTEGERATION}`, true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const { isDefinedAndNotNull } = require('@rudderstack/integrations-lib');
const { constructPayload } = require('../../../../v0/util');
const { lineItemsMappingJSON, productMappingJSON } = require('../config');
const {
lineItemsMappingJSON,
productMappingJSON,
SERVERSIDE_STITCHED_EVENTS,
} = require('../config');

/**
* Returns an array of products from the lineItems array received from the webhook event
Expand Down Expand Up @@ -54,8 +58,21 @@ const getAnonymousIdFromAttributes = (event) => {
return rudderAnonymousIdObject ? rudderAnonymousIdObject.value : null;
};

/**
* Returns the cart_token from the event message
* @param {Object} message
* @returns {String} cart_token
*/
const getCartToken = (event, message) => {
if (SERVERSIDE_STITCHED_EVENTS.includes(message?.event) || (message?.event) === 'Cart Update') {
return event.cart_token || null;
}
return null;
};

module.exports = {
createPropertiesForEcomEventFromWebhook,
getProductsFromLineItems,
getAnonymousIdFromAttributes,
getCartToken,
};
Loading