Skip to content

Commit

Permalink
add cookie data check and reformat code
Browse files Browse the repository at this point in the history
Signed-off-by: Akash Gupta <[email protected]>
  • Loading branch information
akashrpo committed Aug 20, 2021
1 parent 3a3a9fb commit b919b30
Showing 1 changed file with 83 additions and 13 deletions.
96 changes: 83 additions & 13 deletions integrations/SnapPixel/browser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* eslint-disable class-methods-use-this */
import get from "get-value";
import is from "is";
import sha256 from "crypto-js/sha256";
import Storage from "../../utils/storage";
import logger from "../../utils/logUtil";

import {
isDefinedAndNotNull,
removeUndefinedAndNullValues,
Expand Down Expand Up @@ -40,12 +43,13 @@ class SnapPixel {
"LIST_VIEW",
];

this.customEvents = [];
this.customEvents.push(config.customEvent1);
this.customEvents.push(config.customEvent2);
this.customEvents.push(config.customEvent3);
this.customEvents.push(config.customEvent4);
this.customEvents.push(config.customEvent5);
this.customEvents = [
config.customEvent1,
config.customEvent2,
config.customEvent3,
config.customEvent4,
config.customEvent5,
];
}

init() {
Expand All @@ -66,6 +70,34 @@ class SnapPixel {
var u = t.getElementsByTagName(s)[0];
u.parentNode.insertBefore(r, u);
})(window, document, "https://sc-static.net/scevent.min.js");

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"
);
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 = removeUndefinedAndNullValues(payload);
window.snaptr("init", this.pixelId, payload);
}

isLoaded() {
Expand All @@ -88,6 +120,11 @@ class SnapPixel {
user_phone_number: get(message, "context.traits.phone"),
};

if (!payload.user_email && !payload.user_phone_number) {
logger.error("User parameter (email or phone number) is required");
return;
}

if (this.hashMethod === "sha256") {
if (isDefinedAndNotNull(payload.user_email)) {
payload.user_email = sha256(payload.user_email).toString();
Expand All @@ -99,11 +136,6 @@ class SnapPixel {
}
}

if (!payload.user_email && !payload.user_phone_number) {
logger.error("User parameter (email or phone number) is required");
return;
}

payload = removeUndefinedAndNullValues(payload);
window.snaptr("init", this.pixelId, payload);
}
Expand All @@ -119,7 +151,26 @@ class SnapPixel {
return;
}

let payload = get(message, "properties");
let payload = {
price: get(message, "properties.price"),
currency: get(message, "properties.currency"),
transaction_id: get(message, "properties.transactionId"),
item_ids: get(message, "properties.itemId"),
item_category: get(message, "properties.category"),
description: get(message, "properties.description"),
search_string: get(message, "properties.search"),
number_items: get(message, "properties.numberItems"),
payment_info_available: get(message, "properties.paymentInfoAvailable"),
sign_up_method: get(message, "properties.signUpMethod"),
success: get(message, "properties.success"),
};

if (!is.boolean(payload.payment_info_available)) {
payload.payment_info_available = null;
}
if (!is.boolean(payload.success)) {
payload.success = null;
}
payload = removeUndefinedAndNullValues(payload);

switch (event.toLowerCase()) {
Expand Down Expand Up @@ -162,7 +213,26 @@ class SnapPixel {

const { message } = rudderElement;

let payload = get(message, "properties");
let payload = {
price: get(message, "properties.price"),
currency: get(message, "properties.currency"),
transaction_id: get(message, "properties.transactionId"),
item_ids: get(message, "properties.itemId"),
item_category: get(message, "properties.category"),
description: get(message, "properties.description"),
search_string: get(message, "properties.search"),
number_items: get(message, "properties.numberItems"),
payment_info_available: get(message, "properties.paymentInfoAvailable"),
sign_up_method: get(message, "properties.signUpMethod"),
success: get(message, "properties.success"),
};

if (!is.boolean(payload.payment_info_available)) {
payload.payment_info_available = null;
}
if (!is.boolean(payload.success)) {
payload.success = null;
}
payload = removeUndefinedAndNullValues(payload);

window.snaptr("track", "PAGE_VIEW", payload);
Expand Down

0 comments on commit b919b30

Please sign in to comment.