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

Sync to DEV: Updating purchase event DL with new requirements #252

Merged
merged 4 commits into from
Oct 23, 2024
Merged
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
63 changes: 42 additions & 21 deletions blocks/thank-you/thank-you.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,11 @@ export default async function decorate() {
const urlParams = new URLSearchParams(window.location.search);
const paymentProcessorId = urlParams.get('PaymentProcessorCustomerId');
const data = await getPaymentCustomerIDFromUUID(paymentProcessorId);
let getOwnerDetails = await getOwner(data.paymentPortalCustomerId);
const currencyValue = isCanada ? CURRENCY_CANADA : CURRENCY_US;
const productTypes = [];
const dlItems = [];
let getOwnerDetails = await getOwner(data.paymentPortalCustomerId);
let totalShipping = 0;

await putUpdateOwnerSaleStatus(getOwnerDetails.id);

Expand All @@ -117,8 +120,9 @@ export default async function decorate() {

const h1 = document.querySelector('h1');
const {
cartFlow, firstName, lastName, nonInsPromoCode,
cartFlow, firstName, lastName,
} = getOwnerDetails;
const isMembershipFlow = cartFlow === 2;
const { petSummaries } = getPurchaseSummaryDetails;
const contentColumn = document.querySelector('.thank-you-purchase .columns > div:nth-child(1) > div');

Expand All @@ -142,6 +146,24 @@ export default async function decorate() {
ul.innerHTML += membershipItem;

contentColumn.appendChild(ul);

// update analyics values if cart flow is membership
if (isMembershipFlow) {
totalShipping += pet.nonInsurancePetSummary.shipping;
productTypes.push(pet.membershipName ?? '');
// push each item object to items array
dlItems.push({
item_name: pet.membershipName ?? '',
currency: currencyValue,
discount: pet.nonInsurancePetSummary?.discount ?? '',
item_category: 'membership', // membership
item_variant: '', // okay to be left empty
price: pet.nonInsurancePetSummary?.amount ?? '',
quantity: pet.nonInsurancePetSummary?.membership?.quantity ?? '1',
microchip_number: pet.microChipNumber ?? '',
product_type: pet.membershipName ?? '',
});
}
});

// build sub-total, tax, and total of items purchased
Expand All @@ -167,25 +189,24 @@ export default async function decorate() {

contentColumn.appendChild(totals);

const trackingData = {
ecommerce: {
microchip_number: petSummaries[0].microChipNumber,
product_type: petSummaries[0].membershipName,
transaction_id: externalTransactionID,
affiliation: '24petwatch',
tax: summary.salesTaxes,
payment_type: paymentMethod,
value: summary.totalDueToday,
currency: currencyValue,
shipping: petSummaries[0].nonInsurancePetSummary.shipping,
coupon: nonInsPromoCode,
flow: cartFlow,
customerid: getOwnerDetails.id,
},
};

// send the GTM event
trackGTMEvent('purchase', trackingData);
if (isMembershipFlow) {
const trackingData = {
ecommerce: {
transaction_id: externalTransactionID,
affiliation: '24petwatch',
value: summary.totalDueToday ?? '',
tax: summary.salesTaxes ?? '',
shipping: totalShipping ? totalShipping.toFixed(2) : '0.00',
currency: currencyValue,
payment_type: paymentMethod,
product_type: productTypes.join(', '),
items: dlItems,
},
};

// send the GTM event
trackGTMEvent('purchase', trackingData);
}

// Salesforce Upsert
async function setUpsertToSalesforce(email) {
Expand Down
4 changes: 2 additions & 2 deletions scripts/24petwatch-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export const MICROCHIP_REGEX = /^([A-Z0-9]{15}|[A-Z0-9]{10}|[A-Z0-9]{9})$/i;
export const POSTAL_CODE_CANADA_REGEX = /^[ABCEGHJ-NPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][ -]?\d[ABCEGHJ-NPRSTV-Z]\d$/i;
export const EMAIL_REGEX = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

export const CURRENCY_CANADA = 'cad';
export const CURRENCY_US = 'usd';
export const CURRENCY_CANADA = 'CAD';
export const CURRENCY_US = 'USD';

// ----- general helpers -----
export function getQueryParam(param, defaultValue = null) {
Expand Down
Loading