Skip to content
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

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

Merged
merged 16 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from 11 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"sourceKeys": "utm_campaign",
"destKeys": "name"
},
{
"sourceKeys": "utm_medium",
"destKeys": "medium"
},
{
"sourceKeys": "utm_term",
"destKeys": "term"
},
{
"sourceKeys": "utm_content",
"destKeys": "content"
}
]
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: 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 warning on line 13 in src/v1/sources/shopify/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v1/sources/shopify/transform.js#L13

Added line #L13 was not covered by tests
}
// 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 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 @@
getProductsFromLineItems,
getAnonymousIdFromAttributes,
} = require('./serverSideUtlis');
const { getCartToken } = require('./serverSideUtlis');

const NO_OPERATION_SUCCESS = {
outputToSource: {
Expand Down Expand Up @@ -125,6 +127,15 @@
const anonymousId = getAnonymousIdFromAttributes(event);
if (isDefinedAndNotNull(anonymousId)) {
message.setProperty('anonymousId', anonymousId);
} else {

Check warning on line 130 in src/v1/sources/shopify/webhookTransformations/serverSideTransform.js

View check run for this annotation

Codecov / codecov/patch

src/v1/sources/shopify/webhookTransformations/serverSideTransform.js#L130

Added line #L130 was not covered by tests
// if anonymousId is not present in note_attributes or note_attributes is not present, query redis for anonymousId
const cartToken = getCartToken(event);

Check warning on line 132 in src/v1/sources/shopify/webhookTransformations/serverSideTransform.js

View check run for this annotation

Codecov / codecov/patch

src/v1/sources/shopify/webhookTransformations/serverSideTransform.js#L132

Added line #L132 was not covered by tests
if (cartToken) {
const redisData = await RedisDB.getVal(cartToken);

Check warning on line 134 in src/v1/sources/shopify/webhookTransformations/serverSideTransform.js

View check run for this annotation

Codecov / codecov/patch

src/v1/sources/shopify/webhookTransformations/serverSideTransform.js#L134

Added line #L134 was not covered by tests
if (redisData?.anonymousId) {
message.setProperty('anonymousId', redisData.anonymousId);

Check warning on line 136 in src/v1/sources/shopify/webhookTransformations/serverSideTransform.js

View check run for this annotation

Codecov / codecov/patch

src/v1/sources/shopify/webhookTransformations/serverSideTransform.js#L136

Added line #L136 was not covered by tests
}
}
}
}
message.setProperty(`integrations.${INTEGERATION}`, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ const {
getAnonymousIdFromAttributes,
} = require('./serverSideUtlis');

const { constructPayload } = require('../../../../v0/util');

const {
lineItemsMappingJSON,
productMappingJSON,
} = require('../../../../v0/sources/shopify/config');
const { lineItemsMappingJSON } = require('../../../../v0/sources/shopify/config');
const Message = require('../../../../v0/sources/message');
jest.mock('../../../../v0/sources/message');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const { isDefinedAndNotNull } = require('@rudderstack/integrations-lib');
const { constructPayload } = require('../../../../v0/util');
const { lineItemsMappingJSON, productMappingJSON } = require('../config');
const {
lineItemsMappingJSON,
productMappingJSON,
} = require('../config');

/**
* Returns an array of products from the lineItems array received from the webhook event
Expand Down Expand Up @@ -54,8 +57,16 @@ 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) => event?.cart_token || null;

module.exports = {
createPropertiesForEcomEventFromWebhook,
getProductsFromLineItems,
getAnonymousIdFromAttributes,
getCartToken,
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const {
checkoutStepEventBuilder,
searchEventBuilder,
} = require('./pixelUtils');
const campaignObjectMappings = require('../pixelEventsMappings/campaignObjectMappings.json');
const {
INTEGERATION,
PIXEL_EVENT_TOPICS,
Expand Down Expand Up @@ -85,7 +86,7 @@ const handleCartTokenRedisOperations = async (inputEvent, clientId) => {

function processPixelEvent(inputEvent) {
// eslint-disable-next-line @typescript-eslint/naming-convention
const { name, query_parameters, clientId, data, id } = inputEvent;
const { name, query_parameters, context, clientId, data, id } = inputEvent;
const shopifyDetails = { ...inputEvent };
delete shopifyDetails.context;
delete shopifyDetails.query_parameters;
Expand Down Expand Up @@ -147,6 +148,36 @@ function processPixelEvent(inputEvent) {
});
message.setProperty('context.topic', name);
message.setProperty('context.shopifyDetails', shopifyDetails);

// adding campaign object to the message
if (context?.document?.location?.href) {
const url = new URL(context.document.location.href);
const campaignParams = {};

// Loop through mappings and extract UTM parameters
campaignObjectMappings.forEach((mapping) => {
const value = url.searchParams.get(mapping.sourceKeys);
if (value) {
campaignParams[mapping.destKeys] = value;
}
});

// Extract any UTM parameters not in the mappings
const campaignObjectSourceKeys = campaignObjectMappings.flatMap(
(mapping) => mapping.sourceKeys,
);
url.searchParams.forEach((value, key) => {
if (key.startsWith('utm_') && !campaignObjectSourceKeys.includes(key)) {
campaignParams[key] = value;
}
});

// Only add campaign object if we have any UTM parameters
if (Object.keys(campaignParams).length > 0) {
message.context = message.context || {};
message.context.campaign = campaignParams;
}
}
message.messageId = id;
message = removeUndefinedAndNullValues(message);
return message;
Expand Down
Loading
Loading