diff --git a/blocks/header-paid/header-paid.js b/blocks/header-paid/header-paid.js index 27eaa6c..9d1c6f1 100644 --- a/blocks/header-paid/header-paid.js +++ b/blocks/header-paid/header-paid.js @@ -27,10 +27,18 @@ function instrumentTrackingEvents(header) { } export default async function decorate(block) { + // default paid header let baseHeaderUrl = '/fragments/us/header-paid'; if (isCanada) { baseHeaderUrl = '/fragments/ca/header-paid'; } + // paid blog page header + if (document.body.className.includes('paid-blog-page')) { + baseHeaderUrl = '/fragments/us/header-paid-blog'; + if (isCanada) { + baseHeaderUrl = '/fragments/ca/header-paid-blog'; + } + } const headerPaidContent = await loadFragment(baseHeaderUrl); diff --git a/blocks/plans-quote/form.js b/blocks/plans-quote/form.js index 59a6ca3..465831c 100644 --- a/blocks/plans-quote/form.js +++ b/blocks/plans-quote/form.js @@ -4,6 +4,7 @@ import Loader from './loader.js'; import APIClient from '../../scripts/24petwatch-api.js'; import { COOKIE_NAME_SAVED_OWNER_ID, + SS_KEY_FORM_ENTRY_URL, EMAIL_REGEX, MICROCHIP_REGEX, PET_PLANS_LPM_URL, @@ -691,6 +692,7 @@ export default function formDecoration(block, apiBaseUrl) { // remember the critical information for future steps setCookie(COOKIE_NAME_SAVED_OWNER_ID, formData.ownerId); + sessionStorage.setItem(SS_KEY_FORM_ENTRY_URL, window.location.href); window.location.href = `.${PET_PLANS_SUMMARY_QUOTE_URL}`; // ex: './summary-quote' } diff --git a/blocks/plans-quote/summary-quote.js b/blocks/plans-quote/summary-quote.js index f6fa892..f068f91 100644 --- a/blocks/plans-quote/summary-quote.js +++ b/blocks/plans-quote/summary-quote.js @@ -4,17 +4,21 @@ import Loader from './loader.js'; import formDecoration from './form.js'; import { COOKIE_NAME_SAVED_OWNER_ID, + SS_KEY_FORM_ENTRY_URL, getCookie, getSelectedProductAdditionalInfo, getItemInfoFragment, } from '../../scripts/24petwatch-utils.js'; +import { getConfigValue } from '../../scripts/configs.js'; export default async function decorateSummaryQuote(block, apiBaseUrl) { // initialize form based on results from the previous step const APIClientObj = new APIClient(apiBaseUrl); Loader.addLoader(); + const salesforceProxyEndpoint = await getConfigValue('salesforce-proxy'); const ownerId = getCookie(COOKIE_NAME_SAVED_OWNER_ID); + const entryURL = sessionStorage.getItem(SS_KEY_FORM_ENTRY_URL); let ownerData = []; let petsList = []; @@ -73,8 +77,68 @@ export default async function decorateSummaryQuote(block, apiBaseUrl) { console.log('Failed to get the purchase summary for owner:', ownerData.id, ' status:', status); } } + Loader.hideLoader(); + async function sendDataToSalesforce(owner, products, pets) { + Loader.showLoader(); + if (!owner || !owner.email || !owner.id) { + // eslint-disable-next-line no-console + console.error('invalid owner data'); + } + + if (!products || !products[0] || !products[0].petID) { + // eslint-disable-next-line no-console + console.error('Invalid selected products data'); + } + + if (!pets || !pets[0] || !pets[0].petName || !pets[0].speciesId === undefined) { + // eslint-disable-next-line no-console + console.error('Invalid pets list data'); + } + + if (!entryURL) { + // eslint-disable-next-line no-console + console.error('Invalid entry URL'); + } + + const payload = { + payload: { + Data: { + ContactKey: ownerData.email, + EmailAddress: ownerData.email, + OrderCompleted: false, + OwnerId: ownerData.id, + PetId: selectedProducts[0].petID, + PetName: petsList[0].petName, + SiteURL: entryURL, + Species: petsList[0].speciesId === 1 ? 'Dog' : 'Cat', + }, + EventDefinitionKey: 'APIEvent-6723a35b-b066-640c-1d7b-222f98caa9e1', + ContactKey: ownerData.email, + }, + }; + + const options = { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(payload), + }; + await fetch(salesforceProxyEndpoint, options); + + Loader.hideLoader(); + } + + // Send data for abandoned cart journey + try { + await sendDataToSalesforce(ownerData, selectedProducts, petsList); + } catch (error) { + // eslint-disable-next-line no-console + console.error('There was an error sending the data to Salesforce', error); + } + function getSelectedProduct(petId) { return selectedProducts.find((item) => item.petID === petId); } diff --git a/blocks/thank-you/thank-you.js b/blocks/thank-you/thank-you.js index 37a9d3a..097a1b9 100644 --- a/blocks/thank-you/thank-you.js +++ b/blocks/thank-you/thank-you.js @@ -2,17 +2,22 @@ import APIClient, { getAPIBaseUrl } from '../../scripts/24petwatch-api.js'; import { COOKIE_NAME_SAVED_OWNER_ID, deleteCookie, + SS_KEY_FORM_ENTRY_URL, CURRENCY_CANADA, CURRENCY_US, } from '../../scripts/24petwatch-utils.js'; import { isCanada } from '../../scripts/lib-franklin.js'; import { trackGTMEvent } from '../../scripts/lib-analytics.js'; +import { getConfigValue } from '../../scripts/configs.js'; // prep for API calls const apiBaseUrl = await getAPIBaseUrl(); const APIClientObj = new APIClient(apiBaseUrl); +// salesforce proxy +const salesforceProxyEndpoint = await getConfigValue('salesforce-proxy'); + // Sequence of steps to get the owner info from the API // Step 1 - get the paymentPortalCustomerId from the UUID (aka paymentProcessorId) @@ -89,6 +94,8 @@ async function getTransaction(paymentProcessorId) { export default async function decorate() { // delete cookie with customer id deleteCookie(COOKIE_NAME_SAVED_OWNER_ID); + // unset sessionStorage form entry URL + sessionStorage.removeItem(SS_KEY_FORM_ENTRY_URL); const urlParams = new URLSearchParams(window.location.search); const paymentProcessorId = urlParams.get('PaymentProcessorCustomerId'); @@ -180,6 +187,35 @@ export default async function decorate() { // send the GTM event trackGTMEvent('purchase', trackingData); + // Salesforce Upsert + async function setUpsertToSalesforce(email) { + const payload = { + payload: { + Data: { + OrderCompleted: true, + }, + ContactKey: email, + }, + }; + + const options = { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(payload), + }; + await fetch(salesforceProxyEndpoint, options); + } + + // Send data for abandoned cart journey + try { + await setUpsertToSalesforce(getOwnerDetails.email); + } catch (error) { + // eslint-disable-next-line no-console + console.error('There was an error sending the data to Salesforce', error); + } + const printButton = document.createElement('button'); printButton.classList.add('button'); printButton.innerHTML = 'Print this page'; diff --git a/scripts/24petwatch-utils.js b/scripts/24petwatch-utils.js index 900e45f..48c76b3 100644 --- a/scripts/24petwatch-utils.js +++ b/scripts/24petwatch-utils.js @@ -19,6 +19,9 @@ export function getQueryParam(param, defaultValue = null) { return urlParams.has(param) ? urlParams.get(param) : defaultValue; } +// ----- sessionStorage / localStorage helpers ----- +export const SS_KEY_FORM_ENTRY_URL = 'formEntryURL'; + // ----- cookie helpers ----- export const COOKIE_NAME_FOR_PET_TAGS = 'ph.PetTagQuote'; export const COOKIE_NAME_FOR_PET_PLANS = 'ph.PetPlanQuote';