Skip to content

Commit

Permalink
Sync to DEV: Updating purchase event DL with new requirements (#252)
Browse files Browse the repository at this point in the history
* Updating purchase event  DL with new requirements

* Adding condition to only update DL when cart flow is membership

* Adding nullish checks on item values

* changing item_name and product_type values
  • Loading branch information
pgilmore-phi authored Oct 23, 2024
1 parent 50f61d7 commit 2d02414
Showing 1 changed file with 42 additions and 21 deletions.
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

0 comments on commit 2d02414

Please sign in to comment.