-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbannerRouter.ts
More file actions
173 lines (156 loc) · 6.27 KB
/
bannerRouter.ts
File metadata and controls
173 lines (156 loc) · 6.27 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
import express, { Router } from 'express';
import { getQueryParams, Params } from '../lib/params';
import {
BannerProps,
BannerTargeting,
BannerTest,
AmountsTests,
Prices,
TestTracking,
BannerDesignFromTool,
Tracking,
hideSRMessagingForInfoPageIds,
} from '../../shared/types';
import { selectAmountsTestVariant } from '../lib/ab';
import { ChannelSwitches } from '../channelSwitches';
import { selectBannerTest } from '../tests/banners/bannerSelection';
import { baseUrl } from '../lib/env';
import { BannerDeployTimesProvider } from '../tests/banners/bannerDeployTimes';
import { countryCodeToCountryGroupId } from '../../shared/lib';
import { TickerDataProvider } from '../lib/fetchTickerData';
import { getArticleViewCounts } from '../lib/history';
import { Debug } from '../tests/epics/epicSelection';
import { getDeviceType } from '../lib/deviceType';
import { ValueProvider } from '../utils/valueReloader';
import { getDesignForVariant } from '../tests/banners/channelBannerTests';
import { buildBannerCampaignCode } from '../lib/tracking';
import { BanditData } from '../bandit/banditData';
interface BannerDataResponse {
data?: {
module: {
name: string;
props: BannerProps;
};
meta: TestTracking;
};
debug?: Debug;
}
export const buildBannerRouter = (
channelSwitches: ValueProvider<ChannelSwitches>,
tickerData: TickerDataProvider,
productPrices: ValueProvider<Prices | undefined>,
bannerTests: ValueProvider<BannerTest[]>,
bannerDeployTimes: BannerDeployTimesProvider,
choiceCardAmounts: ValueProvider<AmountsTests>,
bannerDesigns: ValueProvider<BannerDesignFromTool[]>,
banditData: ValueProvider<BanditData[]>,
): Router => {
const router = Router();
const buildBannerData = (
targeting: BannerTargeting,
params: Params,
req: express.Request,
): BannerDataResponse => {
const { enableBanners, enableHardcodedBannerTests, enableScheduledBannerDeploys } =
channelSwitches.get();
if (!enableBanners) {
return {};
}
if (hideSRMessagingForInfoPageIds(targeting)) {
return {};
}
const selectedTest = selectBannerTest(
targeting,
getDeviceType(req),
baseUrl(req),
bannerTests.get(),
bannerDeployTimes,
enableHardcodedBannerTests,
enableScheduledBannerDeploys,
banditData.get(),
params.force,
);
if (selectedTest) {
const { test, variant, moduleName, targetingAbTest } = selectedTest;
const testTracking: TestTracking = {
abTestName: test.name,
abTestVariant: variant.name,
campaignCode: buildBannerCampaignCode(test, variant),
componentType: variant.componentType,
targetingAbTest,
...(variant.products && { products: variant.products }),
};
const tickerSettings =
variant.tickerSettings &&
tickerData.addTickerDataToSettings(variant.tickerSettings);
const contributionAmounts = choiceCardAmounts.get();
const requiredCountry = targeting.countryCode ?? 'GB';
const requiredRegion = countryCodeToCountryGroupId(requiredCountry);
const targetingMvtId = targeting.mvtId || 1;
const variantAmounts = selectAmountsTestVariant(
contributionAmounts,
requiredCountry,
requiredRegion,
targetingMvtId,
);
const props: BannerProps = {
tracking: testTracking as Tracking, // PageTracking is added client-side
bannerChannel: test.bannerChannel,
isSupporter: !targeting.showSupportMessaging,
countryCode: targeting.countryCode,
content: variant.bannerContent,
mobileContent: variant.mobileBannerContent,
articleCounts: getArticleViewCounts(
targeting.weeklyArticleHistory,
test.articlesViewedSettings?.periodInWeeks,
test.articlesViewedSettings?.tagIds,
),
hasOptedOutOfArticleCount: targeting.hasOptedOutOfArticleCount,
tickerSettings,
separateArticleCount: variant.separateArticleCount,
separateArticleCountSettings: variant.separateArticleCountSettings,
prices: productPrices.get(),
choiceCardAmounts: variantAmounts,
design: getDesignForVariant(variant, bannerDesigns.get()),
abandonedBasket: targeting.abandonedBasket,
};
return {
data: {
module: {
name: moduleName,
props: props,
},
meta: testTracking,
},
};
} else {
// No banner
return { data: undefined };
}
};
router.post(
'/banner',
(req: express.Request, res: express.Response, next: express.NextFunction) => {
try {
const { targeting } = req.body;
const params = getQueryParams(req.query);
const response = buildBannerData(targeting, params, req);
// for response logging
res.locals.didRenderBanner = !!response.data;
// be specific about which fields to log, to avoid accidentally logging inappropriate things in future
res.locals.bannerTargeting = {
shouldHideReaderRevenue: targeting.shouldHideReaderRevenue,
showSupportMessaging: targeting.showSupportMessaging,
countryCode: targeting.countryCode,
engagementBannerLastClosedAt: targeting.engagementBannerLastClosedAt,
subscriptionBannerLastClosedAt: targeting.subscriptionBannerLastClosedAt,
isPaidContent: targeting.isPaidContent,
};
res.send(response);
} catch (error) {
next(error);
}
},
);
return router;
};