From 6b8c8e0414b7cdda06f8e4c0d755d04f7dd5db7d Mon Sep 17 00:00:00 2001 From: pgilmore-phi Date: Thu, 9 Jan 2025 16:01:28 -0500 Subject: [PATCH] PM-806: Add DL fallback for cta_locations --- blocks/header/header.js | 1 + scripts/scripts.js | 46 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/blocks/header/header.js b/blocks/header/header.js index 4827f78..7909aa8 100644 --- a/blocks/header/header.js +++ b/blocks/header/header.js @@ -299,6 +299,7 @@ function instrumentTrackingEvents(header) { // track cta clicks on header if (e.target.classList.contains('button')) { trackGTMEvent('cta_click', { + cta_location: 'header_cta', link_text: linkText, link_url: linkUrl, }); diff --git a/scripts/scripts.js b/scripts/scripts.js index ffe0367..36b54a0 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -335,17 +335,53 @@ function instrumentTrackingEvents(main) { ctaLocation = 'body_1_cta'; } else if (e.target.closest('.callout-get-a-quote2')) { ctaLocation = 'body_2_cta'; + } else { + const containerBlock = e.target.closest('[data-block-name]'); + // if containerBlock is null, fallback to sections + if (containerBlock) { + const blockList = document.querySelectorAll(`[data-block-name=${containerBlock.dataset.blockName}]`); + blockList.forEach((block, key) => { + if (block === containerBlock) { + ctaLocation = `${containerBlock.dataset.blockName}_${key}`; + } + }); + } else { + // check for the closest section + const parentSection = e.target.closest('.section'); + if (parentSection) { + const sectionList = document.querySelectorAll('.section'); + sectionList.forEach((section, key) => { + if (section === parentSection) { + ctaLocation = `section_${key}`; + } + }); + } + } } trackCTAEvent(ctaLocation); return; } else { const containerBlock = e.target.closest('[data-block-name]'); - const blockList = document.querySelectorAll(`[data-block-name=${containerBlock.dataset.blockName}]`); - blockList.forEach((block, key) => { - if (block === containerBlock) { - ctaLocation = `${containerBlock.dataset.blockName}_${key}`; + // if containerBlock is null, fallback to sections + if (containerBlock) { + const blockList = document.querySelectorAll(`[data-block-name=${containerBlock.dataset.blockName}]`); + blockList.forEach((block, key) => { + if (block === containerBlock) { + ctaLocation = `${containerBlock.dataset.blockName}_${key}`; + } + }); + } else { + // check for the closest section + const parentSection = e.target.closest('.section'); + if (parentSection) { + const sectionList = document.querySelectorAll('.section'); + sectionList.forEach((section, key) => { + if (section === parentSection) { + ctaLocation = `section_${key}`; + } + }); } - }); + } trackCTAEvent(ctaLocation); return; }