|
| 1 | +import { |
| 2 | + initSourcepointCmp, |
| 3 | + interceptManageCookiesLinks, |
| 4 | + properties, |
| 5 | +} from "@financial-times/cmp-client"; |
1 | 6 | import Debug from "debug"; |
2 | | -const debug = Debug("@phantomstudios/ft-lib"); |
| 7 | + |
| 8 | +import { enqueueCmpCallback, loadFtCmpScript } from "../cmp/loadFtCmp"; |
| 9 | + |
| 10 | +const debug = Debug("@phantomstudios/ft-lib/consentMonitor"); |
3 | 11 |
|
4 | 12 | const DEFAULT_DEV_HOSTS = ["localhost", "phq", ".app", "preview"]; |
5 | 13 |
|
6 | | -export class consentMonitor { |
7 | | - protected _consent = false; |
8 | | - protected _devHosts: string[]; |
9 | | - protected _isDevEnvironment = false; |
10 | | - protected _hostname: string; |
11 | | - protected _isInitialized = false; |
| 14 | +interface ConsentReadyInfo { |
| 15 | + consentedToAll: boolean; |
| 16 | +} |
| 17 | +type ConsentReadyHandler = ( |
| 18 | + legislation: string, |
| 19 | + uuid: string, |
| 20 | + tcData: unknown, |
| 21 | + info: ConsentReadyInfo, |
| 22 | +) => void; |
| 23 | +type MessageChoiceHandler = ( |
| 24 | + legislation: string, |
| 25 | + choiceId: number, |
| 26 | + choiceTypeId: number, |
| 27 | +) => void; |
12 | 28 |
|
13 | | - constructor(hostname?: string, devHosts?: string[] | string) { |
14 | | - if (Array.isArray(devHosts)) { |
15 | | - this._devHosts = devHosts.concat(DEFAULT_DEV_HOSTS); |
16 | | - } else if (devHosts === undefined) { |
17 | | - this._devHosts = DEFAULT_DEV_HOSTS; |
18 | | - } else { |
19 | | - this._devHosts = DEFAULT_DEV_HOSTS; |
20 | | - this._devHosts.push(devHosts); |
21 | | - } |
| 29 | +const CMP_CHOICE_ACCEPT_ALL = 11; |
| 30 | +const CMP_CHOICE_REJECT_ALL = 13; |
22 | 31 |
|
23 | | - this._hostname = hostname || window.location.hostname; |
24 | | - this.init(); |
25 | | - } |
| 32 | +export class ConsentMonitor { |
| 33 | + private _consent = false; |
| 34 | + private _devHosts: string[]; |
| 35 | + private _isDevEnvironment = false; |
| 36 | + private _isInitialized = false; |
| 37 | + private _hostname: string; |
26 | 38 |
|
27 | | - get consent(): boolean { |
| 39 | + public get consent(): boolean { |
28 | 40 | return this._consent; |
29 | 41 | } |
30 | | - |
31 | | - get devHosts(): string[] | string { |
| 42 | + public get devHosts(): string[] { |
32 | 43 | return this._devHosts; |
33 | 44 | } |
34 | | - |
35 | | - get isDevEnvironment(): boolean { |
| 45 | + public get isDevEnvironment(): boolean { |
36 | 46 | return this._isDevEnvironment; |
37 | 47 | } |
38 | | - |
39 | | - get isInitialized(): boolean { |
| 48 | + public get isInitialized(): boolean { |
40 | 49 | return this._isInitialized; |
41 | 50 | } |
42 | 51 |
|
| 52 | + public get userHasConsented(): boolean { |
| 53 | + return this._consent; |
| 54 | + } |
| 55 | + |
43 | 56 | getCookieValue = (name: string) => |
44 | 57 | document.cookie.match("(^|;)\\s*" + name + "\\s*=\\s*([^;]+)")?.pop() || ""; |
45 | 58 |
|
46 | | - init = () => { |
47 | | - this.cookieConsentTest(); |
48 | | - setInterval(this.cookieConsentTest, 3000); |
| 59 | + constructor(hostname?: string, devHosts?: string[] | string) { |
| 60 | + if (Array.isArray(devHosts)) { |
| 61 | + this._devHosts = [...devHosts, ...DEFAULT_DEV_HOSTS]; |
| 62 | + } else if (devHosts === undefined) { |
| 63 | + this._devHosts = [...DEFAULT_DEV_HOSTS]; |
| 64 | + } else { |
| 65 | + this._devHosts = [...DEFAULT_DEV_HOSTS, devHosts]; |
| 66 | + } |
49 | 67 |
|
50 | | - //Simulate cookie consent behaviour in non-prod environments |
51 | | - this._devHosts.map( |
52 | | - (devHost) => |
53 | | - this._hostname.includes(devHost) && this.setDevCookieHandler(), |
| 68 | + this._hostname = hostname || window.location.hostname; |
| 69 | + |
| 70 | + this._isDevEnvironment = this._devHosts.some((h) => |
| 71 | + this._hostname.includes(h), |
54 | 72 | ); |
55 | | - }; |
56 | 73 |
|
57 | | - cookieConsentTest = () => { |
58 | | - if (window.permutive) { |
59 | | - if (!this._isInitialized) { |
60 | | - if ( |
61 | | - this.getCookieValue("FTConsent").includes("behaviouraladsOnsite%3Aon") |
62 | | - ) { |
63 | | - this.permutiveConsentOn(); |
| 74 | + loadFtCmpScript() |
| 75 | + .then(() => { |
| 76 | + this.attachCmpListeners(); |
| 77 | + |
| 78 | + const propertyConfig = window.location.hostname.endsWith(".ft.com") |
| 79 | + ? properties["FT_DOTCOM_PROD"] |
| 80 | + : properties["FT_DOTCOM_TEST"]; |
| 81 | + |
| 82 | + // initialize CMP |
| 83 | + initSourcepointCmp({ propertyConfig }); |
| 84 | + // use cmp client lib to intercept footer 'Manage Cookies' links (opens privacy modal) |
| 85 | + // Note, function requires very specific link: text = 'Manage Cookies' and href = 'https://ft.com/preferences/manage-cookies' |
| 86 | + interceptManageCookiesLinks(); |
| 87 | + this._isInitialized = true; |
| 88 | + }) |
| 89 | + .catch((err) => console.error(err)); |
| 90 | + } |
| 91 | + |
| 92 | + private attachCmpListeners(): void { |
| 93 | + enqueueCmpCallback(() => { |
| 94 | + const onReady: ConsentReadyHandler = (_l, _u, _t, info) => { |
| 95 | + debug("onConsentReady:", info); |
| 96 | + if (info.consentedToAll) { |
| 97 | + this.enablePermutive(); |
64 | 98 | } else { |
65 | | - this.permutiveConsentOff(); |
| 99 | + this.disablePermutive(); |
66 | 100 | } |
67 | | - this._isInitialized = true; |
68 | | - } else if ( |
69 | | - this.getCookieValue("FTConsent").includes( |
70 | | - "behaviouraladsOnsite%3Aon", |
71 | | - ) && |
72 | | - !this.consent |
73 | | - ) { |
74 | | - debug("setting permutive tracking consent: on"); |
75 | | - this.permutiveConsentOn(); |
76 | | - } else if ( |
77 | | - !this.getCookieValue("FTConsent").includes( |
78 | | - "behaviouraladsOnsite%3Aon", |
79 | | - ) && |
80 | | - this.consent |
81 | | - ) { |
82 | | - debug("setting permutive tracking consent: off"); |
83 | | - this.permutiveConsentOff(); |
84 | | - } |
85 | | - } |
86 | | - }; |
| 101 | + }; |
87 | 102 |
|
88 | | - setDevCookieHandler = () => { |
89 | | - this._isDevEnvironment = true; |
90 | | - debug("setting development environment from host match"); |
91 | | - const oCookieMessage = |
92 | | - document.getElementsByClassName("o-cookie-message")[0]; |
93 | | - if (oCookieMessage) { |
94 | | - const onCookieMessageAct = () => { |
95 | | - debug("setting development FT consent cookies"); |
96 | | - document.cookie = "FTConsent=behaviouraladsOnsite%3Aon"; |
97 | | - document.cookie = "FTCookieConsentGDPR=true"; |
98 | | - oCookieMessage.removeEventListener( |
99 | | - "oCookieMessage.act", |
100 | | - onCookieMessageAct, |
101 | | - false, |
| 103 | + const onChoice: MessageChoiceHandler = (_l, _c, typeId) => { |
| 104 | + debug("onMessageChoiceSelect:", typeId); |
| 105 | + if (typeId === CMP_CHOICE_ACCEPT_ALL) this.enablePermutive(); |
| 106 | + else if (typeId === CMP_CHOICE_REJECT_ALL) this.disablePermutive(); |
| 107 | + |
| 108 | + //Simulate cookie consent behaviour in non-prod environments as banner does not set cookies in non .ft.com domains |
| 109 | + this._devHosts.map( |
| 110 | + (devHost) => |
| 111 | + this._hostname.includes(devHost) && |
| 112 | + typeId === CMP_CHOICE_ACCEPT_ALL && |
| 113 | + this.setDevConsentCookies(), |
102 | 114 | ); |
| 115 | + |
| 116 | + // banner updated - check new cookie value to fire consent_update event |
| 117 | + setTimeout(this.cookieConsentTest, 3000); |
103 | 118 | }; |
104 | | - oCookieMessage.addEventListener("oCookieMessage.act", onCookieMessageAct); |
105 | | - } |
106 | | - }; |
107 | 119 |
|
108 | | - permutiveConsentOn = () => { |
109 | | - window.permutive.consent({ |
| 120 | + window._sp_.addEventListener?.("onConsentReady", onReady); |
| 121 | + window._sp_.addEventListener?.("onMessageChoiceSelect", onChoice); |
| 122 | + }); |
| 123 | + } |
| 124 | + |
| 125 | + private enablePermutive(): void { |
| 126 | + if (this._consent) return; |
| 127 | + debug("Permutive consent: ON"); |
| 128 | + window.permutive?.consent({ |
110 | 129 | opt_in: true, |
111 | 130 | token: "behaviouraladsOnsite:on", |
112 | 131 | }); |
113 | 132 | this._consent = true; |
114 | | - }; |
| 133 | + } |
115 | 134 |
|
116 | | - permutiveConsentOff = () => { |
117 | | - window.permutive.consent({ opt_in: false }); |
| 135 | + private disablePermutive(): void { |
| 136 | + if (!this._consent) return; |
| 137 | + debug("Permutive consent: OFF"); |
| 138 | + window.permutive?.consent({ opt_in: false }); |
118 | 139 | this._consent = false; |
| 140 | + } |
| 141 | + |
| 142 | + //check for FTConsent - cookiesOnSite to trigger custom consent_update event for GTM tags (banner updated) |
| 143 | + cookieConsentTest = () => { |
| 144 | + if (!this._isInitialized || !window || !window.dataLayer) return; |
| 145 | + if (this.getCookieValue("FTConsent").includes("cookiesOnsite%3Aon")) { |
| 146 | + //send consent_update event |
| 147 | + window.dataLayer.push({ |
| 148 | + event: "consent_update", |
| 149 | + consent: true, |
| 150 | + }); |
| 151 | + } else { |
| 152 | + window.dataLayer.push({ |
| 153 | + event: "consent_update", |
| 154 | + consent: false, |
| 155 | + }); |
| 156 | + } |
| 157 | + }; |
| 158 | + |
| 159 | + setDevConsentCookies = () => { |
| 160 | + this._isDevEnvironment = true; |
| 161 | + debug("setting development FT consent cookies"); |
| 162 | + document.cookie = |
| 163 | + "FTConsent=behaviouraladsOnsite%3Aon%2CcookiesOnsite%3Aon%2CpermutiveadsOnsite%3Aon"; |
| 164 | + document.cookie = "FTCookieConsentGDPR=true"; |
119 | 165 | }; |
120 | 166 | } |
| 167 | + |
| 168 | +export { ConsentMonitor as consentMonitor }; |
0 commit comments