diff --git a/src/v1/sources/shopify/pixelWebhookEventTransform.js b/src/v1/sources/shopify/pixelWebhookEventTransform.js deleted file mode 100644 index 43b741f837..0000000000 --- a/src/v1/sources/shopify/pixelWebhookEventTransform.js +++ /dev/null @@ -1,88 +0,0 @@ -const lodash = require('lodash'); -const get = require('get-value'); -const { isDefinedNotNullNotEmpty } = require('@rudderstack/integrations-lib'); -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: - case ECOM_TOPICS.CHECKOUTS_UPDATE: - message = ecomPayloadBuilder(event, shopifyTopic); - break; - case 'carts_update': - message = trackPayloadBuilder(event, shopifyTopic); - break; - default: - if (!SUPPORTED_TRACK_EVENTS.includes(shopifyTopic)) { - stats.increment('invalid_shopify_event', { - writeKey: metricMetadata.writeKey, - source: metricMetadata.source, - shopifyTopic: metricMetadata.shopifyTopic, - }); - return NO_OPERATION_SUCCESS; - } - message = trackPayloadBuilder(event, shopifyTopic); - break; - } - - if (event.customer && isDefinedNotNullNotEmpty(event?.customer?.id)) { - message.userId = String(event.customer.id); - } - message.anonymousId = generateUUID(); - - if (!get(message, 'traits.email')) { - const email = extractEmailFromPayload(event); - if (email) { - message.setProperty('traits.email', email); - } - } - message.setProperty(`integrations.${INTEGERATION}`, true); - message.setProperty('context.library', { - name: 'RudderStack Shopify Cloud', - eventOrigin: 'server', - 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); - } - message = removeUndefinedAndNullValues(message); - return message; -}; - -module.exports = { processPixelWebhookEvent }; diff --git a/src/v1/sources/shopify/transform.js b/src/v1/sources/shopify/transform.js index ff9aadbca4..dee5a14a9d 100644 --- a/src/v1/sources/shopify/transform.js +++ b/src/v1/sources/shopify/transform.js @@ -1,32 +1,20 @@ /* eslint-disable @typescript-eslint/naming-convention */ -const { isDefinedAndNotNull } = require('../../../v0/util'); const { processEventFromPixel } = require('./pixelTransform'); -const { processPixelWebhookEvent } = require('./pixelWebhookEventTransform'); -const { process: processLegacyEvents } = require('../../../v0/sources/shopify/transform'); +const { process: processWebhookEvents } = require('../../../v0/sources/shopify/transform'); const process = async (inputEvent) => { - const { event, source } = inputEvent; - const metricMetadata = { - // eslint-disable-next-line unicorn/consistent-destructuring - writeKey: source?.WriteKey || event.query_parameters?.writeKey?.[0], - sourceId: source?.ID, - source: 'SHOPIFY', - }; + const { event } = inputEvent; // check on the source Config to identify the event is from the tracker-based (legacy) // or the pixel-based (latest) implementation. - if (source && isDefinedAndNotNull(source.Config) && source?.Config?.version === 'pixel') { - const { pixelEventLabel: pixelClientEventLabel } = event; - if (pixelClientEventLabel) { - // this is a event fired from the web pixel loaded on the browser - // by the user interactions with the store. - const responseV2 = await processEventFromPixel(event); - return responseV2; - } - const webhookEventResponse = await processPixelWebhookEvent(event, metricMetadata, source); - return webhookEventResponse; + const { pixelEventLabel: pixelClientEventLabel } = event; + if (pixelClientEventLabel) { + // this is a event fired from the web pixel loaded on the browser + // by the user interactions with the store. + const responseV2 = await processEventFromPixel(event); + return responseV2; } - // this is for default legacy tracker based server-side events processing - const response = await processLegacyEvents(event); + // this is for common logic for server-side events processing for both pixel and tracker apps. + const response = await processWebhookEvents(event); return response; }; diff --git a/test/integrations/sources/shopify/v1ServerSideEventsTests.ts b/test/integrations/sources/shopify/v1ServerSideEventsTests.ts index a91a213873..2c323cb370 100644 --- a/test/integrations/sources/shopify/v1ServerSideEventsTests.ts +++ b/test/integrations/sources/shopify/v1ServerSideEventsTests.ts @@ -2,7 +2,7 @@ // the v1 transformation flow import utils from '../../../../src/v0/util'; const defaultMockFns = () => { - jest.spyOn(utils, 'generateUUID').mockReturnValue('97fcd7b2-cc24-47d7-b776-057b7b199513'); + jest.spyOn(utils, 'generateUUID').mockReturnValue('5d3e2cb6-4011-5c9c-b7ee-11bc1e905097'); }; import { dummySourceConfig } from './constants'; @@ -207,8 +207,7 @@ export const v1ServerSideEventsScenarios = [ context: { library: { name: 'RudderStack Shopify Cloud', - eventOrigin: 'server', - version: '2.0.0', + version: '1.0.0', }, integration: { name: 'SHOPIFY', @@ -367,7 +366,7 @@ export const v1ServerSideEventsScenarios = [ }, }, timestamp: '2024-09-17T07:29:02.000Z', - anonymousId: '97fcd7b2-cc24-47d7-b776-057b7b199513', + anonymousId: '5d3e2cb6-4011-5c9c-b7ee-11bc1e905097', }, ], }, @@ -491,8 +490,7 @@ export const v1ServerSideEventsScenarios = [ context: { library: { name: 'RudderStack Shopify Cloud', - eventOrigin: 'server', - version: '2.0.0', + version: '1.0.0', }, integration: { name: 'SHOPIFY', @@ -583,7 +581,7 @@ export const v1ServerSideEventsScenarios = [ }, ], }, - anonymousId: '97fcd7b2-cc24-47d7-b776-057b7b199513', + anonymousId: '5d3e2cb6-4011-5c9c-b7ee-11bc1e905097', }, ], },