@shware/analytics 8.1.0 → 8.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/README.md +63 -2
- package/dist/_virtual/_rolldown/runtime.cjs +1 -1
- package/dist/_virtual/_rolldown/runtime.mjs +1 -1
- package/dist/click-id/index.cjs +119 -6
- package/dist/click-id/index.cjs.map +1 -1
- package/dist/click-id/index.d.cts +36 -1
- package/dist/click-id/index.d.cts.map +1 -1
- package/dist/click-id/index.d.mts +36 -1
- package/dist/click-id/index.d.mts.map +1 -1
- package/dist/click-id/index.mjs +111 -6
- package/dist/click-id/index.mjs.map +1 -1
- package/dist/feedback/index.cjs.map +1 -1
- package/dist/hooks/use-previous.cjs.map +1 -1
- package/dist/hooks/use-track-impression.cjs.map +1 -1
- package/dist/hooks/use-web-analytics.cjs.map +1 -1
- package/dist/link/index.cjs.map +1 -1
- package/dist/native/fingerprint.cjs.map +1 -1
- package/dist/native/firebase.cjs.map +1 -1
- package/dist/native/setup.cjs.map +1 -1
- package/dist/next/index.cjs +2 -1
- package/dist/next/index.cjs.map +1 -1
- package/dist/next/index.mjs +2 -1
- package/dist/next/index.mjs.map +1 -1
- package/dist/react-router/index.cjs +1 -1
- package/dist/react-router/index.cjs.map +1 -1
- package/dist/react-router/index.mjs +1 -1
- package/dist/schema/index.cjs.map +1 -1
- package/dist/server/google-data-manager.cjs +2 -0
- package/dist/server/google-data-manager.cjs.map +1 -1
- package/dist/server/google-data-manager.d.cts.map +1 -1
- package/dist/server/google-data-manager.d.mts.map +1 -1
- package/dist/server/google-data-manager.mjs +2 -0
- package/dist/server/google-data-manager.mjs.map +1 -1
- package/dist/server/index.cjs +12 -0
- package/dist/server/index.d.cts +5 -4
- package/dist/server/index.d.mts +5 -4
- package/dist/server/index.mjs +5 -4
- package/dist/server/linkedin-conversions-api.cjs.map +1 -1
- package/dist/server/meta-capi.cjs +19 -17
- package/dist/server/meta-capi.cjs.map +1 -1
- package/dist/server/meta-capi.mjs +19 -17
- package/dist/server/meta-capi.mjs.map +1 -1
- package/dist/server/meta-conversions-api.cjs +40 -36
- package/dist/server/meta-conversions-api.cjs.map +1 -1
- package/dist/server/meta-conversions-api.mjs +40 -36
- package/dist/server/meta-conversions-api.mjs.map +1 -1
- package/dist/server/microsoft-conversions-api.cjs +171 -0
- package/dist/server/microsoft-conversions-api.cjs.map +1 -0
- package/dist/server/microsoft-conversions-api.d.cts +117 -0
- package/dist/server/microsoft-conversions-api.d.cts.map +1 -0
- package/dist/server/microsoft-conversions-api.d.mts +117 -0
- package/dist/server/microsoft-conversions-api.d.mts.map +1 -0
- package/dist/server/microsoft-conversions-api.mjs +168 -0
- package/dist/server/microsoft-conversions-api.mjs.map +1 -0
- package/dist/server/openai-conversions-api.cjs.map +1 -1
- package/dist/server/reddit-conversions-api.cjs.map +1 -1
- package/dist/setup/session.cjs +1 -1
- package/dist/setup/session.cjs.map +1 -1
- package/dist/setup/session.mjs +1 -1
- package/dist/setup/session.mjs.map +1 -1
- package/dist/tanstack/analytics.cjs +1 -1
- package/dist/tanstack/analytics.cjs.map +1 -1
- package/dist/tanstack/analytics.mjs +1 -1
- package/dist/tanstack/middleware.cjs +2 -1
- package/dist/tanstack/middleware.cjs.map +1 -1
- package/dist/tanstack/middleware.d.cts +2 -1
- package/dist/tanstack/middleware.d.cts.map +1 -1
- package/dist/tanstack/middleware.d.mts +2 -1
- package/dist/tanstack/middleware.d.mts.map +1 -1
- package/dist/tanstack/middleware.mjs +2 -1
- package/dist/tanstack/middleware.mjs.map +1 -1
- package/dist/third-parties/index.cjs +5 -0
- package/dist/third-parties/index.d.cts +3 -1
- package/dist/third-parties/index.d.mts +3 -1
- package/dist/third-parties/index.mjs +2 -1
- package/dist/third-parties/microsoft-uet.cjs +119 -0
- package/dist/third-parties/microsoft-uet.cjs.map +1 -0
- package/dist/third-parties/microsoft-uet.d.cts +70 -0
- package/dist/third-parties/microsoft-uet.d.cts.map +1 -0
- package/dist/third-parties/microsoft-uet.d.mts +70 -0
- package/dist/third-parties/microsoft-uet.d.mts.map +1 -0
- package/dist/third-parties/microsoft-uet.mjs +115 -0
- package/dist/third-parties/microsoft-uet.mjs.map +1 -0
- package/dist/third-parties/openai-pixel.cjs +2 -7
- package/dist/third-parties/openai-pixel.cjs.map +1 -1
- package/dist/third-parties/openai-pixel.d.cts.map +1 -1
- package/dist/third-parties/openai-pixel.d.mts.map +1 -1
- package/dist/third-parties/openai-pixel.mjs +1 -6
- package/dist/third-parties/openai-pixel.mjs.map +1 -1
- package/dist/track/index.cjs.map +1 -1
- package/dist/track/uetq.cjs +80 -0
- package/dist/track/uetq.cjs.map +1 -0
- package/dist/track/uetq.d.cts +111 -0
- package/dist/track/uetq.d.cts.map +1 -0
- package/dist/track/uetq.d.mts +111 -0
- package/dist/track/uetq.d.mts.map +1 -0
- package/dist/track/uetq.mjs +78 -0
- package/dist/track/uetq.mjs.map +1 -0
- package/dist/utils/sha256.cjs +19 -0
- package/dist/utils/sha256.cjs.map +1 -0
- package/dist/utils/sha256.d.cts +13 -0
- package/dist/utils/sha256.d.cts.map +1 -0
- package/dist/utils/sha256.d.mts +13 -0
- package/dist/utils/sha256.d.mts.map +1 -0
- package/dist/utils/sha256.mjs +18 -0
- package/dist/utils/sha256.mjs.map +1 -0
- package/dist/visitor/index.cjs.map +1 -1
- package/dist/web/index.cjs +4 -3
- package/dist/web/index.cjs.map +1 -1
- package/dist/web/index.d.cts.map +1 -1
- package/dist/web/index.d.mts.map +1 -1
- package/dist/web/index.mjs +4 -3
- package/dist/web/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -9,8 +9,8 @@ Workers), but calling `track()` there shares one session and one visitor across
|
|
|
9
9
|
isolate serves.
|
|
10
10
|
|
|
11
11
|
Server-side conversions go the other way: store the client's events, then hand them to
|
|
12
|
-
`@shware/analytics/server`, which takes a `TrackEvent` and forwards it to the Meta, Reddit, OpenAI
|
|
13
|
-
and
|
|
12
|
+
`@shware/analytics/server`, which takes a `TrackEvent` and forwards it to the Meta, Reddit, OpenAI,
|
|
13
|
+
LinkedIn, Google Ads and Microsoft Advertising conversions APIs.
|
|
14
14
|
|
|
15
15
|
## Config
|
|
16
16
|
|
|
@@ -124,4 +124,65 @@ We strongly recommend using the Reddit Pixel and Conversions API (CAPI) together
|
|
|
124
124
|
If we receive an Insight Tag event and a Conversions API event from the same account with the same eventId, we discard the Conversions API event and count only the Insight Tag event in campaign reporting.
|
|
125
125
|
``
|
|
126
126
|
|
|
127
|
+
### Microsoft Advertising (Bing Ads)
|
|
128
|
+
|
|
129
|
+
Microsoft recommends the UET tag and the Conversions API (CAPI) together. The two deduplicate on
|
|
130
|
+
(`tagId`, `eventName`, `eventId`): the browser sends the internal event name as the UET action with
|
|
131
|
+
the event id as `event_id`, the server sends the same name as `eventName` with the same id.
|
|
132
|
+
|
|
133
|
+
Browser — install the UET snippet from the tag's "Set up tagging" step, then:
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
import {
|
|
137
|
+
sendUETEvent,
|
|
138
|
+
setUETUser,
|
|
139
|
+
setUETConsent,
|
|
140
|
+
sendUETIdSync,
|
|
141
|
+
} from '@shware/analytics/third-parties';
|
|
142
|
+
|
|
143
|
+
setupAnalytics({
|
|
144
|
+
thirdPartyTrackers: [sendUETEvent],
|
|
145
|
+
// Enhanced conversions: the tag hashes the email/phone itself before they leave the page.
|
|
146
|
+
thirdPartyUserSetters: [setUETUser()],
|
|
147
|
+
});
|
|
148
|
+
// Consent mode (EEA/UK/CH): push `default` before the tag loads, `update` on the visitor's choice.
|
|
149
|
+
setUETConsent('default', { ad_storage: 'denied' });
|
|
150
|
+
// ID Sync, once per session: ties the visitor id CAPI sends as `anonymousId` to Microsoft's ids.
|
|
151
|
+
// Required for remarketing built from CAPI events, recommended for measurement.
|
|
152
|
+
sendUETIdSync({ customerId: 255004870, visitorId: (await getVisitor()).id });
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Server — the token comes from the UET tag's "Use Conversions API" step (or the Campaign Management
|
|
156
|
+
API's `UetTagAuthKey/Query`; an account whose tag setup lacks that option has to ask Microsoft
|
|
157
|
+
Advertising support), `tagId` is the UET tag id:
|
|
158
|
+
|
|
159
|
+
```ts
|
|
160
|
+
import { sendMicrosoftEvents } from '@shware/analytics/server';
|
|
161
|
+
|
|
162
|
+
await sendMicrosoftEvents(process.env.MS_ADS_CAPI_TOKEN, 97267979, events, userData, {
|
|
163
|
+
consent: 'granted',
|
|
164
|
+
});
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
- msclkid: from url params, kept in the tag's own `_uetmsclkid` cookie (`_uet<id>`, 90 days). That
|
|
168
|
+
cookie is Microsoft's ITP answer, but bat.js writes it with `document.cookie`, which Safari caps
|
|
169
|
+
at 7 days (24h on a landing page decorated by a classified domain — a bing.com click). Register
|
|
170
|
+
the click-id middleware and `resolveClickIdCookies` re-issues it over HTTP on every document
|
|
171
|
+
response, restoring the 90-day window; give it the same `domain` the tag uses (eTLD+1) so the
|
|
172
|
+
two write one cookie, not two. Sent to CAPI as a dashed UUID.
|
|
173
|
+
- anonymousId / externalId: the SDK visitor id and SHA-256 of the user id — the same two values
|
|
174
|
+
`sendUETIdSync` sends as `VID` / `UID`, which Microsoft requires to match.
|
|
175
|
+
- Consent: the tag writes `_uetmsclkid` only with `ad_storage` granted; the middleware writes it
|
|
176
|
+
from the URL regardless, so gate it with the middleware's `shouldPersist` where consent applies
|
|
177
|
+
(as for `_fbc` and `_gcl_*`).
|
|
178
|
+
- Validation warnings on a 200 (a removed field, a skipped event) are logged with `console.warn`;
|
|
179
|
+
monitor them, the status code does not show them.
|
|
180
|
+
- em / ph: SHA-256 of the normalized email (dots and `+alias` stripped from the user part for every
|
|
181
|
+
domain, lowercase) and of the E.164 phone.
|
|
182
|
+
- `page_view` events are dropped by default (the tag already reports every page load); pass
|
|
183
|
+
`pageLoads: true` for a CAPI-only site to send them as `pageLoad` events. Not yet modelled in
|
|
184
|
+
that mode: `pageLoadId` (the id that links a `custom` event to the page load it happened on) and
|
|
185
|
+
the revenue-only `custom` event a destination-URL goal with variable revenue needs alongside its
|
|
186
|
+
`pageLoad`. With the tag on the page neither applies — the tag reports page loads itself.
|
|
187
|
+
|
|
127
188
|
- [Click IDs](https://learn.microsoft.com/en-us/linkedin/marketing/conversions/enabling-first-party-cookies?view=li-lms-2025-10&source=recommendations): get li_fat_id from url params and cookie
|
|
@@ -16,7 +16,7 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
}
|
|
17
17
|
return to;
|
|
18
18
|
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
|
|
20
20
|
value: mod,
|
|
21
21
|
enumerable: true
|
|
22
22
|
}) : target, mod));
|
|
@@ -16,7 +16,7 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
}
|
|
17
17
|
return to;
|
|
18
18
|
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
|
|
20
20
|
value: mod,
|
|
21
21
|
enumerable: true
|
|
22
22
|
}) : target, mod));
|
package/dist/click-id/index.cjs
CHANGED
|
@@ -2,7 +2,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
2
2
|
let cookie = require("cookie");
|
|
3
3
|
//#region src/click-id/index.ts
|
|
4
4
|
/**
|
|
5
|
-
* Server-side resolution of ad-click-id cookies (`_fbc`, `_rdt_cid
|
|
5
|
+
* Server-side resolution of ad-click-id cookies (`_fbc`, `_gcl_aw`/`_gcl_gb`, `_rdt_cid`,
|
|
6
|
+
* `_uetmsclkid`) from the incoming request.
|
|
6
7
|
*
|
|
7
8
|
* This is the framework-agnostic core meant to run on the *document* response (e.g. TanStack Start
|
|
8
9
|
* server middleware, Next middleware). Setting `_fbc` via an HTTP `Set-Cookie` header on the top
|
|
@@ -11,13 +12,33 @@ let cookie = require("cookie");
|
|
|
11
12
|
* cookies on a fbclid-decorated landing page to 24 hours, and the document response is never
|
|
12
13
|
* classified as CNAME/IP cloaking (it is the reference the browser measures cloaking against).
|
|
13
14
|
*
|
|
14
|
-
*
|
|
15
|
+
* The Google pair follows the same architecture Google itself ships for server containers —
|
|
16
|
+
* sGTM's Conversion Linker sets FPGCLAW via Set-Cookie for 90 days — and the sGTM ecosystem
|
|
17
|
+
* productized for gtag's own cookies (stape Cookie Keeper re-issues `_gcl_*` over HTTP; vendor
|
|
18
|
+
* case studies report 5–20% of Google Ads conversions recovered, up to +44% on the Safari
|
|
19
|
+
* slice). One deployment caveat from WebKit's rules: the HTTP-cookie exemption holds only for
|
|
20
|
+
* responses from the site's own infrastructure — a CNAME or third-party-IP host (a tagging
|
|
21
|
+
* subdomain on someone else's cloud) gets its Set-Cookie silently capped to 7 days. This module
|
|
22
|
+
* runs on the host's own document response, which is exactly the exempt case.
|
|
23
|
+
*
|
|
24
|
+
* references:
|
|
25
|
+
* - https://developers.facebook.com/docs/marketing-api/conversions-api/parameters/fbp-and-fbc
|
|
26
|
+
* - https://webkit.org/tracking-prevention/ (7d JS cookies, 24h on ad-decorated landings,
|
|
27
|
+
* CNAME/third-party-IP HTTP cookies capped to 7d, same-origin HTTP cookies exempt)
|
|
28
|
+
* - https://www.simoahava.com/analytics/google-ads-server-side-tagging-google-tag-manager/
|
|
29
|
+
* (Conversion Linker → FPGCLAW via Set-Cookie, Google's own server-side equivalent)
|
|
30
|
+
* - https://stape.io/helpdesk/documentation/cookie-keeper-power-up (the productized re-issue)
|
|
15
31
|
*/
|
|
16
|
-
const FBC_TTL_MS =
|
|
17
|
-
const RDT_CID_TTL_MS =
|
|
18
|
-
const
|
|
32
|
+
const FBC_TTL_MS = 7776e6;
|
|
33
|
+
const RDT_CID_TTL_MS = 7776e6;
|
|
34
|
+
const MSCLKID_TTL_MS = 7776e6;
|
|
35
|
+
const GCL_TTL_MS = 7776e6;
|
|
36
|
+
const CLOCK_SKEW_MS = 864e5;
|
|
19
37
|
const FBC_COOKIE = "_fbc";
|
|
20
38
|
const RDT_CID_COOKIE = "_rdt_cid";
|
|
39
|
+
const GCL_AW_COOKIE = "_gcl_aw";
|
|
40
|
+
const GCL_GB_COOKIE = "_gcl_gb";
|
|
41
|
+
const UET_MSCLKID_COOKIE = "_uetmsclkid";
|
|
21
42
|
/**
|
|
22
43
|
* Parse `fb.<subdomainIndex>.<creationTime>.<fbclid>`.
|
|
23
44
|
* Returns undefined for anything malformed — a fbclid never contains a dot, but joining the tail
|
|
@@ -45,6 +66,58 @@ function parseFbc(raw, now = Date.now()) {
|
|
|
45
66
|
function formatFbc(fbclid, now, subdomainIndex = 1) {
|
|
46
67
|
return `fb.${subdomainIndex}.${now}.${fbclid}`;
|
|
47
68
|
}
|
|
69
|
+
const GCL_CLICK_ID = /^[\w-]+$/;
|
|
70
|
+
/**
|
|
71
|
+
* Parse a `_gcl_aw`/`_gcl_gb` value: `GCL.<seconds>.<clickId>[.<labels…>]` — note the timestamp
|
|
72
|
+
* is in SECONDS, unlike `_fbc`'s milliseconds. gtag's parser accepts version `GCL` or `1` and
|
|
73
|
+
* validates the segments exactly this way (`/^\d+$/` timestamp, `/^[\w-]+$/` click id); any
|
|
74
|
+
* labels tail is preserved through `raw` so a re-issue stays byte-identical.
|
|
75
|
+
*/
|
|
76
|
+
function parseGcl(raw, now = Date.now()) {
|
|
77
|
+
if (!raw) return void 0;
|
|
78
|
+
const parts = raw.split(".");
|
|
79
|
+
if (parts.length < 3) return void 0;
|
|
80
|
+
if (parts[0] !== "GCL" && parts[0] !== "1") return void 0;
|
|
81
|
+
const seconds = parts[1];
|
|
82
|
+
const clickId = parts[2];
|
|
83
|
+
if (seconds === void 0 || !/^\d+$/.test(seconds)) return void 0;
|
|
84
|
+
if (clickId === void 0 || !GCL_CLICK_ID.test(clickId)) return void 0;
|
|
85
|
+
const creationTime = Number(seconds) * 1e3;
|
|
86
|
+
if (creationTime >= 0x38d7ea4c68000 || creationTime > now + CLOCK_SKEW_MS) return void 0;
|
|
87
|
+
return {
|
|
88
|
+
raw,
|
|
89
|
+
creationTime,
|
|
90
|
+
clickId
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
/** Build a fresh `_gcl_aw`/`_gcl_gb` value in gtag's own format. */
|
|
94
|
+
function formatGcl(clickId, now) {
|
|
95
|
+
return `GCL.${Math.floor(now / 1e3)}.${clickId}`;
|
|
96
|
+
}
|
|
97
|
+
const UET_MSCLKID_PREFIX = "_uet";
|
|
98
|
+
const MSCLKID = /^[0-9a-f]{32}$/i;
|
|
99
|
+
/**
|
|
100
|
+
* Parse a `_uetmsclkid` value: `_uet<32 hex>`. Returns the click id, lowercased, or undefined
|
|
101
|
+
* for anything bat.js itself would not read back.
|
|
102
|
+
*/
|
|
103
|
+
function parseUetMsclkid(raw) {
|
|
104
|
+
if (!raw || !raw.startsWith(UET_MSCLKID_PREFIX)) return void 0;
|
|
105
|
+
const clickId = raw.slice(4);
|
|
106
|
+
return MSCLKID.test(clickId) ? clickId.toLowerCase() : void 0;
|
|
107
|
+
}
|
|
108
|
+
/** Build a `_uetmsclkid` value in bat.js's own format. */
|
|
109
|
+
function formatUetMsclkid(clickId) {
|
|
110
|
+
return `${UET_MSCLKID_PREFIX}${clickId.toLowerCase()}`;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* The click id as the Conversions API wants it: a dashed, lowercase UUID. The URL and the cookie
|
|
114
|
+
* carry the 32-hex form; a value in any other shape is returned untouched for the API to judge.
|
|
115
|
+
*/
|
|
116
|
+
function formatMsclkid(clickId) {
|
|
117
|
+
const id = clickId.trim().toLowerCase();
|
|
118
|
+
if (!MSCLKID.test(id)) return id;
|
|
119
|
+
return `${id.slice(0, 8)}-${id.slice(8, 12)}-${id.slice(12, 16)}-${id.slice(16, 20)}-${id.slice(20)}`;
|
|
120
|
+
}
|
|
48
121
|
function searchParams(url) {
|
|
49
122
|
try {
|
|
50
123
|
return new URL(url).searchParams;
|
|
@@ -102,23 +175,63 @@ function resolveClickIdCookies(input) {
|
|
|
102
175
|
if (refresh) set(FBC_COOKIE, existingFbc.raw, remainingMs);
|
|
103
176
|
}
|
|
104
177
|
} else if (jar["_fbc"]) del(FBC_COOKIE);
|
|
178
|
+
const gclsrc = params.get("gclsrc") || void 0;
|
|
179
|
+
const google = [{
|
|
180
|
+
cookie: GCL_AW_COOKIE,
|
|
181
|
+
urlClickId: !gclsrc || gclsrc === "aw.ds" ? params.get("gclid") || void 0 : void 0,
|
|
182
|
+
assign: (value) => result.gclid = value
|
|
183
|
+
}, {
|
|
184
|
+
cookie: GCL_GB_COOKIE,
|
|
185
|
+
urlClickId: params.get("wbraid") || void 0,
|
|
186
|
+
assign: (value) => result.wbraid = value
|
|
187
|
+
}];
|
|
188
|
+
for (const { cookie: cookie$1, urlClickId, assign } of google) {
|
|
189
|
+
const existing = parseGcl(jar[cookie$1], now);
|
|
190
|
+
if (urlClickId && GCL_CLICK_ID.test(urlClickId) && urlClickId !== existing?.clickId) {
|
|
191
|
+
set(cookie$1, formatGcl(urlClickId, now), GCL_TTL_MS);
|
|
192
|
+
assign(urlClickId);
|
|
193
|
+
} else if (existing) {
|
|
194
|
+
const remainingMs = existing.creationTime + GCL_TTL_MS - now;
|
|
195
|
+
if (remainingMs > 0) {
|
|
196
|
+
assign(existing.clickId);
|
|
197
|
+
if (refresh) set(cookie$1, existing.raw, remainingMs);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
105
201
|
const urlRdtCid = params.get("rdt_cid") || void 0;
|
|
106
202
|
const existingRdtCid = jar["_rdt_cid"] || void 0;
|
|
107
203
|
if (urlRdtCid && urlRdtCid !== existingRdtCid) {
|
|
108
204
|
set(RDT_CID_COOKIE, urlRdtCid, RDT_CID_TTL_MS);
|
|
109
205
|
result.rdt_cid = urlRdtCid;
|
|
110
206
|
} else if (existingRdtCid) result.rdt_cid = existingRdtCid;
|
|
207
|
+
const urlMsclkid = params.get("msclkid")?.trim();
|
|
208
|
+
const existingMsclkid = parseUetMsclkid(jar[UET_MSCLKID_COOKIE]);
|
|
209
|
+
if (urlMsclkid && MSCLKID.test(urlMsclkid) && urlMsclkid.toLowerCase() !== existingMsclkid) {
|
|
210
|
+
set(UET_MSCLKID_COOKIE, formatUetMsclkid(urlMsclkid), MSCLKID_TTL_MS);
|
|
211
|
+
result.msclkid = urlMsclkid.toLowerCase();
|
|
212
|
+
} else if (existingMsclkid) {
|
|
213
|
+
result.msclkid = existingMsclkid;
|
|
214
|
+
if (refresh) set(UET_MSCLKID_COOKIE, formatUetMsclkid(existingMsclkid), MSCLKID_TTL_MS);
|
|
215
|
+
}
|
|
111
216
|
return result;
|
|
112
217
|
}
|
|
113
218
|
/** Serialize the resolved cookies into `Set-Cookie` header values. */
|
|
114
219
|
function toSetCookieHeaders(cookies) {
|
|
115
|
-
return cookies.map((cookie$
|
|
220
|
+
return cookies.map((cookie$2) => (0, cookie.stringifySetCookie)(cookie$2));
|
|
116
221
|
}
|
|
117
222
|
//#endregion
|
|
118
223
|
exports.FBC_COOKIE = FBC_COOKIE;
|
|
224
|
+
exports.GCL_AW_COOKIE = GCL_AW_COOKIE;
|
|
225
|
+
exports.GCL_GB_COOKIE = GCL_GB_COOKIE;
|
|
119
226
|
exports.RDT_CID_COOKIE = RDT_CID_COOKIE;
|
|
227
|
+
exports.UET_MSCLKID_COOKIE = UET_MSCLKID_COOKIE;
|
|
120
228
|
exports.formatFbc = formatFbc;
|
|
229
|
+
exports.formatGcl = formatGcl;
|
|
230
|
+
exports.formatMsclkid = formatMsclkid;
|
|
231
|
+
exports.formatUetMsclkid = formatUetMsclkid;
|
|
121
232
|
exports.parseFbc = parseFbc;
|
|
233
|
+
exports.parseGcl = parseGcl;
|
|
234
|
+
exports.parseUetMsclkid = parseUetMsclkid;
|
|
122
235
|
exports.resolveClickIdCookies = resolveClickIdCookies;
|
|
123
236
|
exports.toSetCookieHeaders = toSetCookieHeaders;
|
|
124
237
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["cookie"],"sources":["../../src/click-id/index.ts"],"sourcesContent":["import { type SetCookie, parseCookie, stringifySetCookie } from 'cookie';\n\n/**\n * Server-side resolution of ad-click-id cookies (`_fbc`, `_rdt_cid`) from the incoming request.\n *\n * This is the framework-agnostic core meant to run on the *document* response (e.g. TanStack Start\n * server middleware, Next middleware). Setting `_fbc` via an HTTP `Set-Cookie` header on the top\n * document — rather than `document.cookie` on the client — is what Meta officially recommends and is\n * the only reliable way to keep the cookie alive for 90 days in Safari: ITP caps JavaScript-set\n * cookies on a fbclid-decorated landing page to 24 hours, and the document response is never\n * classified as CNAME/IP cloaking (it is the reference the browser measures cloaking against).\n *\n * reference: https://developers.facebook.com/docs/marketing-api/conversions-api/parameters/fbp-and-fbc\n */\n\n// Meta's recommended _fbc cookie expiry. Events Manager warns (\"Server sending expired fbclid\")\n// when the embedded creationTime is older than 90 days and match quality/attribution may degrade;\n// no hard drop is documented.\nconst FBC_TTL_MS = 90 * 24 * 60 * 60 * 1000;\nconst RDT_CID_TTL_MS = 90 * 24 * 60 * 60 * 1000;\n// Tolerate a little clock skew when validating a creationTime against \"now\".\nconst CLOCK_SKEW_MS = 24 * 60 * 60 * 1000;\n\nexport const FBC_COOKIE = '_fbc';\nexport const RDT_CID_COOKIE = '_rdt_cid';\n\nexport type ParsedFbc = { raw: string; creationTime: number; fbclid: string };\n\n/**\n * Parse `fb.<subdomainIndex>.<creationTime>.<fbclid>`.\n * Returns undefined for anything malformed — a fbclid never contains a dot, but joining the tail\n * back together keeps us forward-compatible if that ever changes.\n */\nexport function parseFbc(\n raw: string | undefined | null,\n now: number = Date.now()\n): ParsedFbc | undefined {\n if (!raw) return undefined;\n const parts = raw.split('.');\n if (parts.length < 4 || parts[0] !== 'fb') return undefined;\n\n const creationTime = Number(parts[2]);\n const fbclid = parts.slice(3).join('.');\n if (!fbclid) return undefined;\n // creationTime is UNIX ms; reject seconds-precision or future-dated values as malformed.\n if (!Number.isFinite(creationTime)) return undefined;\n if (creationTime < 1e12 || creationTime > now + CLOCK_SKEW_MS) return undefined;\n\n return { raw, creationTime, fbclid };\n}\n\n/**\n * Build a fresh `_fbc` value. subdomainIndex is the cookie's domain level (com=0, example.com=1,\n * www.example.com=2); 1 is correct for an apex-hosted cookie.\n */\nexport function formatFbc(fbclid: string, now: number, subdomainIndex = 1): string {\n return `fb.${subdomainIndex}.${now}.${fbclid}`;\n}\n\nexport type ResolveClickIdCookiesInput = {\n /** The absolute request URL (must include the query string). */\n url: string;\n /** The raw `Cookie` request header, if any. */\n cookieHeader?: string | null;\n /** Overridable clock, primarily for tests. Defaults to `Date.now()`. */\n now?: number;\n /** `Domain` attribute for the emitted cookies, e.g. `.shware.io`. Omit for a host-only cookie. */\n domain?: string;\n /** `Secure` attribute, default true. Set false only for local http testing. */\n secure?: boolean;\n /** subdomainIndex for a freshly built `_fbc` (see {@link formatFbc}). Default 1. */\n subdomainIndex?: number;\n /**\n * Re-issue a still-valid `_fbc` at its *remaining* lifetime on every call. On by default, as an\n * ITP self-heal: cookies follow last-writer-wins, so if the Meta Pixel overwrote `_fbc` via\n * `document.cookie` (Safari caps JS-written cookies on a fbclid-decorated landing to 24h),\n * re-issuing the long-lived HTTP cookie on the next navigation restores it — the same\n * continuous-re-issue mitigation the sGTM ecosystem uses (stape Cookie Keeper, Cookie Monster).\n * The value (and its creationTime) is byte-for-byte identical and the window never slides, so it\n * cannot trigger Meta's expired/modified-fbclid warnings.\n *\n * Set false to strictly follow Meta's documented conditional-write rule (\"only set the cookie if\n * it doesn't exist or the fbclid changed\") — e.g. to keep pages CDN-cacheable, since the\n * re-issue attaches a per-user `Set-Cookie` (forcing `no-store`) to every response carrying an\n * `_fbc`.\n */\n refresh?: boolean;\n};\n\nexport type ResolveClickIdCookiesResult = {\n /**\n * Cookies to emit on the response, ready for `Set-Cookie`. Empty when nothing needs to change.\n * A `value: ''`, `maxAge: 0` entry is a deletion (an expired or malformed leftover).\n */\n cookies: SetCookie[];\n /** The resolved `_fbc` value, for immediate server-side use (e.g. Conversions API). */\n fbc?: string;\n /** The resolved `_rdt_cid` value. */\n rdt_cid?: string;\n};\n\nfunction searchParams(url: string): URLSearchParams {\n try {\n return new URL(url).searchParams;\n } catch {\n // Fall back to a manual split so a relative or slightly malformed URL still yields the query.\n const q = url.indexOf('?');\n return new URLSearchParams(q !== -1 ? url.slice(q + 1) : '');\n }\n}\n\n/**\n * Resolve the click-id cookies to set on the current document response.\n *\n * `_fbc` follows Meta's documented conditional-write rule value-wise: a new fbclid (or an absent\n * cookie) opens a fresh 90-day window; a same-fbclid cookie keeps its original value and\n * `creationTime`. Expired (>90d) or malformed values are cleared instead of forwarded. By default,\n * ({@link ResolveClickIdCookiesInput.refresh}) a still-valid cookie is re-issued unchanged at its\n * *remaining* lifetime — never `now + 90d` — so a returning visitor's window cannot slide forward\n * (which is what makes Meta flag an expired fbclid).\n */\nexport function resolveClickIdCookies(\n input: ResolveClickIdCookiesInput\n): ResolveClickIdCookiesResult {\n const { url, cookieHeader, domain, secure = true, subdomainIndex = 1, refresh = true } = input;\n const now = input.now ?? Date.now();\n\n const params = searchParams(url);\n const jar = parseCookie(cookieHeader ?? '');\n const cookies: SetCookie[] = [];\n const result: ResolveClickIdCookiesResult = { cookies };\n\n const base = { path: '/', secure, sameSite: 'lax', domain } as const;\n const set = (name: string, value: string, ttlMs: number) =>\n cookies.push({ name, value, maxAge: Math.floor(ttlMs / 1000), ...base });\n const del = (name: string) => cookies.push({ name, value: '', maxAge: 0, ...base });\n\n // --- Meta _fbc ---\n const urlFbclid = params.get('fbclid') || undefined;\n const existingFbc = parseFbc(jar[FBC_COOKIE], now);\n\n if (urlFbclid && urlFbclid !== existingFbc?.fbclid) {\n // A new click always wins and opens a fresh 90-day window.\n const raw = formatFbc(urlFbclid, now, subdomainIndex);\n set(FBC_COOKIE, raw, FBC_TTL_MS);\n result.fbc = raw;\n } else if (existingFbc) {\n const remainingMs = existingFbc.creationTime + FBC_TTL_MS - now;\n if (remainingMs <= 0) {\n del(FBC_COOKIE);\n } else {\n // Same fbclid: the value and creationTime are preserved (Meta's rule); refresh (default)\n // re-issues it unchanged at its remaining lifetime as an ITP self-heal.\n result.fbc = existingFbc.raw;\n if (refresh) set(FBC_COOKIE, existingFbc.raw, remainingMs);\n }\n } else if (jar[FBC_COOKIE]) {\n // Malformed leftover — clear it rather than forwarding it to Meta.\n del(FBC_COOKIE);\n }\n\n // --- Reddit _rdt_cid ---\n // No embedded timestamp, so it can only be anchored at first capture; set it once from the URL and\n // otherwise leave the existing cookie untouched (re-issuing would slide its window).\n const urlRdtCid = params.get('rdt_cid') || undefined;\n const existingRdtCid = jar[RDT_CID_COOKIE] || undefined;\n if (urlRdtCid && urlRdtCid !== existingRdtCid) {\n set(RDT_CID_COOKIE, urlRdtCid, RDT_CID_TTL_MS);\n result.rdt_cid = urlRdtCid;\n } else if (existingRdtCid) {\n result.rdt_cid = existingRdtCid;\n }\n\n return result;\n}\n\n/** Serialize the resolved cookies into `Set-Cookie` header values. */\nexport function toSetCookieHeaders(cookies: SetCookie[]): string[] {\n return cookies.map((cookie) => stringifySetCookie(cookie));\n}\n"],"mappings":";;;;;;;;;;;;;;;AAkBA,MAAM,aAAa,OAAU,KAAK,KAAK;AACvC,MAAM,iBAAiB,OAAU,KAAK,KAAK;AAE3C,MAAM,gBAAgB,OAAU,KAAK;AAErC,MAAa,aAA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KAAK;EAAE;EAAM;EAAO,QAAQ,KAAK,MAAM,QAAQ,GAAI;EAAG,GAAG;CAAK,CAAC;CACzE,MAAM,OAAO,SAAiB,QAAQ,KAAK;EAAE;EAAM,OAAO;EAAI,QAAQ;EAAG,GAAG;CAAK,CAAC;CAGlF,MAAM,YAAY,OAAO,IAAI,QAAQ,KAAK,KAAA;CAC1C,MAAM,cAAc,SAAS,IAAI,aAAa,GAAG;CAEjD,IAAI,aAAa,cAAc,aAAa,QAAQ;EAElD,MAAM,MAAM,UAAU,WAAW,KAAK,cAAc;EACpD,IAAI,YAAY,KAAK,UAAU;EAC/B,OAAO,MAAM;CACf,OAAO,IAAI,aAAa;EACtB,MAAM,cAAc,YAAY,eAAe,aAAa;EAC5D,IAAI,eAAe,GACjB,IAAI,UAAU;OACT;GAGL,OAAO,MAAM,YAAY;GACzB,IAAI,SAAS,IAAI,YAAY,YAAY,KAAK,WAAW;EAC3D;CACF,OAAO,IAAI,IAAA,SAET,IAAI,UAAU;CAMhB,MAAM,YAAY,OAAO,IAAI,SAAS,KAAK,KAAA;CAC3C,MAAM,iBAAiB,IAAA,eAAuB,KAAA;CAC9C,IAAI,aAAa,cAAc,gBAAgB;EAC7C,IAAI,gBAAgB,WAAW,cAAc;EAC7C,OAAO,UAAU;CACnB,OAAO,IAAI,gBACT,OAAO,UAAU;CAGnB,OAAO;AACT;;AAGA,SAAgB,mBAAmB,SAAgC;CACjE,OAAO,QAAQ,KAAK,cAAA,GAAA,OAAA,mBAAA,CAA8BA,QAAM,CAAC;AAC3D"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["parseCookie","cookie","stringifySetCookie"],"sources":["../../src/click-id/index.ts"],"sourcesContent":["import { type SetCookie, parseCookie, stringifySetCookie } from 'cookie';\n\n/**\n * Server-side resolution of ad-click-id cookies (`_fbc`, `_gcl_aw`/`_gcl_gb`, `_rdt_cid`,\n * `_uetmsclkid`) from the incoming request.\n *\n * This is the framework-agnostic core meant to run on the *document* response (e.g. TanStack Start\n * server middleware, Next middleware). Setting `_fbc` via an HTTP `Set-Cookie` header on the top\n * document — rather than `document.cookie` on the client — is what Meta officially recommends and is\n * the only reliable way to keep the cookie alive for 90 days in Safari: ITP caps JavaScript-set\n * cookies on a fbclid-decorated landing page to 24 hours, and the document response is never\n * classified as CNAME/IP cloaking (it is the reference the browser measures cloaking against).\n *\n * The Google pair follows the same architecture Google itself ships for server containers —\n * sGTM's Conversion Linker sets FPGCLAW via Set-Cookie for 90 days — and the sGTM ecosystem\n * productized for gtag's own cookies (stape Cookie Keeper re-issues `_gcl_*` over HTTP; vendor\n * case studies report 5–20% of Google Ads conversions recovered, up to +44% on the Safari\n * slice). One deployment caveat from WebKit's rules: the HTTP-cookie exemption holds only for\n * responses from the site's own infrastructure — a CNAME or third-party-IP host (a tagging\n * subdomain on someone else's cloud) gets its Set-Cookie silently capped to 7 days. This module\n * runs on the host's own document response, which is exactly the exempt case.\n *\n * references:\n * - https://developers.facebook.com/docs/marketing-api/conversions-api/parameters/fbp-and-fbc\n * - https://webkit.org/tracking-prevention/ (7d JS cookies, 24h on ad-decorated landings,\n * CNAME/third-party-IP HTTP cookies capped to 7d, same-origin HTTP cookies exempt)\n * - https://www.simoahava.com/analytics/google-ads-server-side-tagging-google-tag-manager/\n * (Conversion Linker → FPGCLAW via Set-Cookie, Google's own server-side equivalent)\n * - https://stape.io/helpdesk/documentation/cookie-keeper-power-up (the productized re-issue)\n */\n\n// Meta's recommended _fbc cookie expiry. Events Manager warns (\"Server sending expired fbclid\")\n// when the embedded creationTime is older than 90 days and match quality/attribution may degrade;\n// no hard drop is documented.\nconst FBC_TTL_MS = 90 * 24 * 60 * 60 * 1000;\nconst RDT_CID_TTL_MS = 90 * 24 * 60 * 60 * 1000;\n// bat.js's own expiry for _uetmsclkid (`msClkIdExpirationTime`), the Microsoft Ads click window.\nconst MSCLKID_TTL_MS = 90 * 24 * 60 * 60 * 1000;\n// gtag's own expiry for _gcl_* — 90 days, the length of a Google Ads click's upload window.\nconst GCL_TTL_MS = 90 * 24 * 60 * 60 * 1000;\n// Tolerate a little clock skew when validating a creationTime against \"now\".\nconst CLOCK_SKEW_MS = 24 * 60 * 60 * 1000;\n\nexport const FBC_COOKIE = '_fbc';\nexport const RDT_CID_COOKIE = '_rdt_cid';\nexport const GCL_AW_COOKIE = '_gcl_aw';\nexport const GCL_GB_COOKIE = '_gcl_gb';\nexport const UET_MSCLKID_COOKIE = '_uetmsclkid';\n\nexport type ParsedFbc = { raw: string; creationTime: number; fbclid: string };\n\n/**\n * Parse `fb.<subdomainIndex>.<creationTime>.<fbclid>`.\n * Returns undefined for anything malformed — a fbclid never contains a dot, but joining the tail\n * back together keeps us forward-compatible if that ever changes.\n */\nexport function parseFbc(\n raw: string | undefined | null,\n now: number = Date.now()\n): ParsedFbc | undefined {\n if (!raw) return undefined;\n const parts = raw.split('.');\n if (parts.length < 4 || parts[0] !== 'fb') return undefined;\n\n const creationTime = Number(parts[2]);\n const fbclid = parts.slice(3).join('.');\n if (!fbclid) return undefined;\n // creationTime is UNIX ms; reject seconds-precision or future-dated values as malformed.\n if (!Number.isFinite(creationTime)) return undefined;\n if (creationTime < 1e12 || creationTime > now + CLOCK_SKEW_MS) return undefined;\n\n return { raw, creationTime, fbclid };\n}\n\n/**\n * Build a fresh `_fbc` value. subdomainIndex is the cookie's domain level (com=0, example.com=1,\n * www.example.com=2); 1 is correct for an apex-hosted cookie.\n */\nexport function formatFbc(fbclid: string, now: number, subdomainIndex = 1): string {\n return `fb.${subdomainIndex}.${now}.${fbclid}`;\n}\n\nexport type ParsedGcl = { raw: string; creationTime: number; clickId: string };\n\n// gtag's own value validator, extracted from the live gtag.js bundle (2026-09): its parser\n// accepts version \"GCL\" or \"1\", a /^\\d+$/ seconds timestamp, and a click id matching this.\n// Re-verify against gtag.js before changing anything here — the format is gtag's, not ours.\nconst GCL_CLICK_ID = /^[\\w-]+$/;\n\n/**\n * Parse a `_gcl_aw`/`_gcl_gb` value: `GCL.<seconds>.<clickId>[.<labels…>]` — note the timestamp\n * is in SECONDS, unlike `_fbc`'s milliseconds. gtag's parser accepts version `GCL` or `1` and\n * validates the segments exactly this way (`/^\\d+$/` timestamp, `/^[\\w-]+$/` click id); any\n * labels tail is preserved through `raw` so a re-issue stays byte-identical.\n */\nexport function parseGcl(\n raw: string | undefined | null,\n now: number = Date.now()\n): ParsedGcl | undefined {\n if (!raw) return undefined;\n const parts = raw.split('.');\n if (parts.length < 3) return undefined;\n if (parts[0] !== 'GCL' && parts[0] !== '1') return undefined;\n const seconds = parts[1];\n const clickId = parts[2];\n if (seconds === undefined || !/^\\d+$/.test(seconds)) return undefined;\n if (clickId === undefined || !GCL_CLICK_ID.test(clickId)) return undefined;\n const creationTime = Number(seconds) * 1000;\n // Reject millisecond-precision or future-dated values as malformed.\n if (creationTime >= 1e15 || creationTime > now + CLOCK_SKEW_MS) return undefined;\n return { raw, creationTime, clickId };\n}\n\n/** Build a fresh `_gcl_aw`/`_gcl_gb` value in gtag's own format. */\nexport function formatGcl(clickId: string, now: number): string {\n return `GCL.${Math.floor(now / 1000)}.${clickId}`;\n}\n\n// bat.js writes the click id as `_uet<msclkid>` and reads it back only when what follows the\n// prefix is at most 32 characters (`msClkIdCookieValuePrefix`, `lengthMsClkId`); a msclkid is a\n// 32-hex-digit UUID without dashes. Re-verify against bat.js before changing anything here —\n// the format is UET's, not ours.\nconst UET_MSCLKID_PREFIX = '_uet';\nconst MSCLKID = /^[0-9a-f]{32}$/i;\n\n/**\n * Parse a `_uetmsclkid` value: `_uet<32 hex>`. Returns the click id, lowercased, or undefined\n * for anything bat.js itself would not read back.\n */\nexport function parseUetMsclkid(raw: string | undefined | null): string | undefined {\n if (!raw || !raw.startsWith(UET_MSCLKID_PREFIX)) return undefined;\n const clickId = raw.slice(UET_MSCLKID_PREFIX.length);\n return MSCLKID.test(clickId) ? clickId.toLowerCase() : undefined;\n}\n\n/** Build a `_uetmsclkid` value in bat.js's own format. */\nexport function formatUetMsclkid(clickId: string): string {\n return `${UET_MSCLKID_PREFIX}${clickId.toLowerCase()}`;\n}\n\n/**\n * The click id as the Conversions API wants it: a dashed, lowercase UUID. The URL and the cookie\n * carry the 32-hex form; a value in any other shape is returned untouched for the API to judge.\n */\nexport function formatMsclkid(clickId: string): string {\n const id = clickId.trim().toLowerCase();\n if (!MSCLKID.test(id)) return id;\n return `${id.slice(0, 8)}-${id.slice(8, 12)}-${id.slice(12, 16)}-${id.slice(16, 20)}-${id.slice(20)}`;\n}\n\nexport type ResolveClickIdCookiesInput = {\n /** The absolute request URL (must include the query string). */\n url: string;\n /** The raw `Cookie` request header, if any. */\n cookieHeader?: string | null;\n /** Overridable clock, primarily for tests. Defaults to `Date.now()`. */\n now?: number;\n /** `Domain` attribute for the emitted cookies, e.g. `.shware.io`. Omit for a host-only cookie. */\n domain?: string;\n /** `Secure` attribute, default true. Set false only for local http testing. */\n secure?: boolean;\n /** subdomainIndex for a freshly built `_fbc` (see {@link formatFbc}). Default 1. */\n subdomainIndex?: number;\n /**\n * Re-issue a still-valid `_fbc` at its *remaining* lifetime on every call. On by default, as an\n * ITP self-heal: cookies follow last-writer-wins, so if the Meta Pixel overwrote `_fbc` via\n * `document.cookie` (Safari caps JS-written cookies on a fbclid-decorated landing to 24h),\n * re-issuing the long-lived HTTP cookie on the next navigation restores it — the same\n * continuous-re-issue mitigation the sGTM ecosystem uses (stape Cookie Keeper, Cookie Monster).\n * The value (and its creationTime) is byte-for-byte identical and the window never slides, so it\n * cannot trigger Meta's expired/modified-fbclid warnings.\n *\n * Set false to strictly follow Meta's documented conditional-write rule (\"only set the cookie if\n * it doesn't exist or the fbclid changed\") — e.g. to keep pages CDN-cacheable, since the\n * re-issue attaches a per-user `Set-Cookie` (forcing `no-store`) to every response carrying an\n * `_fbc`.\n */\n refresh?: boolean;\n};\n\nexport type ResolveClickIdCookiesResult = {\n /**\n * Cookies to emit on the response, ready for `Set-Cookie`. Empty when nothing needs to change.\n * A `value: ''`, `maxAge: 0` entry is a deletion (an expired or malformed leftover).\n */\n cookies: SetCookie[];\n /** The resolved `_fbc` value, for immediate server-side use (e.g. Conversions API). */\n fbc?: string;\n /** The resolved `_rdt_cid` value. */\n rdt_cid?: string;\n /** The resolved Google Ads click id (from the URL or a still-valid `_gcl_aw`). */\n gclid?: string;\n /** The resolved iOS web-to-app click id (from the URL or a still-valid `_gcl_gb`). */\n wbraid?: string;\n /** The resolved Microsoft Ads click id (from the URL or `_uetmsclkid`), 32-hex form. */\n msclkid?: string;\n};\n\nfunction searchParams(url: string): URLSearchParams {\n try {\n return new URL(url).searchParams;\n } catch {\n // Fall back to a manual split so a relative or slightly malformed URL still yields the query.\n const q = url.indexOf('?');\n return new URLSearchParams(q !== -1 ? url.slice(q + 1) : '');\n }\n}\n\n/**\n * Resolve the click-id cookies to set on the current document response.\n *\n * `_fbc` follows Meta's documented conditional-write rule value-wise: a new fbclid (or an absent\n * cookie) opens a fresh 90-day window; a same-fbclid cookie keeps its original value and\n * `creationTime`. Expired (>90d) or malformed values are cleared instead of forwarded. By default,\n * ({@link ResolveClickIdCookiesInput.refresh}) a still-valid cookie is re-issued unchanged at its\n * *remaining* lifetime — never `now + 90d` — so a returning visitor's window cannot slide forward\n * (which is what makes Meta flag an expired fbclid).\n */\nexport function resolveClickIdCookies(\n input: ResolveClickIdCookiesInput\n): ResolveClickIdCookiesResult {\n const { url, cookieHeader, domain, secure = true, subdomainIndex = 1, refresh = true } = input;\n const now = input.now ?? Date.now();\n\n const params = searchParams(url);\n const jar = parseCookie(cookieHeader ?? '');\n const cookies: SetCookie[] = [];\n const result: ResolveClickIdCookiesResult = { cookies };\n\n const base = { path: '/', secure, sameSite: 'lax', domain } as const;\n const set = (name: string, value: string, ttlMs: number) =>\n cookies.push({ name, value, maxAge: Math.floor(ttlMs / 1000), ...base });\n const del = (name: string) => cookies.push({ name, value: '', maxAge: 0, ...base });\n\n // --- Meta _fbc ---\n const urlFbclid = params.get('fbclid') || undefined;\n const existingFbc = parseFbc(jar[FBC_COOKIE], now);\n\n if (urlFbclid && urlFbclid !== existingFbc?.fbclid) {\n // A new click always wins and opens a fresh 90-day window.\n const raw = formatFbc(urlFbclid, now, subdomainIndex);\n set(FBC_COOKIE, raw, FBC_TTL_MS);\n result.fbc = raw;\n } else if (existingFbc) {\n const remainingMs = existingFbc.creationTime + FBC_TTL_MS - now;\n if (remainingMs <= 0) {\n del(FBC_COOKIE);\n } else {\n // Same fbclid: the value and creationTime are preserved (Meta's rule); refresh (default)\n // re-issues it unchanged at its remaining lifetime as an ITP self-heal.\n result.fbc = existingFbc.raw;\n if (refresh) set(FBC_COOKIE, existingFbc.raw, remainingMs);\n }\n } else if (jar[FBC_COOKIE]) {\n // Malformed leftover — clear it rather than forwarding it to Meta.\n del(FBC_COOKIE);\n }\n\n // --- Google Ads _gcl_aw / _gcl_gb ---\n // The same first-party HTTP persistence Google's own server-side Conversion Linker performs\n // (it sets FPGCLAW via Set-Cookie for 90 days), applied to gtag's cookies the way the sGTM\n // ecosystem does (stape Cookie Keeper re-issues _gcl_* over HTTP). Ownership is shared with\n // gtag, so the rules are stricter than for _fbc: a value this module cannot parse is left\n // untouched — never rewritten, never deleted — and a re-issue is byte-identical (labels tail\n // included) at the window's REMAINING lifetime, so the 90-day click window never slides.\n const gclsrc = params.get('gclsrc') || undefined;\n const google = [\n {\n cookie: GCL_AW_COOKIE,\n // gtag's own gating, verbatim: a gclid enters _gcl_aw only when gclsrc is absent or\n // 'aw.ds' — 'ds'/'3p.ds' clicks are Search Ads 360's and belong to _gcl_dc.\n urlClickId: !gclsrc || gclsrc === 'aw.ds' ? params.get('gclid') || undefined : undefined,\n assign: (value: string) => (result.gclid = value),\n },\n {\n cookie: GCL_GB_COOKIE,\n urlClickId: params.get('wbraid') || undefined,\n assign: (value: string) => (result.wbraid = value),\n },\n ];\n for (const { cookie, urlClickId, assign } of google) {\n const existing = parseGcl(jar[cookie], now);\n if (urlClickId && GCL_CLICK_ID.test(urlClickId) && urlClickId !== existing?.clickId) {\n // A new click always wins and opens a fresh 90-day window, in gtag's exact format.\n set(cookie, formatGcl(urlClickId, now), GCL_TTL_MS);\n assign(urlClickId);\n } else if (existing) {\n const remainingMs = existing.creationTime + GCL_TTL_MS - now;\n if (remainingMs > 0) {\n assign(existing.clickId);\n if (refresh) set(cookie, existing.raw, remainingMs);\n }\n // Expired: not returned and not re-issued, but gtag owns the deletion.\n }\n }\n\n // --- Reddit _rdt_cid ---\n // No embedded timestamp, so it can only be anchored at first capture; set it once from the URL and\n // otherwise leave the existing cookie untouched (re-issuing would slide its window).\n const urlRdtCid = params.get('rdt_cid') || undefined;\n const existingRdtCid = jar[RDT_CID_COOKIE] || undefined;\n if (urlRdtCid && urlRdtCid !== existingRdtCid) {\n set(RDT_CID_COOKIE, urlRdtCid, RDT_CID_TTL_MS);\n result.rdt_cid = urlRdtCid;\n } else if (existingRdtCid) {\n result.rdt_cid = existingRdtCid;\n }\n\n // --- Microsoft Ads _uetmsclkid ---\n // The UET tag's own cookie, in its own format, so the tag and this module read each other's\n // writes. Microsoft's ITP answer is this first-party cookie (plus a localStorage backup), but\n // bat.js writes it through `document.cookie`, which Safari caps at 7 days — 24 hours on a\n // landing page decorated by a classified domain, and a bing.com ad click is exactly that.\n // Persisting it here over HTTP on the document response restores the 90-day window.\n //\n // The re-issue (default on, see `refresh`) is the same self-heal as for `_fbc`, and it matters\n // more here: bat.js rewrites this cookie on EVERY page load, so without the re-issue the HTTP\n // copy is downgraded to a JS cookie on the very next page. There is no embedded timestamp to\n // compute a remaining lifetime from, so it is re-issued at a fresh 90 days — which is exactly\n // what bat.js itself does on each page (`msClkIdExpirationTime` from now), so the window\n // slides no more than the tag already slides it. A value this module cannot parse belongs to\n // the tag: never rewritten, never deleted.\n const urlMsclkid = params.get('msclkid')?.trim();\n const existingMsclkid = parseUetMsclkid(jar[UET_MSCLKID_COOKIE]);\n if (urlMsclkid && MSCLKID.test(urlMsclkid) && urlMsclkid.toLowerCase() !== existingMsclkid) {\n set(UET_MSCLKID_COOKIE, formatUetMsclkid(urlMsclkid), MSCLKID_TTL_MS);\n result.msclkid = urlMsclkid.toLowerCase();\n } else if (existingMsclkid) {\n result.msclkid = existingMsclkid;\n if (refresh) set(UET_MSCLKID_COOKIE, formatUetMsclkid(existingMsclkid), MSCLKID_TTL_MS);\n }\n\n return result;\n}\n\n/** Serialize the resolved cookies into `Set-Cookie` header values. */\nexport function toSetCookieHeaders(cookies: SetCookie[]): string[] {\n return cookies.map((cookie) => stringifySetCookie(cookie));\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,MAAM,aAAa;AACnB,MAAM,iBAAiB;AAEvB,MAAM,iBAAiB;AAEvB,MAAM,aAAa;AAEnB,MAAM,gBAAgB;AAEtB,MAAa,aAAa;AAC1B,MAAa,iBAAiB;AAC9B,MAAa,gBAAgB;AAC7B,MAAa,gBAAgB;AAC7B,MAAa,qBAAqB;;;;;;AASlC,SAAgB,SACd,KACA,MAAc,KAAK,IAAI,GACA;CACvB,IAAI,CAAC,KAAK,OAAO,KAAA;CACjB,MAAM,QAAQ,IAAI,MAAM,GAAG;CAC3B,IAAI,MAAM,SAAS,KAAK,MAAM,OAAO,MAAM,OAAO,KAAA;CAElD,MAAM,eAAe,OAAO,MAAM,EAAE;CACpC,MAAM,SAAS,MAAM,MAAM,CAAC,CAAC,CAAC,KAAK,GAAG;CACtC,IAAI,CAAC,QAAQ,OAAO,KAAA;CAEpB,IAAI,CAAC,OAAO,SAAS,YAAY,GAAG,OAAO,KAAA;CAC3C,IAAI,eAAe,gBAAQ,eAAe,MAAM,eAAe,OAAO,KAAA;CAEtE,OAAO;EAAE;EAAK;EAAc;CAAO;AACrC;;;;;AAMA,SAAgB,UAAU,QAAgB,KAAa,iBAAiB,GAAW;CACjF,OAAO,MAAM,eAAe,GAAG,IAAI,GAAG;AACxC;AAOA,MAAM,eAAe;;;;;;;AAQrB,SAAgB,SACd,KACA,MAAc,KAAK,IAAI,GACA;CACvB,IAAI,CAAC,KAAK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UAAU;EACjB,IAAI,SAAS,IAAI,oBAAoB,iBAAiB,eAAe,GAAG,cAAc;CACxF;CAEA,OAAO;AACT;;AAGA,SAAgB,mBAAmB,SAAgC;CACjE,OAAO,QAAQ,KAAK,cAAA,GAAWC,OAAAA,mBAAAA,CAAmBD,QAAM,CAAC;AAC3D"}
|
|
@@ -2,6 +2,9 @@ import { SetCookie } from "cookie";
|
|
|
2
2
|
//#region src/click-id/index.d.ts
|
|
3
3
|
declare const FBC_COOKIE = "_fbc";
|
|
4
4
|
declare const RDT_CID_COOKIE = "_rdt_cid";
|
|
5
|
+
declare const GCL_AW_COOKIE = "_gcl_aw";
|
|
6
|
+
declare const GCL_GB_COOKIE = "_gcl_gb";
|
|
7
|
+
declare const UET_MSCLKID_COOKIE = "_uetmsclkid";
|
|
5
8
|
type ParsedFbc = {
|
|
6
9
|
raw: string;
|
|
7
10
|
creationTime: number;
|
|
@@ -18,6 +21,32 @@ declare function parseFbc(raw: string | undefined | null, now?: number): ParsedF
|
|
|
18
21
|
* www.example.com=2); 1 is correct for an apex-hosted cookie.
|
|
19
22
|
*/
|
|
20
23
|
declare function formatFbc(fbclid: string, now: number, subdomainIndex?: number): string;
|
|
24
|
+
type ParsedGcl = {
|
|
25
|
+
raw: string;
|
|
26
|
+
creationTime: number;
|
|
27
|
+
clickId: string;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Parse a `_gcl_aw`/`_gcl_gb` value: `GCL.<seconds>.<clickId>[.<labels…>]` — note the timestamp
|
|
31
|
+
* is in SECONDS, unlike `_fbc`'s milliseconds. gtag's parser accepts version `GCL` or `1` and
|
|
32
|
+
* validates the segments exactly this way (`/^\d+$/` timestamp, `/^[\w-]+$/` click id); any
|
|
33
|
+
* labels tail is preserved through `raw` so a re-issue stays byte-identical.
|
|
34
|
+
*/
|
|
35
|
+
declare function parseGcl(raw: string | undefined | null, now?: number): ParsedGcl | undefined;
|
|
36
|
+
/** Build a fresh `_gcl_aw`/`_gcl_gb` value in gtag's own format. */
|
|
37
|
+
declare function formatGcl(clickId: string, now: number): string;
|
|
38
|
+
/**
|
|
39
|
+
* Parse a `_uetmsclkid` value: `_uet<32 hex>`. Returns the click id, lowercased, or undefined
|
|
40
|
+
* for anything bat.js itself would not read back.
|
|
41
|
+
*/
|
|
42
|
+
declare function parseUetMsclkid(raw: string | undefined | null): string | undefined;
|
|
43
|
+
/** Build a `_uetmsclkid` value in bat.js's own format. */
|
|
44
|
+
declare function formatUetMsclkid(clickId: string): string;
|
|
45
|
+
/**
|
|
46
|
+
* The click id as the Conversions API wants it: a dashed, lowercase UUID. The URL and the cookie
|
|
47
|
+
* carry the 32-hex form; a value in any other shape is returned untouched for the API to judge.
|
|
48
|
+
*/
|
|
49
|
+
declare function formatMsclkid(clickId: string): string;
|
|
21
50
|
type ResolveClickIdCookiesInput = {
|
|
22
51
|
/** The absolute request URL (must include the query string). */
|
|
23
52
|
url: string;
|
|
@@ -57,6 +86,12 @@ type ResolveClickIdCookiesResult = {
|
|
|
57
86
|
fbc?: string;
|
|
58
87
|
/** The resolved `_rdt_cid` value. */
|
|
59
88
|
rdt_cid?: string;
|
|
89
|
+
/** The resolved Google Ads click id (from the URL or a still-valid `_gcl_aw`). */
|
|
90
|
+
gclid?: string;
|
|
91
|
+
/** The resolved iOS web-to-app click id (from the URL or a still-valid `_gcl_gb`). */
|
|
92
|
+
wbraid?: string;
|
|
93
|
+
/** The resolved Microsoft Ads click id (from the URL or `_uetmsclkid`), 32-hex form. */
|
|
94
|
+
msclkid?: string;
|
|
60
95
|
};
|
|
61
96
|
/**
|
|
62
97
|
* Resolve the click-id cookies to set on the current document response.
|
|
@@ -72,5 +107,5 @@ declare function resolveClickIdCookies(input: ResolveClickIdCookiesInput): Resol
|
|
|
72
107
|
/** Serialize the resolved cookies into `Set-Cookie` header values. */
|
|
73
108
|
declare function toSetCookieHeaders(cookies: SetCookie[]): string[];
|
|
74
109
|
//#endregion
|
|
75
|
-
export { FBC_COOKIE, ParsedFbc, RDT_CID_COOKIE, ResolveClickIdCookiesInput, ResolveClickIdCookiesResult, formatFbc, parseFbc, resolveClickIdCookies, toSetCookieHeaders };
|
|
110
|
+
export { FBC_COOKIE, GCL_AW_COOKIE, GCL_GB_COOKIE, ParsedFbc, ParsedGcl, RDT_CID_COOKIE, ResolveClickIdCookiesInput, ResolveClickIdCookiesResult, UET_MSCLKID_COOKIE, formatFbc, formatGcl, formatMsclkid, formatUetMsclkid, parseFbc, parseGcl, parseUetMsclkid, resolveClickIdCookies, toSetCookieHeaders };
|
|
76
111
|
//# sourceMappingURL=index.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","names":[],"sources":["../../src/click-id/index.ts"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../../src/click-id/index.ts"],"mappings":";;cA2Ca;cACA;cACA;cACA;cACA;KAED;EAAc;EAAa;EAAsB;;;;;;;iBAO7C,SACd,gCACA,eACC;;;;;iBAmBa,UAAU,gBAAgB,aAAa;KAI3C;EAAc;EAAa;EAAsB;;;;;;;;iBAa7C,SACd,gCACA,eACC;;iBAgBa,UAAU,iBAAiB;;;;;iBAe3B,gBAAgB;;iBAOhB,iBAAiB;;;;;iBAQjB,cAAc;KAMlB;;EAEV;;EAEA;;EAEA;;EAEA;;EAEA;;EAEA;;;;;;;;;;;;;;;EAeA;;KAGU;;;;;EAKV,SAAS;;EAET;;EAEA;;EAEA;;EAEA;;EAEA;;;;;;;;;;;;iBAuBc,sBACd,OAAO,6BACN;;iBAoHa,mBAAmB,SAAS"}
|
|
@@ -2,6 +2,9 @@ import { SetCookie } from "cookie";
|
|
|
2
2
|
//#region src/click-id/index.d.ts
|
|
3
3
|
declare const FBC_COOKIE = "_fbc";
|
|
4
4
|
declare const RDT_CID_COOKIE = "_rdt_cid";
|
|
5
|
+
declare const GCL_AW_COOKIE = "_gcl_aw";
|
|
6
|
+
declare const GCL_GB_COOKIE = "_gcl_gb";
|
|
7
|
+
declare const UET_MSCLKID_COOKIE = "_uetmsclkid";
|
|
5
8
|
type ParsedFbc = {
|
|
6
9
|
raw: string;
|
|
7
10
|
creationTime: number;
|
|
@@ -18,6 +21,32 @@ declare function parseFbc(raw: string | undefined | null, now?: number): ParsedF
|
|
|
18
21
|
* www.example.com=2); 1 is correct for an apex-hosted cookie.
|
|
19
22
|
*/
|
|
20
23
|
declare function formatFbc(fbclid: string, now: number, subdomainIndex?: number): string;
|
|
24
|
+
type ParsedGcl = {
|
|
25
|
+
raw: string;
|
|
26
|
+
creationTime: number;
|
|
27
|
+
clickId: string;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Parse a `_gcl_aw`/`_gcl_gb` value: `GCL.<seconds>.<clickId>[.<labels…>]` — note the timestamp
|
|
31
|
+
* is in SECONDS, unlike `_fbc`'s milliseconds. gtag's parser accepts version `GCL` or `1` and
|
|
32
|
+
* validates the segments exactly this way (`/^\d+$/` timestamp, `/^[\w-]+$/` click id); any
|
|
33
|
+
* labels tail is preserved through `raw` so a re-issue stays byte-identical.
|
|
34
|
+
*/
|
|
35
|
+
declare function parseGcl(raw: string | undefined | null, now?: number): ParsedGcl | undefined;
|
|
36
|
+
/** Build a fresh `_gcl_aw`/`_gcl_gb` value in gtag's own format. */
|
|
37
|
+
declare function formatGcl(clickId: string, now: number): string;
|
|
38
|
+
/**
|
|
39
|
+
* Parse a `_uetmsclkid` value: `_uet<32 hex>`. Returns the click id, lowercased, or undefined
|
|
40
|
+
* for anything bat.js itself would not read back.
|
|
41
|
+
*/
|
|
42
|
+
declare function parseUetMsclkid(raw: string | undefined | null): string | undefined;
|
|
43
|
+
/** Build a `_uetmsclkid` value in bat.js's own format. */
|
|
44
|
+
declare function formatUetMsclkid(clickId: string): string;
|
|
45
|
+
/**
|
|
46
|
+
* The click id as the Conversions API wants it: a dashed, lowercase UUID. The URL and the cookie
|
|
47
|
+
* carry the 32-hex form; a value in any other shape is returned untouched for the API to judge.
|
|
48
|
+
*/
|
|
49
|
+
declare function formatMsclkid(clickId: string): string;
|
|
21
50
|
type ResolveClickIdCookiesInput = {
|
|
22
51
|
/** The absolute request URL (must include the query string). */
|
|
23
52
|
url: string;
|
|
@@ -57,6 +86,12 @@ type ResolveClickIdCookiesResult = {
|
|
|
57
86
|
fbc?: string;
|
|
58
87
|
/** The resolved `_rdt_cid` value. */
|
|
59
88
|
rdt_cid?: string;
|
|
89
|
+
/** The resolved Google Ads click id (from the URL or a still-valid `_gcl_aw`). */
|
|
90
|
+
gclid?: string;
|
|
91
|
+
/** The resolved iOS web-to-app click id (from the URL or a still-valid `_gcl_gb`). */
|
|
92
|
+
wbraid?: string;
|
|
93
|
+
/** The resolved Microsoft Ads click id (from the URL or `_uetmsclkid`), 32-hex form. */
|
|
94
|
+
msclkid?: string;
|
|
60
95
|
};
|
|
61
96
|
/**
|
|
62
97
|
* Resolve the click-id cookies to set on the current document response.
|
|
@@ -72,5 +107,5 @@ declare function resolveClickIdCookies(input: ResolveClickIdCookiesInput): Resol
|
|
|
72
107
|
/** Serialize the resolved cookies into `Set-Cookie` header values. */
|
|
73
108
|
declare function toSetCookieHeaders(cookies: SetCookie[]): string[];
|
|
74
109
|
//#endregion
|
|
75
|
-
export { FBC_COOKIE, ParsedFbc, RDT_CID_COOKIE, ResolveClickIdCookiesInput, ResolveClickIdCookiesResult, formatFbc, parseFbc, resolveClickIdCookies, toSetCookieHeaders };
|
|
110
|
+
export { FBC_COOKIE, GCL_AW_COOKIE, GCL_GB_COOKIE, ParsedFbc, ParsedGcl, RDT_CID_COOKIE, ResolveClickIdCookiesInput, ResolveClickIdCookiesResult, UET_MSCLKID_COOKIE, formatFbc, formatGcl, formatMsclkid, formatUetMsclkid, parseFbc, parseGcl, parseUetMsclkid, resolveClickIdCookies, toSetCookieHeaders };
|
|
76
111
|
//# sourceMappingURL=index.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/click-id/index.ts"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/click-id/index.ts"],"mappings":";;cA2Ca;cACA;cACA;cACA;cACA;KAED;EAAc;EAAa;EAAsB;;;;;;;iBAO7C,SACd,gCACA,eACC;;;;;iBAmBa,UAAU,gBAAgB,aAAa;KAI3C;EAAc;EAAa;EAAsB;;;;;;;;iBAa7C,SACd,gCACA,eACC;;iBAgBa,UAAU,iBAAiB;;;;;iBAe3B,gBAAgB;;iBAOhB,iBAAiB;;;;;iBAQjB,cAAc;KAMlB;;EAEV;;EAEA;;EAEA;;EAEA;;EAEA;;EAEA;;;;;;;;;;;;;;;EAeA;;KAGU;;;;;EAKV,SAAS;;EAET;;EAEA;;EAEA;;EAEA;;EAEA;;;;;;;;;;;;iBAuBc,sBACd,OAAO,6BACN;;iBAoHa,mBAAmB,SAAS"}
|
package/dist/click-id/index.mjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { parseCookie, stringifySetCookie } from "cookie";
|
|
2
2
|
//#region src/click-id/index.ts
|
|
3
3
|
/**
|
|
4
|
-
* Server-side resolution of ad-click-id cookies (`_fbc`, `_rdt_cid
|
|
4
|
+
* Server-side resolution of ad-click-id cookies (`_fbc`, `_gcl_aw`/`_gcl_gb`, `_rdt_cid`,
|
|
5
|
+
* `_uetmsclkid`) from the incoming request.
|
|
5
6
|
*
|
|
6
7
|
* This is the framework-agnostic core meant to run on the *document* response (e.g. TanStack Start
|
|
7
8
|
* server middleware, Next middleware). Setting `_fbc` via an HTTP `Set-Cookie` header on the top
|
|
@@ -10,13 +11,33 @@ import { parseCookie, stringifySetCookie } from "cookie";
|
|
|
10
11
|
* cookies on a fbclid-decorated landing page to 24 hours, and the document response is never
|
|
11
12
|
* classified as CNAME/IP cloaking (it is the reference the browser measures cloaking against).
|
|
12
13
|
*
|
|
13
|
-
*
|
|
14
|
+
* The Google pair follows the same architecture Google itself ships for server containers —
|
|
15
|
+
* sGTM's Conversion Linker sets FPGCLAW via Set-Cookie for 90 days — and the sGTM ecosystem
|
|
16
|
+
* productized for gtag's own cookies (stape Cookie Keeper re-issues `_gcl_*` over HTTP; vendor
|
|
17
|
+
* case studies report 5–20% of Google Ads conversions recovered, up to +44% on the Safari
|
|
18
|
+
* slice). One deployment caveat from WebKit's rules: the HTTP-cookie exemption holds only for
|
|
19
|
+
* responses from the site's own infrastructure — a CNAME or third-party-IP host (a tagging
|
|
20
|
+
* subdomain on someone else's cloud) gets its Set-Cookie silently capped to 7 days. This module
|
|
21
|
+
* runs on the host's own document response, which is exactly the exempt case.
|
|
22
|
+
*
|
|
23
|
+
* references:
|
|
24
|
+
* - https://developers.facebook.com/docs/marketing-api/conversions-api/parameters/fbp-and-fbc
|
|
25
|
+
* - https://webkit.org/tracking-prevention/ (7d JS cookies, 24h on ad-decorated landings,
|
|
26
|
+
* CNAME/third-party-IP HTTP cookies capped to 7d, same-origin HTTP cookies exempt)
|
|
27
|
+
* - https://www.simoahava.com/analytics/google-ads-server-side-tagging-google-tag-manager/
|
|
28
|
+
* (Conversion Linker → FPGCLAW via Set-Cookie, Google's own server-side equivalent)
|
|
29
|
+
* - https://stape.io/helpdesk/documentation/cookie-keeper-power-up (the productized re-issue)
|
|
14
30
|
*/
|
|
15
|
-
const FBC_TTL_MS =
|
|
16
|
-
const RDT_CID_TTL_MS =
|
|
17
|
-
const
|
|
31
|
+
const FBC_TTL_MS = 7776e6;
|
|
32
|
+
const RDT_CID_TTL_MS = 7776e6;
|
|
33
|
+
const MSCLKID_TTL_MS = 7776e6;
|
|
34
|
+
const GCL_TTL_MS = 7776e6;
|
|
35
|
+
const CLOCK_SKEW_MS = 864e5;
|
|
18
36
|
const FBC_COOKIE = "_fbc";
|
|
19
37
|
const RDT_CID_COOKIE = "_rdt_cid";
|
|
38
|
+
const GCL_AW_COOKIE = "_gcl_aw";
|
|
39
|
+
const GCL_GB_COOKIE = "_gcl_gb";
|
|
40
|
+
const UET_MSCLKID_COOKIE = "_uetmsclkid";
|
|
20
41
|
/**
|
|
21
42
|
* Parse `fb.<subdomainIndex>.<creationTime>.<fbclid>`.
|
|
22
43
|
* Returns undefined for anything malformed — a fbclid never contains a dot, but joining the tail
|
|
@@ -44,6 +65,58 @@ function parseFbc(raw, now = Date.now()) {
|
|
|
44
65
|
function formatFbc(fbclid, now, subdomainIndex = 1) {
|
|
45
66
|
return `fb.${subdomainIndex}.${now}.${fbclid}`;
|
|
46
67
|
}
|
|
68
|
+
const GCL_CLICK_ID = /^[\w-]+$/;
|
|
69
|
+
/**
|
|
70
|
+
* Parse a `_gcl_aw`/`_gcl_gb` value: `GCL.<seconds>.<clickId>[.<labels…>]` — note the timestamp
|
|
71
|
+
* is in SECONDS, unlike `_fbc`'s milliseconds. gtag's parser accepts version `GCL` or `1` and
|
|
72
|
+
* validates the segments exactly this way (`/^\d+$/` timestamp, `/^[\w-]+$/` click id); any
|
|
73
|
+
* labels tail is preserved through `raw` so a re-issue stays byte-identical.
|
|
74
|
+
*/
|
|
75
|
+
function parseGcl(raw, now = Date.now()) {
|
|
76
|
+
if (!raw) return void 0;
|
|
77
|
+
const parts = raw.split(".");
|
|
78
|
+
if (parts.length < 3) return void 0;
|
|
79
|
+
if (parts[0] !== "GCL" && parts[0] !== "1") return void 0;
|
|
80
|
+
const seconds = parts[1];
|
|
81
|
+
const clickId = parts[2];
|
|
82
|
+
if (seconds === void 0 || !/^\d+$/.test(seconds)) return void 0;
|
|
83
|
+
if (clickId === void 0 || !GCL_CLICK_ID.test(clickId)) return void 0;
|
|
84
|
+
const creationTime = Number(seconds) * 1e3;
|
|
85
|
+
if (creationTime >= 0x38d7ea4c68000 || creationTime > now + CLOCK_SKEW_MS) return void 0;
|
|
86
|
+
return {
|
|
87
|
+
raw,
|
|
88
|
+
creationTime,
|
|
89
|
+
clickId
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/** Build a fresh `_gcl_aw`/`_gcl_gb` value in gtag's own format. */
|
|
93
|
+
function formatGcl(clickId, now) {
|
|
94
|
+
return `GCL.${Math.floor(now / 1e3)}.${clickId}`;
|
|
95
|
+
}
|
|
96
|
+
const UET_MSCLKID_PREFIX = "_uet";
|
|
97
|
+
const MSCLKID = /^[0-9a-f]{32}$/i;
|
|
98
|
+
/**
|
|
99
|
+
* Parse a `_uetmsclkid` value: `_uet<32 hex>`. Returns the click id, lowercased, or undefined
|
|
100
|
+
* for anything bat.js itself would not read back.
|
|
101
|
+
*/
|
|
102
|
+
function parseUetMsclkid(raw) {
|
|
103
|
+
if (!raw || !raw.startsWith(UET_MSCLKID_PREFIX)) return void 0;
|
|
104
|
+
const clickId = raw.slice(4);
|
|
105
|
+
return MSCLKID.test(clickId) ? clickId.toLowerCase() : void 0;
|
|
106
|
+
}
|
|
107
|
+
/** Build a `_uetmsclkid` value in bat.js's own format. */
|
|
108
|
+
function formatUetMsclkid(clickId) {
|
|
109
|
+
return `${UET_MSCLKID_PREFIX}${clickId.toLowerCase()}`;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* The click id as the Conversions API wants it: a dashed, lowercase UUID. The URL and the cookie
|
|
113
|
+
* carry the 32-hex form; a value in any other shape is returned untouched for the API to judge.
|
|
114
|
+
*/
|
|
115
|
+
function formatMsclkid(clickId) {
|
|
116
|
+
const id = clickId.trim().toLowerCase();
|
|
117
|
+
if (!MSCLKID.test(id)) return id;
|
|
118
|
+
return `${id.slice(0, 8)}-${id.slice(8, 12)}-${id.slice(12, 16)}-${id.slice(16, 20)}-${id.slice(20)}`;
|
|
119
|
+
}
|
|
47
120
|
function searchParams(url) {
|
|
48
121
|
try {
|
|
49
122
|
return new URL(url).searchParams;
|
|
@@ -101,12 +174,44 @@ function resolveClickIdCookies(input) {
|
|
|
101
174
|
if (refresh) set(FBC_COOKIE, existingFbc.raw, remainingMs);
|
|
102
175
|
}
|
|
103
176
|
} else if (jar["_fbc"]) del(FBC_COOKIE);
|
|
177
|
+
const gclsrc = params.get("gclsrc") || void 0;
|
|
178
|
+
const google = [{
|
|
179
|
+
cookie: GCL_AW_COOKIE,
|
|
180
|
+
urlClickId: !gclsrc || gclsrc === "aw.ds" ? params.get("gclid") || void 0 : void 0,
|
|
181
|
+
assign: (value) => result.gclid = value
|
|
182
|
+
}, {
|
|
183
|
+
cookie: GCL_GB_COOKIE,
|
|
184
|
+
urlClickId: params.get("wbraid") || void 0,
|
|
185
|
+
assign: (value) => result.wbraid = value
|
|
186
|
+
}];
|
|
187
|
+
for (const { cookie, urlClickId, assign } of google) {
|
|
188
|
+
const existing = parseGcl(jar[cookie], now);
|
|
189
|
+
if (urlClickId && GCL_CLICK_ID.test(urlClickId) && urlClickId !== existing?.clickId) {
|
|
190
|
+
set(cookie, formatGcl(urlClickId, now), GCL_TTL_MS);
|
|
191
|
+
assign(urlClickId);
|
|
192
|
+
} else if (existing) {
|
|
193
|
+
const remainingMs = existing.creationTime + GCL_TTL_MS - now;
|
|
194
|
+
if (remainingMs > 0) {
|
|
195
|
+
assign(existing.clickId);
|
|
196
|
+
if (refresh) set(cookie, existing.raw, remainingMs);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
104
200
|
const urlRdtCid = params.get("rdt_cid") || void 0;
|
|
105
201
|
const existingRdtCid = jar["_rdt_cid"] || void 0;
|
|
106
202
|
if (urlRdtCid && urlRdtCid !== existingRdtCid) {
|
|
107
203
|
set(RDT_CID_COOKIE, urlRdtCid, RDT_CID_TTL_MS);
|
|
108
204
|
result.rdt_cid = urlRdtCid;
|
|
109
205
|
} else if (existingRdtCid) result.rdt_cid = existingRdtCid;
|
|
206
|
+
const urlMsclkid = params.get("msclkid")?.trim();
|
|
207
|
+
const existingMsclkid = parseUetMsclkid(jar[UET_MSCLKID_COOKIE]);
|
|
208
|
+
if (urlMsclkid && MSCLKID.test(urlMsclkid) && urlMsclkid.toLowerCase() !== existingMsclkid) {
|
|
209
|
+
set(UET_MSCLKID_COOKIE, formatUetMsclkid(urlMsclkid), MSCLKID_TTL_MS);
|
|
210
|
+
result.msclkid = urlMsclkid.toLowerCase();
|
|
211
|
+
} else if (existingMsclkid) {
|
|
212
|
+
result.msclkid = existingMsclkid;
|
|
213
|
+
if (refresh) set(UET_MSCLKID_COOKIE, formatUetMsclkid(existingMsclkid), MSCLKID_TTL_MS);
|
|
214
|
+
}
|
|
110
215
|
return result;
|
|
111
216
|
}
|
|
112
217
|
/** Serialize the resolved cookies into `Set-Cookie` header values. */
|
|
@@ -114,6 +219,6 @@ function toSetCookieHeaders(cookies) {
|
|
|
114
219
|
return cookies.map((cookie) => stringifySetCookie(cookie));
|
|
115
220
|
}
|
|
116
221
|
//#endregion
|
|
117
|
-
export { FBC_COOKIE, RDT_CID_COOKIE, formatFbc, parseFbc, resolveClickIdCookies, toSetCookieHeaders };
|
|
222
|
+
export { FBC_COOKIE, GCL_AW_COOKIE, GCL_GB_COOKIE, RDT_CID_COOKIE, UET_MSCLKID_COOKIE, formatFbc, formatGcl, formatMsclkid, formatUetMsclkid, parseFbc, parseGcl, parseUetMsclkid, resolveClickIdCookies, toSetCookieHeaders };
|
|
118
223
|
|
|
119
224
|
//# sourceMappingURL=index.mjs.map
|