Skip to content

Commit

Permalink
Merge pull request #8593 from cfpb/tccp-analytics
Browse files Browse the repository at this point in the history
Push tccp filter analytics to the datalayer
  • Loading branch information
wpears authored Sep 25, 2024
2 parents c64c2d0 + 4062c90 commit d257d0c
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion cfgov/unprocessed/apps/tccp/js/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import tippy from 'tippy.js';
import { analyticsSendEvent } from '@cfpb/cfpb-analytics';
import { attach } from '@cfpb/cfpb-design-system/src/index.js';

import orderingDropdown from './ordering';
Expand Down Expand Up @@ -27,7 +28,40 @@ function init() {
// Initialize any tooltips on the page
initializeTooltips();
// Reinitialize tooltips after an htmx request replaces DOM nodes
attach(document, 'htmx:afterSwap', initializeTooltips);
attach(document, 'htmx:afterSwap', initializeAndReport);
}

/**
*
* @param {Event} event - htmx event
*/
function initializeAndReport(event) {
initializeTooltips();
reportFilter(event);
}

/**
*
* @param {Event} event - htmx event
*/
function reportFilter(event) {
let category, value;
const { target } = event.detail.requestConfig.triggeringEvent;
if (target.tagName === 'SELECT') {
category = target.name;
value = target.value;
} else {
category = target
.closest('fieldset')
.firstElementChild.textContent.toLowerCase();
value = target.labels[0].textContent.trim();
}

analyticsSendEvent({
event: 'tccp:card-list-refinement',
category,
value,
});
}

/**
Expand Down

0 comments on commit d257d0c

Please sign in to comment.