Skip to content

Commit

Permalink
addressed review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
utsabc committed Mar 12, 2021
1 parent e837e04 commit 4d3bfd1
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 42 deletions.
93 changes: 52 additions & 41 deletions integrations/Clevertap/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
import get from "get-value";
import logger from "../../utils/logUtil";
import ScriptLoader from "../ScriptLoader";
import { extractCustomFields, getDefinedTraits } from "../../utils/utils";
import {
extractCustomFields,
getDefinedTraits,
isArray,
isObject,
} from "../../utils/utils";

class Clevertap {
constructor(config) {
Expand All @@ -25,19 +30,6 @@ class Clevertap {
"Birthday",
"anonymousId",
"userId",
"msgEmail",
"msgemail",
"msg_email",
"msgPush",
"msgpush",
"msg_push",
"msgSms",
"msgsms",
"msg_sms",
"msgSMS",
"msgWhatsapp",
"msgwhatsapp",
"msg_whatsapp",
];
}

Expand All @@ -48,8 +40,6 @@ class Clevertap {
? "https://d2r1yp2w7bby2u.cloudfront.net/js/a.js"
: "http://static.clevertap.com/js/a.js";

ScriptLoader("clevertap-integration", sourceUrl);

window.clevertap = {
event: [],
profile: [],
Expand All @@ -62,16 +52,18 @@ class Clevertap {
if (this.region && this.region !== "none") {
window.clevertap.region.push(this.region);
}

ScriptLoader("clevertap-integration", sourceUrl);
}

isLoaded() {
logger.debug("in clevertap isLoaded");
return !!window.clevertap && window.clevertap.logout !== "undefined";
return !!window.clevertap && window.clevertap.logout !== undefined;
}

isReady() {
logger.debug("in clevertap isReady");
return !!window.clevertap && window.clevertap.logout !== "undefined";
return !!window.clevertap && window.clevertap.logout !== undefined;
}

identify(rudderElement) {
Expand All @@ -90,27 +82,6 @@ class Clevertap {
Phone: phone,
Gender: get(message, "context.traits.gender"),
DOB: get(message, "context.traits.birthday"),
// optional fields. controls whether the user will be sent email, push etc.
"MSG-email":
get(message, "context.traits.msgEmail") ||
get(message, "context.traits.msgemail") ||
get(message, "context.traits.msg_email"),
"MSG-push":
get(message, "context.traits.msgPush") ||
get(message, "context.traits.msgpush") ||
get(message, "context.traits.msg_push"),
// Enable push notifications
"MSG-sms":
get(message, "context.traits.msgSms") ||
get(message, "context.traits.msgsms") ||
get(message, "context.traits.msg_sms") ||
get(message, "context.traits.msgSMS"),
// Enable sms notifications
"MSG-whatsapp":
get(message, "context.traits.msgWhatsapp") ||
get(message, "context.traits.msgwhatsapp") ||
get(message, "context.traits.msg_whatsapp"),
// Enable WhatsApp notifications
};
// Extract other K-V property from traits about user custom properties
try {
Expand All @@ -123,7 +94,12 @@ class Clevertap {
} catch (err) {
logger.debug(`Error occured at extractCustomFields ${err}`);
}

Object.values(payload).map((vals) => {
if (isObject(vals)) {
logger.debug("cannot process, unsupported traits");
return;
}
});
window.clevertap.onUserLogin.push({
Site: payload,
});
Expand All @@ -133,13 +109,42 @@ class Clevertap {
logger.debug("in clevertap track");
const { event, properties } = rudderElement.message;
if (properties) {
window.clevertap.event.push(event, properties);
if (event === "Order Completed") {
let ecomProperties = {
"Charged ID": properties.checkout_id,
Amount: properties.revenue,
Items: properties.products,
};
// Extract other K-V property from traits about user custom properties
try {
ecomProperties = extractCustomFields(
rudderElement.message,
ecomProperties,
["properties"],
["checkout_id", "revenue", "products"]
);
} catch (err) {
logger.debug(`Error occured at extractCustomFields ${err}`);
}
window.clevertap.event.push("Charged", ecomProperties);
} else {
Object.values(properties).map((vals) => {
if (isObject(vals) || isArray(vals)) {
logger.debug("cannot process, unsupported event");
return;
}
});
window.clevertap.event.push(event, properties);
}
} else if (event === "Order Completed") {
window.clevertap.event.push("Charged");
} else {
window.clevertap.event.push(event);
}
}

page(rudderElement) {
logger.debug("in clevertap page");
const { name, properties } = rudderElement.message;
let eventName;
if (properties && properties.category && name) {
Expand All @@ -150,6 +155,12 @@ class Clevertap {
eventName = "Viewed a Page";
}
if (properties) {
Object.values(properties).map((vals) => {
if (isObject(vals) || isArray(vals)) {
logger.debug("cannot process, unsupported event");
return;
}
});
window.clevertap.event.push(eventName, properties);
} else {
window.clevertap.event.push(eventName);
Expand Down
18 changes: 17 additions & 1 deletion utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,20 @@ function getDefinedTraits(message) {
return traitsValue;
}

/**
* To check if a variable is storing object or not
*/
const isObject = (obj) => {
return Object.prototype.toString.call(obj) === '[object Object]';
};

/**
* To check if a variable is storing object or not
*/
const isArray = (obj) => {
return Object.prototype.toString.call(obj) === '[object Array]';
};

export {
replacer,
generateUUID,
Expand All @@ -612,5 +626,7 @@ export {
getReferrer,
getReferringDomain,
extractCustomFields,
getDefinedTraits
getDefinedTraits,
isObject,
isArray
};

0 comments on commit 4d3bfd1

Please sign in to comment.