Skip to content

Commit

Permalink
Merge pull request #10 from stape-io/reformat-with-prettier
Browse files Browse the repository at this point in the history
reformat with prettier
  • Loading branch information
Bukashk0zzz authored Mar 1, 2024
2 parents 2714f99 + fac9251 commit 558c1dd
Show file tree
Hide file tree
Showing 3 changed files with 260 additions and 241 deletions.
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"singleQuote": true
"singleQuote": true,
"trailingComma": "none"
}
249 changes: 129 additions & 120 deletions template.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const eventPropertiesToIgnore = [
'user_agent',
'x-ga-js_client_id',
'screen_resolution',
'x-ga-mp2-user_properties',
'x-ga-mp2-user_properties'
];

const isLoggingEnabled = determinateIsLoggingEnabled();
Expand All @@ -51,22 +51,22 @@ function sendEvent() {
let eventNameLogs = data.type === 'active_on_site' ? 'page_view' : data.event;

let klaviyoEventData = {
'data': {
'type': 'event',
'attributes': {
'properties': {},
'metric': {
'data': {
'type': 'metric',
'attributes': {
'name': eventName
data: {
type: 'event',
attributes: {
properties: {},
metric: {
data: {
type: 'metric',
attributes: {
name: eventName
}
}
},
'profile': {
'data': {
'type': 'profile',
'attributes': {}
profile: {
data: {
type: 'profile',
attributes: {}
}
}
}
Expand All @@ -77,64 +77,67 @@ function sendEvent() {
else if (eventData.value) klaviyoEventData.data.attributes.value = data.value;

if (data.uniqueId) klaviyoEventData.data.attributes.unique_id = data.uniqueId;
else if (eventData.unique_id) klaviyoEventData.data.attributes.unique_id = data.unique_id;
else if (eventData.unique_id)
klaviyoEventData.data.attributes.unique_id = data.unique_id;

klaviyoEventData.data.attributes.properties = getProperties();
addViewedItemsIfNeeded(eventName, klaviyoEventData);

klaviyoEventData.data.attributes.profile.data.attributes = getCustomerProperties();
if (data.klaviyoUserId) klaviyoEventData.data.attributes.profile.data.id = data.klaviyoUserId;
klaviyoEventData.data.attributes.profile.data.attributes =
getCustomerProperties();
if (data.klaviyoUserId)
klaviyoEventData.data.attributes.profile.data.id = data.klaviyoUserId;

let url = 'https://a.klaviyo.com/api/events/';

if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'Klaviyo',
Type: 'Request',
TraceId: traceId,
EventName: eventNameLogs,
RequestMethod: 'POST',
RequestUrl: url,
RequestBody: klaviyoEventData
})
);
}

sendHttpRequest(
url,
(statusCode, headers, body) => {
logToConsole(
JSON.stringify({
Name: 'Klaviyo',
Type: 'Request',
Type: 'Response',
TraceId: traceId,
EventName: eventNameLogs,
RequestMethod: 'POST',
RequestUrl: url,
RequestBody: klaviyoEventData,
ResponseStatusCode: statusCode,
ResponseHeaders: headers,
ResponseBody: body
})
);
}
);

sendHttpRequest(
url,
(statusCode, headers, body) => {
logToConsole(
JSON.stringify({
Name: 'Klaviyo',
Type: 'Response',
TraceId: traceId,
EventName: eventNameLogs,
ResponseStatusCode: statusCode,
ResponseHeaders: headers,
ResponseBody: body,
})
);

if (!data.useOptimisticScenario) {
if (statusCode >= 200 && statusCode < 300) {
data.gtmOnSuccess();
} else {
data.gtmOnFailure();
}
if (!data.useOptimisticScenario) {
if (statusCode >= 200 && statusCode < 300) {
data.gtmOnSuccess();
} else {
data.gtmOnFailure();
}
}
},
{
headers: {
'X-Forwarded-For': eventData.ip_override || getRemoteAddress(),
'Content-Type': 'application/json',
Accept: 'application/json',
Revision: klaviyoApiRevision,
Authorization: 'Klaviyo-API-Key ' + data.apiKey
},
{
headers: {
'X-Forwarded-For': eventData.ip_override || getRemoteAddress(),
'Content-Type': 'application/json',
'Accept': 'application/json',
'Revision': klaviyoApiRevision,
'Authorization': 'Klaviyo-API-Key ' + data.apiKey,
},
method: 'POST'
},
JSON.stringify(klaviyoEventData)
method: 'POST'
},
JSON.stringify(klaviyoEventData)
);

if (data.useOptimisticScenario) {
Expand All @@ -147,7 +150,8 @@ function getProperties() {

if (data.forwardAllProperties) {
let excludeKeys = [];
if (data.excludeForwardingProperties) excludeKeys = data.excludeForwardingProperties.map((n) => n.name);
if (data.excludeForwardingProperties)
excludeKeys = data.excludeForwardingProperties.map((n) => n.name);

for (let key in eventData) {
const shouldIgnore = hasItem(eventPropertiesToIgnore, key);
Expand Down Expand Up @@ -179,7 +183,7 @@ function getProperties() {

function getCustomerProperties() {
let customerProperties = {
'properties': {},
properties: {}
};

if (data.email) customerProperties.email = data.email;
Expand All @@ -198,25 +202,29 @@ function getCustomerProperties() {
if (kxCookie.length) customerProperties['_kx'] = kxCookie[0];
}

if (eventData.page_referrer) customerProperties.properties['$last_referrer'] = eventData.page_referrer;
if (eventData.page_referrer)
customerProperties.properties['$last_referrer'] = eventData.page_referrer;

if (data.customerStandardProperties) {
for (let key in data.customerStandardProperties) {
customerProperties[data.customerStandardProperties[key].name] = data.customerStandardProperties[key].value;
customerProperties[data.customerStandardProperties[key].name] =
data.customerStandardProperties[key].value;
}
}

if (data.customerLocationProperties) {
customerProperties.location = {};

for (let key in data.customerLocationProperties) {
customerProperties.location[data.customerLocationProperties[key].name] = data.customerLocationProperties[key].value;
customerProperties.location[data.customerLocationProperties[key].name] =
data.customerLocationProperties[key].value;
}
}

if (data.customerProperties) {
for (let key in data.customerProperties) {
customerProperties.properties[data.customerProperties[key].name] = data.customerProperties[key].value;
customerProperties.properties[data.customerProperties[key].name] =
data.customerProperties[key].value;
}
}

Expand All @@ -238,7 +246,7 @@ function storeCookie(name, value) {
samesite: 'Lax',
secure: true,
'max-age': 63072000, // 2 years
httpOnly: false,
httpOnly: false
});
}

Expand Down Expand Up @@ -266,8 +274,8 @@ function getViewedItems() {
function updateViewedItems(viewedItems) {
for (let key in viewedItems) {
if (
viewedItems[key].ItemId &&
makeString(viewedItems[key].ItemId) === eventData.ItemId
viewedItems[key].ItemId &&
makeString(viewedItems[key].ItemId) === eventData.ItemId
) {
viewedItems[key].Views = makeInteger(viewedItems[key].Views) + 1;

Expand All @@ -284,10 +292,10 @@ function updateViewedItems(viewedItems) {
Metadata: {
Brand: eventData.Brand || eventData.brand,
Price: eventData.Price || eventData.price,
CompareAtPrice: eventData.CompareAtPrice,
CompareAtPrice: eventData.CompareAtPrice
},
Views: 1,
LastViewedDate: makeInteger(getTimestampMillis() / 1000),
LastViewedDate: makeInteger(getTimestampMillis() / 1000)
});

return viewedItems;
Expand All @@ -296,8 +304,8 @@ function updateViewedItems(viewedItems) {
function determinateIsLoggingEnabled() {
const containerVersion = getContainerVersion();
const isDebug = !!(
containerVersion &&
(containerVersion.debugMode || containerVersion.previewMode)
containerVersion &&
(containerVersion.debugMode || containerVersion.previewMode)
);

if (!data.logType) {
Expand All @@ -319,25 +327,25 @@ function addToList() {
let url = 'https://a.klaviyo.com/api/profile-subscription-bulk-create-jobs/';

let addToListData = {
'data': {
'type': 'profile-subscription-bulk-create-job',
'attributes': {
'profiles': {
'data': [
data: {
type: 'profile-subscription-bulk-create-job',
attributes: {
profiles: {
data: [
{
'type': 'profile',
'attributes': {
'email': data.email
type: 'profile',
attributes: {
email: data.email
}
}
]
}
},
'relationships': {
'list': {
'data': {
'type': 'list',
'id': data.listId
relationships: {
list: {
data: {
type: 'list',
id: data.listId
}
}
}
Expand All @@ -346,52 +354,52 @@ function addToList() {

if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'Klaviyo',
Type: 'Request',
TraceId: traceId,
EventName: 'add_to_list',
RequestMethod: 'POST',
RequestUrl: url,
RequestBody: addToListData
})
);
}

sendHttpRequest(
url,
(statusCode, headers, body) => {
logToConsole(
JSON.stringify({
Name: 'Klaviyo',
Type: 'Request',
Type: 'Response',
TraceId: traceId,
EventName: 'add_to_list',
RequestMethod: 'POST',
RequestUrl: url,
RequestBody: addToListData,
ResponseStatusCode: statusCode,
ResponseHeaders: headers,
ResponseBody: body
})
);
}
);

sendHttpRequest(
url,
(statusCode, headers, body) => {
logToConsole(
JSON.stringify({
Name: 'Klaviyo',
Type: 'Response',
TraceId: traceId,
EventName: 'add_to_list',
ResponseStatusCode: statusCode,
ResponseHeaders: headers,
ResponseBody: body,
})
);

if (!data.useOptimisticScenario) {
if (statusCode >= 200 && statusCode < 300) {
data.gtmOnSuccess();
} else {
data.gtmOnFailure();
}
if (!data.useOptimisticScenario) {
if (statusCode >= 200 && statusCode < 300) {
data.gtmOnSuccess();
} else {
data.gtmOnFailure();
}
}
},
{
headers: {
'X-Forwarded-For': getRemoteAddress(),
'Content-Type': 'application/json',
Accept: 'application/json',
Revision: klaviyoApiRevision,
Authorization: 'Klaviyo-API-Key ' + data.apiKey
},
{
headers: {
'X-Forwarded-For': getRemoteAddress(),
'Content-Type': 'application/json',
'Accept': 'application/json',
'Revision': klaviyoApiRevision,
'Authorization': 'Klaviyo-API-Key ' + data.apiKey,
},
method: 'POST',
},
JSON.stringify(addToListData)
method: 'POST'
},
JSON.stringify(addToListData)
);

if (data.useOptimisticScenario) {
Expand All @@ -412,7 +420,8 @@ function addViewedItemsIfNeeded(eventName, klaviyoEventData) {
let viewedItems = getViewedItems();

if (viewedItems.length > 0) {
klaviyoEventData.data.attributes.properties['$viewed_items'] = getViewedItems();
klaviyoEventData.data.attributes.properties['$viewed_items'] =
getViewedItems();
}
}
}
Loading

0 comments on commit 558c1dd

Please sign in to comment.