Skip to content

Commit fd536f7

Browse files
author
Juarez Mota
committed
Remove per-country Gandalf pageview counter in favor of global count
The Gandalf sign-in gate counter is now global rather than per-country. Storage keys, function signatures, and tests updated to remove countryCode parameter. Counter still advances once per eligible pageview via idempotent pageview ID tracking.
1 parent 31259dc commit fd536f7

5 files changed

Lines changed: 60 additions & 83 deletions

File tree

dotcom-rendering/src/components/SignInGate/types.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,9 @@ export interface AuxiaProxyGetTreatmentsPayload {
195195
// countries via the gandalfSignInGateCountries channel switch.
196196
//
197197
// `gandalfPageViewCount` is the 0-based number of eligible pageviews the
198-
// reader has already completed in the request's country under the active
199-
// Gandalf rules (see src/lib/gandalf.ts). Counters are per country, because
200-
// campaigns differ by country group. It is optional so older payloads and
201-
// traffic outside the Gandalf countries are unaffected; SDC treats a missing
202-
// value as 0.
198+
// reader has already completed under the active Gandalf rules (see
199+
// src/lib/gandalf.ts). It is optional so older payloads and traffic outside
200+
// the Gandalf countries are unaffected; SDC treats a missing value as 0.
203201

204202
export interface AuxiaProxyGetTreatmentsResponse {
205203
status: boolean;

dotcom-rendering/src/components/StickyBottomBanner/SignInGatePortal.test.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ describe('SignInGatePortal', () => {
224224
});
225225

226226
describe('Gandalf (Guardian-managed sign-in gate journey)', () => {
227-
it('sends the current per-country pageview count to SDC', async () => {
227+
it('sends the current pageview count to SDC', async () => {
228228
mockGetElementById.mockReturnValue(document.createElement('div'));
229229
(
230230
buildAuxiaGateDisplayData as jest.MockedFunction<
@@ -266,7 +266,6 @@ describe('SignInGatePortal', () => {
266266
},
267267
});
268268
expect(mockIncrementGandalfPageViewCount).toHaveBeenCalledWith(
269-
'NZ',
270269
'test-page-view-id',
271270
);
272271
});
@@ -290,7 +289,6 @@ describe('SignInGatePortal', () => {
290289
meta: { ...auxiaReturn, gandalfCountryCode: 'NZ' },
291290
});
292291
expect(mockIncrementGandalfPageViewCount).toHaveBeenCalledWith(
293-
'NZ',
294292
'test-page-view-id',
295293
);
296294
});

dotcom-rendering/src/components/StickyBottomBanner/SignInGatePortal.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,19 +210,19 @@ export const canShowSignInGatePortal = async ({
210210
sectionId,
211211
tags,
212212
retrieveLastGateDismissedCount('AuxiaSignInGate'),
213-
// 0-based count of previously completed eligible pageviews for this
214-
// country. SDC only consumes this for active Gandalf traffic; the
215-
// counter itself is advanced below once SDC confirms the pageview
213+
// 0-based count of previously completed eligible pageviews. SDC
214+
// only consumes this for active Gandalf traffic; the counter
215+
// itself is advanced below once SDC confirms the pageview
216216
// counted.
217-
getGandalfPageViewCount(countryCode ?? ''),
217+
getGandalfPageViewCount(),
218218
);
219219

220220
// Gandalf (the Guardian-managed sign-in gate journey): SDC marks
221221
// responses produced by the active Gandalf rules. The pageview counted
222-
// towards the country's free allowance even when no gate is displayed,
223-
// so record it exactly once per pageview.
222+
// towards the free allowance even when no gate is displayed, so record
223+
// it exactly once per pageview.
224224
if (auxiaData?.auxiaData.gandalfSignInGate === true) {
225-
incrementGandalfPageViewCount(countryCode ?? '', ophanPageViewId);
225+
incrementGandalfPageViewCount(ophanPageViewId);
226226
}
227227

228228
const meta = (

dotcom-rendering/src/lib/gandalf.test.ts

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,73 +4,63 @@ import {
44
} from './gandalf';
55

66
// Mirrors the storage key shape in gandalf.ts
7-
const countKey = (countryCode: string): string =>
8-
`gu.gandalf.pageViewCount.${countryCode.toLowerCase()}`;
7+
const countKey = 'gu.gandalf.pageViewCount';
98

109
describe('gandalf pageview counter', () => {
1110
beforeEach(() => {
1211
localStorage.clear();
1312
});
1413

1514
describe('getGandalfPageViewCount', () => {
16-
it('returns 0 when nothing is stored for the country', () => {
17-
expect(getGandalfPageViewCount('NZ')).toBe(0);
15+
it('returns 0 when nothing is stored', () => {
16+
expect(getGandalfPageViewCount()).toBe(0);
1817
});
1918

20-
it('returns the stored count for the country', () => {
21-
localStorage.setItem(countKey('NZ'), '4');
22-
expect(getGandalfPageViewCount('NZ')).toBe(4);
19+
it('returns the stored count', () => {
20+
localStorage.setItem(countKey, '4');
21+
expect(getGandalfPageViewCount()).toBe(4);
2322
});
2423

2524
it('fails safe to 0 for malformed values', () => {
26-
localStorage.setItem(countKey('NZ'), 'not-a-number');
27-
expect(getGandalfPageViewCount('NZ')).toBe(0);
25+
localStorage.setItem(countKey, 'not-a-number');
26+
expect(getGandalfPageViewCount()).toBe(0);
2827
});
2928

3029
it('fails safe to 0 for negative values', () => {
31-
localStorage.setItem(countKey('NZ'), '-3');
32-
expect(getGandalfPageViewCount('NZ')).toBe(0);
30+
localStorage.setItem(countKey, '-3');
31+
expect(getGandalfPageViewCount()).toBe(0);
3332
});
3433
});
3534

3635
describe('incrementGandalfPageViewCount', () => {
37-
it('increments the country count by one', () => {
38-
incrementGandalfPageViewCount('NZ', 'page-view-1');
39-
expect(getGandalfPageViewCount('NZ')).toBe(1);
36+
it('increments the count by one', () => {
37+
incrementGandalfPageViewCount('page-view-1');
38+
expect(getGandalfPageViewCount()).toBe(1);
4039
});
4140

42-
it('keeps one counter per country', () => {
43-
incrementGandalfPageViewCount('NZ', 'page-view-1');
44-
incrementGandalfPageViewCount('NZ', 'page-view-2');
45-
incrementGandalfPageViewCount('AU', 'page-view-2');
46-
expect(getGandalfPageViewCount('NZ')).toBe(2);
47-
expect(getGandalfPageViewCount('AU')).toBe(1);
41+
it('is idempotent for the same pageview id', () => {
42+
incrementGandalfPageViewCount('page-view-1');
43+
incrementGandalfPageViewCount('page-view-1');
44+
incrementGandalfPageViewCount('page-view-1');
45+
expect(getGandalfPageViewCount()).toBe(1);
4846
});
4947

50-
it('is idempotent for the same pageview id within a country', () => {
51-
incrementGandalfPageViewCount('NZ', 'page-view-1');
52-
incrementGandalfPageViewCount('NZ', 'page-view-1');
53-
incrementGandalfPageViewCount('NZ', 'page-view-1');
54-
expect(getGandalfPageViewCount('NZ')).toBe(1);
55-
});
56-
57-
it('counts the same pageview id separately per country', () => {
58-
incrementGandalfPageViewCount('NZ', 'page-view-1');
59-
incrementGandalfPageViewCount('AU', 'page-view-2');
60-
expect(getGandalfPageViewCount('NZ')).toBe(1);
61-
expect(getGandalfPageViewCount('AU')).toBe(1);
48+
it('counts distinct pageview ids separately', () => {
49+
incrementGandalfPageViewCount('page-view-1');
50+
incrementGandalfPageViewCount('page-view-2');
51+
expect(getGandalfPageViewCount()).toBe(2);
6252
});
6353

6454
it('continues from a pre-existing stored count', () => {
65-
localStorage.setItem(countKey('NZ'), '2');
66-
incrementGandalfPageViewCount('NZ', 'page-view-1');
67-
expect(getGandalfPageViewCount('NZ')).toBe(3);
55+
localStorage.setItem(countKey, '2');
56+
incrementGandalfPageViewCount('page-view-1');
57+
expect(getGandalfPageViewCount()).toBe(3);
6858
});
6959

7060
it('resets a malformed stored value to 1 on increment', () => {
71-
localStorage.setItem(countKey('NZ'), 'garbage');
72-
incrementGandalfPageViewCount('NZ', 'page-view-1');
73-
expect(getGandalfPageViewCount('NZ')).toBe(1);
61+
localStorage.setItem(countKey, 'garbage');
62+
incrementGandalfPageViewCount('page-view-1');
63+
expect(getGandalfPageViewCount()).toBe(1);
7464
});
7565
});
7666
});

dotcom-rendering/src/lib/gandalf.ts

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,46 @@ import { storage } from '@guardian/libs';
66
// currently live for New Zealand and extendable to further countries via the
77
// gandalfSignInGateCountries channel switch.
88
//
9-
// A dedicated, persistent counter of completed eligible pageviews, kept per
10-
// country because campaigns differ by country group. It is deliberately
11-
// independent of `gu.history.dailyArticleCount` (which resets daily and only
12-
// counts a subset of content types) and of `gate_display_count` (which counts
13-
// gate renders, not pageviews).
9+
// A dedicated, persistent counter of completed eligible pageviews. It is
10+
// deliberately independent of `gu.history.dailyArticleCount` (which resets
11+
// daily and only counts a subset of content types), of `gate_display_count`
12+
// (which counts gate renders, not pageviews) and of `gu.alreadyVisited`
13+
// (which counts every consented pageview and is an ad-targeting input).
1414
//
1515
// The counter is 0-based: it stores the number of eligible pageviews already
16-
// completed for the country. The first three eligible pageviews (counts 0, 1,
17-
// 2 sent to SDC) are free; from the fourth onwards SDC returns the
18-
// non-dismissible popup.
16+
// completed. The first three eligible pageviews (counts 0, 1, 2 sent to SDC)
17+
// are free; from the fourth onwards SDC returns the non-dismissible popup.
1918
//
2019
// The counter only advances after SDC confirms the pageview was an active,
2120
// eligible pageview for a Gandalf country (see the gandalfSignInGate response
2221
// marker), so unlisted-country traffic and excluded pages never consume the
2322
// allowance. It is browser-local: clearing storage or using a new/incognito
2423
// browser resets the allowance. This is accepted for the proof of concept.
2524

26-
const pageViewCountKey = (countryCode: string): string =>
27-
`gu.gandalf.pageViewCount.${countryCode.toLowerCase()}`;
25+
const pageViewCountKey = 'gu.gandalf.pageViewCount';
2826

29-
const lastCountedPageViewIdKey = (countryCode: string): string =>
30-
`gu.gandalf.lastCountedPageViewId.${countryCode.toLowerCase()}`;
27+
const lastCountedPageViewIdKey = 'gu.gandalf.lastCountedPageViewId';
3128

3229
/**
33-
* Returns the 0-based number of eligible pageviews already completed in the
34-
* given country. Fails safe to 0 if the stored value is missing or malformed.
30+
* Returns the 0-based number of eligible pageviews already completed.
31+
* Fails safe to 0 if the stored value is missing or malformed.
3532
*/
36-
export const getGandalfPageViewCount = (countryCode: string): number => {
37-
const raw = storage.local.getRaw(pageViewCountKey(countryCode));
33+
export const getGandalfPageViewCount = (): number => {
34+
const raw = storage.local.getRaw(pageViewCountKey);
3835
const count = parseInt(raw ?? '', 10);
3936
return Number.isInteger(count) && count >= 0 ? count : 0;
4037
};
4138

4239
/**
43-
* Records one completed eligible pageview for the country. Idempotent per
44-
* pageview ID, so React effect re-runs (e.g. Strict Mode) increment at most
45-
* once per pageview.
40+
* Records one completed eligible pageview. Idempotent per pageview ID, so
41+
* React effect re-runs (e.g. Strict Mode) increment at most once per
42+
* pageview.
4643
*/
47-
export const incrementGandalfPageViewCount = (
48-
countryCode: string,
49-
pageViewId: string,
50-
): void => {
51-
if (
52-
storage.local.getRaw(lastCountedPageViewIdKey(countryCode)) ===
53-
pageViewId
54-
) {
44+
export const incrementGandalfPageViewCount = (pageViewId: string): void => {
45+
if (storage.local.getRaw(lastCountedPageViewIdKey) === pageViewId) {
5546
return;
5647
}
57-
storage.local.setRaw(lastCountedPageViewIdKey(countryCode), pageViewId);
58-
const count = getGandalfPageViewCount(countryCode);
59-
storage.local.setRaw(pageViewCountKey(countryCode), (count + 1).toString());
48+
storage.local.setRaw(lastCountedPageViewIdKey, pageViewId);
49+
const count = getGandalfPageViewCount();
50+
storage.local.setRaw(pageViewCountKey, (count + 1).toString());
6051
};

0 commit comments

Comments
 (0)