-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move webhook specific logic for v2 pixel, address comments
- Loading branch information
1 parent
c8c77f2
commit 2885968
Showing
5 changed files
with
107 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
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 (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); | ||
} | ||
} | ||
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); | ||
} | ||
message = removeUndefinedAndNullValues(message); | ||
return message; | ||
}; | ||
|
||
module.exports = { processPixelWebhookEvent }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters