Skip to content

Commit

Permalink
Add test to compare sendBeacon and fetch numbers for users with no ad…
Browse files Browse the repository at this point in the history
… blocker (#1416)

* Add test to compare sendBeacon and fetch numbers for users with no adblocker

* Tidy label

* Changeset
  • Loading branch information
emma-imber committed Jun 17, 2024
1 parent 500a157 commit 9e47081
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-pens-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@guardian/commercial': patch
---

Add sendBeacon test to messenger code
32 changes: 32 additions & 0 deletions src/core/messenger/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,38 @@ const setupBackground = async (
specs.scrollType,
);

/* We're sending both sendBeacon and fetch messages here to test if we receive the same numbers of each.
Our hypothesis is that sendBeacon is less reliable because it gets blocked by some ad blockers. This messenger
code only fires if a template ad (eg fabric or interscroller) is present on the page and sends a message. This
means we can control for ad blockers, as there should be no ad blocker present if we reach this code. So if
our hypothesis is true, we should observe parity in the number of messages received using each method */

const endpoint = window.guardian.config.page.isDev
? '//logs.code.dev-guardianapis.com/log'
: '//logs.guardianapis.com/log';

const shouldTestBeacon = Math.random() <= 10 / 100;

if (shouldTestBeacon) {
const beaconEvent = {
label: 'commercial.test_send_beacon',
properties: [{ name: 'userAgent', value: navigator.userAgent }],
};

const fetchEvent = {
label: 'commercial.test_fetch',
properties: [{ name: 'userAgent', value: navigator.userAgent }],
};

window.navigator.sendBeacon(endpoint, JSON.stringify(beaconEvent));

void fetch(endpoint, {
method: 'POST',
body: JSON.stringify(fetchEvent),
keepalive: true,
});
}

return fastdom.mutate(() => {
setBackgroundStyles(specs, background);

Expand Down

0 comments on commit 9e47081

Please sign in to comment.