diff --git a/integrations/GoogleAds/browser.js b/integrations/GoogleAds/browser.js index 3e17fefa68..04ba3e027a 100644 --- a/integrations/GoogleAds/browser.js +++ b/integrations/GoogleAds/browser.js @@ -1,7 +1,11 @@ /* eslint-disable class-methods-use-this */ import logger from "../../utils/logUtil"; import { LOAD_ORIGIN } from "../ScriptLoader"; -import { removeUndefinedAndNullValues } from "../utils/commonUtils"; +import { + getHashFromArrayWithDuplicate, + removeUndefinedAndNullValues, + getEventMappingFromConfig, +} from "../utils/commonUtils"; import { NAME } from "./constants"; class GoogleAds { @@ -16,6 +20,7 @@ class GoogleAds { this.conversionLinker = config.conversionLinker || true; this.disableAdPersonalization = config.disableAdPersonalization || false; this.name = NAME; + this.eventMappingFromConfig = config.eventMappingFromConfig; } init() { @@ -88,16 +93,29 @@ class GoogleAds { logger.error("Event name not present"); return; } - + // modify the event name to mapped event name from the config + const eventsHashmap = getHashFromArrayWithDuplicate( + this.eventMappingFromConfig, + "from", + "to", + false + ); let payload = {}; const sendToValue = this.conversionId; if (rudderElement.message.properties) { payload = rudderElement.message.properties; } - payload.send_to = sendToValue; - window.gtag("event", event, payload); + + const events = getEventMappingFromConfig(event, eventsHashmap); + if (events) { + events.forEach((ev) => { + window.gtag("event", ev, payload); + }); + } else { + window.gtag("event", event, payload); + } } } diff --git a/integrations/RedditPixel/browser.js b/integrations/RedditPixel/browser.js index 92fcef6c1b..59fd3649b5 100644 --- a/integrations/RedditPixel/browser.js +++ b/integrations/RedditPixel/browser.js @@ -10,11 +10,16 @@ import logger from "../../utils/logUtil"; import { LOAD_ORIGIN } from "../ScriptLoader"; import { NAME } from "./constants"; +import { + getHashFromArrayWithDuplicate, + getEventMappingFromConfig, +} from "../utils/commonUtils"; class RedditPixel { constructor(config) { this.advertiserId = config.advertiserId; this.name = NAME; + this.eventMappingFromConfig = config.eventMappingFromConfig; } init() { @@ -30,7 +35,7 @@ class RedditPixel { p.callQueue = []; var t = d.createElement("script"); (t.src = "https://www.redditstatic.com/ads/pixel.js"), (t.async = !0); - (t.setAttribute("data-loader", LOAD_ORIGIN)); + t.setAttribute("data-loader", LOAD_ORIGIN); var s = d.getElementsByTagName("script")[0]; s.parentNode.insertBefore(t, s); } @@ -62,29 +67,45 @@ class RedditPixel { logger.error("Event name is not present"); return; } - - switch (event.toLowerCase()) { - case "product added": - window.rdt("track", "AddToCart"); - break; - case "product added to wishlist": - window.rdt("track", "AddToWishlist"); - break; - case "order completed": - window.rdt("track", "Purchase"); - break; - case "lead": - window.rdt("track", "Lead"); - break; - case "view content": - window.rdt("track", "ViewContent"); - break; - case "search": - window.rdt("track", "Search"); - break; - default: - logger.error(`Invalid event ${event}. Track call not supported`); - break; + const eventMappingFromConfigMap = getHashFromArrayWithDuplicate( + this.eventMappingFromConfig, + "from", + "to", + false + ); + if (eventMappingFromConfigMap[event]) { + // mapping event from UI + const events = getEventMappingFromConfig( + event, + eventMappingFromConfigMap + ); + events.forEach((ev) => { + window.rdt("track", ev); + }); + } else { + switch (event.toLowerCase()) { + case "product added": + window.rdt("track", "AddToCart"); + break; + case "product added to wishlist": + window.rdt("track", "AddToWishlist"); + break; + case "order completed": + window.rdt("track", "Purchase"); + break; + case "lead": + window.rdt("track", "Lead"); + break; + case "view content": + window.rdt("track", "ViewContent"); + break; + case "search": + window.rdt("track", "Search"); + break; + default: + logger.error(`Invalid event ${event}. Track call not supported`); + break; + } } } diff --git a/integrations/SnapPixel/browser.js b/integrations/SnapPixel/browser.js index 4218ced042..5a6c526aa9 100644 --- a/integrations/SnapPixel/browser.js +++ b/integrations/SnapPixel/browser.js @@ -3,7 +3,11 @@ import get from "get-value"; import Storage from "../../utils/storage"; import logger from "../../utils/logUtil"; -import { removeUndefinedAndNullValues } from "../utils/commonUtils"; +import { + getEventMappingFromConfig, + removeUndefinedAndNullValues, + getHashFromArrayWithDuplicate, +} from "../utils/commonUtils"; import { ecommEventPayload, eventPayload, @@ -20,6 +24,7 @@ class SnapPixel { this.name = NAME; this.deduplicationKey = config.deduplicationKey; this.enableDeduplication = config.enableDeduplication; + this.eventMappingFromConfig = config.eventMappingFromConfig; this.trackEvents = [ "SIGN_UP", "OPEN_APP", @@ -132,6 +137,12 @@ class SnapPixel { const { message } = rudderElement; const { event } = message; + const eventMappingFromConfigMap = getHashFromArrayWithDuplicate( + this.eventMappingFromConfig, + "from", + "to", + false + ); if (!event) { logger.error("Event name not present"); @@ -139,32 +150,15 @@ class SnapPixel { } try { - switch (event.toLowerCase().trim()) { - case "order completed": - sendEvent( - this.ecomEvents.PURCHASE, - ecommEventPayload( - event, - message, - this.deduplicationKey, - this.enableDeduplication - ) - ); - break; - case "checkout started": - sendEvent( - this.ecomEvents.START_CHECKOUT, - ecommEventPayload( - event, - message, - this.deduplicationKey, - this.enableDeduplication - ) - ); - break; - case "product added": + if (eventMappingFromConfigMap[event]) { + // mapping event from UI + const events = getEventMappingFromConfig( + event, + eventMappingFromConfigMap + ); + events.forEach((ev) => { sendEvent( - this.ecomEvents.ADD_CART, + ev, ecommEventPayload( event, message, @@ -172,91 +166,127 @@ class SnapPixel { this.enableDeduplication ) ); - break; - case "payment info entered": - sendEvent( - this.ecomEvents.ADD_BILLING, - ecommEventPayload( - event, - message, - this.deduplicationKey, - this.enableDeduplication - ) - ); - break; - case "promotion clicked": - sendEvent( - this.ecomEvents.AD_CLICK, - ecommEventPayload( - event, - message, - this.deduplicationKey, - this.enableDeduplication - ) - ); - break; - case "promotion viewed": - sendEvent( - this.ecomEvents.AD_VIEW, - ecommEventPayload( + }); + } else { + switch (event.toLowerCase().trim()) { + case "order completed": + sendEvent( + this.ecomEvents.PURCHASE, + ecommEventPayload( + event, + message, + this.deduplicationKey, + this.enableDeduplication + ) + ); + break; + case "checkout started": + sendEvent( + this.ecomEvents.START_CHECKOUT, + ecommEventPayload( + event, + message, + this.deduplicationKey, + this.enableDeduplication + ) + ); + break; + case "product added": + sendEvent( + this.ecomEvents.ADD_CART, + ecommEventPayload( + event, + message, + this.deduplicationKey, + this.enableDeduplication + ) + ); + break; + case "payment info entered": + sendEvent( + this.ecomEvents.ADD_BILLING, + ecommEventPayload( + event, + message, + this.deduplicationKey, + this.enableDeduplication + ) + ); + break; + case "promotion clicked": + sendEvent( + this.ecomEvents.AD_CLICK, + ecommEventPayload( + event, + message, + this.deduplicationKey, + this.enableDeduplication + ) + ); + break; + case "promotion viewed": + sendEvent( + this.ecomEvents.AD_VIEW, + ecommEventPayload( + event, + message, + this.deduplicationKey, + this.enableDeduplication + ) + ); + break; + case "product added to wishlist": + sendEvent( + this.ecomEvents.ADD_TO_WISHLIST, + ecommEventPayload( + event, + message, + this.deduplicationKey, + this.enableDeduplication + ) + ); + break; + case "product viewed": + case "product list viewed": + sendEvent( + this.ecomEvents.VIEW_CONTENT, + ecommEventPayload( + event, + message, + this.deduplicationKey, + this.enableDeduplication + ) + ); + break; + case "products searched": + sendEvent( + this.ecomEvents.SEARCH, + ecommEventPayload( + event, + message, + this.deduplicationKey, + this.enableDeduplication + ) + ); + break; + default: + if ( + !this.trackEvents.includes(event.trim().toUpperCase()) && + !this.customEvents.includes(event.trim().toLowerCase()) + ) { + logger.error("Event doesn't match with Snap Pixel Events!"); + return; + } + sendEvent( event, - message, - this.deduplicationKey, - this.enableDeduplication - ) - ); - break; - case "product added to wishlist": - sendEvent( - this.ecomEvents.ADD_TO_WISHLIST, - ecommEventPayload( - event, - message, - this.deduplicationKey, - this.enableDeduplication - ) - ); - break; - case "product viewed": - case "product list viewed": - sendEvent( - this.ecomEvents.VIEW_CONTENT, - ecommEventPayload( - event, - message, - this.deduplicationKey, - this.enableDeduplication - ) - ); - break; - case "products searched": - sendEvent( - this.ecomEvents.SEARCH, - ecommEventPayload( - event, - message, - this.deduplicationKey, - this.enableDeduplication - ) - ); - break; - default: - if ( - !this.trackEvents.includes(event.trim().toUpperCase()) && - !this.customEvents.includes(event.trim().toLowerCase()) - ) { - logger.error("Event doesn't match with Snap Pixel Events!"); - return; - } - sendEvent( - event, - eventPayload( - message, - this.deduplicationKey, - this.enableDeduplication - ) - ); - break; + eventPayload( + message, + this.deduplicationKey, + this.enableDeduplication + ) + ); + break; + } } } catch (err) { logger.error("[Snap Pixel] track failed with following error", err); diff --git a/integrations/utils/commonUtils.js b/integrations/utils/commonUtils.js index 530c31a19e..137bfef065 100644 --- a/integrations/utils/commonUtils.js +++ b/integrations/utils/commonUtils.js @@ -116,7 +116,24 @@ function flattenJson(data) { return result; } +/** + * Check whether the passed eventname is mapped in the config + * and return the mapped event name. + * @param {*} event + * @param {*} eventsHashmap + * @returns mappedEventName + */ +function getEventMappingFromConfig(event, eventsHashmap) { + // if the event name is mapped in the config, use the mapped name + // else use the original event name + if (eventsHashmap[event]) { + return eventsHashmap[event]; + } + return null; +} + export { + getEventMappingFromConfig, getHashFromArrayWithDuplicate, getHashFromArray, toIso,