@temps-sdk/analytics-core 0.0.5 → 0.0.6

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.
@@ -4,7 +4,7 @@ import { record } from "rrweb";
4
4
  import { pack } from "@rrweb/packer";
5
5
  import { SESSION_RECORDER_ENDPOINT, DEFAULT_BASE_PATH, DEFAULT_EXCLUDED_PATHS } from "./constants";
6
6
  import { getOrCreateVisitorId } from "./identity";
7
- import { ingestKeyHeaders, withIngestKey } from "./utils";
7
+ import { beaconBlob, ingestKeyHeaders, withIngestKey } from "./utils";
8
8
  /** Interaction that counts as the visitor still being present. */
9
9
  const ACTIVITY_EVENTS = [
10
10
  "pointerdown",
@@ -355,7 +355,7 @@ export class SessionRecorder {
355
355
  // sendBeacon reports queueing, not delivery, so there is no ack to wait
356
356
  // for: release the batch and accept the small risk of loss on unload.
357
357
  if (navigator.sendBeacon) {
358
- const blob = new Blob([JSON.stringify(payload)], { type: "application/json" });
358
+ const blob = beaconBlob(JSON.stringify(payload), this.ingestKey);
359
359
  if (!navigator.sendBeacon(unloadUrl, blob)) {
360
360
  await fetch(unloadUrl, {
361
361
  method: "POST",
package/dist/utils.d.ts CHANGED
@@ -14,6 +14,22 @@ export declare function ingestKeyHeaders(ingestKey?: string): Record<string, str
14
14
  * is configured.
15
15
  */
16
16
  export declare function withIngestKey(url: string, ingestKey?: string): string;
17
+ /**
18
+ * Wraps a JSON payload for `navigator.sendBeacon`.
19
+ *
20
+ * Beacons always use credentials mode `include`. A beacon whose Blob type is
21
+ * not CORS-safelisted (`application/json` is not) is preflighted, and a
22
+ * cross-origin preflight for a credentialed request fails unless the server
23
+ * answers with a credentialed grant — so on keyed, cross-origin ingest the
24
+ * unload events (`page_leave`, late Web Vitals, the final replay batch) were
25
+ * silently dropped. `text/plain` is safelisted: the beacon goes out with no
26
+ * preflight, and the server parses its body as JSON exactly as before.
27
+ *
28
+ * Only keyed requests are sent as `text/plain`. The server accepts that type
29
+ * on the keyed path alone, and an app without a key is served by Temps on its
30
+ * own origin, where `application/json` was never a problem.
31
+ */
32
+ export declare function beaconBlob(payload: string, ingestKey?: string): Blob;
17
33
  export declare function sendAnalytics(endpoint: string, data: Record<string, JsonValue>, method: "POST" | "PUT" | "PATCH" | undefined, basePath: string, ingestKey?: string): Promise<void>;
18
34
  export declare function sendAnalyticsReliable(endpoint: string, data: Record<string, JsonValue>, basePath: string, ingestKey?: string): boolean;
19
35
  //# sourceMappingURL=utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAEzC,wBAAgB,YAAY,IAAI,MAAM,GAAG,SAAS,CAcjD;AAED,wBAAgB,eAAe,IAAI,OAAO,CASzC;AAED,wBAAgB,iBAAiB,IAAI,OAAO,CAS3C;AAoBD;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAE3E;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,CAIrE;AAED,wBAAsB,aAAa,CACjC,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,EAC/B,MAAM,EAAE,MAAM,GAAG,KAAK,GAAG,OAAO,YAAS,EACzC,QAAQ,EAAE,MAAM,EAChB,SAAS,CAAC,EAAE,MAAM,GACjB,OAAO,CAAC,IAAI,CAAC,CAWf;AAED,wBAAgB,qBAAqB,CACnC,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,EAC/B,QAAQ,EAAE,MAAM,EAChB,SAAS,CAAC,EAAE,MAAM,GACjB,OAAO,CA4CT"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAEzC,wBAAgB,YAAY,IAAI,MAAM,GAAG,SAAS,CAcjD;AAED,wBAAgB,eAAe,IAAI,OAAO,CASzC;AAED,wBAAgB,iBAAiB,IAAI,OAAO,CAS3C;AAoBD;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAE3E;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,CAIrE;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAEpE;AAED,wBAAsB,aAAa,CACjC,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,EAC/B,MAAM,EAAE,MAAM,GAAG,KAAK,GAAG,OAAO,YAAS,EACzC,QAAQ,EAAE,MAAM,EAChB,SAAS,CAAC,EAAE,MAAM,GACjB,OAAO,CAAC,IAAI,CAAC,CAWf;AAED,wBAAgB,qBAAqB,CACnC,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,EAC/B,QAAQ,EAAE,MAAM,EAChB,SAAS,CAAC,EAAE,MAAM,GACjB,OAAO,CA2CT"}
package/dist/utils.js CHANGED
@@ -77,6 +77,24 @@ export function withIngestKey(url, ingestKey) {
77
77
  const separator = url.includes("?") ? "&" : "?";
78
78
  return `${url}${separator}${INGEST_KEY_QUERY_PARAM}=${encodeURIComponent(ingestKey)}`;
79
79
  }
80
+ /**
81
+ * Wraps a JSON payload for `navigator.sendBeacon`.
82
+ *
83
+ * Beacons always use credentials mode `include`. A beacon whose Blob type is
84
+ * not CORS-safelisted (`application/json` is not) is preflighted, and a
85
+ * cross-origin preflight for a credentialed request fails unless the server
86
+ * answers with a credentialed grant — so on keyed, cross-origin ingest the
87
+ * unload events (`page_leave`, late Web Vitals, the final replay batch) were
88
+ * silently dropped. `text/plain` is safelisted: the beacon goes out with no
89
+ * preflight, and the server parses its body as JSON exactly as before.
90
+ *
91
+ * Only keyed requests are sent as `text/plain`. The server accepts that type
92
+ * on the keyed path alone, and an app without a key is served by Temps on its
93
+ * own origin, where `application/json` was never a problem.
94
+ */
95
+ export function beaconBlob(payload, ingestKey) {
96
+ return new Blob([payload], { type: ingestKey ? "text/plain" : "application/json" });
97
+ }
80
98
  export async function sendAnalytics(endpoint, data, method = "POST", basePath, ingestKey) {
81
99
  try {
82
100
  await fetch(`${basePath}/${endpoint}`, {
@@ -107,8 +125,7 @@ export function sendAnalyticsReliable(endpoint, data, basePath, ingestKey) {
107
125
  navigator.sendBeacon &&
108
126
  typeof navigator.sendBeacon === "function") {
109
127
  try {
110
- const blob = new Blob([payload], { type: "application/json" });
111
- if (navigator.sendBeacon(url, blob))
128
+ if (navigator.sendBeacon(url, beaconBlob(payload, ingestKey)))
112
129
  return true;
113
130
  }
114
131
  catch {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@temps-sdk/analytics-core",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "description": "Framework-agnostic core for Temps analytics, engagement, Web Vitals, and session replay.",
5
5
  "private": false,
6
6
  "type": "module",