@shware/analytics 16.3.2 → 16.3.4
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/hooks/use-outbound-click-analytics.cjs +53 -3
- package/dist/hooks/use-outbound-click-analytics.cjs.map +1 -1
- package/dist/hooks/use-outbound-click-analytics.d.cts +18 -2
- package/dist/hooks/use-outbound-click-analytics.d.cts.map +1 -1
- package/dist/hooks/use-outbound-click-analytics.d.mts +18 -2
- package/dist/hooks/use-outbound-click-analytics.d.mts.map +1 -1
- package/dist/hooks/use-outbound-click-analytics.mjs +52 -4
- package/dist/hooks/use-outbound-click-analytics.mjs.map +1 -1
- package/package.json +2 -2
|
@@ -3,17 +3,65 @@ const require_track_index = require("../track/index.cjs");
|
|
|
3
3
|
let react = require("react");
|
|
4
4
|
// src/hooks/use-outbound-click-analytics.ts
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
* from the
|
|
6
|
+
* The registrable domain (eTLD+1) of `hostname`, found the way gtag's `cookie_domain: 'auto'`
|
|
7
|
+
* finds it: a cookie is tried on each suffix from the shortest up, and the first one the browser
|
|
8
|
+
* accepts is the domain — browsers refuse public suffixes (`com`, `co.uk`), so no suffix list is
|
|
9
|
+
* needed (gtag's `cookie_domain: 'auto'` walks the same list). A hostname without dots
|
|
10
|
+
* (`localhost`) or one no suffix accepts (an IP address) is its own domain, as a host-only cookie
|
|
11
|
+
* would be. `probe` tries one suffix and says whether the cookie stuck.
|
|
12
|
+
*/
|
|
13
|
+
function registrableDomain(hostname, probe = canSetCookieOn) {
|
|
14
|
+
const labels = hostname.split(".");
|
|
15
|
+
if (labels.length < 2 || /^[\d.]+$/.test(hostname)) return hostname;
|
|
16
|
+
for (let i = labels.length - 2; i >= 0; i -= 1) {
|
|
17
|
+
const candidate = labels.slice(i).join(".");
|
|
18
|
+
if (probe(candidate)) return candidate;
|
|
19
|
+
}
|
|
20
|
+
return hostname;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Whether the browser accepts a cookie for `domain`, read back by a value made for this one probe:
|
|
24
|
+
* a cookie of the same name the site already holds can then neither pass the check for a refused
|
|
25
|
+
* suffix nor hide the probe (gtag writes its real cookie this way and keeps it; this one is a
|
|
26
|
+
* throwaway, deleted at once).
|
|
27
|
+
*/
|
|
28
|
+
function canSetCookieOn(domain) {
|
|
29
|
+
const name = "_sa_d";
|
|
30
|
+
const value = Math.random().toString(36).slice(2);
|
|
31
|
+
try {
|
|
32
|
+
document.cookie = `${name}=${value}; domain=${domain}; path=/; SameSite=Lax`;
|
|
33
|
+
const stuck = document.cookie.split("; ").includes(`${name}=${value}`);
|
|
34
|
+
if (stuck) document.cookie = `${name}=; domain=${domain}; path=/; max-age=0; SameSite=Lax`;
|
|
35
|
+
return stuck;
|
|
36
|
+
} catch {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Whether a link leads away from the site, by GA4's definition of an outbound click: an http(s)
|
|
42
|
+
* link to a host that is not the site's registrable domain or a subdomain of it. `mailto:`,
|
|
43
|
+
* `tel:` and the like are not outbound clicks (GA4 never counts them), and `www.` → `app.` is
|
|
44
|
+
* not leaving the site.
|
|
45
|
+
*/
|
|
46
|
+
function isOutboundLink(url, siteDomain) {
|
|
47
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") return false;
|
|
48
|
+
const host = url.hostname;
|
|
49
|
+
return host !== siteDomain && !host.endsWith(`.${siteDomain}`);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Tracks outbound link clicks - when a user clicks a link that leads away from the site to
|
|
53
|
+
* another website (see `isOutboundLink`).
|
|
8
54
|
*/
|
|
9
55
|
function useOutboundClickAnalytics() {
|
|
10
56
|
(0, react.useEffect)(() => {
|
|
57
|
+
const siteDomain = registrableDomain(window.location.hostname);
|
|
11
58
|
const onClick = (event) => {
|
|
12
59
|
const anchor = event.target.closest("a");
|
|
13
60
|
if (!anchor?.href) return;
|
|
14
61
|
try {
|
|
15
62
|
const url = new URL(anchor.href, window.location.origin);
|
|
16
|
-
if (url
|
|
63
|
+
if (!isOutboundLink(url, siteDomain)) return;
|
|
64
|
+
require_track_index.track("click", {
|
|
17
65
|
outbound: true,
|
|
18
66
|
link_id: anchor.id || "",
|
|
19
67
|
link_url: anchor.href,
|
|
@@ -30,6 +78,8 @@ function useOutboundClickAnalytics() {
|
|
|
30
78
|
return () => document.removeEventListener("click", onClick, { capture: true });
|
|
31
79
|
}, []);
|
|
32
80
|
}
|
|
81
|
+
exports.isOutboundLink = isOutboundLink;
|
|
82
|
+
exports.registrableDomain = registrableDomain;
|
|
33
83
|
exports.useOutboundClickAnalytics = useOutboundClickAnalytics;
|
|
34
84
|
|
|
35
85
|
//# sourceMappingURL=use-outbound-click-analytics.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-outbound-click-analytics.cjs","names":[],"sources":["../../src/hooks/use-outbound-click-analytics.ts"],"sourcesContent":["import { useEffect } from 'react';\nimport { track } from '../track/index';\n\n/**\n * Tracks outbound link clicks - when a user clicks a link that leads away
|
|
1
|
+
{"version":3,"file":"use-outbound-click-analytics.cjs","names":[],"sources":["../../src/hooks/use-outbound-click-analytics.ts"],"sourcesContent":["import { useEffect } from 'react';\nimport { track } from '../track/index';\n\n/**\n * The registrable domain (eTLD+1) of `hostname`, found the way gtag's `cookie_domain: 'auto'`\n * finds it: a cookie is tried on each suffix from the shortest up, and the first one the browser\n * accepts is the domain — browsers refuse public suffixes (`com`, `co.uk`), so no suffix list is\n * needed (gtag's `cookie_domain: 'auto'` walks the same list). A hostname without dots\n * (`localhost`) or one no suffix accepts (an IP address) is its own domain, as a host-only cookie\n * would be. `probe` tries one suffix and says whether the cookie stuck.\n */\nexport function registrableDomain(\n hostname: string,\n probe: (domain: string) => boolean = canSetCookieOn\n): string {\n const labels = hostname.split('.');\n if (labels.length < 2 || /^[\\d.]+$/.test(hostname)) return hostname;\n for (let i = labels.length - 2; i >= 0; i -= 1) {\n const candidate = labels.slice(i).join('.');\n if (probe(candidate)) return candidate;\n }\n return hostname;\n}\n\n/**\n * Whether the browser accepts a cookie for `domain`, read back by a value made for this one probe:\n * a cookie of the same name the site already holds can then neither pass the check for a refused\n * suffix nor hide the probe (gtag writes its real cookie this way and keeps it; this one is a\n * throwaway, deleted at once).\n */\nfunction canSetCookieOn(domain: string): boolean {\n const name = '_sa_d';\n const value = Math.random().toString(36).slice(2);\n try {\n document.cookie = `${name}=${value}; domain=${domain}; path=/; SameSite=Lax`;\n const stuck = document.cookie.split('; ').includes(`${name}=${value}`);\n if (stuck) document.cookie = `${name}=; domain=${domain}; path=/; max-age=0; SameSite=Lax`;\n return stuck;\n } catch {\n return false;\n }\n}\n\n/**\n * Whether a link leads away from the site, by GA4's definition of an outbound click: an http(s)\n * link to a host that is not the site's registrable domain or a subdomain of it. `mailto:`,\n * `tel:` and the like are not outbound clicks (GA4 never counts them), and `www.` → `app.` is\n * not leaving the site.\n */\nexport function isOutboundLink(url: URL, siteDomain: string): boolean {\n if (url.protocol !== 'http:' && url.protocol !== 'https:') return false;\n const host = url.hostname;\n return host !== siteDomain && !host.endsWith(`.${siteDomain}`);\n}\n\n/**\n * Tracks outbound link clicks - when a user clicks a link that leads away from the site to\n * another website (see `isOutboundLink`).\n */\nexport function useOutboundClickAnalytics() {\n useEffect(() => {\n const siteDomain = registrableDomain(window.location.hostname);\n const onClick = (event: MouseEvent) => {\n // Find the closest anchor element from the clicked target\n const target = event.target as HTMLElement;\n const anchor = target.closest('a');\n if (!anchor?.href) return;\n\n try {\n const url = new URL(anchor.href, window.location.origin);\n if (!isOutboundLink(url, siteDomain)) return;\n track('click', {\n outbound: true,\n link_id: anchor.id || '',\n link_url: anchor.href,\n // An anchor can wrap a whole card, so its text runs to kilobytes of markup content\n // that no report ever looks at. GA4 keeps 100 characters of a text event parameter;\n // 512 leaves room for the whole label of a long button while staying well under the\n // transport limit.\n link_text: anchor.textContent.trim().slice(0, 512),\n link_domain: url.hostname,\n link_classes: anchor.className || '',\n });\n } catch {\n // Invalid URL, ignore\n }\n };\n\n document.addEventListener('click', onClick, { passive: true, capture: true });\n return () => document.removeEventListener('click', onClick, { capture: true });\n }, []);\n}\n"],"mappings":";;;;;;;;;;;;AAWA,SAAgB,kBACd,UACA,QAAqC,gBAC7B;CACR,MAAM,SAAS,SAAS,MAAM,GAAG;CACjC,IAAI,OAAO,SAAS,KAAK,WAAW,KAAK,QAAQ,GAAG,OAAO;CAC3D,KAAK,IAAI,IAAI,OAAO,SAAS,GAAG,KAAK,GAAG,KAAK,GAAG;EAC9C,MAAM,YAAY,OAAO,MAAM,CAAC,CAAC,CAAC,KAAK,GAAG;EAC1C,IAAI,MAAM,SAAS,GAAG,OAAO;CAC/B;CACA,OAAO;AACT;;;;;;;AAQA,SAAS,eAAe,QAAyB;CAC/C,MAAM,OAAO;CACb,MAAM,QAAQ,KAAK,OAAO,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,MAAM,CAAC;CAChD,IAAI;EACF,SAAS,SAAS,GAAG,KAAK,GAAG,MAAM,WAAW,OAAO;EACrD,MAAM,QAAQ,SAAS,OAAO,MAAM,IAAI,CAAC,CAAC,SAAS,GAAG,KAAK,GAAG,OAAO;EACrE,IAAI,OAAO,SAAS,SAAS,GAAG,KAAK,YAAY,OAAO;EACxD,OAAO;CACT,QAAQ;EACN,OAAO;CACT;AACF;;;;;;;AAQA,SAAgB,eAAe,KAAU,YAA6B;CACpE,IAAI,IAAI,aAAa,WAAW,IAAI,aAAa,UAAU,OAAO;CAClE,MAAM,OAAO,IAAI;CACjB,OAAO,SAAS,cAAc,CAAC,KAAK,SAAS,IAAI,YAAY;AAC/D;;;;;AAMA,SAAgB,4BAA4B;CAC1C,CAAA,GAAA,MAAA,UAAA,OAAgB;EACd,MAAM,aAAa,kBAAkB,OAAO,SAAS,QAAQ;EAC7D,MAAM,WAAW,UAAsB;GAGrC,MAAM,SADS,MAAM,OACC,QAAQ,GAAG;GACjC,IAAI,CAAC,QAAQ,MAAM;GAEnB,IAAI;IACF,MAAM,MAAM,IAAI,IAAI,OAAO,MAAM,OAAO,SAAS,MAAM;IACvD,IAAI,CAAC,eAAe,KAAK,UAAU,GAAG;IACtC,oBAAA,MAAM,SAAS;KACb,UAAU;KACV,SAAS,OAAO,MAAM;KACtB,UAAU,OAAO;KAKjB,WAAW,OAAO,YAAY,KAAK,CAAC,CAAC,MAAM,GAAG,GAAG;KACjD,aAAa,IAAI;KACjB,cAAc,OAAO,aAAa;IACpC,CAAC;GACH,QAAQ,CAER;EACF;EAEA,SAAS,iBAAiB,SAAS,SAAS;GAAE,SAAS;GAAM,SAAS;EAAK,CAAC;EAC5E,aAAa,SAAS,oBAAoB,SAAS,SAAS,EAAE,SAAS,KAAK,CAAC;CAC/E,GAAG,CAAC,CAAC;AACP"}
|
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* from the
|
|
2
|
+
* The registrable domain (eTLD+1) of `hostname`, found the way gtag's `cookie_domain: 'auto'`
|
|
3
|
+
* finds it: a cookie is tried on each suffix from the shortest up, and the first one the browser
|
|
4
|
+
* accepts is the domain — browsers refuse public suffixes (`com`, `co.uk`), so no suffix list is
|
|
5
|
+
* needed (gtag's `cookie_domain: 'auto'` walks the same list). A hostname without dots
|
|
6
|
+
* (`localhost`) or one no suffix accepts (an IP address) is its own domain, as a host-only cookie
|
|
7
|
+
* would be. `probe` tries one suffix and says whether the cookie stuck.
|
|
8
|
+
*/
|
|
9
|
+
export declare function registrableDomain(hostname: string, probe?: (domain: string) => boolean): string;
|
|
10
|
+
/**
|
|
11
|
+
* Whether a link leads away from the site, by GA4's definition of an outbound click: an http(s)
|
|
12
|
+
* link to a host that is not the site's registrable domain or a subdomain of it. `mailto:`,
|
|
13
|
+
* `tel:` and the like are not outbound clicks (GA4 never counts them), and `www.` → `app.` is
|
|
14
|
+
* not leaving the site.
|
|
15
|
+
*/
|
|
16
|
+
export declare function isOutboundLink(url: URL, siteDomain: string): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Tracks outbound link clicks - when a user clicks a link that leads away from the site to
|
|
19
|
+
* another website (see `isOutboundLink`).
|
|
4
20
|
*/
|
|
5
21
|
export declare function useOutboundClickAnalytics(): void;
|
|
6
22
|
//# sourceMappingURL=use-outbound-click-analytics.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-outbound-click-analytics.d.cts","names":[],"sources":["../../src/hooks/use-outbound-click-analytics.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"use-outbound-click-analytics.d.cts","names":[],"sources":["../../src/hooks/use-outbound-click-analytics.ts"],"mappings":";;;;;;;;wBAWgB,kBACd,kBACA,SAAQ;;;;;;;wBAoCM,eAAe,KAAK,KAAK;;;;;wBAUzB"}
|
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* from the
|
|
2
|
+
* The registrable domain (eTLD+1) of `hostname`, found the way gtag's `cookie_domain: 'auto'`
|
|
3
|
+
* finds it: a cookie is tried on each suffix from the shortest up, and the first one the browser
|
|
4
|
+
* accepts is the domain — browsers refuse public suffixes (`com`, `co.uk`), so no suffix list is
|
|
5
|
+
* needed (gtag's `cookie_domain: 'auto'` walks the same list). A hostname without dots
|
|
6
|
+
* (`localhost`) or one no suffix accepts (an IP address) is its own domain, as a host-only cookie
|
|
7
|
+
* would be. `probe` tries one suffix and says whether the cookie stuck.
|
|
8
|
+
*/
|
|
9
|
+
export declare function registrableDomain(hostname: string, probe?: (domain: string) => boolean): string;
|
|
10
|
+
/**
|
|
11
|
+
* Whether a link leads away from the site, by GA4's definition of an outbound click: an http(s)
|
|
12
|
+
* link to a host that is not the site's registrable domain or a subdomain of it. `mailto:`,
|
|
13
|
+
* `tel:` and the like are not outbound clicks (GA4 never counts them), and `www.` → `app.` is
|
|
14
|
+
* not leaving the site.
|
|
15
|
+
*/
|
|
16
|
+
export declare function isOutboundLink(url: URL, siteDomain: string): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Tracks outbound link clicks - when a user clicks a link that leads away from the site to
|
|
19
|
+
* another website (see `isOutboundLink`).
|
|
4
20
|
*/
|
|
5
21
|
export declare function useOutboundClickAnalytics(): void;
|
|
6
22
|
//# sourceMappingURL=use-outbound-click-analytics.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-outbound-click-analytics.d.mts","names":[],"sources":["../../src/hooks/use-outbound-click-analytics.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"use-outbound-click-analytics.d.mts","names":[],"sources":["../../src/hooks/use-outbound-click-analytics.ts"],"mappings":";;;;;;;;wBAWgB,kBACd,kBACA,SAAQ;;;;;;;wBAoCM,eAAe,KAAK,KAAK;;;;;wBAUzB"}
|
|
@@ -2,17 +2,65 @@ import { track } from "../track/index.mjs";
|
|
|
2
2
|
import { useEffect } from "react";
|
|
3
3
|
// src/hooks/use-outbound-click-analytics.ts
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
* from the
|
|
5
|
+
* The registrable domain (eTLD+1) of `hostname`, found the way gtag's `cookie_domain: 'auto'`
|
|
6
|
+
* finds it: a cookie is tried on each suffix from the shortest up, and the first one the browser
|
|
7
|
+
* accepts is the domain — browsers refuse public suffixes (`com`, `co.uk`), so no suffix list is
|
|
8
|
+
* needed (gtag's `cookie_domain: 'auto'` walks the same list). A hostname without dots
|
|
9
|
+
* (`localhost`) or one no suffix accepts (an IP address) is its own domain, as a host-only cookie
|
|
10
|
+
* would be. `probe` tries one suffix and says whether the cookie stuck.
|
|
11
|
+
*/
|
|
12
|
+
function registrableDomain(hostname, probe = canSetCookieOn) {
|
|
13
|
+
const labels = hostname.split(".");
|
|
14
|
+
if (labels.length < 2 || /^[\d.]+$/.test(hostname)) return hostname;
|
|
15
|
+
for (let i = labels.length - 2; i >= 0; i -= 1) {
|
|
16
|
+
const candidate = labels.slice(i).join(".");
|
|
17
|
+
if (probe(candidate)) return candidate;
|
|
18
|
+
}
|
|
19
|
+
return hostname;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Whether the browser accepts a cookie for `domain`, read back by a value made for this one probe:
|
|
23
|
+
* a cookie of the same name the site already holds can then neither pass the check for a refused
|
|
24
|
+
* suffix nor hide the probe (gtag writes its real cookie this way and keeps it; this one is a
|
|
25
|
+
* throwaway, deleted at once).
|
|
26
|
+
*/
|
|
27
|
+
function canSetCookieOn(domain) {
|
|
28
|
+
const name = "_sa_d";
|
|
29
|
+
const value = Math.random().toString(36).slice(2);
|
|
30
|
+
try {
|
|
31
|
+
document.cookie = `${name}=${value}; domain=${domain}; path=/; SameSite=Lax`;
|
|
32
|
+
const stuck = document.cookie.split("; ").includes(`${name}=${value}`);
|
|
33
|
+
if (stuck) document.cookie = `${name}=; domain=${domain}; path=/; max-age=0; SameSite=Lax`;
|
|
34
|
+
return stuck;
|
|
35
|
+
} catch {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Whether a link leads away from the site, by GA4's definition of an outbound click: an http(s)
|
|
41
|
+
* link to a host that is not the site's registrable domain or a subdomain of it. `mailto:`,
|
|
42
|
+
* `tel:` and the like are not outbound clicks (GA4 never counts them), and `www.` → `app.` is
|
|
43
|
+
* not leaving the site.
|
|
44
|
+
*/
|
|
45
|
+
function isOutboundLink(url, siteDomain) {
|
|
46
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") return false;
|
|
47
|
+
const host = url.hostname;
|
|
48
|
+
return host !== siteDomain && !host.endsWith(`.${siteDomain}`);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Tracks outbound link clicks - when a user clicks a link that leads away from the site to
|
|
52
|
+
* another website (see `isOutboundLink`).
|
|
7
53
|
*/
|
|
8
54
|
function useOutboundClickAnalytics() {
|
|
9
55
|
useEffect(() => {
|
|
56
|
+
const siteDomain = registrableDomain(window.location.hostname);
|
|
10
57
|
const onClick = (event) => {
|
|
11
58
|
const anchor = event.target.closest("a");
|
|
12
59
|
if (!anchor?.href) return;
|
|
13
60
|
try {
|
|
14
61
|
const url = new URL(anchor.href, window.location.origin);
|
|
15
|
-
if (url
|
|
62
|
+
if (!isOutboundLink(url, siteDomain)) return;
|
|
63
|
+
track("click", {
|
|
16
64
|
outbound: true,
|
|
17
65
|
link_id: anchor.id || "",
|
|
18
66
|
link_url: anchor.href,
|
|
@@ -29,6 +77,6 @@ function useOutboundClickAnalytics() {
|
|
|
29
77
|
return () => document.removeEventListener("click", onClick, { capture: true });
|
|
30
78
|
}, []);
|
|
31
79
|
}
|
|
32
|
-
export { useOutboundClickAnalytics };
|
|
80
|
+
export { isOutboundLink, registrableDomain, useOutboundClickAnalytics };
|
|
33
81
|
|
|
34
82
|
//# sourceMappingURL=use-outbound-click-analytics.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-outbound-click-analytics.mjs","names":[],"sources":["../../src/hooks/use-outbound-click-analytics.ts"],"sourcesContent":["import { useEffect } from 'react';\nimport { track } from '../track/index';\n\n/**\n * Tracks outbound link clicks - when a user clicks a link that leads away
|
|
1
|
+
{"version":3,"file":"use-outbound-click-analytics.mjs","names":[],"sources":["../../src/hooks/use-outbound-click-analytics.ts"],"sourcesContent":["import { useEffect } from 'react';\nimport { track } from '../track/index';\n\n/**\n * The registrable domain (eTLD+1) of `hostname`, found the way gtag's `cookie_domain: 'auto'`\n * finds it: a cookie is tried on each suffix from the shortest up, and the first one the browser\n * accepts is the domain — browsers refuse public suffixes (`com`, `co.uk`), so no suffix list is\n * needed (gtag's `cookie_domain: 'auto'` walks the same list). A hostname without dots\n * (`localhost`) or one no suffix accepts (an IP address) is its own domain, as a host-only cookie\n * would be. `probe` tries one suffix and says whether the cookie stuck.\n */\nexport function registrableDomain(\n hostname: string,\n probe: (domain: string) => boolean = canSetCookieOn\n): string {\n const labels = hostname.split('.');\n if (labels.length < 2 || /^[\\d.]+$/.test(hostname)) return hostname;\n for (let i = labels.length - 2; i >= 0; i -= 1) {\n const candidate = labels.slice(i).join('.');\n if (probe(candidate)) return candidate;\n }\n return hostname;\n}\n\n/**\n * Whether the browser accepts a cookie for `domain`, read back by a value made for this one probe:\n * a cookie of the same name the site already holds can then neither pass the check for a refused\n * suffix nor hide the probe (gtag writes its real cookie this way and keeps it; this one is a\n * throwaway, deleted at once).\n */\nfunction canSetCookieOn(domain: string): boolean {\n const name = '_sa_d';\n const value = Math.random().toString(36).slice(2);\n try {\n document.cookie = `${name}=${value}; domain=${domain}; path=/; SameSite=Lax`;\n const stuck = document.cookie.split('; ').includes(`${name}=${value}`);\n if (stuck) document.cookie = `${name}=; domain=${domain}; path=/; max-age=0; SameSite=Lax`;\n return stuck;\n } catch {\n return false;\n }\n}\n\n/**\n * Whether a link leads away from the site, by GA4's definition of an outbound click: an http(s)\n * link to a host that is not the site's registrable domain or a subdomain of it. `mailto:`,\n * `tel:` and the like are not outbound clicks (GA4 never counts them), and `www.` → `app.` is\n * not leaving the site.\n */\nexport function isOutboundLink(url: URL, siteDomain: string): boolean {\n if (url.protocol !== 'http:' && url.protocol !== 'https:') return false;\n const host = url.hostname;\n return host !== siteDomain && !host.endsWith(`.${siteDomain}`);\n}\n\n/**\n * Tracks outbound link clicks - when a user clicks a link that leads away from the site to\n * another website (see `isOutboundLink`).\n */\nexport function useOutboundClickAnalytics() {\n useEffect(() => {\n const siteDomain = registrableDomain(window.location.hostname);\n const onClick = (event: MouseEvent) => {\n // Find the closest anchor element from the clicked target\n const target = event.target as HTMLElement;\n const anchor = target.closest('a');\n if (!anchor?.href) return;\n\n try {\n const url = new URL(anchor.href, window.location.origin);\n if (!isOutboundLink(url, siteDomain)) return;\n track('click', {\n outbound: true,\n link_id: anchor.id || '',\n link_url: anchor.href,\n // An anchor can wrap a whole card, so its text runs to kilobytes of markup content\n // that no report ever looks at. GA4 keeps 100 characters of a text event parameter;\n // 512 leaves room for the whole label of a long button while staying well under the\n // transport limit.\n link_text: anchor.textContent.trim().slice(0, 512),\n link_domain: url.hostname,\n link_classes: anchor.className || '',\n });\n } catch {\n // Invalid URL, ignore\n }\n };\n\n document.addEventListener('click', onClick, { passive: true, capture: true });\n return () => document.removeEventListener('click', onClick, { capture: true });\n }, []);\n}\n"],"mappings":";;;;;;;;;;;AAWA,SAAgB,kBACd,UACA,QAAqC,gBAC7B;CACR,MAAM,SAAS,SAAS,MAAM,GAAG;CACjC,IAAI,OAAO,SAAS,KAAK,WAAW,KAAK,QAAQ,GAAG,OAAO;CAC3D,KAAK,IAAI,IAAI,OAAO,SAAS,GAAG,KAAK,GAAG,KAAK,GAAG;EAC9C,MAAM,YAAY,OAAO,MAAM,CAAC,CAAC,CAAC,KAAK,GAAG;EAC1C,IAAI,MAAM,SAAS,GAAG,OAAO;CAC/B;CACA,OAAO;AACT;;;;;;;AAQA,SAAS,eAAe,QAAyB;CAC/C,MAAM,OAAO;CACb,MAAM,QAAQ,KAAK,OAAO,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,MAAM,CAAC;CAChD,IAAI;EACF,SAAS,SAAS,GAAG,KAAK,GAAG,MAAM,WAAW,OAAO;EACrD,MAAM,QAAQ,SAAS,OAAO,MAAM,IAAI,CAAC,CAAC,SAAS,GAAG,KAAK,GAAG,OAAO;EACrE,IAAI,OAAO,SAAS,SAAS,GAAG,KAAK,YAAY,OAAO;EACxD,OAAO;CACT,QAAQ;EACN,OAAO;CACT;AACF;;;;;;;AAQA,SAAgB,eAAe,KAAU,YAA6B;CACpE,IAAI,IAAI,aAAa,WAAW,IAAI,aAAa,UAAU,OAAO;CAClE,MAAM,OAAO,IAAI;CACjB,OAAO,SAAS,cAAc,CAAC,KAAK,SAAS,IAAI,YAAY;AAC/D;;;;;AAMA,SAAgB,4BAA4B;CAC1C,gBAAgB;EACd,MAAM,aAAa,kBAAkB,OAAO,SAAS,QAAQ;EAC7D,MAAM,WAAW,UAAsB;GAGrC,MAAM,SADS,MAAM,OACC,QAAQ,GAAG;GACjC,IAAI,CAAC,QAAQ,MAAM;GAEnB,IAAI;IACF,MAAM,MAAM,IAAI,IAAI,OAAO,MAAM,OAAO,SAAS,MAAM;IACvD,IAAI,CAAC,eAAe,KAAK,UAAU,GAAG;IACtC,MAAM,SAAS;KACb,UAAU;KACV,SAAS,OAAO,MAAM;KACtB,UAAU,OAAO;KAKjB,WAAW,OAAO,YAAY,KAAK,CAAC,CAAC,MAAM,GAAG,GAAG;KACjD,aAAa,IAAI;KACjB,cAAc,OAAO,aAAa;IACpC,CAAC;GACH,QAAQ,CAER;EACF;EAEA,SAAS,iBAAiB,SAAS,SAAS;GAAE,SAAS;GAAM,SAAS;EAAK,CAAC;EAC5E,aAAa,SAAS,oBAAoB,SAAS,SAAS,EAAE,SAAS,KAAK,CAAC;CAC/E,GAAG,CAAC,CAAC;AACP"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shware/analytics",
|
|
3
|
-
"version": "16.3.
|
|
3
|
+
"version": "16.3.4",
|
|
4
4
|
"private": false,
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -137,7 +137,7 @@
|
|
|
137
137
|
},
|
|
138
138
|
"dependencies": {
|
|
139
139
|
"@posthog/types": "^1.419.0",
|
|
140
|
-
"@shware/utils": "^2.0.
|
|
140
|
+
"@shware/utils": "^2.0.3",
|
|
141
141
|
"capi-param-builder-nodejs": "~1.3.2",
|
|
142
142
|
"cookie": "^2.0.1",
|
|
143
143
|
"uuid": "^14.0.3",
|