Skip to content

Commit

Permalink
feat: introduce navigation and traffic acquisition data in form audit
Browse files Browse the repository at this point in the history
  • Loading branch information
Rahul Kumar Singh authored and Rahul Kumar Singh committed Jan 25, 2025
1 parent 1b25d28 commit a788940
Showing 1 changed file with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function populateFormsInternalNavigation(bundles, formVitals) {
dataChunks.filter = { checkpoint: ['navigate'] };
dataChunks.filtered.forEach((bundle) => {
const forminternalnavigation = bundle.events.find((e) => e.checkpoint === 'navigate');
if (forminternalnavigation
if (forminternalnavigation && formVitals[bundle.url]
&& !formVitals[bundle.url].forminternalnavigation
.some((e) => e.url === forminternalnavigation.source)) {
formVitals[bundle.url].forminternalnavigation.push({
Expand All @@ -67,6 +67,50 @@ function populateFormsInternalNavigation(bundles, formVitals) {
});
}

function findFormCTAForInternalNavigation(bundles, formVitals) {
formVitals.forEach((item) => {
const { url, forminternalnavigation } = item;
if (forminternalnavigation && Array.isArray(forminternalnavigation)) {
forminternalnavigation.forEach((nav) => {
if (nav.url) {
let totalClickOnPage = 0;
const CTAs = new Map();
const clickCheckpointBundles = bundles.filter((bundle) => bundle.url === nav.url && bundle.events.find((e) => e.checkpoint === 'click'));
clickCheckpointBundles.forEach((bundle) => {
totalClickOnPage += bundle.weight;
const clickCheckpoint = bundle.events.find((e) => e.checkpoint === 'click' && e.target === url);

if (clickCheckpoint) {
const { source } = clickCheckpoint;
// Retrieves the existing CTA object if it exists; otherwise,
// initializes a new one with default values.
const existingCTA = CTAs.get(source) || { source, clicks: 0 };
existingCTA.clicks += bundle.weight;
CTAs.set(source, existingCTA);
}
// if (clickCheckpoint) {
// const { source } = clickCheckpoint;
// if (CTAs.has(source)) {
// let { clicks } = CTAs.get(source);
// clicks += bundle.weight;
// CTAs.set(source, { source, clicks });
// } else {
// CTAs.set(source, { source, clicks: bundle.weight });
// }
// }
});

// Convert CTAs Map to an array and store it in the nav object
// eslint-disable-next-line no-param-reassign
nav.CTAs = Array.from(CTAs.values());
// eslint-disable-next-line no-param-reassign
nav.totalClicksOnPage = totalClickOnPage;
}
});
}
});
}

function containsFormVitals(row) {
return METRICS.some((metric) => Object.keys(row[metric]).length > 0);
}
Expand Down Expand Up @@ -106,7 +150,9 @@ function handler(bundles) {
// populate internal navigation data
populateFormsInternalNavigation(bundles, formVitals);
// filter out pages with no form vitals
return Object.values(formVitals).filter(containsFormVitals);
const filteredFormVitals = Object.values(formVitals).filter(containsFormVitals);
findFormCTAForInternalNavigation(bundles, filteredFormVitals);
return filteredFormVitals;
}

export default {
Expand Down

0 comments on commit a788940

Please sign in to comment.