Skip to content

Commit

Permalink
Refactor metrics bypassing (#1463)
Browse files Browse the repository at this point in the history
* Change bypass method

* Temporarily stop bypassing sampling for dev environment

* Changeset

* Collect all metrics for dev environment

* Move bypass check above switch statement
  • Loading branch information
emma-imber authored Jul 15, 2024
1 parent aa3cbc1 commit 25d0df8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-peaches-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@guardian/commercial': minor
---

Refactor metrics bypassing
24 changes: 14 additions & 10 deletions src/core/send-commercial-metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,17 @@ function gatherMetricsOnPageUnload(): void {
}

const listener = (e: Event): void => {
switch (e.type) {
case 'visibilitychange':
if (document.visibilityState === 'hidden') {
if (window.guardian.config.shouldSendCommercialMetrics === true) {
switch (e.type) {
case 'visibilitychange':
if (document.visibilityState === 'hidden') {
gatherMetricsOnPageUnload();
}
return;
case 'pagehide':
gatherMetricsOnPageUnload();
}
return;
case 'pagehide':
gatherMetricsOnPageUnload();
return;
return;
}
}
};

Expand Down Expand Up @@ -232,7 +234,7 @@ async function bypassCommercialMetricsSampling(): Promise<void> {
const consented = await checkConsent();

if (consented) {
addVisibilityListeners();
window.guardian.config.shouldSendCommercialMetrics = true;
} else {
log('commercial', "Metrics won't be sent because consent wasn't given");
}
Expand Down Expand Up @@ -267,6 +269,7 @@ async function initCommercialMetrics({
setEndpoint(isDev);
setDevProperties(isDev);
setAdBlockerProperties(adBlockerInUse);
addVisibilityListeners();

if (window.guardian.config.commercialMetricsInitialised) {
return false;
Expand All @@ -279,7 +282,7 @@ async function initCommercialMetrics({
if (isDev || userIsInSamplingGroup) {
const consented = await checkConsent();
if (consented) {
addVisibilityListeners();
window.guardian.config.shouldSendCommercialMetrics = true;
return true;
}
log('commercial', "Metrics won't be sent because consent wasn't given");
Expand All @@ -296,6 +299,7 @@ export const _ = {
transformToObjectEntries,
reset: (): void => {
window.guardian.config.commercialMetricsInitialised = false;
window.guardian.config.shouldSendCommercialMetrics = false;
commercialMetricsPayload = {
page_view_id: undefined,
browser_id: undefined,
Expand Down
1 change: 1 addition & 0 deletions src/types/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ interface Config {
pageViewId: string;
};
page: PageConfig;
shouldSendCommercialMetrics?: boolean;
stage: Stage;
switches: Record<string, boolean | undefined>;
tests?: {
Expand Down

0 comments on commit 25d0df8

Please sign in to comment.