Skip to content

Commit 1df9702

Browse files
authored
Feat/dev host fix (#5)
* devHosts match fix * Updated readme * Updated variable
1 parent e6b7886 commit 1df9702

5 files changed

Lines changed: 52 additions & 27 deletions

File tree

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ npm i @phntms/ft-lib
2323

2424
## Usage
2525

26-
consentMonitor - Adds an FT consent cookie poller to enable/disable Permutive consent (only useful if 'consentRequired' is set as an option in the site's Permutive script - see here[https://support.permutive.com/hc/en-us/articles/360010845519-Seeking-User-Consent#h_00327830-509b-422a-952b-1906264031f1])
27-
The optional constructor arg (default ["localhost", "phq"]) accepts a string or array of strings to includes match against window.location.host.
28-
The utility also includes an FT banner event listener ((see here[https://registry.origami.ft.com/components/o-cookie-message@6.0.1/readme?brand=master] for a list of banner DOM events) to set session-level cookies for development environments
26+
consentMonitor - Adds an FT consent cookie poller to enable/disable Permutive consent (only useful if 'consentRequired' is set as an option in the site's Permutive script - see [here](https://support.permutive.com/hc/en-us/articles/360010845519-Seeking-User-Consent#h_00327830-509b-422a-952b-1906264031f1)
27+
There are optional constructor args for the hostname (defaults to window.location.hostname) and the dev environment host matches (defaults to ["localhost", "phq", "vercel.app"]) which are used to determine development environments in order to generate an FT banner event listener ((see [here](https://registry.origami.ft.com/components/o-cookie-message@6.0.1/readme?brand=master) for a list of banner DOM events) to set session-level cookies for development environments
2928

3029
```Javascript
3130
import { consentMonitor } from "@phntms/ft-lib";
3231

33-
new consentMonitor(["localhost", "phq", "staging"]);
32+
new consentMonitor("FT.staging.testsite.com". ["localhost", "phq", "staging"]);
3433
```
3534

3635
permutiveVideoUtils - emitPermutiveProgressEvents - used within a video player's progress event handler to fire Permutive video progress events at [0, 25, 50, 75, 100] percent progress.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@phntms/ft-lib",
33
"description": "A collection of Javascript UI & tracking utils for FT sites",
4-
"version": "0.0.1",
4+
"version": "0.0.2",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",
77
"homepage": "https://github.com/phantomstudios/ft-lib#readme",

src/consentMonitor/index.ts

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@ const debug = Debug("@phntms/ft-lib");
88
export class consentMonitor {
99
protected _consent = false;
1010
protected _devHosts: string[] | string;
11+
protected _isDevEnvironment = false;
12+
protected _hostname: string;
1113

12-
constructor(devHosts: string[] | string = ["localhost", "phq"]) {
14+
constructor(
15+
hostname: string = window.location.hostname,
16+
devHosts: string[] | string = ["localhost", "phq", "vercel.app"]
17+
) {
1318
this._devHosts = devHosts;
19+
this._hostname = hostname;
1420
this.init();
1521
}
1622

@@ -22,6 +28,10 @@ export class consentMonitor {
2228
return this._devHosts;
2329
}
2430

31+
get isDevEnvironment(): boolean {
32+
return this._isDevEnvironment;
33+
}
34+
2535
getCookieValue = (name: string) =>
2636
document.cookie.match("(^|;)\\s*" + name + "\\s*=\\s*([^;]+)")?.pop() || "";
2737

@@ -56,26 +66,33 @@ export class consentMonitor {
5666
}, 3000);
5767

5868
//Simulate cookie consent behaviour in non-prod environments - TODO confirm best non production conditional..
59-
if (this._devHosts.includes(window.location.hostname)) {
60-
debug("setting development environment from host match");
61-
const oCookieMessage =
62-
document.getElementsByClassName("o-cookie-message")[0];
63-
if (oCookieMessage) {
64-
const onCookieMessageAct = () => {
65-
debug("setting development FT consent cookies");
66-
document.cookie = "FTConsent=behaviouraladsOnsite%3Aon";
67-
document.cookie = "FTCookieConsentGDPR=true";
68-
oCookieMessage.removeEventListener(
69-
"oCookieMessage.act",
70-
onCookieMessageAct,
71-
false
72-
);
73-
};
74-
oCookieMessage.addEventListener(
69+
if (Array.isArray(this._devHosts)) {
70+
this._devHosts.map(
71+
(devHost) =>
72+
this._hostname.includes(devHost) && this.setDevCookieHandler()
73+
);
74+
} else {
75+
if (this._hostname.includes(this._devHosts)) this.setDevCookieHandler();
76+
}
77+
};
78+
79+
setDevCookieHandler = () => {
80+
this._isDevEnvironment = true;
81+
debug("setting development environment from host match");
82+
const oCookieMessage =
83+
document.getElementsByClassName("o-cookie-message")[0];
84+
if (oCookieMessage) {
85+
const onCookieMessageAct = () => {
86+
debug("setting development FT consent cookies");
87+
document.cookie = "FTConsent=behaviouraladsOnsite%3Aon";
88+
document.cookie = "FTCookieConsentGDPR=true";
89+
oCookieMessage.removeEventListener(
7590
"oCookieMessage.act",
76-
onCookieMessageAct
91+
onCookieMessageAct,
92+
false
7793
);
78-
}
94+
};
95+
oCookieMessage.addEventListener("oCookieMessage.act", onCookieMessageAct);
7996
}
8097
};
8198
}

test/index.test.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,17 @@ describe("consentMonitor", () => {
77
expect(obj.consent).toBe(false);
88
});
99
it("sets default dev hosts property", async () => {
10-
const obj = new consentMonitor();
11-
expect(obj.devHosts).toEqual(["localhost", "phq"]);
10+
const obj = new consentMonitor("https://FT-staging.com");
11+
expect(obj.devHosts).toEqual(["localhost", "phq", "vercel.app"]);
12+
});
13+
it("matches hostname to devHosts", async () => {
14+
const obj = new consentMonitor("https://FT-staging.devhost.com", [
15+
"localhost",
16+
"vercel.app",
17+
"phq",
18+
"devhost",
19+
]);
20+
expect(obj.isDevEnvironment).toBe(true);
1221
});
1322
//TODO - mock document.cookie get/set and test consent value
1423
});

0 commit comments

Comments
 (0)