Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
akashrpo committed Sep 14, 2021
1 parent 1ef638f commit 5536ca0
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions integrations/ProfitWell/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@
import get from "get-value";
import logger from "../../utils/logUtil";
import Storage from "../../utils/storage";
import { removeUndefinedAndNullValues } from "../utils/commonUtils";
import { generateUUID } from "../../utils/utils";

class ProfitWell {
constructor(config) {
this.publicApiToken = config.publicApiToken;
this.publicApiKey = config.publicApiKey;
this.siteType = config.siteType;
this.name = "ProfitWell";
}

init() {
logger.debug("===In init ProfitWell===");

window.publicApiKey = this.publicApiKey;

const scriptTag = document.createElement("script");
scriptTag.setAttribute("id", "profitwell-js");
scriptTag.setAttribute("data-pw-auth", window.publicApiKey);
document.body.appendChild(scriptTag);

(function (i, s, o, g, r, a, m) {
i[o] =
i[o] ||
Expand All @@ -22,10 +30,7 @@ class ProfitWell {
a = s.createElement(g);
m = s.getElementsByTagName(g)[0];
a.async = 1;
a.src =
r +
"?auth=" +
s.getElementById(o + "-js").getAttribute(this.publicApiToken);
a.src = r + "?auth=" + window.publicApiKey;
m.parentNode.insertBefore(a, m);
})(
window,
Expand All @@ -35,28 +40,27 @@ class ProfitWell {
"https://public.profitwell.com/js/profitwell.js"
);

const cookieData = Storage.getUserTraits();
const userId = generateUUID().toString();
if (this.siteType === "marketing") {
window.profitwell("start", { user_id: userId });
return;
}

let payload = {
user_email: cookieData.email,
user_id: cookieData.userId,
};
const cookieUserId = Storage.getUserId();
const cookieEmail = Storage.getUserTraits().email;

if (!payload.user_email && !payload.user_id) {
if (!cookieUserId && !cookieEmail) {
logger.debug(
"User parameter (email or id) not found in cookie. identify is required"
"User parameter (email or id) not found in cookie. Identify is required"
);
return;
}

if (payload.user_email) {
delete payload.user_id;
if (cookieUserId) {
window.profitwell("start", { user_id: cookieUserId });
} else {
delete payload.user_email;
window.profitwell("start", { user_email: cookieEmail });
}

payload = removeUndefinedAndNullValues(payload);
window.profitwell("start", payload);
}

isLoaded() {
Expand All @@ -75,22 +79,20 @@ class ProfitWell {
const { message } = rudderElement;

let payload = {
user_email: get(message, "context.traits.email"),
user_id: get(message, "context.userId"),
user_id: get(message, "userId"),
};

if (!payload.user_email && !payload.user_id) {
logger.error("User parameter (email or id) is required");
return;
if (!payload.user_id) {
payload = {
user_email: get(message, "context.traits.email"),
};
}

if (payload.user_email) {
delete payload.user_id;
} else {
delete payload.user_email;
if (!payload.user_id && !payload.user_email) {
logger.error("User parameter (email or id) is required");
return;
}

payload = removeUndefinedAndNullValues(payload);
window.profitwell("start", payload);
}
}
Expand Down

0 comments on commit 5536ca0

Please sign in to comment.