Skip to content

Commit 3b8d5b6

Browse files
committed
fix: prevent docs pageview replay after uncertain delivery
Signed-off-by: Jeremi Joslin <jeremi@joslin.fr>
1 parent ae038b2 commit 3b8d5b6

2 files changed

Lines changed: 4 additions & 21 deletions

File tree

‎docs/site/scripts/shared-tracking.test.mjs‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,6 @@ test('tracking controls use the shared bridge and remain available after failure
112112
assert.match(component, /main Registry Stack website and this\s+documentation/i);
113113
assert.match(component, /data-tracking-error[^>]*role="alert"/);
114114
assert.match(component, /client\.pageview\(pageview\)/);
115-
assert.match(component, /let sentGeneration: string \| null = null/);
116-
assert.match(component, /let pendingGeneration: string \| null = null/);
117-
assert.match(component, /state\.generation === sentGeneration/);
118-
assert.match(component, /state\.generation === pendingGeneration/);
119-
assert.match(component, /sentGeneration = state\.generation/);
120-
assert.match(component, /currentState\.generation === state\.generation/);
121115
assert.match(component, /addEventListener\('pagehide', \(event\) => \{/);
122116
assert.match(component, /if \(!event\.persisted\) trackingClient\.close\(\)/);
123117
assert.doesNotMatch(component, /location\.reload/);

‎docs/site/src/components/RegistryTrackingPreferences.astro‎

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,8 @@ const { canonicalOrigin, privacyUrl } = Astro.props;
6868
? new URL(window.location.pathname, canonicalOrigin).toString()
6969
: undefined;
7070
const browserDeniesTracking = trackingDeniedByBrowser();
71-
let sentGeneration: string | null = null;
72-
let pendingGeneration: string | null = null;
73-
let currentState: TrackingState | null = null;
71+
// Retain attempts after ambiguous delivery so recovery cannot count a visit twice.
72+
let attemptedGeneration: string | null = null;
7473
let client: ReturnType<typeof createSharedTrackingClient> | undefined;
7574

7675
const showPanel = () => {
@@ -96,7 +95,6 @@ const { canonicalOrigin, privacyUrl } = Astro.props;
9695
errorMessage.hidden = true;
9796
delete errorMessage.dataset.trackingFailure;
9897
}
99-
currentState = state;
10098
if (state.choice === null) {
10199
showPanel();
102100
return;
@@ -111,36 +109,27 @@ const { canonicalOrigin, privacyUrl } = Astro.props;
111109
if (
112110
state.choice !== 'accepted' ||
113111
!state.generation ||
114-
state.generation === sentGeneration ||
115-
state.generation === pendingGeneration ||
112+
state.generation === attemptedGeneration ||
116113
!client
117114
)
118115
return;
119116

120-
pendingGeneration = state.generation;
121117
try {
122118
const pageview = buildMauticPageview({
123119
canonicalUrl,
124120
currentUrl: window.location.href,
125121
pageTitle: document.title,
126122
referrer: document.referrer,
127123
});
124+
attemptedGeneration = state.generation;
128125
await client.pageview(pageview);
129-
if (
130-
currentState?.choice === 'accepted' &&
131-
currentState.generation === state.generation
132-
) {
133-
sentGeneration = state.generation;
134-
}
135126
} catch (error) {
136127
if (
137128
client.state.choice === 'accepted' &&
138129
client.state.generation === state.generation
139130
) {
140131
showFailure(error);
141132
}
142-
} finally {
143-
if (pendingGeneration === state.generation) pendingGeneration = null;
144133
}
145134
};
146135

0 commit comments

Comments
 (0)