Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updating terminology to events instead of emails #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .idea/SFCC_Klaviyo.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ var productMgr = require('dw/catalog/ProductMgr');
var imageSize = Site.getCurrent().getCustomPreferenceValue('klaviyo_image_size') || null;

/**
* Sends an order to Klaviyo with the order email type.
* Sends an order to Klaviyo with the order event type.
*
* @param order
* @param mailType
* @returns
*/
function sendOrderEmail(order, mailType) {
var logger = Logger.getLogger('Klaviyo', 'emailUtils - sendOrderEmail()');
function sendOrderEvent(order, mailType) {
var logger = Logger.getLogger('Klaviyo', 'emailUtils - sendOrderEvent()');
try {
var isFutureOrder = (mailType == 'Auto Delivery Order Confirmation');
var orderPayload = prepareOrderPayload(order, isFutureOrder, mailType);
require('*/cartridge/scripts/utils/klaviyo/klaviyoUtils').sendEmail(order.getCustomerEmail(), orderPayload, mailType);
require('*/cartridge/scripts/utils/klaviyo/klaviyoUtils').sendEvent(order.getCustomerEmail(), orderPayload, mailType);
} catch (e) {
logger.error('sendOrderEmail() failed for order: ' + order.getOrderNo() + ', mailType: ' + mailType + '. Error: ' + e.message);
logger.error('sendOrderEvent() failed for order: ' + order.getOrderNo() + ', mailType: ' + mailType + '. Error: ' + e.message);
}
}


/**
* Prepares the order in JSON format for email send.
* Prepares the order in JSON format for event send.
* @param order
* @param isFutureOrder
* @param mailType
Expand Down Expand Up @@ -372,6 +372,6 @@ function prepareOrderPayload(order, isFutureOrder, mailType) {
}

module.exports = {
sendOrderEmail : sendOrderEmail,
sendOrderEvent : sendOrderEvent,
prepareOrderPayload : prepareOrderPayload
};
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ var WHITELISTED_EVENTS = ['Searched Site', 'Viewed Product', 'Viewed Category',
* @param event
* @returns
*/
function sendEmail(email, data, event) {
function sendEvent(email, data, event) {
var requestBody = {};
var resultObj = {};

var logger = Logger.getLogger('Klaviyo', 'klaviyoUtils - sendEmail()');
var logger = Logger.getLogger('Klaviyo', 'klaviyoUtils - sendEvent()');

if (KlaviyoTrackService == null || empty(email)) {
logger.error('sendEmail() failed for email: ' + obfuscateKlEmail(email) + '. Service Connection for send email via Klaviyo returned null.');
logger.error('sendEvent() failed for email: ' + obfuscateKlEmail(email) + '. Service Connection for send event to Klaviyo returned null.');
return;
}

Expand All @@ -41,19 +41,19 @@ function sendEmail(email, data, event) {
var result = KlaviyoTrackService.call(requestBody);

if (result == null) {
logger.error('Result for send email via Klaviyo returned null.');
logger.error('Result for send event to Klaviyo returned null.');
return;
}

resultObj = JSON.parse(result.object);

if (resultObj == 1) {
logger.info('Send email via Klaviyo is successful.');
logger.info('Send event to Klaviyo is successful.');
} else {
logger.error('Send email via Klaviyo failed.');
}

return resultObj;
logger.error('Send event to Klaviyo failed.');
}
return resultObj;
}


Expand All @@ -66,7 +66,7 @@ function sendEmail(email, data, event) {
* @param event
* @returns
*/
function preparePayload(email, data, event) {
function preparePayload(email, data, event) {
var jsonData = {};
jsonData.token = Site.getCurrent().getCustomPreferenceValue('klaviyo_account');
jsonData.event = event;
Expand All @@ -81,7 +81,7 @@ function preparePayload(email, data, event) {

var klaviyoData = JSON.stringify(jsonData);

return StringUtils.encodeBase64(klaviyoData);
return StringUtils.encodeBase64(klaviyoData);
}


Expand Down Expand Up @@ -126,7 +126,7 @@ function prepareProductObj(lineItem, basketProduct, currentProductID) {
productObj['Product UPC'] = basketProduct.UPC;
productObj['Product Availability Model'] = basketProduct.availabilityModel.availability;
productObj.Categories = createCategories(basketProduct);
return productObj;
return productObj;
}

/**
Expand All @@ -152,8 +152,8 @@ function prepareViewedProductEventData(pageProductID, viewedProduct) {
klData.viewedProductPageURL = require('dw/web/URLUtils').https('Product-Show', 'pid', pageProductID).toString();
klData.viewedProductUPC = viewedProduct.UPC;
klData.viewedProductCategories = createCategories(viewedProduct);
klData.viewedProductPrimaryCategory = !empty(viewedProduct.getPrimaryCategory()) ? viewedProduct.getPrimaryCategory().displayName : '';
return klData;
klData.viewedProductPrimaryCategory = !empty(viewedProduct.getPrimaryCategory()) ? viewedProduct.getPrimaryCategory().displayName : '';
return klData;
}

/**
Expand All @@ -162,8 +162,8 @@ function prepareViewedProductEventData(pageProductID, viewedProduct) {
* @param product
* @returns categories
*/
function createCategories(product) {
var productCategoryIndex,
function createCategories(product) {
var productCategoryIndex,
currentCategory;
var arrayOfCategories = [];

Expand Down Expand Up @@ -206,7 +206,7 @@ function removeDuplicates(items) {
* @returns datalayer
*/
function prepareCheckoutEventForKlaviyo(currentBasket) {
try {
try {
var klData = {};
var basketItems = currentBasket.getProductLineItems().toArray();
// Create some top-level event data
Expand Down Expand Up @@ -234,10 +234,10 @@ function prepareCheckoutEventForKlaviyo(currentBasket) {
klData.Items.push(klData.line_items[itemIndex]['Product Name']);
}
}
} catch (e) {
} catch (e) {
klData.data.debug_error = [e.message, e.lineNumber];
}
return klData;
return klData;
}

/**
Expand All @@ -253,7 +253,7 @@ function prepareOrderConfirmationEventForKlaviyo(currentOrder) {
// site specific order object */
var emailUtils = require('*/cartridge/scripts/utils/klaviyo/emailUtils');
var dwareOrder = emailUtils.prepareOrderPayload(currentOrder, false, 'orderConfirmation');
sendEmail(currentOrder.getCustomerEmail(), dwareOrder, 'Order Confirmation');
sendEvent(currentOrder.getCustomerEmail(), dwareOrder, 'Order Confirmation');

// giftcards
var giftCertCollection = currentOrder.getGiftCertificateLineItems().toArray();
Expand All @@ -267,13 +267,13 @@ function prepareOrderConfirmationEventForKlaviyo(currentOrder) {
var giftCard = giftCertCollection[giftCertIndex];
var giftCardObj = {};
giftCardObj = preparegiftCardObject(giftCard);
orderGiftCards.push(giftCardObj);
orderGiftCards.push(giftCardObj);
}

// send an event for transactional gift certificate emails
for (var totalOrderGiftCards = 0; totalOrderGiftCards < orderGiftCards.length; totalOrderGiftCards++) {
var theGiftCard = orderGiftCards[totalOrderGiftCards];
sendEmail(theGiftCard['Recipient Email'], theGiftCard, 'e-Giftcard Notification');
sendEvent(theGiftCard['Recipient Email'], theGiftCard, 'e-Giftcard Notification');
}
} catch (e) {
logger.debug('prepareOrderConfirmationEventForKlaviyo -- error ' + e.message + ' at ' + e.lineNumber);
Expand Down Expand Up @@ -350,7 +350,7 @@ function sendShipmentConfirmation(orderID) {
var order = orderList[i];
try {
var emailUtils = require('*/cartridge/scripts/utils/klaviyo/emailUtils');
emailUtils.sendOrderEmail(order, 'Shipping Confirmation');
emailUtils.sendOrderEvent(order, 'Shipping Confirmation');
sendStatus = true;
} catch (e) {
logger.error('resendKlaviyoShipmentEmailsJob failed for order: ' + order.getOrderNo() + '. Error: ' + e.message);
Expand All @@ -365,10 +365,10 @@ function sendShipmentConfirmation(orderID) {
*/
var buildDataLayer = function () {
var klData = {};
var pageContext,
currentBasket,
basketHasLength,
currentOrder,
var pageContext,
currentBasket,
basketHasLength,
currentOrder,
viewedProduct,
httpParameterMap,
pageProductID,
Expand All @@ -388,7 +388,7 @@ var buildDataLayer = function () {
searchTerm = httpParameterMap.searchterm.value;
pageCategoryId = httpParameterMap.pagecgid.value;

try {
try {
// Checkout Started event

if (pageContext == 'checkout') {
Expand Down Expand Up @@ -424,7 +424,7 @@ var buildDataLayer = function () {
klData.event = 'Searched Site';
klData.searchTerm = searchTerm;
klData.searchResultsCount = (!empty(searchResultsCount)) ? searchResultsCount.value : 0;
}
}
} catch (e) {
klData.data.debug_error = [e.message, e.lineNumber];
}
Expand All @@ -436,7 +436,7 @@ var buildDataLayer = function () {
*/
var buildCartDataLayer = function () {
var klData = {};
var isValidBasket,
var isValidBasket,
basketHasLength;

isValidBasket = (basketMgr.getCurrentBasket());
Expand Down Expand Up @@ -477,7 +477,7 @@ var trackAddToCart = function () {
email = currentUser.email;
}
var event = 'Add To Cart';
sendEmail(email, klaviyoDataLayer, event);
sendEvent(email, klaviyoDataLayer, event);
};

/**
Expand All @@ -499,7 +499,7 @@ function obfuscateKlEmail(email) {
}

module.exports = {
sendEmail : sendEmail,
sendEvent : sendEvent,
preparegiftCardObject : preparegiftCardObject,
prepareViewedProductEventData : prepareViewedProductEventData,
prepareProductObj : prepareProductObj,
Expand Down
4 changes: 2 additions & 2 deletions test/mocks/EmailUtils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

function sendOrderEmail(order, confirmationType) {
function sendOrderEvent(order, confirmationType) {
return 1;
}

module.exports = {
sendOrderEmail: sendOrderEmail
sendOrderEvent: sendOrderEvent
}
4 changes: 2 additions & 2 deletions test/unit/KlaviyoUtils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ describe('klaviyoUtils.js script', function() {
$email: '[email protected]'
}
};
describe('sendEmail function', function() {
describe('sendEvent function', function() {
it('should return a 1 if track call succeeds', function() {
var trackCallResult = klaviyoUtilsFile.sendEmail(payloadObj.customer_properties.$email, {}, 'Event Name');
var trackCallResult = klaviyoUtilsFile.sendEvent(payloadObj.customer_properties.$email, {}, 'Event Name');
expect(trackCallResult).to.equal(1);
});
});
Expand Down