Skip to content

Commit

Permalink
modify few conditions and refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
akashrpo committed Aug 5, 2021
1 parent 3f36a5c commit c96ce03
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 22 deletions.
45 changes: 24 additions & 21 deletions integrations/Drip/browser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import get from "get-value";
import logger from "../../utils/logUtil";
import { removeUndefinedAndNullValues } from "../utils/commonUtils";
import { getDestinationExternalID } from "./utils";

class Drip {
constructor(config) {
Expand All @@ -26,36 +27,51 @@ class Drip {
})();
}

isLoaded() {
logger.debug("===In isLoaded Drip===");
return !!(window._dcq && window._dcq.push !== Array.prototype.push);
}

isReady() {
logger.debug("===In isReady Drip===");
return !!(window._dcq && window._dcq.push !== Array.prototype.push);
}

identify(rudderElement) {
logger.debug("===In Drip identify===");

const { message } = rudderElement;
if (!message.context) {
logger.error("user context not present");
return;
}

if (!message.context.traits) {
logger.error("user traits not present");
return;
}

const { email } = message.context.traits;
const email = get(message, "context.traits.email");
if (!email) {
logger.error("email is required for the call");
logger.error("email is required for identify");
return;
}

const { euConsent } = message.context.traits;
const euConsent = get(message, "context.traits.euConsent");
if (
euConsent &&
!(
euConsent.toLowercase() === "granted" ||
euConsent.toLowercase() === "denied"
)
) {
euConsent = "";
euConsent = null;
}

let payload = {
email: email,
new_email: get(message, "context.traits.newEmail"),
user_id: get(message, "userId"),
user_id: get(message, "userId") || get(message, "anonymousId"),
tags: get(message, "context.traits.tags"),
remove_tags: get(message, "context.traits.removeTags"),
prospect: get(message, "context.traits.prospect"),
Expand All @@ -68,11 +84,10 @@ class Drip {
},
};
payload = removeUndefinedAndNullValues(payload);
window._dcq.push(["identify", payload]);

const campaignId =
get(message, "context.traits.campaignId") || this.campaignId;

window._dcq.push(["identify", payload]);
getDestinationExternalID(message, "dripCampaignId") || this.campaignId;

if (campaignId) {
const fields = get(message, "context.traits");
Expand Down Expand Up @@ -125,7 +140,6 @@ class Drip {
};

payload = removeUndefinedAndNullValues(payload);

window._dcq.push(["track", "Viewed a Product", payload]);
} else {
payload = {
Expand All @@ -142,20 +156,9 @@ class Drip {
};

payload = removeUndefinedAndNullValues(payload);

window._dcq.push(["track", event, payload]);
}
}

isLoaded() {
logger.debug("===In isLoaded Drip===");
return !!(window._dcq && window._dcq.push !== Array.prototype.push);
}

isReady() {
logger.debug("===In isReady Drip===");
return !!(window._dcq && window._dcq.push !== Array.prototype.push);
}
}

export { Drip };
export default Drip;
2 changes: 1 addition & 1 deletion integrations/Drip/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { Drip } from "./browser";
import Drip from "./browser";

export default Drip;
29 changes: 29 additions & 0 deletions integrations/Drip/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// External ID format
// {
// "context": {
// "externalId": [
// {
// "type": "kustomerId",
// "id": "12345678"
// }
// ]
// }
// }
// to get destination specific external id passed in context.
function getDestinationExternalID(message, type) {
let externalIdArray = null;
let destinationExternalId = null;
if (message.context && message.context.externalId) {
externalIdArray = message.context.externalId;
}
if (externalIdArray) {
externalIdArray.forEach((extIdObj) => {
if (extIdObj.type === type) {
destinationExternalId = extIdObj.id;
}
});
}
return destinationExternalId;
}

export { getDestinationExternalID };

0 comments on commit c96ce03

Please sign in to comment.