-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbannerSelection.ts
More file actions
271 lines (242 loc) · 9.22 KB
/
bannerSelection.ts
File metadata and controls
271 lines (242 loc) · 9.22 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
import { isAfter, subDays } from 'date-fns';
import { countryCodeToCountryGroupId, inTargetedCountry } from '../../../shared/lib';
import type {
BannerTargeting,
BannerTest,
BannerTestSelection,
BannerVariant,
UserDeviceType,
} from '../../../shared/types';
import { uiIsDesign } from '../../../shared/types';
import { daysSince } from '../../lib/dates';
import { historyWithinArticlesViewedSettings } from '../../lib/history';
import type { TestVariant } from '../../lib/params';
import {
abandonedBasketMatches,
audienceMatches,
consentStatusMatches,
correctSignedInStatus,
deviceTypeMatches,
pageContextMatches,
} from '../../lib/targeting';
import { selectTargetingTest } from '../../lib/targetingTesting';
import type { BanditData } from '../../selection/banditData';
import { selectVariant } from '../../selection/selectVariant';
import type { ScheduledBannerDeploys } from './bannerDeploySchedule';
import { defaultDeploySchedule, getLastScheduledDeploy } from './bannerDeploySchedule';
import type { BannerDeployTimesProvider, ReaderRevenueRegion } from './bannerDeployTimes';
import { bannerTargetingTests } from './bannerTargetingTests';
export const readerRevenueRegionFromCountryCode = (countryCode: string): ReaderRevenueRegion => {
switch (true) {
case countryCode === 'GB':
return 'UnitedKingdom';
case countryCode === 'US':
return 'UnitedStates';
case countryCode === 'AU':
return 'Australia';
case countryCodeToCountryGroupId(countryCode) === 'EURCountries':
return 'EuropeanUnion';
default:
return 'RestOfWorld';
}
};
// Don't show the abandonedBasket banner if it was closed less than 1 day ago
function canShowAbandonedBasketBanner(
abandonedBasketBannerLastClosedAt: string | undefined,
now: Date,
): boolean {
if (!abandonedBasketBannerLastClosedAt) {
return true;
}
return daysSince(new Date(abandonedBasketBannerLastClosedAt), now) > 0;
}
export const isCountryTargetedForBanner = (
test: BannerTest,
targeting: BannerTargeting,
): boolean => {
const targetedCountryGroups = test.regionTargeting
? test.regionTargeting.targetedCountryGroups
: test.locations;
const targetedCountryCodes = test.regionTargeting
? test.regionTargeting.targetedCountryCodes
: [];
return inTargetedCountry(
targeting.countryCode,
targetedCountryGroups, // Country groups/region
targetedCountryCodes, // Individual country codes
);
};
/**
* If the banner has been closed previously, can we show it again?
* Takes into account both the manual deploys (from RRCP) and the scheduled deploys.
*
* @param targeting - the targeting data from the client
* @param test - the banner test config
* @param manualBannerDeployTimes - holds the times of manual banner deploys
* @param now - the time now
* @param scheduledBannerDeploys - the configured banner channel deploy schedule
*/
export const canShowBannerAgain = (
targeting: BannerTargeting,
test: BannerTest,
manualBannerDeployTimes: BannerDeployTimesProvider,
now: Date,
scheduledBannerDeploys?: ScheduledBannerDeploys,
): boolean => {
const {
subscriptionBannerLastClosedAt,
engagementBannerLastClosedAt,
signInBannerLastClosedAt,
abandonedBasketBannerLastClosedAt,
} = targeting;
const { bannerChannel, deploySchedule } = test;
const region = readerRevenueRegionFromCountryCode(targeting.countryCode);
// Never show a sign in prompt banner if it has been closed previously
if (bannerChannel === 'signIn') {
return !signInBannerLastClosedAt;
}
if (bannerChannel === 'abandonedBasket') {
return canShowAbandonedBasketBanner(abandonedBasketBannerLastClosedAt, now);
}
const canShow = (lastClosedRaw: string | undefined): boolean => {
if (!lastClosedRaw) {
return true; // banner not yet closed
}
if (deploySchedule) {
// this test has its own deploy schedule
const lastClosed = new Date(lastClosedRaw);
return isAfter(subDays(now, deploySchedule.daysBetween), lastClosed);
}
const deployTimes = manualBannerDeployTimes.getDeployTimes(bannerChannel);
const lastManualDeploy = deployTimes[region];
const lastClosed = new Date(lastClosedRaw);
return (
lastManualDeploy > lastClosed ||
(!!scheduledBannerDeploys &&
getLastScheduledDeploy(now, scheduledBannerDeploys[bannerChannel]) > lastClosed)
);
};
return canShow(
bannerChannel === 'subscriptions'
? subscriptionBannerLastClosedAt
: engagementBannerLastClosedAt,
);
};
const DESIGNABLE_BANNER_TEMPLATE_NAME = 'DesignableBanner';
const getModuleNameForVariant = (variant: BannerVariant): string => {
if (uiIsDesign(variant.template)) {
return DESIGNABLE_BANNER_TEMPLATE_NAME;
} else {
return variant.template;
}
};
const getForcedVariant = (
forcedTestVariant: TestVariant,
tests: BannerTest[],
): BannerTestSelection | null => {
const test = tests.find(
(test) => test.name.toLowerCase() === forcedTestVariant.testName.toLowerCase(),
);
const variant = test?.variants.find(
(v) => v.name.toLowerCase() === forcedTestVariant.variantName.toLowerCase(),
);
if (test && variant) {
return {
test,
variant,
moduleName: getModuleNameForVariant(variant),
};
}
return null;
};
const purchaseMatches = (
test: BannerTest,
purchaseInfo: BannerTargeting['purchaseInfo'],
isSignedIn: boolean,
) => {
const { purchaseInfo: testPurchaseInfo } = test;
// Ignore tests specifying purchase info if user is signed in / if no purchase info in targeting
if (isSignedIn || !purchaseInfo) {
return !testPurchaseInfo;
}
const { product, userType } = purchaseInfo;
const productValid = testPurchaseInfo?.product.includes(product);
const userValid = testPurchaseInfo?.userType.includes(userType);
return productValid && userValid;
};
const TAYLOR_REPORT_TAG_ID = 'news/series/cotton-capital';
const isTaylorReportPage = (targeting: BannerTargeting): boolean => {
return Boolean(targeting.tagIds?.includes(TAYLOR_REPORT_TAG_ID));
};
const matchesFrontsOnlyRequirement = (test: BannerTest, targeting: BannerTargeting): boolean => {
if (test.frontsOnly) {
return targeting.contentType === 'Network Front';
}
return true;
};
export const selectBannerTest = (
targeting: BannerTargeting,
userDeviceType: UserDeviceType,
baseUrl: string,
tests: BannerTest[],
bannerDeployTimes: BannerDeployTimesProvider,
enableHardcodedBannerTests: boolean,
enableScheduledDeploys: boolean,
banditData: BanditData[],
forcedTestVariant?: TestVariant,
now: Date = new Date(),
): BannerTestSelection | null => {
if (isTaylorReportPage(targeting)) {
return null;
}
if (forcedTestVariant) {
return getForcedVariant(forcedTestVariant, tests);
}
const targetingTest = selectTargetingTest(targeting.mvtId, targeting, bannerTargetingTests);
if (targetingTest && !targetingTest.canShow) {
return null;
}
for (const test of tests) {
const deploySchedule = enableScheduledDeploys
? (targetingTest?.deploySchedule ?? defaultDeploySchedule)
: undefined;
if (
test.status === 'Live' &&
(!test.canRun || test.canRun(targeting)) &&
(enableHardcodedBannerTests || !test.isHardcoded) &&
!targeting.shouldHideReaderRevenue &&
!targeting.isPaidContent &&
audienceMatches(targeting.showSupportMessaging, test.userCohort) &&
isCountryTargetedForBanner(test, targeting) &&
!(test.articlesViewedSettings && targeting.hasOptedOutOfArticleCount) &&
historyWithinArticlesViewedSettings(
test.articlesViewedSettings,
targeting.weeklyArticleHistory,
now,
) &&
deviceTypeMatches(test, userDeviceType) &&
purchaseMatches(test, targeting.purchaseInfo, targeting.isSignedIn) &&
canShowBannerAgain(targeting, test, bannerDeployTimes, now, deploySchedule) &&
correctSignedInStatus(targeting.isSignedIn, test.signedInStatus) &&
pageContextMatches(targeting, test.contextTargeting) &&
consentStatusMatches(targeting.hasConsented, test.consentStatus) &&
abandonedBasketMatches(test.bannerChannel, targeting.abandonedBasket) &&
matchesFrontsOnlyRequirement(test, targeting)
) {
const result = selectVariant<BannerVariant, BannerTest>(
test,
targeting.mvtId,
banditData,
);
if (result) {
return {
test: result.test,
variant: result.variant,
moduleName: getModuleNameForVariant(result.variant),
targetingAbTest: targetingTest ? targetingTest.test : undefined,
};
}
}
}
return null;
};