@naturalcycles/internal-web-lib 1.1.1 → 1.2.1
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 +19 -19
- package/bundle/analyticsClient.js.map +1 -1
- package/bundle/conversionClient.js +1027 -0
- package/bundle/conversionClient.js.map +1 -0
- package/bundle/cookieMonster.js +1 -1
- package/bundle/cookieMonster.js.map +1 -1
- package/dist/conversion/conversion.util.d.ts +1 -0
- package/dist/conversion/conversion.util.js +7 -0
- package/dist/conversion/conversionClient.d.ts +45 -0
- package/dist/conversion/conversionClient.js +103 -0
- package/dist/conversion/index.d.ts +2 -0
- package/dist/conversion/index.js +3 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -1
- package/package.json +5 -2
- package/readme.md +5 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function generateConversionEventId(): string;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { StringMap } from '@naturalcycles/js-lib/types';
|
|
2
|
+
import type { ClientId, PageViewInput, PurchaseInput } from '@naturalcycles/shared';
|
|
3
|
+
/**
|
|
4
|
+
* Client for our Conversions API endpoints (`web/p`, `web/purchase`). Captures ad click ids from
|
|
5
|
+
* the URL into cookies and sends them as headers. Calls never throw, failures go to `onError`.
|
|
6
|
+
*/
|
|
7
|
+
export declare class ConversionClient {
|
|
8
|
+
constructor(cfg: ConversionClientCfg);
|
|
9
|
+
private readonly cfg;
|
|
10
|
+
private readonly fetcher;
|
|
11
|
+
private lastCapturedHref;
|
|
12
|
+
/** Captures ad click ids from the landing URL. Call on page load, before consent is known. */
|
|
13
|
+
init(): void;
|
|
14
|
+
pageView(input: PageViewInput, opt?: ConversionRequestOptions): void;
|
|
15
|
+
purchase(input: PurchaseInput, opt?: ConversionRequestOptions): void;
|
|
16
|
+
private send;
|
|
17
|
+
getHeaders(): ConversionHeaders;
|
|
18
|
+
private tryOrReport;
|
|
19
|
+
private captureClickIds;
|
|
20
|
+
/** Returns undefined when the cookie already holds this click, so its timestamp is kept. */
|
|
21
|
+
private toFbc;
|
|
22
|
+
}
|
|
23
|
+
export interface ConversionClientCfg {
|
|
24
|
+
/** Backend base url including `/web`, e.g. `https://api.example.com/web`. */
|
|
25
|
+
url: string;
|
|
26
|
+
clientId: ClientId;
|
|
27
|
+
/** `Domain` of the click-id cookies, e.g. `.naturalcycles.com`. Host-only when unset. */
|
|
28
|
+
cookieDomain?: string;
|
|
29
|
+
/** Pass false to make every call a no-op, e.g. for bots and e2e tests. Defaults to true. */
|
|
30
|
+
enabled?: boolean;
|
|
31
|
+
onError?: (err: Error) => void;
|
|
32
|
+
}
|
|
33
|
+
export interface ConversionRequestOptions {
|
|
34
|
+
extraHeaders?: StringMap;
|
|
35
|
+
}
|
|
36
|
+
export interface ConversionHeaders {
|
|
37
|
+
clientId: string;
|
|
38
|
+
fbc?: string;
|
|
39
|
+
fbp?: string;
|
|
40
|
+
scCid?: string;
|
|
41
|
+
ttclid?: string;
|
|
42
|
+
rdtCid?: string;
|
|
43
|
+
epik?: string;
|
|
44
|
+
uuid_c1?: string;
|
|
45
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { runInBackground } from "@naturalcycles/js-lib";
|
|
2
|
+
import { _anyToError } from "@naturalcycles/js-lib/error";
|
|
3
|
+
import { getFetcher } from "@naturalcycles/js-lib/http";
|
|
4
|
+
import { _filterEmptyValues } from "@naturalcycles/js-lib/object";
|
|
5
|
+
import { _noop } from "@naturalcycles/js-lib/types";
|
|
6
|
+
import { WebSignupQueryParam, clickIdParameters, cookieService, formatMetaClickId, sanitizeFbclid } from "@naturalcycles/shared";
|
|
7
|
+
//#region src/conversion/conversionClient.ts
|
|
8
|
+
var CLICK_ID_COOKIE_DAYS = 30;
|
|
9
|
+
var MAX_CLICK_ID_LENGTH = 200;
|
|
10
|
+
var MIN_FBCLID_LENGTH = 20;
|
|
11
|
+
/**
|
|
12
|
+
* Client for our Conversions API endpoints (`web/p`, `web/purchase`). Captures ad click ids from
|
|
13
|
+
* the URL into cookies and sends them as headers. Calls never throw, failures go to `onError`.
|
|
14
|
+
*/
|
|
15
|
+
var ConversionClient = class {
|
|
16
|
+
constructor(cfg) {
|
|
17
|
+
this.cfg = {
|
|
18
|
+
...cfg,
|
|
19
|
+
enabled: cfg.enabled ?? true,
|
|
20
|
+
onError: cfg.onError ?? _noop
|
|
21
|
+
};
|
|
22
|
+
this.fetcher = getFetcher({ baseUrl: cfg.url });
|
|
23
|
+
}
|
|
24
|
+
cfg;
|
|
25
|
+
fetcher;
|
|
26
|
+
lastCapturedHref;
|
|
27
|
+
/** Captures ad click ids from the landing URL. Call on page load, before consent is known. */
|
|
28
|
+
init() {
|
|
29
|
+
if (!this.cfg.enabled) return;
|
|
30
|
+
this.tryOrReport(() => this.captureClickIds(), void 0);
|
|
31
|
+
}
|
|
32
|
+
pageView(input, opt = {}) {
|
|
33
|
+
if (!this.cfg.enabled) return;
|
|
34
|
+
runInBackground(this.send("p", input, opt));
|
|
35
|
+
}
|
|
36
|
+
purchase(input, opt = {}) {
|
|
37
|
+
if (!this.cfg.enabled) return;
|
|
38
|
+
runInBackground(this.send("purchase", input, opt));
|
|
39
|
+
}
|
|
40
|
+
async send(path, json, opt) {
|
|
41
|
+
try {
|
|
42
|
+
const res = await this.fetcher.doFetch({
|
|
43
|
+
url: path,
|
|
44
|
+
method: "POST",
|
|
45
|
+
json,
|
|
46
|
+
headers: {
|
|
47
|
+
...this.getHeaders(),
|
|
48
|
+
...opt.extraHeaders
|
|
49
|
+
},
|
|
50
|
+
keepalive: true,
|
|
51
|
+
responseType: "void"
|
|
52
|
+
});
|
|
53
|
+
if (res.err) this.cfg.onError(res.err);
|
|
54
|
+
} catch (err) {
|
|
55
|
+
this.cfg.onError(_anyToError(err));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
getHeaders() {
|
|
59
|
+
const clientId = String(this.cfg.clientId);
|
|
60
|
+
return this.tryOrReport(() => {
|
|
61
|
+
if (this.cfg.enabled) this.captureClickIds();
|
|
62
|
+
return _filterEmptyValues({
|
|
63
|
+
clientId,
|
|
64
|
+
fbc: cookieService.getCookie("_fbc") || formatMetaClickId(cookieService.getCookie(WebSignupQueryParam.fbclid)),
|
|
65
|
+
fbp: cookieService.getCookie("_fbp"),
|
|
66
|
+
scCid: cookieService.getCookie(WebSignupQueryParam.ScCid),
|
|
67
|
+
ttclid: cookieService.getCookie(WebSignupQueryParam.ttclid),
|
|
68
|
+
rdtCid: cookieService.getCookie(WebSignupQueryParam.rdt_cid),
|
|
69
|
+
epik: cookieService.getCookie("_epik") || cookieService.getCookie(WebSignupQueryParam.epik),
|
|
70
|
+
uuid_c1: cookieService.getCookie("_scid") || cookieService.getCookie("uuid_c1")
|
|
71
|
+
});
|
|
72
|
+
}, { clientId });
|
|
73
|
+
}
|
|
74
|
+
tryOrReport(fn, fallback) {
|
|
75
|
+
try {
|
|
76
|
+
return fn();
|
|
77
|
+
} catch (err) {
|
|
78
|
+
this.cfg.onError(_anyToError(err));
|
|
79
|
+
return fallback;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
captureClickIds() {
|
|
83
|
+
const { href, search } = globalThis.location;
|
|
84
|
+
if (href === this.lastCapturedHref) return;
|
|
85
|
+
this.lastCapturedHref = href;
|
|
86
|
+
const searchParams = new URLSearchParams(search);
|
|
87
|
+
for (const param of clickIdParameters) {
|
|
88
|
+
const raw = searchParams.get(param) || searchParams.get(param.toLowerCase());
|
|
89
|
+
if (!raw) continue;
|
|
90
|
+
const value = param === WebSignupQueryParam.fbclid ? this.toFbc(raw) : raw.slice(0, MAX_CLICK_ID_LENGTH);
|
|
91
|
+
if (value) cookieService.setCookie(param, value, CLICK_ID_COOKIE_DAYS, this.cfg.cookieDomain);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/** Returns undefined when the cookie already holds this click, so its timestamp is kept. */
|
|
95
|
+
toFbc(raw) {
|
|
96
|
+
const fbclid = sanitizeFbclid(raw);
|
|
97
|
+
if (!fbclid || fbclid.length < MIN_FBCLID_LENGTH) return;
|
|
98
|
+
if (cookieService.getCookie(WebSignupQueryParam.fbclid)?.endsWith(`.${fbclid}`)) return;
|
|
99
|
+
return formatMetaClickId(fbclid);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
//#endregion
|
|
103
|
+
export { ConversionClient };
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
import { AnalyticsClient, AnalyticsClientError, AnalyticsIdentity, getCookie, setCookie } from "./analytics/analyticsClient.js";
|
|
2
|
-
|
|
2
|
+
import { generateConversionEventId } from "./conversion/conversion.util.js";
|
|
3
|
+
import { ConversionClient } from "./conversion/conversionClient.js";
|
|
4
|
+
export { AnalyticsClient, AnalyticsClientError, AnalyticsIdentity, ConversionClient, generateConversionEventId, getCookie, setCookie };
|
package/package.json
CHANGED
|
@@ -20,6 +20,8 @@
|
|
|
20
20
|
".": "./dist/index.js",
|
|
21
21
|
"./analytics": "./dist/analytics/index.js",
|
|
22
22
|
"./analytics/*.js": "./dist/analytics/*.js",
|
|
23
|
+
"./conversion": "./dist/conversion/index.js",
|
|
24
|
+
"./conversion/*.js": "./dist/conversion/*.js",
|
|
23
25
|
"./cookie-monster": "./dist/cookie-monster/index.js"
|
|
24
26
|
},
|
|
25
27
|
"files": [
|
|
@@ -40,14 +42,15 @@
|
|
|
40
42
|
"publishConfig": {
|
|
41
43
|
"access": "public"
|
|
42
44
|
},
|
|
43
|
-
"version": "1.
|
|
45
|
+
"version": "1.2.1",
|
|
44
46
|
"description": "Internal web libraries for Natural Cycles, served via CDN and published from the NCBackend3 monorepo",
|
|
45
47
|
"author": "Natural Cycles Team",
|
|
46
48
|
"license": "UNLICENSED",
|
|
47
49
|
"scripts": {
|
|
48
50
|
"build": "vite build && pnpm bundle && tsc -P tsconfig.prod.json",
|
|
49
|
-
"bundle": "rm -rf bundle && pnpm bundle:analytics-client && pnpm bundle:cookie-monster",
|
|
51
|
+
"bundle": "rm -rf bundle && pnpm bundle:analytics-client && pnpm bundle:conversion-client && pnpm bundle:cookie-monster",
|
|
50
52
|
"bundle:analytics-client": "BUNDLE_ENTRY=analyticsClient vite build --config vite.bundle.config.ts",
|
|
53
|
+
"bundle:conversion-client": "BUNDLE_ENTRY=conversionClient vite build --config vite.bundle.config.ts",
|
|
51
54
|
"bundle:cookie-monster": "BUNDLE_ENTRY=cookieMonster vite build --config vite.bundle.config.ts",
|
|
52
55
|
"demo": "vite",
|
|
53
56
|
"build:demo": "vite build --config vite.demo.config.ts",
|
package/readme.md
CHANGED
|
@@ -66,6 +66,11 @@ Pin the jsDelivr url to a major, as above. An unpinned url follows `latest`, shi
|
|
|
66
66
|
to every embedding page the moment a release lands. A major keeps fixes and features flowing while
|
|
67
67
|
holding back the releases that would need the page changed.
|
|
68
68
|
|
|
69
|
+
## Conversion client on a page
|
|
70
|
+
|
|
71
|
+
`bundle/conversionClient.js` exports `ConversionClient`, which posts conversion events with the ad
|
|
72
|
+
click ids captured from the URL.
|
|
73
|
+
|
|
69
74
|
## Releases
|
|
70
75
|
|
|
71
76
|
Cut from `master` by `ci-release-packages.yml` on every commit under `packages/**`, driven by
|