@shware/analytics 16.3.3 → 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.
@@ -6,8 +6,9 @@ let react = require("react");
6
6
  * The registrable domain (eTLD+1) of `hostname`, found the way gtag's `cookie_domain: 'auto'`
7
7
  * finds it: a cookie is tried on each suffix from the shortest up, and the first one the browser
8
8
  * accepts is the domain — browsers refuse public suffixes (`com`, `co.uk`), so no suffix list is
9
- * needed. A hostname without dots (`localhost`) or one no suffix accepts (an IP address) is its
10
- * own domain. `probe` tries one suffix and says whether the cookie stuck.
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.
11
12
  */
12
13
  function registrableDomain(hostname, probe = canSetCookieOn) {
13
14
  const labels = hostname.split(".");
@@ -18,12 +19,19 @@ function registrableDomain(hostname, probe = canSetCookieOn) {
18
19
  }
19
20
  return hostname;
20
21
  }
21
- const PROBE = "_sa_d";
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
+ */
22
28
  function canSetCookieOn(domain) {
29
+ const name = "_sa_d";
30
+ const value = Math.random().toString(36).slice(2);
23
31
  try {
24
- document.cookie = `${PROBE}=1; domain=${domain}; path=/; SameSite=Lax`;
25
- const stuck = document.cookie.split("; ").includes(`${PROBE}=1`);
26
- if (stuck) document.cookie = `${PROBE}=; domain=${domain}; path=/; max-age=0; SameSite=Lax`;
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`;
27
35
  return stuck;
28
36
  } catch {
29
37
  return false;
@@ -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 * 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. A hostname without dots (`localhost`) or one no suffix accepts (an IP address) is its\n * own domain. `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\nconst PROBE = '_sa_d';\n\nfunction canSetCookieOn(domain: string): boolean {\n try {\n document.cookie = `${PROBE}=1; domain=${domain}; path=/; SameSite=Lax`;\n const stuck = document.cookie.split('; ').includes(`${PROBE}=1`);\n if (stuck) document.cookie = `${PROBE}=; 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":";;;;;;;;;;;AAUA,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;AAEA,MAAM,QAAQ;AAEd,SAAS,eAAe,QAAyB;CAC/C,IAAI;EACF,SAAS,SAAS,GAAG,MAAM,aAAa,OAAO;EAC/C,MAAM,QAAQ,SAAS,OAAO,MAAM,IAAI,CAAC,CAAC,SAAS,GAAG,MAAM,GAAG;EAC/D,IAAI,OAAO,SAAS,SAAS,GAAG,MAAM,YAAY,OAAO;EACzD,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
+ {"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"}
@@ -2,8 +2,9 @@
2
2
  * The registrable domain (eTLD+1) of `hostname`, found the way gtag's `cookie_domain: 'auto'`
3
3
  * finds it: a cookie is tried on each suffix from the shortest up, and the first one the browser
4
4
  * accepts is the domain — browsers refuse public suffixes (`com`, `co.uk`), so no suffix list is
5
- * needed. A hostname without dots (`localhost`) or one no suffix accepts (an IP address) is its
6
- * own domain. `probe` tries one suffix and says whether the cookie stuck.
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.
7
8
  */
8
9
  export declare function registrableDomain(hostname: string, probe?: (domain: string) => boolean): string;
9
10
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"use-outbound-click-analytics.d.cts","names":[],"sources":["../../src/hooks/use-outbound-click-analytics.ts"],"mappings":";;;;;;;wBAUgB,kBACd,kBACA,SAAQ;;;;;;;wBA8BM,eAAe,KAAK,KAAK;;;;;wBAUzB"}
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"}
@@ -2,8 +2,9 @@
2
2
  * The registrable domain (eTLD+1) of `hostname`, found the way gtag's `cookie_domain: 'auto'`
3
3
  * finds it: a cookie is tried on each suffix from the shortest up, and the first one the browser
4
4
  * accepts is the domain — browsers refuse public suffixes (`com`, `co.uk`), so no suffix list is
5
- * needed. A hostname without dots (`localhost`) or one no suffix accepts (an IP address) is its
6
- * own domain. `probe` tries one suffix and says whether the cookie stuck.
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.
7
8
  */
8
9
  export declare function registrableDomain(hostname: string, probe?: (domain: string) => boolean): string;
9
10
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"use-outbound-click-analytics.d.mts","names":[],"sources":["../../src/hooks/use-outbound-click-analytics.ts"],"mappings":";;;;;;;wBAUgB,kBACd,kBACA,SAAQ;;;;;;;wBA8BM,eAAe,KAAK,KAAK;;;;;wBAUzB"}
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"}
@@ -5,8 +5,9 @@ import { useEffect } from "react";
5
5
  * The registrable domain (eTLD+1) of `hostname`, found the way gtag's `cookie_domain: 'auto'`
6
6
  * finds it: a cookie is tried on each suffix from the shortest up, and the first one the browser
7
7
  * accepts is the domain — browsers refuse public suffixes (`com`, `co.uk`), so no suffix list is
8
- * needed. A hostname without dots (`localhost`) or one no suffix accepts (an IP address) is its
9
- * own domain. `probe` tries one suffix and says whether the cookie stuck.
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.
10
11
  */
11
12
  function registrableDomain(hostname, probe = canSetCookieOn) {
12
13
  const labels = hostname.split(".");
@@ -17,12 +18,19 @@ function registrableDomain(hostname, probe = canSetCookieOn) {
17
18
  }
18
19
  return hostname;
19
20
  }
20
- const PROBE = "_sa_d";
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
+ */
21
27
  function canSetCookieOn(domain) {
28
+ const name = "_sa_d";
29
+ const value = Math.random().toString(36).slice(2);
22
30
  try {
23
- document.cookie = `${PROBE}=1; domain=${domain}; path=/; SameSite=Lax`;
24
- const stuck = document.cookie.split("; ").includes(`${PROBE}=1`);
25
- if (stuck) document.cookie = `${PROBE}=; domain=${domain}; path=/; max-age=0; SameSite=Lax`;
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`;
26
34
  return stuck;
27
35
  } catch {
28
36
  return false;
@@ -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 * 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. A hostname without dots (`localhost`) or one no suffix accepts (an IP address) is its\n * own domain. `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\nconst PROBE = '_sa_d';\n\nfunction canSetCookieOn(domain: string): boolean {\n try {\n document.cookie = `${PROBE}=1; domain=${domain}; path=/; SameSite=Lax`;\n const stuck = document.cookie.split('; ').includes(`${PROBE}=1`);\n if (stuck) document.cookie = `${PROBE}=; 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":";;;;;;;;;;AAUA,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;AAEA,MAAM,QAAQ;AAEd,SAAS,eAAe,QAAyB;CAC/C,IAAI;EACF,SAAS,SAAS,GAAG,MAAM,aAAa,OAAO;EAC/C,MAAM,QAAQ,SAAS,OAAO,MAAM,IAAI,CAAC,CAAC,SAAS,GAAG,MAAM,GAAG;EAC/D,IAAI,OAAO,SAAS,SAAS,GAAG,MAAM,YAAY,OAAO;EACzD,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"}
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",
3
+ "version": "16.3.4",
4
4
  "private": false,
5
5
  "repository": {
6
6
  "type": "git",