Skip to content

Commit

Permalink
Sample prebid analytics at 10% (#1727)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakeii authored Jan 8, 2025
1 parent dab9c86 commit 1ed5373
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-ghosts-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@guardian/commercial': minor
---

Sample prebid analytics at 10%
17 changes: 15 additions & 2 deletions src/lib/header-bidding/prebid/prebid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { PREBID_TIMEOUT } from '../../../core/constants/prebid-timeout';
import { EventTimer } from '../../../core/event-timer';
import type { PageTargeting } from '../../../core/targeting/build-page-targeting';
import type { Advert } from '../../../define/Advert';
import { isUserInVariant } from '../../../experiments/ab';
import { getParticipations, isUserInVariant } from '../../../experiments/ab';
import { gpidPrebidAdUnits } from '../../../experiments/tests/gpid-prebid';
import { getPageTargeting } from '../../build-page-targeting';
import { getAdvertById } from '../../dfp/get-advert-by-id';
Expand Down Expand Up @@ -266,6 +266,16 @@ declare global {
}
}

const shouldEnableAnalytics = (): boolean => {
const analyticsSampleRate = 10 / 100;
const isInSample = Math.random() < analyticsSampleRate;

const isInServerSideTest =
Object.keys(window.guardian.config.tests ?? {}).length > 0;
const isInClientSideTest = Object.keys(getParticipations()).length > 0;
return isInServerSideTest || isInClientSideTest || isInSample;
};

/**
* Prebid supports an additional timeout buffer to account for noisiness in
* timing JavaScript on the page. This value is passed to the Prebid config
Expand Down Expand Up @@ -447,7 +457,10 @@ const initialise = (
});
}

if (window.guardian.config.switches.prebidAnalytics) {
if (
window.guardian.config.switches.prebidAnalytics &&
shouldEnableAnalytics()
) {
window.pbjs.enableAnalytics([
{
provider: 'gu',
Expand Down

0 comments on commit 1ed5373

Please sign in to comment.