@hanzo/event 0.3.43 → 0.3.48
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/dist/consent.d.ts +36 -0
- package/dist/consent.d.ts.map +1 -0
- package/dist/cookie.d.ts +5 -0
- package/dist/cookie.d.ts.map +1 -0
- package/dist/events.d.ts +1 -1
- package/dist/events.d.ts.map +1 -1
- package/dist/index.cjs +508 -36
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +487 -38
- package/dist/index.mjs.map +1 -1
- package/dist/items.d.ts +22 -0
- package/dist/items.d.ts.map +1 -0
- package/dist/react.cjs +64 -12
- package/dist/react.cjs.map +1 -1
- package/dist/react.d.cts +8 -0
- package/dist/react.d.ts +8 -0
- package/dist/react.d.ts.map +1 -1
- package/dist/react.mjs +65 -14
- package/dist/react.mjs.map +1 -1
- package/dist/tags.d.ts +55 -0
- package/dist/tags.d.ts.map +1 -0
- package/dist/touch.d.ts +7 -0
- package/dist/touch.d.ts.map +1 -0
- package/package.json +2 -2
- package/src/consent.test.ts +140 -0
- package/src/consent.ts +116 -0
- package/src/cookie.ts +28 -0
- package/src/events.ts +1 -1
- package/src/index.ts +10 -0
- package/src/items.test.ts +22 -0
- package/src/items.ts +44 -0
- package/src/react.tsx +22 -0
- package/src/tags.test.ts +205 -0
- package/src/tags.ts +482 -0
- package/src/touch.ts +68 -0
package/src/touch.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// The ad click a visitor arrived on, kept first-party so a conversion days later
|
|
2
|
+
// is credited to it even when the ad tags never loaded.
|
|
3
|
+
//
|
|
4
|
+
// On every page load the click ids in the address are read: gclid, gbraid and
|
|
5
|
+
// wbraid (Google), fbclid (Meta), li_fat_id (LinkedIn), twclid (X), ttclid
|
|
6
|
+
// (TikTok). When any is there the whole set replaces the last one in `hz_touch`
|
|
7
|
+
// with the time it was captured: the last click is the one credited. It is kept
|
|
8
|
+
// 90 days, the lookback cloud applies before it offers a click to a platform
|
|
9
|
+
// (apps/destination/touch.go).
|
|
10
|
+
//
|
|
11
|
+
// Beside it are the browser ids the platforms match on. `_fbc` is Meta's click
|
|
12
|
+
// cookie, built from fbclid in the shape the Pixel would write it; `_fbp` is its
|
|
13
|
+
// browser id. `cid` is GA4's client id: the last two fields of `_ga`, or a
|
|
14
|
+
// first-party `hz_cid` of the same shape when gtag.js never ran.
|
|
15
|
+
//
|
|
16
|
+
// Nothing here runs without consent: the click and the Meta ids need Marketing,
|
|
17
|
+
// the client id needs Analytics.
|
|
18
|
+
|
|
19
|
+
import type { Choice } from './consent'
|
|
20
|
+
import { get, set } from './cookie'
|
|
21
|
+
|
|
22
|
+
export const CLICK_IDS = ['gclid', 'gbraid', 'wbraid', 'fbclid', 'li_fat_id', 'twclid', 'ttclid'] as const
|
|
23
|
+
|
|
24
|
+
const DAYS = 90
|
|
25
|
+
const now = () => Math.floor(Date.now() / 1000)
|
|
26
|
+
const rand = () => Math.floor(Math.random() * 2 ** 31)
|
|
27
|
+
|
|
28
|
+
/** Records this page's click and the browser ids. Call once per page load. */
|
|
29
|
+
export function capture(c: Choice): void {
|
|
30
|
+
if (typeof window === 'undefined') return
|
|
31
|
+
if (c.marketing) {
|
|
32
|
+
const q = new URLSearchParams(window.location.search)
|
|
33
|
+
const click: Record<string, string | number> = {}
|
|
34
|
+
for (const k of CLICK_IDS) {
|
|
35
|
+
const v = q.get(k)?.trim()
|
|
36
|
+
if (v) click[k] = v
|
|
37
|
+
}
|
|
38
|
+
if (Object.keys(click).length) {
|
|
39
|
+
click.clicked = now()
|
|
40
|
+
set('hz_touch', JSON.stringify(click), DAYS)
|
|
41
|
+
if (click.fbclid) set('_fbc', `fb.1.${Date.now()}.${click.fbclid}`, DAYS)
|
|
42
|
+
}
|
|
43
|
+
if (!get('_fbp')) set('_fbp', `fb.1.${Date.now()}.${rand()}`, DAYS)
|
|
44
|
+
}
|
|
45
|
+
if (c.analytics && !get('_ga') && !get('hz_cid')) set('hz_cid', `${rand()}.${now()}`, 730)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The properties an event carries for cloud to match it on. */
|
|
49
|
+
export function touch(c: Choice): Record<string, string | number> {
|
|
50
|
+
const out: Record<string, string | number> = {}
|
|
51
|
+
if (c.marketing) {
|
|
52
|
+
try {
|
|
53
|
+
Object.assign(out, JSON.parse(get('hz_touch') ?? '{}'))
|
|
54
|
+
} catch {
|
|
55
|
+
/* a cookie that does not parse names no click */
|
|
56
|
+
}
|
|
57
|
+
const fbc = get('_fbc')
|
|
58
|
+
const fbp = get('_fbp')
|
|
59
|
+
if (fbc) out.fbc = fbc
|
|
60
|
+
if (fbp) out.fbp = fbp
|
|
61
|
+
}
|
|
62
|
+
if (c.analytics) {
|
|
63
|
+
const ga = get('_ga')?.split('.')
|
|
64
|
+
const cid = ga && ga.length >= 4 ? ga.slice(-2).join('.') : get('hz_cid')
|
|
65
|
+
if (cid) out.cid = cid
|
|
66
|
+
}
|
|
67
|
+
return out
|
|
68
|
+
}
|