Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Implement flood control with 1024 max events #359

Merged
merged 4 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const blocksMO = window.MutationObserver ? new MutationObserver(blocksMCB)
// eslint-disable-next-line no-use-before-define, max-len
const mediaMO = window.MutationObserver ? new MutationObserver(mediaMCB)
/* c8 ignore next */ : {};
// Collecting at least 10 clicks to identify blind clicks efficiently
const MAX_CLICKS_PER_SELECTOR = 10;

function trackCheckpoint(checkpoint, data, t) {
const { weight, id } = window.hlx.rum;
Expand Down Expand Up @@ -301,17 +303,19 @@ function mediaMCB(mutations) {
}

function addTrackingFromConfig() {
let lastSource;
let lastTarget;
const clickCounts = new Map();

document.addEventListener('click', (event) => {
const source = sourceSelector(event.target);
const target = targetSelector(event.target);
if (source !== lastSource || target !== lastTarget) {
const key = `${source}|${target}`;
const count = (clickCounts.get(key) || 0) + 1;
if (count <= MAX_CLICKS_PER_SELECTOR) {
sampleRUM('click', { target, source });
lastSource = source;
lastTarget = target;
clickCounts.set(key, count);
}
});

addCWVTracking();
addFormTracking(window.document.body);
addNavigationTracking();
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions test/it/click.test.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,15 @@
assert(window.called.length === 3 , 'checkpoint for click on link missing');
assert(window.called[2].source === 'a#alink', 'invalid target for alink checkpoint');

a.click();
// Test multiple clicks on link
for (let i = 0; i < 11; i++) {
a.click();
await new Promise((resolve) => setTimeout(resolve, 10));
}

await new Promise((resolve) => {
setTimeout(resolve, 100);
});
console.log(`window.called: ${window.called.length}`);

assert(window.called.length === 3 , '2 consecutive clicks on link should not trigger new checkpoint');
assert(window.called.length === 12 , '11th consecutive clicks on link should not trigger new checkpoint');

const late = document.createElement('a');
late.id = 'alatelink';
Expand All @@ -99,8 +101,8 @@
setTimeout(resolve, 100);
});

assert(window.called.length === 4 , 'checkpoint for click on late added link missing');
assert(window.called[3].source === 'a#alatelink', 'invalid target for alatelink checkpoint');
assert(window.called.length === 13 , 'checkpoint for click on late added link missing');
assert(window.called[12].source === 'a#alatelink', 'invalid target for alatelink checkpoint');
});
});
});
Expand Down