Skip to content

Commit

Permalink
bugfix(SnapPixel): update init, identify and add feature to customise…
Browse files Browse the repository at this point in the history
… dedupKey(#571)

* bugfix[SnapPixel]: update identify and track call

* bugfix[SnapPixel]: updating the key of deduplicationId

* bugfix[SnapChat Pixel]: enable for customise dedupkey

* bugfix[SnapChat Pixel]:  add comments in getUserEmailAndPhone functions
  • Loading branch information
sauravlal-rudderstack authored Jul 21, 2022
1 parent 39346e1 commit 7731e33
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 61 deletions.
151 changes: 97 additions & 54 deletions integrations/SnapPixel/browser.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/* eslint-disable class-methods-use-this */
import get from "get-value";
import sha256 from "crypto-js/sha256";
import Storage from "../../utils/storage";
import logger from "../../utils/logUtil";

import { removeUndefinedAndNullValues } from "../utils/commonUtils";
import {
isDefinedAndNotNull,
removeUndefinedAndNullValues,
} from "../utils/commonUtils";
import { ecommEventPayload, eventPayload, sendEvent } from "./util";
ecommEventPayload,
eventPayload,
getUserEmailAndPhone,
sendEvent,
} from "./util";
import { NAME } from "./constants";
import { LOAD_ORIGIN } from "../ScriptLoader";

Expand All @@ -17,7 +18,8 @@ class SnapPixel {
this.pixelId = config.pixelId;
this.hashMethod = config.hashMethod;
this.name = NAME;

this.deduplicationKey = config.deduplicationKey;
this.enableDeduplication = config.enableDeduplication;
this.trackEvents = [
"SIGN_UP",
"OPEN_APP",
Expand Down Expand Up @@ -78,28 +80,14 @@ class SnapPixel {

const cookieData = Storage.getUserTraits();

let payload = {
user_email: cookieData.email,
user_phone_number: cookieData.phone,
};

if (!payload.user_email && !payload.user_phone_number) {
logger.debug(
"User parameter (email or phone number) not found in cookie. identify is required"
);
return;
}
const userEmail = cookieData.email;
const userPhoneNumber = cookieData.phone;

if (this.hashMethod === "sha256") {
if (isDefinedAndNotNull(payload.user_email)) {
payload.user_email = sha256(payload.user_email).toString();
}
if (isDefinedAndNotNull(payload.user_phone_number)) {
payload.user_phone_number = sha256(
payload.user_phone_number
).toString();
}
}
let payload = getUserEmailAndPhone(
this.hashMethod,
userEmail,
userPhoneNumber
);

payload = removeUndefinedAndNullValues(payload);
window.snaptr("init", this.pixelId, payload);
Expand All @@ -120,26 +108,20 @@ class SnapPixel {

const { message } = rudderElement;

let payload = {
user_email: get(message, "context.traits.email"),
user_phone_number: get(message, "context.traits.phone"),
};
const userEmail = get(message, "context.traits.email");
const userPhoneNumber = get(message, "context.traits.phone");
const ipAddress = get(message, "context.ip") || get(message, "request_ip");
let payload = {};

if (!payload.user_email && !payload.user_phone_number) {
logger.error("User parameter (email or phone number) is required");
if (!userEmail && !userPhoneNumber && !ipAddress) {
logger.error(
"User parameter (email or phone number or ip address) is required for Identify call."
);
return;
}

if (this.hashMethod === "sha256") {
if (isDefinedAndNotNull(payload.user_email)) {
payload.user_email = sha256(payload.user_email).toString();
}
if (isDefinedAndNotNull(payload.user_phone_number)) {
payload.user_phone_number = sha256(
payload.user_phone_number
).toString();
}
}
payload = getUserEmailAndPhone(this.hashMethod, userEmail, userPhoneNumber);
payload = { ...payload, ip_address: ipAddress };

payload = removeUndefinedAndNullValues(payload);
window.snaptr("init", this.pixelId, payload);
Expand All @@ -161,51 +143,102 @@ class SnapPixel {
case "order completed":
sendEvent(
this.ecomEvents.PURCHASE,
ecommEventPayload(event, message)
ecommEventPayload(
event,
message,
this.deduplicationKey,
this.enableDeduplication
)
);
break;
case "checkout started":
sendEvent(
this.ecomEvents.START_CHECKOUT,
ecommEventPayload(event, message)
ecommEventPayload(
event,
message,
this.deduplicationKey,
this.enableDeduplication
)
);
break;
case "product added":
sendEvent(
this.ecomEvents.ADD_CART,
ecommEventPayload(event, message)
ecommEventPayload(
event,
message,
this.deduplicationKey,
this.enableDeduplication
)
);
break;
case "payment info entered":
sendEvent(
this.ecomEvents.ADD_BILLING,
ecommEventPayload(event, message)
ecommEventPayload(
event,
message,
this.deduplicationKey,
this.enableDeduplication
)
);
break;
case "promotion clicked":
sendEvent(
this.ecomEvents.AD_CLICK,
ecommEventPayload(event, message)
ecommEventPayload(
event,
message,
this.deduplicationKey,
this.enableDeduplication
)
);
break;
case "promotion viewed":
sendEvent(this.ecomEvents.AD_VIEW, ecommEventPayload(event, message));
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)
ecommEventPayload(
event,
message,
this.deduplicationKey,
this.enableDeduplication
)
);
break;
case "product viewed":
case "product list viewed":
sendEvent(
this.ecomEvents.VIEW_CONTENT,
ecommEventPayload(event, message)
ecommEventPayload(
event,
message,
this.deduplicationKey,
this.enableDeduplication
)
);
break;
case "products searched":
sendEvent(this.ecomEvents.SEARCH, ecommEventPayload(event, message));
sendEvent(
this.ecomEvents.SEARCH,
ecommEventPayload(
event,
message,
this.deduplicationKey,
this.enableDeduplication
)
);
break;
default:
if (
Expand All @@ -215,7 +248,14 @@ class SnapPixel {
logger.error("Event doesn't match with Snap Pixel Events!");
return;
}
sendEvent(event, eventPayload(message));
sendEvent(
event,
eventPayload(
message,
this.deduplicationKey,
this.enableDeduplication
)
);
break;
}
} catch (err) {
Expand All @@ -227,7 +267,10 @@ class SnapPixel {
logger.debug("===In SnapPixel page===");

const { message } = rudderElement;
sendEvent("PAGE_VIEW", eventPayload(message));
sendEvent(
"PAGE_VIEW",
eventPayload(message, this.deduplicationKey, this.enableDeduplication)
);
}
}

Expand Down
62 changes: 55 additions & 7 deletions integrations/SnapPixel/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import get from "get-value";
import sha256 from "crypto-js/sha256";
import logger from "../../utils/logUtil";
import {
isDefinedAndNotNull,
Expand All @@ -14,7 +15,11 @@ const sendEvent = (event, payload) => {
}
};

const getCommonEventPayload = (message) => {
const getCommonEventPayload = (
message,
deduplicationKey,
enableDeduplication
) => {
let payload = {
price: parseFloat(get(message, "properties.price")),
client_deduplication_id: get(message, "properties.client_deduplication_id"),
Expand Down Expand Up @@ -42,21 +47,39 @@ const getCommonEventPayload = (message) => {
if (payload.success !== 0 && payload.success !== 1) {
payload.success = null;
}
if (enableDeduplication) {
payload.client_deduplication_id = get(
message,
`${deduplicationKey || "messageId"}`
);
}

payload = removeUndefinedAndNullValues(payload);
return payload;
};

const eventPayload = (message) => {
let payload = getCommonEventPayload(message);
const eventPayload = (message, deduplicationKey, enableDeduplication) => {
let payload = getCommonEventPayload(
message,
deduplicationKey,
enableDeduplication
);
payload.item_ids = get(message, "properties.item_ids");
payload = removeUndefinedAndNullValues(payload);
return payload;
};

const ecommEventPayload = (event, message) => {
let payload = getCommonEventPayload(message);

const ecommEventPayload = (
event,
message,
deduplicationKey,
enableDeduplication
) => {
let payload = getCommonEventPayload(
message,
deduplicationKey,
enableDeduplication
);
switch (event.toLowerCase().trim()) {
case "order completed": {
let itemIds = [];
Expand Down Expand Up @@ -208,4 +231,29 @@ const ecommEventPayload = (event, message) => {
return payload;
};

export { sendEvent, ecommEventPayload, eventPayload };
/*
Here, We take user parameters in payload i.e. userEmail and userPhoneNumber and hashMethod, so if hashMethod is `sha256`,
then we convert the userEmail and userPhoneNumber to user_hashed_email and user_hashed_phone_number respectively in `sha256` format.
Otherwise if hashMethod not in `sha256` then we pass the userEmail and userPhoneNumber as user_email and user_phone_number respectively.
*/
const getUserEmailAndPhone = (hashMethod, userEmail, userPhoneNumber) => {
const payload = {};
if (hashMethod === "sha256") {
if (userEmail) {
payload.user_hashed_email = sha256(userEmail).toString();
}
if (userPhoneNumber) {
payload.user_hashed_phone_number = sha256(userPhoneNumber).toString();
}
} else {
if (userEmail) {
payload.user_email = userEmail;
}
if (userPhoneNumber) {
payload.user_phone_number = userPhoneNumber;
}
}
return payload;
};

export { sendEvent, ecommEventPayload, eventPayload, getUserEmailAndPhone };

0 comments on commit 7731e33

Please sign in to comment.