@naturalcycles/internal-web-lib 1.2.1 → 1.3.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/bundle/analyticsClient.js.map +1 -1
- package/bundle/conversionClient.js +75 -29
- package/bundle/conversionClient.js.map +1 -1
- package/bundle/cookieMonster.js +4 -0
- package/bundle/cookieMonster.js.map +1 -1
- package/dist/analytics/analyticsClient.d.ts +2 -2
- package/dist/conversion/conversionClient.d.ts +13 -4
- package/dist/conversion/conversionClient.js +37 -11
- package/dist/cookie-monster/cookie-monster-element.js +4 -0
- package/package.json +3 -3
|
@@ -3,7 +3,7 @@ import { AppError } from '@naturalcycles/js-lib/error';
|
|
|
3
3
|
import type { ErrorData } from '@naturalcycles/js-lib/error';
|
|
4
4
|
import type { CommonLogger } from '@naturalcycles/js-lib/log';
|
|
5
5
|
import type { AnyObject, NumberOfMilliseconds, PositiveInteger, UnixTimestampMillis } from '@naturalcycles/js-lib/types';
|
|
6
|
-
import type { AnalyticsEventListener, ClientId,
|
|
6
|
+
import type { AnalyticsEventListener, ClientId, AnalyticsDistinctId } from '@naturalcycles/shared';
|
|
7
7
|
export type { AnalyticsEventListener } from '@naturalcycles/shared';
|
|
8
8
|
/**
|
|
9
9
|
* Self-hosted analytics event client.
|
|
@@ -151,7 +151,7 @@ export declare class AnalyticsIdentity {
|
|
|
151
151
|
*/
|
|
152
152
|
private memoryEntry;
|
|
153
153
|
private hasRefreshedExpiry;
|
|
154
|
-
getDistinctId():
|
|
154
|
+
getDistinctId(): AnalyticsDistinctId;
|
|
155
155
|
/**
|
|
156
156
|
* The bare (unprefixed) device UUID. Undefined for legacy identities persisted before
|
|
157
157
|
* a device id was stored (their `distinct_id` has no device prefix either).
|
|
@@ -2,22 +2,28 @@ import type { StringMap } from '@naturalcycles/js-lib/types';
|
|
|
2
2
|
import type { ClientId, PageViewInput, PurchaseInput } from '@naturalcycles/shared';
|
|
3
3
|
/**
|
|
4
4
|
* Client for our Conversions API endpoints (`web/p`, `web/purchase`). Captures ad click ids from
|
|
5
|
-
* the URL
|
|
5
|
+
* the URL, stores them in cookies once marketing consent is given and sends them as headers. Calls
|
|
6
|
+
* never throw, failures go to `onError`.
|
|
6
7
|
*/
|
|
7
8
|
export declare class ConversionClient {
|
|
8
9
|
constructor(cfg: ConversionClientCfg);
|
|
9
10
|
private readonly cfg;
|
|
10
11
|
private readonly fetcher;
|
|
11
12
|
private lastCapturedHref;
|
|
12
|
-
|
|
13
|
+
private pendingClickIds;
|
|
14
|
+
/** Captures ad click ids from the landing URL into memory. Call on page load. */
|
|
13
15
|
init(): void;
|
|
14
16
|
pageView(input: PageViewInput, opt?: ConversionRequestOptions): void;
|
|
15
17
|
purchase(input: PurchaseInput, opt?: ConversionRequestOptions): void;
|
|
18
|
+
/** Returns null outside the allowed domains or on a path of their exclusion list. */
|
|
19
|
+
private getAllowedPathname;
|
|
16
20
|
private send;
|
|
17
|
-
|
|
21
|
+
/** Click ids are only included with marketing consent. */
|
|
22
|
+
getHeaders(input: ConversionHeadersInput): ConversionHeaders;
|
|
23
|
+
private storeClickIds;
|
|
18
24
|
private tryOrReport;
|
|
19
25
|
private captureClickIds;
|
|
20
|
-
/** Returns undefined when
|
|
26
|
+
/** Returns undefined when this click is already captured, so its timestamp is kept. */
|
|
21
27
|
private toFbc;
|
|
22
28
|
}
|
|
23
29
|
export interface ConversionClientCfg {
|
|
@@ -33,6 +39,9 @@ export interface ConversionClientCfg {
|
|
|
33
39
|
export interface ConversionRequestOptions {
|
|
34
40
|
extraHeaders?: StringMap;
|
|
35
41
|
}
|
|
42
|
+
export interface ConversionHeadersInput {
|
|
43
|
+
marketingConsent: boolean | undefined;
|
|
44
|
+
}
|
|
36
45
|
export interface ConversionHeaders {
|
|
37
46
|
clientId: string;
|
|
38
47
|
fbc?: string;
|
|
@@ -3,14 +3,15 @@ import { _anyToError } from "@naturalcycles/js-lib/error";
|
|
|
3
3
|
import { getFetcher } from "@naturalcycles/js-lib/http";
|
|
4
4
|
import { _filterEmptyValues } from "@naturalcycles/js-lib/object";
|
|
5
5
|
import { _noop } from "@naturalcycles/js-lib/types";
|
|
6
|
-
import { WebSignupQueryParam, clickIdParameters, cookieService, formatMetaClickId, sanitizeFbclid } from "@naturalcycles/shared";
|
|
6
|
+
import { WebSignupQueryParam, allowedDomains, clickIdParameters, cookieService, debugDomainMap, domainPathKeywordSuppressionMap, formatMetaClickId, sanitizeFbclid } from "@naturalcycles/shared";
|
|
7
7
|
//#region src/conversion/conversionClient.ts
|
|
8
8
|
var CLICK_ID_COOKIE_DAYS = 30;
|
|
9
9
|
var MAX_CLICK_ID_LENGTH = 200;
|
|
10
10
|
var MIN_FBCLID_LENGTH = 20;
|
|
11
11
|
/**
|
|
12
12
|
* Client for our Conversions API endpoints (`web/p`, `web/purchase`). Captures ad click ids from
|
|
13
|
-
* the URL
|
|
13
|
+
* the URL, stores them in cookies once marketing consent is given and sends them as headers. Calls
|
|
14
|
+
* never throw, failures go to `onError`.
|
|
14
15
|
*/
|
|
15
16
|
var ConversionClient = class {
|
|
16
17
|
constructor(cfg) {
|
|
@@ -24,18 +25,36 @@ var ConversionClient = class {
|
|
|
24
25
|
cfg;
|
|
25
26
|
fetcher;
|
|
26
27
|
lastCapturedHref;
|
|
27
|
-
|
|
28
|
+
pendingClickIds = {};
|
|
29
|
+
/** Captures ad click ids from the landing URL into memory. Call on page load. */
|
|
28
30
|
init() {
|
|
29
31
|
if (!this.cfg.enabled) return;
|
|
30
32
|
this.tryOrReport(() => this.captureClickIds(), void 0);
|
|
31
33
|
}
|
|
32
34
|
pageView(input, opt = {}) {
|
|
33
35
|
if (!this.cfg.enabled) return;
|
|
34
|
-
|
|
36
|
+
const pathname = this.getAllowedPathname(input.pathname);
|
|
37
|
+
if (pathname === null) return;
|
|
38
|
+
runInBackground(this.send("p", {
|
|
39
|
+
...input,
|
|
40
|
+
pathname
|
|
41
|
+
}, opt));
|
|
35
42
|
}
|
|
36
43
|
purchase(input, opt = {}) {
|
|
37
44
|
if (!this.cfg.enabled) return;
|
|
38
|
-
|
|
45
|
+
const pathname = this.getAllowedPathname(input.pathname);
|
|
46
|
+
if (pathname === null) return;
|
|
47
|
+
runInBackground(this.send("purchase", {
|
|
48
|
+
...input,
|
|
49
|
+
pathname
|
|
50
|
+
}, opt));
|
|
51
|
+
}
|
|
52
|
+
/** Returns null outside the allowed domains or on a path of their exclusion list. */
|
|
53
|
+
getAllowedPathname(pathname) {
|
|
54
|
+
const { href, origin } = globalThis.location;
|
|
55
|
+
const domain = allowedDomains.find((allowedDomain) => href.startsWith(allowedDomain)) || debugDomainMap[origin];
|
|
56
|
+
if (!domain) return null;
|
|
57
|
+
return domainPathKeywordSuppressionMap[domain].some((keyword) => pathname.includes(keyword)) ? null : pathname;
|
|
39
58
|
}
|
|
40
59
|
async send(path, json, opt) {
|
|
41
60
|
try {
|
|
@@ -44,7 +63,7 @@ var ConversionClient = class {
|
|
|
44
63
|
method: "POST",
|
|
45
64
|
json,
|
|
46
65
|
headers: {
|
|
47
|
-
...this.getHeaders(),
|
|
66
|
+
...this.getHeaders({ marketingConsent: json.marketingConsent ?? json.marketingCookie }),
|
|
48
67
|
...opt.extraHeaders
|
|
49
68
|
},
|
|
50
69
|
keepalive: true,
|
|
@@ -55,10 +74,12 @@ var ConversionClient = class {
|
|
|
55
74
|
this.cfg.onError(_anyToError(err));
|
|
56
75
|
}
|
|
57
76
|
}
|
|
58
|
-
|
|
77
|
+
/** Click ids are only included with marketing consent. */
|
|
78
|
+
getHeaders(input) {
|
|
59
79
|
const clientId = String(this.cfg.clientId);
|
|
80
|
+
if (!input.marketingConsent) return { clientId };
|
|
60
81
|
return this.tryOrReport(() => {
|
|
61
|
-
if (this.cfg.enabled) this.
|
|
82
|
+
if (this.cfg.enabled) this.storeClickIds();
|
|
62
83
|
return _filterEmptyValues({
|
|
63
84
|
clientId,
|
|
64
85
|
fbc: cookieService.getCookie("_fbc") || formatMetaClickId(cookieService.getCookie(WebSignupQueryParam.fbclid)),
|
|
@@ -71,6 +92,11 @@ var ConversionClient = class {
|
|
|
71
92
|
});
|
|
72
93
|
}, { clientId });
|
|
73
94
|
}
|
|
95
|
+
storeClickIds() {
|
|
96
|
+
this.captureClickIds();
|
|
97
|
+
for (const [param, value] of Object.entries(this.pendingClickIds)) cookieService.setCookie(param, value, CLICK_ID_COOKIE_DAYS, this.cfg.cookieDomain);
|
|
98
|
+
this.pendingClickIds = {};
|
|
99
|
+
}
|
|
74
100
|
tryOrReport(fn, fallback) {
|
|
75
101
|
try {
|
|
76
102
|
return fn();
|
|
@@ -88,14 +114,14 @@ var ConversionClient = class {
|
|
|
88
114
|
const raw = searchParams.get(param) || searchParams.get(param.toLowerCase());
|
|
89
115
|
if (!raw) continue;
|
|
90
116
|
const value = param === WebSignupQueryParam.fbclid ? this.toFbc(raw) : raw.slice(0, MAX_CLICK_ID_LENGTH);
|
|
91
|
-
if (value)
|
|
117
|
+
if (value) this.pendingClickIds[param] = value;
|
|
92
118
|
}
|
|
93
119
|
}
|
|
94
|
-
/** Returns undefined when
|
|
120
|
+
/** Returns undefined when this click is already captured, so its timestamp is kept. */
|
|
95
121
|
toFbc(raw) {
|
|
96
122
|
const fbclid = sanitizeFbclid(raw);
|
|
97
123
|
if (!fbclid || fbclid.length < MIN_FBCLID_LENGTH) return;
|
|
98
|
-
if (cookieService.getCookie(WebSignupQueryParam.fbclid)?.endsWith(`.${fbclid}`)) return;
|
|
124
|
+
if ((this.pendingClickIds[WebSignupQueryParam.fbclid] || cookieService.getCookie(WebSignupQueryParam.fbclid))?.endsWith(`.${fbclid}`)) return;
|
|
99
125
|
return formatMetaClickId(fbclid);
|
|
100
126
|
}
|
|
101
127
|
};
|
|
@@ -19,6 +19,10 @@ var CookieMonsterElement = class CookieMonsterElement extends HTMLElement {
|
|
|
19
19
|
*/
|
|
20
20
|
static open(options) {
|
|
21
21
|
if (isServerSide()) return;
|
|
22
|
+
if (document.readyState === "loading") {
|
|
23
|
+
document.addEventListener("DOMContentLoaded", () => CookieMonsterElement.open(options), { once: true });
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
22
26
|
const element = CookieMonsterElement.getOrCreate(options?.language);
|
|
23
27
|
if (options?.view === CookieMonsterView.settings) element.showSettings();
|
|
24
28
|
else if (options?.view === CookieMonsterView.banner) element.showBanner();
|
package/package.json
CHANGED
|
@@ -42,13 +42,13 @@
|
|
|
42
42
|
"publishConfig": {
|
|
43
43
|
"access": "public"
|
|
44
44
|
},
|
|
45
|
-
"version": "1.
|
|
45
|
+
"version": "1.3.0",
|
|
46
46
|
"description": "Internal web libraries for Natural Cycles, served via CDN and published from the NCBackend3 monorepo",
|
|
47
47
|
"author": "Natural Cycles Team",
|
|
48
48
|
"license": "UNLICENSED",
|
|
49
49
|
"scripts": {
|
|
50
|
-
"build": "vite build &&
|
|
51
|
-
"bundle": "rm -rf bundle && pnpm bundle:analytics-client && pnpm bundle:conversion-client && pnpm bundle:cookie-monster",
|
|
50
|
+
"build": "vite build && tsc -P tsconfig.prod.json",
|
|
51
|
+
"bundle": "rm -rf bundle && pnpm --silent bundle:analytics-client && pnpm --silent bundle:conversion-client && pnpm --silent bundle:cookie-monster",
|
|
52
52
|
"bundle:analytics-client": "BUNDLE_ENTRY=analyticsClient vite build --config vite.bundle.config.ts",
|
|
53
53
|
"bundle:conversion-client": "BUNDLE_ENTRY=conversionClient vite build --config vite.bundle.config.ts",
|
|
54
54
|
"bundle:cookie-monster": "BUNDLE_ENTRY=cookieMonster vite build --config vite.bundle.config.ts",
|