-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlibPure.ts
More file actions
455 lines (390 loc) · 17.3 KB
/
libPure.ts
File metadata and controls
455 lines (390 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// --------------------------------------------------------
// Pure Functions
// --------------------------------------------------------
import type {
AuxiaAPIGetTreatmentsRequestPayload,
AuxiaAPILogTreatmentInteractionRequestPayload,
GateType,
GetTreatmentsRequestPayload,
ProxyGetTreatmentsAnswerData,
UserTreatment,
UserTreatmentsEnvelop,
} from './types';
export const buildGetTreatmentsRequestPayload = (
projectId: string,
browserId: string,
isSupporter: boolean,
dailyArticleCount: number,
articleIdentifier: string,
editionId: string,
countryCode: string,
hasConsented: boolean,
shouldServeDismissible: boolean,
): AuxiaAPIGetTreatmentsRequestPayload => {
// For the moment we are hard coding the data provided in contextualAttributes and surfaces.
return {
projectId: projectId,
userId: browserId, // In our case the userId is the browserId.
contextualAttributes: [
{
key: 'is_supporter',
boolValue: isSupporter,
},
{
key: 'daily_article_count',
integerValue: dailyArticleCount,
},
{
key: 'article_identifier',
stringValue: articleIdentifier,
},
{
key: 'edition',
stringValue: editionId,
},
{
key: 'country_key',
stringValue: countryCode,
},
{
key: 'has_consented',
boolValue: hasConsented,
},
{
key: 'should_not_serve_mandatory',
boolValue: shouldServeDismissible,
},
],
surfaces: [
{
surface: 'ARTICLE_PAGE',
maximumTreatmentCount: 1,
},
],
languageCode: 'en-GB',
};
};
export const guDismissibleUserTreatment = (): UserTreatment => {
// The contract we have with the client is that a gate is dismissible if the second_cta_name
// is not empty. Otherwise the gate is Mandatory
const title = "Sign in: it's quick and easy";
const subtitle = 'It’s still free to read – this is not a paywall';
const body =
'We’re committed to keeping our quality reporting open. By registering and providing us with insight into your preferences, you’re helping us to engage with you more deeply, and that allows us to keep our journalism free for all.';
const secondCtaName = 'I’ll do it later';
const treatmentContent = {
title,
subtitle,
body,
first_cta_name: 'Create an account',
first_cta_link: 'https://profile.theguardian.com/register?',
second_cta_name: secondCtaName,
second_cta_link: 'https://profile.theguardian.com/signin?',
};
const treatmentContentEncoded = JSON.stringify(treatmentContent);
return {
treatmentId: 'default-treatment-id',
treatmentTrackingId: 'default-treatment-tracking-id',
rank: '1',
contentLanguageCode: 'en-GB',
treatmentContent: treatmentContentEncoded,
treatmentType: 'DISMISSABLE_SIGN_IN_GATE',
surface: 'ARTICLE_PAGE',
};
};
export const guMandatoryUserTreatment = (): UserTreatment => {
// The contract we have with the client is that a gate is dismissible if the second_cta_name
// is not empty. Otherwise the gate is Mandatory
const title = "Sign in: it's quick and easy";
const subtitle = 'It’s still free to read – this is not a paywall';
const body =
'We’re committed to keeping our quality reporting open. By registering and providing us with insight into your preferences, you’re helping us to engage with you more deeply, and that allows us to keep our journalism free for all.';
const treatmentContent = {
title,
subtitle,
body,
first_cta_name: 'Create an account',
first_cta_link: 'https://profile.theguardian.com/register?',
second_cta_name: '', // empty string here makes the gate mandatory
second_cta_link: '',
};
const treatmentContentEncoded = JSON.stringify(treatmentContent);
return {
treatmentId: 'default-treatment-id',
treatmentTrackingId: 'default-treatment-tracking-id',
rank: '1',
contentLanguageCode: 'en-GB',
treatmentContent: treatmentContentEncoded,
treatmentType: 'DISMISSABLE_SIGN_IN_GATE',
surface: 'ARTICLE_PAGE',
};
};
export const buildGuUserTreatmentsEnvelop = (
gateDismissCount: number,
gateDisplayCount: number,
countryCode: string,
): UserTreatmentsEnvelop => {
const responseId = ''; // This value is not important, it is not used by the client.
// First we enforce the GU policy of not showing the gate if the user has dismissed it more than 5 times.
// (We do not want users to have to dismiss the gate 6 times)
if (gateDismissCount > 5) {
return {
responseId,
userTreatments: [],
};
}
// We are now clear to show the default gu gate.
// (comment group: 04f093f0)
// gateDisplayCount was introduced to enrich the behavior of the default gate.
// That number represents the number of times the gate has been displayed, excluding the
// current rendering. Therefore the first time the number is 0.
// At the time these lines are written we want the experience for non consented users
// in Ireland to be that the gates, as they display are (first line) corresponding
// to values of gateDisplayCount (second line)
// -------------------------------------------------------------------------
// | dismissible | dismissible | dismissible | mandatory (remains mandatory) |
// | 0 | 1 | 2 | 3 etc |
// -------------------------------------------------------------------------
// For non consenting users outside ireland, the behavior remains the same
if (countryCode !== 'IE') {
const data: UserTreatmentsEnvelop = {
responseId,
userTreatments: [guDismissibleUserTreatment()],
};
return data;
}
let data: UserTreatmentsEnvelop;
if (gateDisplayCount >= 3) {
data = {
responseId,
userTreatments: [guMandatoryUserTreatment()],
};
} else {
data = {
responseId,
userTreatments: [guDismissibleUserTreatment()],
};
}
return data;
};
export const isValidContentType = (contentType: string): boolean => {
const validTypes = ['Article'];
return validTypes.includes(contentType);
};
export const isValidSection = (sectionId: string): boolean => {
const invalidSections = [
'about',
'info',
'membership',
'help',
'guardian-live-australia',
'gnm-archive',
'thefilter',
];
return !invalidSections.includes(sectionId);
};
export const isValidTagIdCollection = (tagIds: string[]): boolean => {
const invalidTagIds = ['info/newsletter-sign-up'];
// Check that no tagId is in the invalidTagIds list.
return !tagIds.some((tagId: string): boolean => invalidTagIds.includes(tagId));
};
export const userTreatmentsEnvelopToProxyGetTreatmentsAnswerData = (
envelop: UserTreatmentsEnvelop,
): ProxyGetTreatmentsAnswerData | undefined => {
// Note the small difference between ProxyGetTreatmentsAnswerData and UserTreatmentsEnvelop
// In the case of ProxyGetTreatmentsAnswerData, we have an optional userTreatment field,
// instead of an array of userTreatments.
//
// UserTreatmentsEnvelop is generated by either Auxia or us here in SDC, and is
// modelled after Auxia's own schema.
//
// ProxyGetTreatmentsAnswerData is the answer from the Proxy
//
// This is to reflect the what the client expect semantically.
return {
responseId: envelop.responseId,
userTreatment: envelop.userTreatments[0],
};
};
export const buildLogTreatmentInteractionRequestPayload = (
projectId: string,
browserId: string,
treatmentTrackingId: string,
treatmentId: string,
surface: string,
interactionType: string,
interactionTimeMicros: number,
actionName: string,
): AuxiaAPILogTreatmentInteractionRequestPayload => {
return {
projectId: projectId,
userId: browserId, // In our case the userId is the browserId.
treatmentTrackingId,
treatmentId,
surface,
interactionType,
interactionTimeMicros,
actionName,
};
};
export const articleIdentifierIsAllowed = (articleIdentifier: string): boolean => {
// This function was introduced to handle the specific request of not showing a gate for
// this url: https://www.theguardian.com/tips
// articleIdentifier are given to the end point under the following format:
// - 'www.theguardian.com/money/2017/mar/10/ministers-to-criminalise-use-of-ticket-tout-harvesting-software'
// - 'www.theguardian.com/tips'
// For the moment we are only going to check for that one string, we will refactor
// if more come in in the future
const denyPrefixes = [
'www.theguardian.com/tips',
'www.theguardian.com/help/ng-interactive/2017/mar/17/contact-the-guardian-securely',
];
return !denyPrefixes.some((denyIdentifer) => articleIdentifier.startsWith(denyIdentifer));
};
export const mvtIdIsAuxiaAudienceShare = (mvtId: number): boolean => {
/*
In May 2025, we decided that we would decommission the old / previous definition
of the Auxia share of the audience, which was done using a client side defined AB test,
by which the "first" 35% of the audience is sent to Auxia, and the rest (65%) split between
"SignInGateMainVariant" and "SignInGateMainControl"
(
https://github.com/guardian/dotcom-rendering/blob/d6e44406cffb362c99d5734f6e82f6e664682da8/dotcom-rendering/src/experiments/tests/auxia-sign-in-gate.ts
)
... and move to those shares being controlled by SDC and in particular SDC's default gate
taking over the old gate hard coded into DCR.
To maintain invariance of how the cohorts are defined, SDC, must be able to convert a
mvtId into a share of audience. Passing the mvtId to the GetTreatment call was done in these two PRs:
https://github.com/guardian/dotcom-rendering/pull/13938
https://github.com/guardian/dotcom-rendering/pull/13941
In particular we must be able to take a mvtId and simply return
a boolean indicating whether or not it is in the first 35% of the audience. This is what this function
does.
This is the function that needs to be modified when we want to increase the share of the
audience given to the Auxia experiment in the future.
*/
// The MVT calculator is very useful: https://ab-tests.netlify.app
// The Auxia experiment is 35% audience with 0% offset.
// The value numbers we are interested in are between 1 and 350_000 [1]
// (essentially the first 35% of the total of 1_000_000 possible values for mvtId)
// [1] Interestingly, 0 is not considered a valid mvtId number.
return mvtId > 0 && mvtId <= 350_000;
};
export const decideGateTypeNoneOrDismissible = (gateDismissCount: number): GateType => {
// -----------------------------------------------------------------------
// First we enforce the GU policy of not showing the gate if the user has dismissed it more than 5 times.
// (We do not want users to have to dismiss the gate 6 times)
if (gateDismissCount > 5) {
return 'None';
}
// -----------------------------------------------------------------------
// We are now clear to show the default (dismissible) gu gate.
return 'GuDismissible';
};
export const decideGuGateTypeNonConsentedIreland = (
dailyArticleCount: number,
gateDisplayCount: number,
): GateType => {
// -----------------------------------------------------------------------
// If we reach this point, we are in Ireland
if (dailyArticleCount < 3) {
return 'AuxiaAnalyticThenNone';
}
// gateDisplayCount was introduced to enrich the behavior of the default gate.
// That number represents the number of times the gate has been displayed, excluding the
// current rendering. Therefore the first time the number is 0.
// At the time these lines are written we want the experience for non consented users
// in Ireland to be that the gates, as they display are (first line) corresponding
// to values of gateDisplayCount (second line)
// -------------------------------------------------------------------------
// | dismissible | dismissible | dismissible | mandatory (remains mandatory) |
// | 0 | 1 | 2 | 3 etc |
// -------------------------------------------------------------------------
if (gateDisplayCount >= 3) {
return 'AuxiaAnalyticThenGuMandatory';
}
return 'AuxiaAnalyticThenGuDismissible';
};
export const getTreatmentsRequestPayloadToGateType = (
getTreatmentsRequestPayload: GetTreatmentsRequestPayload,
): GateType => {
// This function is a pure function (without any side effects) which gets the body
// of a '/auxia/get-treatments' request and returns the correct GateType.
// It was introduced to separate the choice of the gate from it's actual build,
// which in the case of Auxia, requires an API call, but more importantly to
// encapsulate and more logically test the logic of gate selection.
// The comments are the specs it must comply with.
// --------------------------------------------------------------
// If both conditions are true
//
// 1. body.shouldServeDismissible is true
// which at the moment is controlled by utm_source=newsshowcase,
// (exposed as DRC:decideShouldServeDismissible), and
//
// 2. body.showDefaultGate is defined (regardless of its value)
//
// Then we serve a non dismissible gate. In other words
// body.shouldServeDismissible take priority over the fact that body.showDefaultGate
// could possibly have value 'mandatory'
if (
getTreatmentsRequestPayload.showDefaultGate !== undefined &&
getTreatmentsRequestPayload.shouldServeDismissible
) {
return 'GuDismissible';
}
// --------------------------------------------------------------
// The attribute showDefaultGate overrides any other behavior
if (getTreatmentsRequestPayload.showDefaultGate) {
if (getTreatmentsRequestPayload.showDefaultGate == 'mandatory') {
return 'GuMandatory';
} else {
return 'GuDismissible';
}
}
// We check page metada to comply with Guardian policies.
// If the policies are not met, then we do not display a gate
// Note that at the time these lines are written (Aug 13th 2025),
// these checks are also performed client side, but those client side checks
// might be decommissioned in the future.
if (
!isValidContentType(getTreatmentsRequestPayload.contentType) ||
!isValidSection(getTreatmentsRequestPayload.sectionId) ||
!isValidTagIdCollection(getTreatmentsRequestPayload.tagIds) ||
!articleIdentifierIsAllowed(getTreatmentsRequestPayload.articleIdentifier)
) {
return 'None';
}
// --------------------------------------------------------------
// Then, we need to check whether we are in Ireland ot not. If we are in Ireland
// as a consequence of the great Ireland opening of May 2025 (tm), we send the entire
// traffic (consented or not consented) to Auxia. (For privacy vigilantes reading this,
// Auxia is not going to process non consented traffic for targetting.)
if (getTreatmentsRequestPayload.countryCode === 'IE') {
if (mvtIdIsAuxiaAudienceShare(getTreatmentsRequestPayload.mvtId)) {
return 'AuxiaAPI';
} else {
return decideGuGateTypeNonConsentedIreland(
getTreatmentsRequestPayload.dailyArticleCount,
getTreatmentsRequestPayload.gateDisplayCount,
);
}
}
// --------------------------------------------------------------
// Then, we check whether the call is from the Auxia audience or the non Auxia audience.
// If it is from the non Auxia audience, then we follow the value of
// should_show_legacy_gate_tmp to decide whether to return a default gate or not.
// If it is from the Auxia audience, then move to the next section
// Note that "Auxia audience" and "non Auxia audience" are concepts from the way the audience
// was split between 35% expose to the Auxia gate and 65% being shown the default GU gate
// That split used to be done client side, but it's now been moved to SDC and is driven by
// `mvtIdIsAuxiaAudienceShare`.
if (!mvtIdIsAuxiaAudienceShare(getTreatmentsRequestPayload.mvtId)) {
if (getTreatmentsRequestPayload.should_show_legacy_gate_tmp) {
return decideGateTypeNoneOrDismissible(getTreatmentsRequestPayload.gateDismissCount);
} else {
return 'None';
}
}
// --------------------------------------------------------------
// Auxia share of the audience (outside Ireland)
return 'AuxiaAPI';
};