Skip to content

Commit

Permalink
chore: move webhook specific logic for v2 pixel, address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yashasvibajpai committed Sep 20, 2024
1 parent c8c77f2 commit 2885968
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/v0/sources/shopify/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,7 @@ module.exports = {
processEvent,
isIdentifierEvent,
processIdentifierEvent,
identifyPayloadBuilder,
ecomPayloadBuilder,
trackPayloadBuilder,
};
5 changes: 2 additions & 3 deletions src/v1/sources/shopify/pixelTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,11 @@ function processPixelEvent(inputEvent) {
return message;
}

const processEventV2 = async (event) => {
const processEventFromPixel = async (event) => {
const pixelEvent = processPixelEvent(event);
return removeUndefinedAndNullValues(pixelEvent);
};

module.exports = {
processEventV2,
processPixelEvent,
processEventFromPixel,
};
86 changes: 86 additions & 0 deletions src/v1/sources/shopify/pixelWebhookEventTransform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const lodash = require('lodash');
const get = require('get-value');
const stats = require('../../../util/stats');
const { getShopifyTopic, extractEmailFromPayload } = require('../../../v0/sources/shopify/util');
const {
identifyPayloadBuilder,
trackPayloadBuilder,
ecomPayloadBuilder,
} = require('../../../v0/sources/shopify/transform');
const { removeUndefinedAndNullValues, generateUUID } = require('../../../v0/util');
const Message = require('../../../v0/sources/message');
const {
INTEGERATION,
IDENTIFY_TOPICS,
ECOM_TOPICS,
SUPPORTED_TRACK_EVENTS,
} = require('../../../v0/sources/shopify/config');

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

const processPixelWebhookEvent = async (inputEvent, metricMetadata) => {
let message = new Message(INTEGERATION);
const event = lodash.cloneDeep(inputEvent);
const shopifyTopic = getShopifyTopic(event);
delete event.query_parameters;
switch (shopifyTopic) {
case IDENTIFY_TOPICS.CUSTOMERS_CREATE:
case IDENTIFY_TOPICS.CUSTOMERS_UPDATE:
message = identifyPayloadBuilder(event);
break;
case ECOM_TOPICS.ORDERS_CREATE:
case ECOM_TOPICS.ORDERS_UPDATE:
case ECOM_TOPICS.CHECKOUTS_CREATE:

Check warning on line 39 in src/v1/sources/shopify/pixelWebhookEventTransform.js

View check run for this annotation

Codecov / codecov/patch

src/v1/sources/shopify/pixelWebhookEventTransform.js#L33-L39

Added lines #L33 - L39 were not covered by tests
case ECOM_TOPICS.CHECKOUTS_UPDATE:
message = ecomPayloadBuilder(event, shopifyTopic);
break;
case 'carts_update':
message = trackPayloadBuilder(event, shopifyTopic);
break;
default:

Check warning on line 46 in src/v1/sources/shopify/pixelWebhookEventTransform.js

View check run for this annotation

Codecov / codecov/patch

src/v1/sources/shopify/pixelWebhookEventTransform.js#L46

Added line #L46 was not covered by tests
if (!SUPPORTED_TRACK_EVENTS.includes(shopifyTopic)) {
stats.increment('invalid_shopify_event', {

Check warning on line 48 in src/v1/sources/shopify/pixelWebhookEventTransform.js

View check run for this annotation

Codecov / codecov/patch

src/v1/sources/shopify/pixelWebhookEventTransform.js#L48

Added line #L48 was not covered by tests
writeKey: metricMetadata.writeKey,
source: metricMetadata.source,
shopifyTopic: metricMetadata.shopifyTopic,
});
return NO_OPERATION_SUCCESS;

Check warning on line 53 in src/v1/sources/shopify/pixelWebhookEventTransform.js

View check run for this annotation

Codecov / codecov/patch

src/v1/sources/shopify/pixelWebhookEventTransform.js#L53

Added line #L53 was not covered by tests
}
message = trackPayloadBuilder(event, shopifyTopic);
break;

Check warning on line 56 in src/v1/sources/shopify/pixelWebhookEventTransform.js

View check run for this annotation

Codecov / codecov/patch

src/v1/sources/shopify/pixelWebhookEventTransform.js#L55-L56

Added lines #L55 - L56 were not covered by tests
}

if (message.userId) {
message.userId = String(message.userId);
}
message.anonymousId = generateUUID();

if (!get(message, 'traits.email')) {
const email = extractEmailFromPayload(event);
if (email) {
message.setProperty('traits.email', email);

Check warning on line 67 in src/v1/sources/shopify/pixelWebhookEventTransform.js

View check run for this annotation

Codecov / codecov/patch

src/v1/sources/shopify/pixelWebhookEventTransform.js#L67

Added line #L67 was not covered by tests
}
}
message.setProperty(`integrations.${INTEGERATION}`, true);
message.setProperty('context.library', {
name: 'RudderStack Shopify Cloud',
version: '2.0.0',
});
message.setProperty('context.topic', shopifyTopic);
// attaching cart, checkout and order tokens in context object
message.setProperty(`context.cart_token`, event.cart_token);
message.setProperty(`context.checkout_token`, event.checkout_token);
if (shopifyTopic === 'orders_updated') {
message.setProperty(`context.order_token`, event.token);

Check warning on line 80 in src/v1/sources/shopify/pixelWebhookEventTransform.js

View check run for this annotation

Codecov / codecov/patch

src/v1/sources/shopify/pixelWebhookEventTransform.js#L80

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

module.exports = { processPixelWebhookEvent };
11 changes: 4 additions & 7 deletions src/v1/sources/shopify/transform.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
const { isDefinedAndNotNull } = require('../../../v0/util');
const { processEventV2 } = require('./pixelTransform');
const { processEventFromPixel } = require('./pixelTransform');
const { processPixelWebhookEvent } = require('./pixelWebhookEventTransform');
const {
processEvent,
isIdentifierEvent,
Expand All @@ -22,14 +23,10 @@ const process = async (inputEvent) => {
if (pixelEventLabel) {
// this is a event fired from the web pixel loaded on the browser
// by the user interactions with the store.
const responseV2 = await processEventV2(event);
const responseV2 = await processEventFromPixel(event);
return responseV2;
}
const webhookEventResponse = await processEvent(event, metricMetadata, source);
webhookEventResponse.context.library = {
name: 'RudderStack Shopify Cloud',
version: '2.0.0',
};
const webhookEventResponse = await processPixelWebhookEvent(event, metricMetadata, source);
return webhookEventResponse;
}
if (isIdentifierEvent(event)) {
Expand Down
14 changes: 12 additions & 2 deletions test/integrations/sources/shopify/data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { skip } from 'node:test';
import utils from '../../../../src/v0/util';
const defaultMockFns = () => {
jest.spyOn(utils, 'generateUUID').mockReturnValue('97fcd7b2-cc24-47d7-b776-057b7b199513');
};

const dummySourceConfig = {
ID: 'dummy-source-id',
Expand Down Expand Up @@ -5546,14 +5550,17 @@ const v1ServerSideEventsScenarios = [
},
},
timestamp: '2024-09-17T07:29:02.000Z',
anonymousId: '5d3e2cb6-4011-5c9c-b7ee-11bc1e905097',
anonymousId: '97fcd7b2-cc24-47d7-b776-057b7b199513',
},
],
},
},
],
},
},
mockFns: () => {
defaultMockFns();
},
},
{
name: 'shopify',
Expand Down Expand Up @@ -5758,14 +5765,17 @@ const v1ServerSideEventsScenarios = [
},
],
},
anonymousId: '5d3e2cb6-4011-5c9c-b7ee-11bc1e905097',
anonymousId: '97fcd7b2-cc24-47d7-b776-057b7b199513',
},
],
},
},
],
},
},
mockFns: () => {
defaultMockFns();
},
},
];

Expand Down

0 comments on commit 2885968

Please sign in to comment.