Skip to content

Commit

Permalink
[MWPW-148000] [ANALYTICS] Add analytics to Modals (#3130)
Browse files Browse the repository at this point in the history
* Initial checkin. Working state.

* PR updates

* Reverting NaN check.

* Update isNan to isInteger

* Import fix.

* Remove duplicate call.

* Remove blank line.

* Update libs/blocks/modal/modal.js

---------

Co-authored-by: Vivian A Goodrich <101133187+vgoodric@users.noreply.github.com>
  • Loading branch information
AdobeLinhart and vgoodric authored Nov 6, 2024
1 parent fb85f29 commit 2407bf6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
3 changes: 2 additions & 1 deletion libs/blocks/modal/modal.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-underscore-dangle */
/* eslint-disable import/no-cycle */
import { createTag, getMetadata, localizeLink, loadStyle, getConfig } from '../../utils/utils.js';
import { decorateSectionAnalytics } from '../../martech/attributes.js';

const FOCUSABLES = 'a:not(.hide-video), button, input, textarea, select, details, [tabindex]:not([tabindex="-1"]';
const CLOSE_ICON = `<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20">
Expand Down Expand Up @@ -179,7 +180,7 @@ export async function getModal(details, custom) {
closeModal(dialog);
}
});

decorateSectionAnalytics(dialog, `${id}-modal`, getConfig());
dialog.append(close);
document.body.append(dialog);
dialogLoadingSet.delete(id);
Expand Down
3 changes: 2 additions & 1 deletion libs/martech/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ export function decorateDefaultLinkAnalytics(block, config) {
}

export async function decorateSectionAnalytics(section, idx, config) {
const id = Number.isInteger(idx) ? `s${idx + 1}` : idx;
document.querySelector('main')?.setAttribute('daa-im', 'true');
section.setAttribute('daa-lh', `s${idx + 1}`);
section.setAttribute('daa-lh', id);
section.querySelectorAll('[data-block] [data-block]').forEach((block) => {
block.removeAttribute('data-block');
});
Expand Down
7 changes: 6 additions & 1 deletion test/blocks/modals/modals.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ describe('Modals', () => {
window.location.hash = '#milo';
await waitForElement('#milo');
init(document.getElementById('milo-modal-link'));
expect(document.getElementById('milo')).to.exist;
const modal = document.getElementById('milo');
expect(modal).to.exist;
expect(modal.getAttribute('daa-lh')).to.equal('milo-modal');
const buttons = modal.querySelectorAll('button');
expect(buttons[0].getAttribute('daa-ll')).to.equal('Milo Button 1-1--Milo');
expect(buttons[1].getAttribute('daa-ll')).to.equal('Milo Button 2-2--Milo');
window.location.hash = '';
await waitForRemoval('#milo');
expect(document.getElementById('milo')).not.to.exist;
Expand Down

0 comments on commit 2407bf6

Please sign in to comment.