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 all commits
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
19 changes: 9 additions & 10 deletions modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,16 @@ 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 */ : {};
/**
* Maximum number of events. The first call will be made by rum-js,
* leaving 1023 events for the enhancer to track
*/
let maxEvents = 1023;
Copy link
Collaborator Author

@rpapani rpapani Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trieloff Since I'm decrementing this, using maxEvents instead of MAX_EVENTS. If using const is recommended, then I'll do something like

const MAX_EVENTS = 1023;
let maxEvents = MAX_EVENTS;

Please let me know if this way is preferred.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would document why 1023...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we've a byte limit, so adding the documentation may take more. Is it ok to add that in the commit?
@trieloff WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments are removed thus all good.


function trackCheckpoint(checkpoint, data, t) {
const { weight, id } = window.hlx.rum;
if (isSelected) {
if (isSelected && maxEvents) {
maxEvents -= 1;
const sendPing = (pdata = data) => {
// eslint-disable-next-line object-curly-newline, max-len
const body = JSON.stringify({ weight, id, referer: urlSanitizers[window.hlx.RUM_MASK_URL || 'path'](), checkpoint, t, ...data }, KNOWN_PROPERTIES);
Expand Down Expand Up @@ -301,17 +307,10 @@ function mediaMCB(mutations) {
}

function addTrackingFromConfig() {
let lastSource;
let lastTarget;
document.addEventListener('click', (event) => {
const source = sourceSelector(event.target);
const target = targetSelector(event.target);
if (source !== lastSource || target !== lastTarget) {
sampleRUM('click', { target, source });
lastSource = source;
lastTarget = target;
}
sampleRUM('click', { target: targetSelector(event.target), source: sourceSelector(event.target) });
});

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.

20 changes: 11 additions & 9 deletions test/it/click.test.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@
};
// we log what's being sent to the "server"
window.called = [];
// we need to track the number of events that have been sent
window.events = [];
// and navigator.sendBeacon has been replaced with
// a call to fakeSendBeacon
window.fakeSendBeacon = function (url, payload) {
// if payload is a string, we assume it's a JSON string
if (typeof payload === 'string') {
const data = JSON.parse(payload);
window.events.push(data);
if (data.checkpoint === 'click') {
window.called.push(data);
}
} else {
// it's a blob
payload.text().then((text) => {
const data = JSON.parse(text);
window.events.push(data);
if (data.checkpoint === 'click') {
window.called.push(data);
}
Expand Down Expand Up @@ -80,14 +84,6 @@
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();

await new Promise((resolve) => {
setTimeout(resolve, 100);
});

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

const late = document.createElement('a');
late.id = 'alatelink';
late.onclick = (e) => { e.preventDefault(); };
Expand All @@ -98,9 +94,15 @@
await new Promise((resolve) => {
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');
// Test max events
const maxEvents = 1026 - window.events.length;
for (let i = 0; i < maxEvents; i++) {
a.click();
await new Promise((resolve) => setTimeout(resolve, 1));
}
assert(window.events.length === 1024 , '1025th event should not trigger new checkpoint');
});
});
});
Expand Down