Skip to content

Commit

Permalink
added util fxn
Browse files Browse the repository at this point in the history
  • Loading branch information
utsabc committed Mar 4, 2021
1 parent cfeb2cc commit 66ee347
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 19 deletions.
42 changes: 24 additions & 18 deletions integrations/Klaviyo/browser.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* eslint-disable class-methods-use-this */
/* eslint-disable no-underscore-dangle */
import get from "get-value";
import logger from "../../utils/logUtil";
import ScriptLoader from "../ScriptLoader";
import { extractCustomFields } from "../../utils/utils";
import { extractCustomFields, getDefinedTraits } from "../../utils/utils";

class Klaviyo {
constructor(config) {
Expand Down Expand Up @@ -56,24 +57,29 @@ class Klaviyo {
logger.error("user traits not present");
return;
}

const {
userId,
email,
phone,
firstName,
lastName,
city,
country,
} = getDefinedTraits(message);

let payload = {
$id: message.userId || message.context.userId || message.anonymousId,
$email: message.context.traits.email,
$phone_number: message.context.traits.phone,
$first_name:
message.context.traits.firstName ||
message.context.traits.firstname ||
message.context.traits.first_name,
$last_name:
message.context.traits.lastName ||
message.context.traits.lastname ||
message.context.traits.last_name,
$organization: message.context.traits.organization,
$title: message.context.traits.title,
$city: message.context.traits.city,
$region: message.context.traits.region,
$country: message.context.traits.country,
$zip: message.context.traits.zip,
$id: userId,
$email: email,
$phone_number: phone,
$first_name: firstName,
$last_name: lastName,
$city: city,
$country: country,
$organization: get(message.context.traits, "organization"),
$title: get(message.context.traits, "title"),
$region: get(message.context.traits, "region"),
$zip: get(message.context.traits, "zip"),
};
if (!payload.$email && !payload.$phone_number && !payload.$id) {
logger.error("user id, phone or email not present");
Expand Down
42 changes: 41 additions & 1 deletion utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,45 @@ function extractCustomFields(message, destination, keys, exclusionFields) {
});
return destination;
}
/**
*
* @param {*} message
*
* Use get-value to retrieve defined trais from message traits
*/
function getDefinedTraits(message) {
const traitsValue = {
userId:
get(message, "userId") ||
get(message.context.traits, "userId") ||
get(message, "anonymousId"),
email:
get(message.context.traits, "email") ||
get(message.context.traits, "Email") ||
get(message.context.traits, "E-mail"),
phone:
get(message.context.traits, "phone") ||
get(message.context.traits, "Phone"),
firstName:
get(message.context.traits, "firstName") ||
get(message.context.traits, "firstname") ||
get(message.context.traits, "first_name"),
lastName:
get(message.context.traits, "lastName") ||
get(message.context.traits, "lastname") ||
get(message.context.traits, "last_name"),
name:
get(message.context.traits, "name") ||
get(message.context.traits, "Name"),
city:
get(message.context.traits, "city") ||
get(message.context.traits, "City"),
country:
get(message.context.traits, "country") ||
get(message.context.traits, "Country"),
};
return traitsValue;
}

export {
replacer,
Expand All @@ -572,5 +611,6 @@ export {
checkReservedKeywords,
getReferrer,
getReferringDomain,
extractCustomFields
extractCustomFields,
getDefinedTraits
};

0 comments on commit 66ee347

Please sign in to comment.