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 support for identity stitching for shopify pixel flow #3818

Merged
merged 14 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
24 changes: 24 additions & 0 deletions src/v1/sources/shopify/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const path = require('path');
const fs = require('fs');

const commonCartTokenLocation = 'context.document.location.pathname';
const commonCartTokenLocationContextual = 'context.cart_token';

const PIXEL_EVENT_TOPICS = {
CART_VIEWED: 'cart_viewed',
PRODUCT_ADDED_TO_CART: 'product_added_to_cart',
Expand Down Expand Up @@ -61,6 +64,25 @@ const checkoutStartedCompletedEventMappingJSON = JSON.parse(
),
);

const pixelEventToCartTokenLocationMapping = {
cart_viewed: 'properties.cart_id',
checkout_address_info_submitted: commonCartTokenLocation,
checkout_contact_info_submitted: commonCartTokenLocation,
checkout_shipping_info_submitted: commonCartTokenLocation,
payment_info_submitted: commonCartTokenLocation,
checkout_started: commonCartTokenLocation,
checkout_completed: commonCartTokenLocation,
};

const serverEventToCartTokenLocationMapping = {
'Cart Update': 'properties.token',
'Checkout Started': commonCartTokenLocationContextual,
'Checkout Updated': commonCartTokenLocationContextual,
'Order Created': commonCartTokenLocationContextual,
'Order Fulfilled': commonCartTokenLocationContextual,
'Order Cancelled': commonCartTokenLocationContextual,
};

const INTEGERATION = 'SHOPIFY';

module.exports = {
Expand All @@ -73,4 +95,6 @@ module.exports = {
productViewedEventMappingJSON,
productToCartEventMappingJSON,
checkoutStartedCompletedEventMappingJSON,
pixelEventToCartTokenLocationMapping,
serverEventToCartTokenLocationMapping,
};
36 changes: 34 additions & 2 deletions src/v1/sources/shopify/pixelTransform.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// eslint-disable-next-line @typescript-eslint/naming-convention
const _ = require('lodash');
const stats = require('../../../util/stats');
const logger = require('../../../logger');
const { removeUndefinedAndNullValues } = require('../../../v0/util');
const { generateUUID, removeUndefinedAndNullValues } = require('../../../v0/util');
const { RedisDB } = require('../../../util/redis/redisConnector');
const {
pageViewedEventBuilder,
cartViewedEventBuilder,
Expand All @@ -11,7 +14,7 @@
checkoutStepEventBuilder,
searchEventBuilder,
} = require('./pixelUtils');
const { INTEGERATION, PIXEL_EVENT_TOPICS } = require('./config');
const { INTEGERATION, PIXEL_EVENT_TOPICS, eventToCartTokenLocationMapping } = require('./config');

const NO_OPERATION_SUCCESS = {
outputToSource: {
Expand All @@ -21,6 +24,33 @@
statusCode: 200,
};

const handleRedisOperations = async (inputEvent) => {
try {
const cartTokenLocation = eventToCartTokenLocationMapping[inputEvent.name];
if (!cartTokenLocation) {
krishna2020 marked this conversation as resolved.
Show resolved Hide resolved
logger.info(`Cart token location not found for event: ${inputEvent.name}`);
return;

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

View check run for this annotation

Codecov / codecov/patch

src/v1/sources/shopify/pixelTransform.js#L31-L32

Added lines #L31 - L32 were not covered by tests
}

const cartToken = _.get(inputEvent, cartTokenLocation);

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

View check run for this annotation

Codecov / codecov/patch

src/v1/sources/shopify/pixelTransform.js#L35

Added line #L35 was not covered by tests
krishna2020 marked this conversation as resolved.
Show resolved Hide resolved
if (!cartToken) {
logger.info(`Cart token not found in input event: ${inputEvent.name}`);
krishna2020 marked this conversation as resolved.
Show resolved Hide resolved
return;

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

View check run for this annotation

Codecov / codecov/patch

src/v1/sources/shopify/pixelTransform.js#L37-L38

Added lines #L37 - L38 were not covered by tests
}

const storedAnonymousIdInRedis = await RedisDB.getVal(cartToken);

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

View check run for this annotation

Codecov / codecov/patch

src/v1/sources/shopify/pixelTransform.js#L41

Added line #L41 was not covered by tests
krishna2020 marked this conversation as resolved.
Show resolved Hide resolved
if (!storedAnonymousIdInRedis) {
const anonymousId = generateUUID();
await RedisDB.setVal(cartToken, anonymousId);
logger.info(`New anonymousId set in Redis for cartToken: ${cartToken}`);
} else {
logger.info(`AnonymousId already exists in Redis for cartToken: ${cartToken}`);

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

View check run for this annotation

Codecov / codecov/patch

src/v1/sources/shopify/pixelTransform.js#L43-L47

Added lines #L43 - L47 were not covered by tests
}
} catch (error) {
logger.error(`Error handling Redis operations for event: ${inputEvent.name}`, error);
}
};

function processPixelEvent(inputEvent) {
// eslint-disable-next-line @typescript-eslint/naming-convention
const { name, query_parameters, clientId, data } = inputEvent;
Expand Down Expand Up @@ -48,13 +78,15 @@
case PIXEL_EVENT_TOPICS.CHECKOUT_STARTED:
case PIXEL_EVENT_TOPICS.CHECKOUT_COMPLETED:
if (customer.id) message.userId = customer.id || '';
handleRedisOperations(inputEvent);
message = checkoutEventBuilder(inputEvent);
break;
case PIXEL_EVENT_TOPICS.CHECKOUT_ADDRESS_INFO_SUBMITTED:
case PIXEL_EVENT_TOPICS.CHECKOUT_CONTACT_INFO_SUBMITTED:
case PIXEL_EVENT_TOPICS.CHECKOUT_SHIPPING_INFO_SUBMITTED:
case PIXEL_EVENT_TOPICS.PAYMENT_INFO_SUBMITTED:
if (customer.id) message.userId = customer.id || '';
handleRedisOperations(inputEvent);
message = checkoutStepEventBuilder(inputEvent);
break;
case PIXEL_EVENT_TOPICS.SEARCH_SUBMITTED:
Expand Down
23 changes: 23 additions & 0 deletions src/v1/sources/shopify/transform.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
/* eslint-disable @typescript-eslint/naming-convention */
const _ = require('lodash');
const logger = require('../../../logger');
const { processEventFromPixel } = require('./pixelTransform');
const { process: processWebhookEvents } = require('../../../v0/sources/shopify/transform');
const { RedisDB } = require('../../../util/redis/redisConnector');
const { serverEventToCartTokenLocationMapping } = require('./config');

const enrichServerSideResponseWithAnonymousId = async (response) => {
if (serverEventToCartTokenLocationMapping[response.event]) {
const cartTokenLocation = serverEventToCartTokenLocationMapping[response.event];
const cartToken = _.get(response, cartTokenLocation);
if (cartToken) {
try {
const anonymousId = await RedisDB.getVal(cartToken);
response.anonymousId = anonymousId;

Check warning on line 16 in src/v1/sources/shopify/transform.js

View check run for this annotation

Codecov / codecov/patch

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

Added line #L16 was not covered by tests
} catch (error) {
logger.error(`Error getting value from Redis: ${error.message}`, error);
}
} else {
logger.info(`Cart token not found for event: ${response.event}`);

Check warning on line 21 in src/v1/sources/shopify/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v1/sources/shopify/transform.js#L20-L21

Added lines #L20 - L21 were not covered by tests
}
}
};

const process = async (inputEvent) => {
const { event } = inputEvent;
Expand All @@ -15,6 +36,8 @@
}
// this is for common logic for server-side events processing for both pixel and tracker apps.
const response = await processWebhookEvents(event);
krishna2020 marked this conversation as resolved.
Show resolved Hide resolved
// get value for anonymousId to enrich the event coming from the webhook.
enrichServerSideResponseWithAnonymousId(response);
return response;
};

Expand Down
Loading