Skip to content

Commit 83a83b1

Browse files
authored
Add localHour in page targeting (#2149)
* Add localHour in page targeting * Fix unit tests for the new localHour page targeting key/value * Remove unused jest mock set and capitalize Local Hour * fix prettier
1 parent 003b92e commit 83a83b1

8 files changed

Lines changed: 42 additions & 3 deletions

bundle/src/lib/page-targeting.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ describe('Build Page Targeting', () => {
6262
ab: ['MtMaster-variantName'],
6363
at: 'ng101',
6464
cc: 'US',
65+
lh: '12',
6566
pv: 'presetOphanPageViewId',
6667
si: 't',
6768
bp: 'desktop',

bundle/src/lib/targeting/build-page-targeting-consentless.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ describe('buildPageTargetingConsentless', () => {
3737
ab: ['MtMaster-variantName'],
3838
at: 'ng101',
3939
cc: 'US',
40+
lh: '12',
4041
pv: 'presetOphanPageViewId',
4142
si: 't',
4243
bp: 'desktop',
@@ -63,6 +64,7 @@ describe('buildPageTargetingConsentless', () => {
6364
ab: ['MtMaster-variantName'],
6465
at: 'ng101',
6566
cc: 'US',
67+
lh: '12',
6668
si: 't',
6769
bp: 'desktop',
6870
skinsize: 's',

bundle/src/lib/targeting/build-page-targeting-consentless.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const consentlessTargetingKeys = [
1111
'bp',
1212
'br',
1313
'cc',
14+
'lh',
1415
'ct',
1516
'dcre',
1617
'edition',

core/src/targeting/build-page-targeting.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { cmp as cmp_, setCookie, storage } from '@guardian/libs';
77
import { getLocale as getLocale_ } from '../geo/get-locale';
88
import type { Edition } from '../types';
99
import { buildPageTargeting } from './build-page-targeting';
10+
import { getLocalHour } from './shared';
1011

1112
const getLocale = getLocale_ as jest.MockedFunction<typeof getLocale_>;
1213

@@ -24,6 +25,11 @@ jest.mock('../geo/get-locale', () => ({
2425
getLocale: jest.fn(),
2526
}));
2627

28+
jest.mock('./shared', () => ({
29+
...jest.requireActual('./shared'),
30+
getLocalHour: jest.fn().mockReturnValue('12'),
31+
}));
32+
2733
jest.mock('@guardian/libs', () => ({
2834
...jest.requireActual<typeof import('@guardian/libs')>('@guardian/libs'),
2935
cmp: {
@@ -170,6 +176,8 @@ describe('Build Page Targeting', () => {
170176

171177
getLocale.mockReturnValue('US');
172178

179+
jest.mocked(getLocalHour).mockImplementation(() => '12');
180+
173181
jest.spyOn(global.Math, 'random').mockReturnValue(0.5);
174182

175183
expect.hasAssertions();
@@ -208,6 +216,7 @@ describe('Build Page Targeting', () => {
208216
expect(pageTargeting.pv).toEqual('presetOphanPageViewId');
209217
expect(pageTargeting.pa).toEqual('f');
210218
expect(pageTargeting.cc).toEqual('US');
219+
expect(pageTargeting.lh).toEqual('12');
211220
expect(pageTargeting.rp).toEqual('dotcom-platform');
212221
expect(pageTargeting.rc).toEqual('7');
213222
expect(pageTargeting.allkw).toEqual([
@@ -456,6 +465,7 @@ describe('Build Page Targeting', () => {
456465
at: 'ng101',
457466
bp: 'mobile',
458467
cc: 'US',
468+
lh: '12',
459469
cmp_interaction: 'na',
460470
consent_tcfv2: 'na',
461471
dcre: 'f',

core/src/targeting/build-page-targeting.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { getPersonalisedTargeting } from './personalised';
1010
import type { SessionTargeting } from './session';
1111
import { getSessionTargeting } from './session';
1212
import type { SharedTargeting } from './shared';
13-
import { getSharedTargeting } from './shared';
13+
import { getLocalHour, getSharedTargeting } from './shared';
1414
import type { True, TrueOrFalse } from './types';
1515
import type { ViewportTargeting } from './viewport';
1616
import { getViewportTargeting } from './viewport';
@@ -25,6 +25,7 @@ type PageTargeting = PartialWithNulls<
2525
at: string; // Ad Test
2626
bp: 'mobile' | 'tablet' | 'desktop'; // BreakPoint
2727
cc: CountryCode; // Country Code
28+
lh: string; // Local Hour
2829
cmp_interaction: string;
2930
consent_tcfv2: string;
3031
dcre: TrueOrFalse; // DotCom-Rendering Eligible
@@ -135,6 +136,7 @@ const buildPageTargeting = ({
135136
const sessionTargeting: SessionTargeting = getSessionTargeting({
136137
adTest: getCookie({ name: 'adtest', shouldMemoize: true }),
137138
countryCode: getLocale(),
139+
localHour: getLocalHour(),
138140
isSignedIn,
139141
pageViewId: window.guardian.config.ophan.pageViewId,
140142
participations: {
@@ -186,5 +188,5 @@ const buildPageTargeting = ({
186188
return pageTargeting;
187189
};
188190

189-
export { buildPageTargeting, filterValues };
191+
export { buildPageTargeting, filterValues, getLocalHour };
190192
export type { PageTargeting };

core/src/targeting/session.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ describe('Session targeting', () => {
77
ab: null,
88
at: null,
99
cc: 'GB',
10+
lh: '12',
1011
pv: '1234567',
1112
ref: null,
1213
si: 'f',
@@ -21,6 +22,7 @@ describe('Session targeting', () => {
2122
adTest: null,
2223
pageViewId: '1234567',
2324
countryCode: 'GB',
25+
localHour: '12',
2426
isSignedIn: false,
2527
});
2628
expect(targeting).toMatchObject(expected);
@@ -48,6 +50,7 @@ describe('Session targeting', () => {
4850
],
4951
at: null,
5052
cc: 'GB',
53+
lh: '12',
5154
pv: '1234567',
5255
ref: null,
5356
si: 'f',
@@ -59,6 +62,7 @@ describe('Session targeting', () => {
5962
adTest: null,
6063
pageViewId: '1234567',
6164
countryCode: 'GB',
65+
localHour: '12',
6266
isSignedIn: false,
6367
});
6468
expect(targeting).toMatchObject(expected);
@@ -77,6 +81,7 @@ describe('Session targeting', () => {
7781
ab: null,
7882
at: null,
7983
cc: 'GB',
84+
lh: '12',
8085
pv: '1234567',
8186
si: 'f',
8287
ref,
@@ -91,6 +96,7 @@ describe('Session targeting', () => {
9196
adTest: null,
9297
pageViewId: '1234567',
9398
countryCode: 'GB',
99+
localHour: '12',
94100
isSignedIn: false,
95101
});
96102
expect(targeting).toMatchObject(expected);
@@ -117,6 +123,7 @@ describe('Session targeting', () => {
117123
adTest: null,
118124
pageViewId: '1234567',
119125
countryCode: 'GB',
126+
localHour: '12',
120127
isSignedIn,
121128
});
122129
expect(targeting).toMatchObject(expected);

core/src/targeting/session.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ type SessionTargeting = {
6666
*/
6767
cc: CountryCode;
6868

69+
/**
70+
* **L**ocal **H**our - [see on Ad Manager][gam]
71+
*
72+
* Type: _Dynamic_
73+
*
74+
* [gam]: https://admanager.google.com/59666047#inventory/custom_targeting/detail/custom_key_id=17299564
75+
*/
76+
lh: string;
77+
6978
/**
7079
* Ophan **P**age **V**iew id – [see on Ad Manager][gam]
7180
*
@@ -161,6 +170,7 @@ const experimentsTargeting = ({
161170
type Session = {
162171
adTest: SessionTargeting['at'];
163172
countryCode: CountryCode;
173+
localHour: string;
164174
isSignedIn: boolean;
165175
pageViewId: SessionTargeting['pv'];
166176
participations: AllParticipations;
@@ -170,6 +180,7 @@ type Session = {
170180
const getSessionTargeting = ({
171181
adTest,
172182
countryCode,
183+
localHour,
173184
isSignedIn,
174185
pageViewId,
175186
participations,
@@ -178,6 +189,7 @@ const getSessionTargeting = ({
178189
ab: experimentsTargeting(participations),
179190
at: adTest,
180191
cc: countryCode,
192+
lh: localHour,
181193
pv: pageViewId,
182194
ref: getReferrer(referrer),
183195
si: isSignedIn ? 't' : 'f',

core/src/targeting/shared.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ const getSurgingParam = (surging: number): SharedTargeting['su'] => {
178178

179179
/* -- Targeting -- */
180180

181+
const getLocalHour = (): string => {
182+
return new Date().getHours().toString();
183+
};
184+
181185
/**
182186
* What goes in comes out
183187
*/
@@ -190,4 +194,4 @@ export const _ = {
190194
};
191195

192196
export type { SharedTargeting };
193-
export { getSharedTargeting };
197+
export { getSharedTargeting, getLocalHour };

0 commit comments

Comments
 (0)