@guardian/commercial-core 30.0.0 → 30.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -0
- package/dist/cjs/email-hash.d.ts +4 -2
- package/dist/cjs/email-hash.js +12 -1
- package/dist/cjs/targeting/build-page-targeting.d.ts +14 -2
- package/dist/cjs/targeting/build-page-targeting.js +2 -1
- package/dist/cjs/targeting/session.d.ts +11 -1
- package/dist/cjs/targeting/session.js +5 -1
- package/dist/cjs/types.d.ts +1 -1
- package/dist/cjs/types.js +1 -1
- package/dist/esm/email-hash.d.ts +4 -2
- package/dist/esm/email-hash.js +12 -2
- package/dist/esm/targeting/build-page-targeting.d.ts +14 -2
- package/dist/esm/targeting/build-page-targeting.js +2 -1
- package/dist/esm/targeting/session.d.ts +11 -1
- package/dist/esm/targeting/session.js +5 -1
- package/dist/esm/types.d.ts +1 -1
- package/dist/esm/types.js +1 -1
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -22,6 +22,15 @@ pnpm build
|
|
|
22
22
|
|
|
23
23
|
This will build the package into the `dist` directory, which is what is published to npm.
|
|
24
24
|
|
|
25
|
+
### Testing
|
|
26
|
+
|
|
27
|
+
To run the unit tests:
|
|
28
|
+
|
|
29
|
+
`pnpm test`
|
|
30
|
+
|
|
31
|
+
This might fail if the base test coverage hasn't been met. This is set in jest.config.js. Ensure you add sufficient tests to meet the threshold if you can. If this is not possible for whatever reason, you can decrease the set thresholds but this should be considered a last resort
|
|
32
|
+
|
|
33
|
+
|
|
25
34
|
#### Beta Releases
|
|
26
35
|
You can add the [beta] @guardian/commercial-core label to your pull request, this will release a beta version of the bundle to NPM, the exact version will be commented on your PR.
|
|
27
36
|
|
package/dist/cjs/email-hash.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
type HashClient = 'id5' | 'uid2';
|
|
1
|
+
type HashClient = 'id5' | 'uid2' | 'euid';
|
|
2
|
+
type Email = `${string}@${string}`;
|
|
3
|
+
declare function normaliseEmail(email: string): Email;
|
|
2
4
|
declare function hashEmailForClient(email: string, client: HashClient): Promise<string>;
|
|
3
|
-
export { hashEmailForClient };
|
|
5
|
+
export { hashEmailForClient, normaliseEmail };
|
package/dist/cjs/email-hash.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.hashEmailForClient = hashEmailForClient;
|
|
4
|
+
exports.normaliseEmail = normaliseEmail;
|
|
4
5
|
function toHex(hashBuffer) {
|
|
5
6
|
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
6
7
|
const hashHex = hashArray
|
|
@@ -13,14 +14,24 @@ function toBase64(hashBuffer) {
|
|
|
13
14
|
const base64Hash = btoa(String.fromCharCode(...hashBytes));
|
|
14
15
|
return base64Hash;
|
|
15
16
|
}
|
|
16
|
-
|
|
17
|
+
function normaliseEmail(email) {
|
|
17
18
|
const normalisedEmail = email.trim().toLowerCase();
|
|
19
|
+
const [name, domain] = normalisedEmail.split('@');
|
|
20
|
+
if (domain !== 'gmail.com') {
|
|
21
|
+
return `${name}@${domain}`;
|
|
22
|
+
}
|
|
23
|
+
const strippedLocal = name?.replaceAll('.', '');
|
|
24
|
+
return `${strippedLocal}@${domain}`;
|
|
25
|
+
}
|
|
26
|
+
async function hashEmailForClient(email, client) {
|
|
27
|
+
const normalisedEmail = normaliseEmail(email);
|
|
18
28
|
const utf8 = new TextEncoder().encode(normalisedEmail);
|
|
19
29
|
const hashBuffer = await crypto.subtle.digest('SHA-256', utf8);
|
|
20
30
|
switch (client) {
|
|
21
31
|
case 'id5':
|
|
22
32
|
return toHex(hashBuffer);
|
|
23
33
|
case 'uid2':
|
|
34
|
+
case 'euid':
|
|
24
35
|
return toBase64(hashBuffer);
|
|
25
36
|
}
|
|
26
37
|
}
|
|
@@ -30,6 +30,7 @@ type PageTargeting = PartialWithNulls<{
|
|
|
30
30
|
s: string;
|
|
31
31
|
sens: TrueOrFalse;
|
|
32
32
|
si: TrueOrFalse;
|
|
33
|
+
idp: string[];
|
|
33
34
|
skinsize: 'l' | 's';
|
|
34
35
|
urlkw: string[];
|
|
35
36
|
vl: string;
|
|
@@ -37,13 +38,24 @@ type PageTargeting = PartialWithNulls<{
|
|
|
37
38
|
[_: string]: string | string[];
|
|
38
39
|
} & SharedTargeting>;
|
|
39
40
|
declare const filterValues: (pageTargets: Record<string, unknown>) => Record<string, string | string[]>;
|
|
41
|
+
type UserId = {
|
|
42
|
+
name: string;
|
|
43
|
+
params?: Record<string, string | number>;
|
|
44
|
+
storage?: {
|
|
45
|
+
type: 'cookie' | 'html5';
|
|
46
|
+
name: string;
|
|
47
|
+
expires: number;
|
|
48
|
+
refreshInSeconds?: number;
|
|
49
|
+
};
|
|
50
|
+
};
|
|
40
51
|
type BuildPageTargetingParams = {
|
|
41
52
|
adFree: boolean;
|
|
42
53
|
clientSideParticipations: Participations;
|
|
43
54
|
consentState: ConsentState;
|
|
44
55
|
isSignedIn?: boolean;
|
|
45
56
|
youtube?: boolean;
|
|
57
|
+
idProviders?: UserId[];
|
|
46
58
|
};
|
|
47
|
-
declare const buildPageTargeting: ({ adFree, clientSideParticipations, consentState, isSignedIn, youtube, }: BuildPageTargetingParams) => Record<string, string | string[]>;
|
|
59
|
+
declare const buildPageTargeting: ({ adFree, clientSideParticipations, consentState, isSignedIn, youtube, idProviders, }: BuildPageTargetingParams) => Record<string, string | string[]>;
|
|
48
60
|
export { buildPageTargeting, filterValues, getLocalHour };
|
|
49
|
-
export type { PageTargeting };
|
|
61
|
+
export type { UserId, PageTargeting };
|
|
@@ -49,7 +49,7 @@ const isFirstVisit = (referrer) => {
|
|
|
49
49
|
}
|
|
50
50
|
return !referrerMatchesHost(referrer);
|
|
51
51
|
};
|
|
52
|
-
const buildPageTargeting = ({ adFree, clientSideParticipations, consentState, isSignedIn = false, youtube = false, }) => {
|
|
52
|
+
const buildPageTargeting = ({ adFree, clientSideParticipations, consentState, isSignedIn = false, youtube = false, idProviders = [], }) => {
|
|
53
53
|
const { page, isDotcomRendering } = window.guardian.config;
|
|
54
54
|
const adFreeTargeting = adFree ? { af: 't' } : {};
|
|
55
55
|
const sharedAdTargeting = page.sharedAdTargeting
|
|
@@ -80,6 +80,7 @@ const buildPageTargeting = ({ adFree, clientSideParticipations, consentState, is
|
|
|
80
80
|
betaAbTestParticipations: window.guardian.modules.abTests?.getParticipations() ?? {},
|
|
81
81
|
},
|
|
82
82
|
referrer,
|
|
83
|
+
idProviders,
|
|
83
84
|
});
|
|
84
85
|
const getViewport = () => {
|
|
85
86
|
return {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Participations } from '@guardian/ab-core';
|
|
2
2
|
import type { CountryCode } from '@guardian/libs';
|
|
3
|
+
import type { UserId } from '../targeting/build-page-targeting.js';
|
|
3
4
|
import type { False, True } from './types.js';
|
|
4
5
|
declare const referrers: readonly [{
|
|
5
6
|
readonly id: "facebook";
|
|
@@ -97,6 +98,14 @@ type SessionTargeting = {
|
|
|
97
98
|
* [gam]: https://admanager.google.com/59666047#inventory/custom_targeting/detail/custom_key_id=215727
|
|
98
99
|
*/
|
|
99
100
|
si: True | False;
|
|
101
|
+
/**
|
|
102
|
+
* **I**d **P**roviders – [see on Ad Manager][gam]
|
|
103
|
+
*
|
|
104
|
+
* Denote which id providers have been integrated.
|
|
105
|
+
*
|
|
106
|
+
* [gam]: To be added
|
|
107
|
+
*/
|
|
108
|
+
idp: string[] | null;
|
|
100
109
|
};
|
|
101
110
|
type AllParticipations = {
|
|
102
111
|
clientSideParticipations: Participations;
|
|
@@ -119,7 +128,8 @@ type Session = {
|
|
|
119
128
|
pageViewId: SessionTargeting['pv'];
|
|
120
129
|
participations: AllParticipations;
|
|
121
130
|
referrer: string;
|
|
131
|
+
idProviders: UserId[];
|
|
122
132
|
};
|
|
123
|
-
declare const getSessionTargeting: ({ adTest, countryCode, localHour, isSignedIn, pageViewId, participations, referrer, }: Session) => SessionTargeting;
|
|
133
|
+
declare const getSessionTargeting: ({ adTest, countryCode, localHour, isSignedIn, pageViewId, participations, referrer, idProviders, }: Session) => SessionTargeting;
|
|
124
134
|
export type { SessionTargeting, AllParticipations };
|
|
125
135
|
export { getSessionTargeting, experimentsTargeting };
|
|
@@ -63,7 +63,10 @@ const experimentsTargeting = ({ clientSideParticipations, serverSideParticipatio
|
|
|
63
63
|
return [...clientSideExperiment, ...serverSideExperiments, ...betaAbTests];
|
|
64
64
|
};
|
|
65
65
|
exports.experimentsTargeting = experimentsTargeting;
|
|
66
|
-
const
|
|
66
|
+
const getIdProviders = (userIds) => {
|
|
67
|
+
return userIds.map((id) => id.name);
|
|
68
|
+
};
|
|
69
|
+
const getSessionTargeting = ({ adTest, countryCode, localHour, isSignedIn, pageViewId, participations, referrer, idProviders, }) => ({
|
|
67
70
|
ab: experimentsTargeting(participations),
|
|
68
71
|
at: adTest,
|
|
69
72
|
cc: countryCode,
|
|
@@ -71,5 +74,6 @@ const getSessionTargeting = ({ adTest, countryCode, localHour, isSignedIn, pageV
|
|
|
71
74
|
pv: pageViewId,
|
|
72
75
|
ref: getReferrer(referrer),
|
|
73
76
|
si: isSignedIn ? 't' : 'f',
|
|
77
|
+
idp: getIdProviders(idProviders),
|
|
74
78
|
});
|
|
75
79
|
exports.getSessionTargeting = getSessionTargeting;
|
package/dist/cjs/types.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { EventPayload } from '@guardian/ophan-tracker-js';
|
|
2
2
|
import type { AdSize, SizeMapping } from './ad-sizes.js';
|
|
3
3
|
import type { PageTargeting } from './targeting/build-page-targeting.js';
|
|
4
|
-
import '
|
|
4
|
+
import '@types/google-publisher-tag';
|
|
5
5
|
type HeaderBiddingSize = AdSize;
|
|
6
6
|
interface Advert {
|
|
7
7
|
id: string;
|
package/dist/cjs/types.js
CHANGED
package/dist/esm/email-hash.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
type HashClient = 'id5' | 'uid2';
|
|
1
|
+
type HashClient = 'id5' | 'uid2' | 'euid';
|
|
2
|
+
type Email = `${string}@${string}`;
|
|
3
|
+
declare function normaliseEmail(email: string): Email;
|
|
2
4
|
declare function hashEmailForClient(email: string, client: HashClient): Promise<string>;
|
|
3
|
-
export { hashEmailForClient };
|
|
5
|
+
export { hashEmailForClient, normaliseEmail };
|
package/dist/esm/email-hash.js
CHANGED
|
@@ -10,15 +10,25 @@ function toBase64(hashBuffer) {
|
|
|
10
10
|
const base64Hash = btoa(String.fromCharCode(...hashBytes));
|
|
11
11
|
return base64Hash;
|
|
12
12
|
}
|
|
13
|
-
|
|
13
|
+
function normaliseEmail(email) {
|
|
14
14
|
const normalisedEmail = email.trim().toLowerCase();
|
|
15
|
+
const [name, domain] = normalisedEmail.split('@');
|
|
16
|
+
if (domain !== 'gmail.com') {
|
|
17
|
+
return `${name}@${domain}`;
|
|
18
|
+
}
|
|
19
|
+
const strippedLocal = name?.replaceAll('.', '');
|
|
20
|
+
return `${strippedLocal}@${domain}`;
|
|
21
|
+
}
|
|
22
|
+
async function hashEmailForClient(email, client) {
|
|
23
|
+
const normalisedEmail = normaliseEmail(email);
|
|
15
24
|
const utf8 = new TextEncoder().encode(normalisedEmail);
|
|
16
25
|
const hashBuffer = await crypto.subtle.digest('SHA-256', utf8);
|
|
17
26
|
switch (client) {
|
|
18
27
|
case 'id5':
|
|
19
28
|
return toHex(hashBuffer);
|
|
20
29
|
case 'uid2':
|
|
30
|
+
case 'euid':
|
|
21
31
|
return toBase64(hashBuffer);
|
|
22
32
|
}
|
|
23
33
|
}
|
|
24
|
-
export { hashEmailForClient };
|
|
34
|
+
export { hashEmailForClient, normaliseEmail };
|
|
@@ -30,6 +30,7 @@ type PageTargeting = PartialWithNulls<{
|
|
|
30
30
|
s: string;
|
|
31
31
|
sens: TrueOrFalse;
|
|
32
32
|
si: TrueOrFalse;
|
|
33
|
+
idp: string[];
|
|
33
34
|
skinsize: 'l' | 's';
|
|
34
35
|
urlkw: string[];
|
|
35
36
|
vl: string;
|
|
@@ -37,13 +38,24 @@ type PageTargeting = PartialWithNulls<{
|
|
|
37
38
|
[_: string]: string | string[];
|
|
38
39
|
} & SharedTargeting>;
|
|
39
40
|
declare const filterValues: (pageTargets: Record<string, unknown>) => Record<string, string | string[]>;
|
|
41
|
+
type UserId = {
|
|
42
|
+
name: string;
|
|
43
|
+
params?: Record<string, string | number>;
|
|
44
|
+
storage?: {
|
|
45
|
+
type: 'cookie' | 'html5';
|
|
46
|
+
name: string;
|
|
47
|
+
expires: number;
|
|
48
|
+
refreshInSeconds?: number;
|
|
49
|
+
};
|
|
50
|
+
};
|
|
40
51
|
type BuildPageTargetingParams = {
|
|
41
52
|
adFree: boolean;
|
|
42
53
|
clientSideParticipations: Participations;
|
|
43
54
|
consentState: ConsentState;
|
|
44
55
|
isSignedIn?: boolean;
|
|
45
56
|
youtube?: boolean;
|
|
57
|
+
idProviders?: UserId[];
|
|
46
58
|
};
|
|
47
|
-
declare const buildPageTargeting: ({ adFree, clientSideParticipations, consentState, isSignedIn, youtube, }: BuildPageTargetingParams) => Record<string, string | string[]>;
|
|
59
|
+
declare const buildPageTargeting: ({ adFree, clientSideParticipations, consentState, isSignedIn, youtube, idProviders, }: BuildPageTargetingParams) => Record<string, string | string[]>;
|
|
48
60
|
export { buildPageTargeting, filterValues, getLocalHour };
|
|
49
|
-
export type { PageTargeting };
|
|
61
|
+
export type { UserId, PageTargeting };
|
|
@@ -44,7 +44,7 @@ const isFirstVisit = (referrer) => {
|
|
|
44
44
|
}
|
|
45
45
|
return !referrerMatchesHost(referrer);
|
|
46
46
|
};
|
|
47
|
-
const buildPageTargeting = ({ adFree, clientSideParticipations, consentState, isSignedIn = false, youtube = false, }) => {
|
|
47
|
+
const buildPageTargeting = ({ adFree, clientSideParticipations, consentState, isSignedIn = false, youtube = false, idProviders = [], }) => {
|
|
48
48
|
const { page, isDotcomRendering } = window.guardian.config;
|
|
49
49
|
const adFreeTargeting = adFree ? { af: 't' } : {};
|
|
50
50
|
const sharedAdTargeting = page.sharedAdTargeting
|
|
@@ -75,6 +75,7 @@ const buildPageTargeting = ({ adFree, clientSideParticipations, consentState, is
|
|
|
75
75
|
betaAbTestParticipations: window.guardian.modules.abTests?.getParticipations() ?? {},
|
|
76
76
|
},
|
|
77
77
|
referrer,
|
|
78
|
+
idProviders,
|
|
78
79
|
});
|
|
79
80
|
const getViewport = () => {
|
|
80
81
|
return {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Participations } from '@guardian/ab-core';
|
|
2
2
|
import type { CountryCode } from '@guardian/libs';
|
|
3
|
+
import type { UserId } from '../targeting/build-page-targeting.js';
|
|
3
4
|
import type { False, True } from './types.js';
|
|
4
5
|
declare const referrers: readonly [{
|
|
5
6
|
readonly id: "facebook";
|
|
@@ -97,6 +98,14 @@ type SessionTargeting = {
|
|
|
97
98
|
* [gam]: https://admanager.google.com/59666047#inventory/custom_targeting/detail/custom_key_id=215727
|
|
98
99
|
*/
|
|
99
100
|
si: True | False;
|
|
101
|
+
/**
|
|
102
|
+
* **I**d **P**roviders – [see on Ad Manager][gam]
|
|
103
|
+
*
|
|
104
|
+
* Denote which id providers have been integrated.
|
|
105
|
+
*
|
|
106
|
+
* [gam]: To be added
|
|
107
|
+
*/
|
|
108
|
+
idp: string[] | null;
|
|
100
109
|
};
|
|
101
110
|
type AllParticipations = {
|
|
102
111
|
clientSideParticipations: Participations;
|
|
@@ -119,7 +128,8 @@ type Session = {
|
|
|
119
128
|
pageViewId: SessionTargeting['pv'];
|
|
120
129
|
participations: AllParticipations;
|
|
121
130
|
referrer: string;
|
|
131
|
+
idProviders: UserId[];
|
|
122
132
|
};
|
|
123
|
-
declare const getSessionTargeting: ({ adTest, countryCode, localHour, isSignedIn, pageViewId, participations, referrer, }: Session) => SessionTargeting;
|
|
133
|
+
declare const getSessionTargeting: ({ adTest, countryCode, localHour, isSignedIn, pageViewId, participations, referrer, idProviders, }: Session) => SessionTargeting;
|
|
124
134
|
export type { SessionTargeting, AllParticipations };
|
|
125
135
|
export { getSessionTargeting, experimentsTargeting };
|
|
@@ -59,7 +59,10 @@ const experimentsTargeting = ({ clientSideParticipations, serverSideParticipatio
|
|
|
59
59
|
}
|
|
60
60
|
return [...clientSideExperiment, ...serverSideExperiments, ...betaAbTests];
|
|
61
61
|
};
|
|
62
|
-
const
|
|
62
|
+
const getIdProviders = (userIds) => {
|
|
63
|
+
return userIds.map((id) => id.name);
|
|
64
|
+
};
|
|
65
|
+
const getSessionTargeting = ({ adTest, countryCode, localHour, isSignedIn, pageViewId, participations, referrer, idProviders, }) => ({
|
|
63
66
|
ab: experimentsTargeting(participations),
|
|
64
67
|
at: adTest,
|
|
65
68
|
cc: countryCode,
|
|
@@ -67,5 +70,6 @@ const getSessionTargeting = ({ adTest, countryCode, localHour, isSignedIn, pageV
|
|
|
67
70
|
pv: pageViewId,
|
|
68
71
|
ref: getReferrer(referrer),
|
|
69
72
|
si: isSignedIn ? 't' : 'f',
|
|
73
|
+
idp: getIdProviders(idProviders),
|
|
70
74
|
});
|
|
71
75
|
export { getSessionTargeting, experimentsTargeting };
|
package/dist/esm/types.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { EventPayload } from '@guardian/ophan-tracker-js';
|
|
2
2
|
import type { AdSize, SizeMapping } from './ad-sizes.js';
|
|
3
3
|
import type { PageTargeting } from './targeting/build-page-targeting.js';
|
|
4
|
-
import '
|
|
4
|
+
import '@types/google-publisher-tag';
|
|
5
5
|
type HeaderBiddingSize = AdSize;
|
|
6
6
|
interface Advert {
|
|
7
7
|
id: string;
|
package/dist/esm/types.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@guardian/commercial-core",
|
|
3
|
-
"version": "30.
|
|
3
|
+
"version": "30.2.0",
|
|
4
4
|
"description": "Guardian advertising business logic",
|
|
5
5
|
"homepage": "https://github.com/guardian/commercial#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -37,11 +37,11 @@
|
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@guardian/ab-core": "9.0.0",
|
|
40
|
-
"@guardian/libs": "27.0.0"
|
|
40
|
+
"@guardian/libs": "27.0.0",
|
|
41
|
+
"@types/google-publisher-tag": "~1.20251117.0"
|
|
41
42
|
},
|
|
42
43
|
"devDependencies": {
|
|
43
44
|
"@guardian/ophan-tracker-js": "2.6.3",
|
|
44
|
-
"@types/google-publisher-tag": "~1.20251117.0",
|
|
45
45
|
"@types/jest": "30.0.0",
|
|
46
46
|
"@types/node": "24.10.1",
|
|
47
47
|
"jest": "^30.2.0",
|
|
@@ -64,6 +64,7 @@
|
|
|
64
64
|
"prettier:check": "prettier . --check --cache",
|
|
65
65
|
"prettier:fix": "prettier . --write --cache",
|
|
66
66
|
"test": "jest",
|
|
67
|
+
"test-cov": "jest --coverage",
|
|
67
68
|
"tsc": "tsc --noEmit"
|
|
68
69
|
}
|
|
69
70
|
}
|