@10x-media/analytics 1.0.0-beta.6 → 1.0.0-beta.7

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.
Files changed (213) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/README.md +5 -1
  3. package/dist/adapters/http/fetchJson.js +1 -1
  4. package/dist/adapters/plausible/plausible.d.ts +10 -0
  5. package/dist/adapters/plausible/plausible.js +40 -0
  6. package/dist/adapters/plausible/plausible.js.map +1 -1
  7. package/dist/adapters/posthog/posthog.d.ts +10 -1
  8. package/dist/adapters/posthog/posthog.js +47 -0
  9. package/dist/adapters/posthog/posthog.js.map +1 -1
  10. package/dist/adapters/umami/umami.d.ts +5 -1
  11. package/dist/adapters/umami/umami.js +26 -0
  12. package/dist/adapters/umami/umami.js.map +1 -1
  13. package/dist/capture/getTrackerConfig.d.ts +20 -0
  14. package/dist/capture/getTrackerConfig.js +31 -0
  15. package/dist/capture/getTrackerConfig.js.map +1 -0
  16. package/dist/capture/mountPath.js +14 -0
  17. package/dist/capture/mountPath.js.map +1 -0
  18. package/dist/capture/proxyEndpoint.js +123 -0
  19. package/dist/capture/proxyEndpoint.js.map +1 -0
  20. package/dist/capture/proxyRoutes.js +108 -0
  21. package/dist/capture/proxyRoutes.js.map +1 -0
  22. package/dist/capture/requestBody.js +68 -0
  23. package/dist/capture/requestBody.js.map +1 -0
  24. package/dist/capture/slots.d.ts +8 -0
  25. package/dist/capture/slots.js +43 -0
  26. package/dist/capture/slots.js.map +1 -0
  27. package/dist/capture/trackerConfig.d.ts +32 -0
  28. package/dist/capture/trackerConfig.js +70 -0
  29. package/dist/capture/trackerConfig.js.map +1 -0
  30. package/dist/capture/trackerEndpoint.js +35 -0
  31. package/dist/capture/trackerEndpoint.js.map +1 -0
  32. package/dist/core/capture.d.ts +49 -0
  33. package/dist/core/contract.d.ts +26 -2
  34. package/dist/core/contract.js.map +1 -1
  35. package/dist/core/options.d.ts +97 -2
  36. package/dist/core/options.js +63 -2
  37. package/dist/core/options.js.map +1 -1
  38. package/dist/core/registry.d.ts +1 -0
  39. package/dist/core/scopeList.js +32 -0
  40. package/dist/core/scopeList.js.map +1 -0
  41. package/dist/core/scopedRead.js +13 -6
  42. package/dist/core/scopedRead.js.map +1 -1
  43. package/dist/exports/next.d.ts +2 -1
  44. package/dist/exports/next.js +2 -1
  45. package/dist/exports/react.d.ts +4 -0
  46. package/dist/exports/react.js +5 -0
  47. package/dist/exports/rsc.d.ts +4 -1
  48. package/dist/exports/rsc.js +3 -1
  49. package/dist/exports/tracker.d.ts +18 -0
  50. package/dist/exports/tracker.js +12 -0
  51. package/dist/exports/types.d.ts +5 -2
  52. package/dist/fields/AnalyticsPanelClient.js +1 -1
  53. package/dist/fields/config/narrow.js +2 -0
  54. package/dist/fields/config/narrow.js.map +1 -1
  55. package/dist/fields/readForDocument.js +1 -1
  56. package/dist/goals/match.js +64 -0
  57. package/dist/goals/match.js.map +1 -0
  58. package/dist/goals/types.d.ts +33 -0
  59. package/dist/goals/types.js +7 -0
  60. package/dist/goals/types.js.map +1 -0
  61. package/dist/index.d.ts +4 -3
  62. package/dist/index.js +33 -9
  63. package/dist/index.js.map +1 -1
  64. package/dist/native/collections/events.js +21 -1
  65. package/dist/native/collections/events.js.map +1 -1
  66. package/dist/native/collections/rollups.js +24 -0
  67. package/dist/native/collections/rollups.js.map +1 -1
  68. package/dist/native/geo/maxmindResolver.js +2 -4
  69. package/dist/native/geo/maxmindResolver.js.map +1 -1
  70. package/dist/native/ingest/clientIp.js +15 -0
  71. package/dist/native/ingest/clientIp.js.map +1 -0
  72. package/dist/native/ingest/endpoint.js +40 -4
  73. package/dist/native/ingest/endpoint.js.map +1 -1
  74. package/dist/native/ingest/flushBatch.js +4 -13
  75. package/dist/native/ingest/flushBatch.js.map +1 -1
  76. package/dist/native/ingest/normalizeEvent.js +76 -9
  77. package/dist/native/ingest/normalizeEvent.js.map +1 -1
  78. package/dist/native/nativeAdapter.js +20 -4
  79. package/dist/native/nativeAdapter.js.map +1 -1
  80. package/dist/native/query/eventAgg.js +39 -13
  81. package/dist/native/query/eventAgg.js.map +1 -1
  82. package/dist/native/rollupAcc.js +12 -1
  83. package/dist/native/rollupAcc.js.map +1 -1
  84. package/dist/native/rollups/bumpRollup.js +5 -1
  85. package/dist/native/rollups/bumpRollup.js.map +1 -1
  86. package/dist/native/rollups/deltas.js +28 -6
  87. package/dist/native/rollups/deltas.js.map +1 -1
  88. package/dist/next/captureRewrites.d.ts +41 -0
  89. package/dist/next/captureRewrites.js +56 -0
  90. package/dist/next/captureRewrites.js.map +1 -0
  91. package/dist/next/posthogProxyRewrites.d.ts +5 -0
  92. package/dist/next/posthogProxyRewrites.js +16 -21
  93. package/dist/next/posthogProxyRewrites.js.map +1 -1
  94. package/dist/plugin/documentEndpoint.js +1 -1
  95. package/dist/plugin/paths.js +11 -1
  96. package/dist/plugin/paths.js.map +1 -1
  97. package/dist/plugin/runtime.d.ts +1 -0
  98. package/dist/plugin/runtime.js.map +1 -1
  99. package/dist/plugin/sourcesEndpoint.js +16 -4
  100. package/dist/plugin/sourcesEndpoint.js.map +1 -1
  101. package/dist/plugin/warmTask.js +0 -0
  102. package/dist/plugin/warmTask.js.map +1 -1
  103. package/dist/providers/collection.js +56 -16
  104. package/dist/providers/collection.js.map +1 -1
  105. package/dist/providers/factory.js +8 -2
  106. package/dist/providers/factory.js.map +1 -1
  107. package/dist/react/AnalyticsProvider.d.ts +43 -0
  108. package/dist/react/AnalyticsProvider.js +66 -0
  109. package/dist/react/AnalyticsProvider.js.map +1 -0
  110. package/dist/react/TrackerBoot.d.ts +22 -0
  111. package/dist/react/TrackerBoot.js +30 -0
  112. package/dist/react/TrackerBoot.js.map +1 -0
  113. package/dist/react/useAnalytics.d.ts +17 -0
  114. package/dist/react/useAnalytics.js +36 -0
  115. package/dist/react/useAnalytics.js.map +1 -0
  116. package/dist/rsc/AnalyticsScripts.d.ts +30 -0
  117. package/dist/rsc/AnalyticsScripts.js +41 -0
  118. package/dist/rsc/AnalyticsScripts.js.map +1 -0
  119. package/dist/surfacing/cacheStore.d.ts +1 -0
  120. package/dist/surfacing/cacheStore.js +18 -2
  121. package/dist/surfacing/cacheStore.js.map +1 -1
  122. package/dist/surfacing/engine.d.ts +1 -0
  123. package/dist/surfacing/engine.js +44 -4
  124. package/dist/surfacing/engine.js.map +1 -1
  125. package/dist/surfacing/queue.js +22 -6
  126. package/dist/surfacing/queue.js.map +1 -1
  127. package/dist/surfacing/rateLimiter.js +130 -0
  128. package/dist/surfacing/rateLimiter.js.map +1 -0
  129. package/dist/surfacing/retryPolicy.js +15 -0
  130. package/dist/surfacing/retryPolicy.js.map +1 -0
  131. package/dist/sync/syncTask.js +60 -33
  132. package/dist/sync/syncTask.js.map +1 -1
  133. package/dist/testing/memoryAdapter.d.ts +2 -0
  134. package/dist/testing/memoryAdapter.js +10 -0
  135. package/dist/testing/memoryAdapter.js.map +1 -1
  136. package/dist/tracker/autoCapture.d.ts +48 -0
  137. package/dist/tracker/autoCapture.js +129 -0
  138. package/dist/tracker/autoCapture.js.map +1 -0
  139. package/dist/tracker/consent.d.ts +24 -0
  140. package/dist/tracker/consent.js +48 -0
  141. package/dist/tracker/consent.js.map +1 -0
  142. package/dist/tracker/createTracker.d.ts +18 -0
  143. package/dist/tracker/createTracker.js +163 -0
  144. package/dist/tracker/createTracker.js.map +1 -0
  145. package/dist/tracker/initTracker.d.ts +22 -0
  146. package/dist/tracker/initTracker.js +33 -0
  147. package/dist/tracker/initTracker.js.map +1 -0
  148. package/dist/tracker/loadScript.d.ts +14 -0
  149. package/dist/tracker/loadScript.js +41 -0
  150. package/dist/tracker/loadScript.js.map +1 -0
  151. package/dist/tracker/pageTracking.d.ts +17 -0
  152. package/dist/tracker/pageTracking.js +38 -0
  153. package/dist/tracker/pageTracking.js.map +1 -0
  154. package/dist/tracker/registry.d.ts +27 -0
  155. package/dist/tracker/registry.js +73 -0
  156. package/dist/tracker/registry.js.map +1 -0
  157. package/dist/tracker/sinks/native.d.ts +22 -0
  158. package/dist/tracker/sinks/native.js +56 -0
  159. package/dist/tracker/sinks/native.js.map +1 -0
  160. package/dist/tracker/sinks/plausible.d.ts +14 -0
  161. package/dist/tracker/sinks/plausible.js +25 -0
  162. package/dist/tracker/sinks/plausible.js.map +1 -0
  163. package/dist/tracker/sinks/posthog.d.ts +14 -0
  164. package/dist/tracker/sinks/posthog.js +18 -0
  165. package/dist/tracker/sinks/posthog.js.map +1 -0
  166. package/dist/tracker/sinks/umami.d.ts +13 -0
  167. package/dist/tracker/sinks/umami.js +17 -0
  168. package/dist/tracker/sinks/umami.js.map +1 -0
  169. package/dist/tracker/sinks/vendor.d.ts +14 -0
  170. package/dist/tracker/sinks/vendor.js +66 -0
  171. package/dist/tracker/sinks/vendor.js.map +1 -0
  172. package/dist/tracker/transport.js +28 -0
  173. package/dist/tracker/transport.js.map +1 -0
  174. package/dist/tracker/types.d.ts +66 -0
  175. package/dist/translations/ar.js +6 -0
  176. package/dist/translations/ar.js.map +1 -1
  177. package/dist/translations/de.js +6 -0
  178. package/dist/translations/de.js.map +1 -1
  179. package/dist/translations/en.js +6 -0
  180. package/dist/translations/en.js.map +1 -1
  181. package/dist/translations/es.js +6 -0
  182. package/dist/translations/es.js.map +1 -1
  183. package/dist/translations/fr.js +6 -0
  184. package/dist/translations/fr.js.map +1 -1
  185. package/dist/translations/id.js +6 -0
  186. package/dist/translations/id.js.map +1 -1
  187. package/dist/translations/keys.d.ts +6 -0
  188. package/dist/translations/keys.js +6 -0
  189. package/dist/translations/keys.js.map +1 -1
  190. package/dist/translations/ko.js +6 -0
  191. package/dist/translations/ko.js.map +1 -1
  192. package/dist/translations/pt.js +6 -0
  193. package/dist/translations/pt.js.map +1 -1
  194. package/dist/translations/ru.js +6 -0
  195. package/dist/translations/ru.js.map +1 -1
  196. package/dist/translations/uk.js +6 -0
  197. package/dist/translations/uk.js.map +1 -1
  198. package/dist/translations/zh.js +6 -0
  199. package/dist/translations/zh.js.map +1 -1
  200. package/dist/widgets/breakdownTypes.js +5 -0
  201. package/dist/widgets/breakdownTypes.js.map +1 -1
  202. package/dist/widgets/readForWidget.js +24 -14
  203. package/dist/widgets/readForWidget.js.map +1 -1
  204. package/dist/widgets/readForWidgetBreakdown.js +23 -14
  205. package/dist/widgets/readForWidgetBreakdown.js.map +1 -1
  206. package/dist/widgets/readForWidgetRealtime.js +1 -1
  207. package/dist/widgets/readForWidgetSeries.js +26 -16
  208. package/dist/widgets/readForWidgetSeries.js.map +1 -1
  209. package/dist/widgets/registerWidgets.js +10 -4
  210. package/dist/widgets/registerWidgets.js.map +1 -1
  211. package/dist/widgets/types.js +4 -1
  212. package/dist/widgets/types.js.map +1 -1
  213. package/package.json +21 -6
@@ -1,15 +1,50 @@
1
+ import { readCappedBody } from "../../capture/requestBody.js";
1
2
  import { flushBatch } from "./flushBatch.js";
2
3
  import { normalizeEvent } from "./normalizeEvent.js";
3
4
  import { dailySalt } from "./salt.js";
4
5
  //#region src/native/ingest/endpoint.ts
6
+ const TYPES = new Set([
7
+ "pageview",
8
+ "event",
9
+ "goal"
10
+ ]);
11
+ /** One event, not a session replay: far above any legitimate payload, far below a DoS. */
12
+ const MAX_INGEST_BODY_BYTES = 64 * 1024;
13
+ /**
14
+ * Parses the buffered body, answering undefined for anything the validator could not judge:
15
+ * a garbage POST is a 400 like any other bad payload rather than a thrown parse error that
16
+ * escapes into Payload's routeError as a logged 500.
17
+ */
18
+ const parseBody = (bytes) => {
19
+ try {
20
+ const parsed = JSON.parse(new TextDecoder().decode(bytes));
21
+ return typeof parsed === "object" && parsed !== null ? parsed : void 0;
22
+ } catch {
23
+ return;
24
+ }
25
+ };
26
+ /**
27
+ * An event is rejected only on its required fields: a known `type`, a `path`, a `hostname`,
28
+ * and a `name` (the event name, or the goal slug on a `goal`) for everything but a pageview.
29
+ * Optional fields are sanitized in `normalizeEvent` and dropped when malformed, so one bad
30
+ * attribute costs an attribute rather than the whole event.
31
+ */
32
+ const nonEmptyString = (value) => typeof value === "string" && value.length > 0;
33
+ const isValid = (raw) => {
34
+ if (!raw || !TYPES.has(raw.type) || !nonEmptyString(raw.path) || !nonEmptyString(raw.hostname)) return false;
35
+ return raw.type === "pageview" || nonEmptyString(raw.name);
36
+ };
5
37
  const makeIngestHandler = (geoResolver, getBuffer = () => null, resolvers = {}) => async (req) => {
6
- const { scope: resolveScope, timezone: resolveTimezone } = resolvers;
7
- const raw = (req.headers.get("content-type") ?? "").startsWith("application/json") ? await req.json?.() : JSON.parse(await req.text?.() ?? "{}");
8
- if (!raw?.path || !raw?.hostname || raw.type !== "pageview" && raw.type !== "event") return Response.json({ error: "invalid payload" }, { status: 400 });
38
+ const { scope: resolveScope, timezone: resolveTimezone, goals: resolveGoals } = resolvers;
39
+ const read = await readCappedBody(req, MAX_INGEST_BODY_BYTES);
40
+ if (!read.ok) return Response.json({ error: read.reason === "too-large" ? "payload too large" : "invalid payload" }, { status: read.reason === "too-large" ? 413 : 400 });
41
+ const raw = parseBody(read.body);
42
+ if (!isValid(raw)) return Response.json({ error: "invalid payload" }, { status: 400 });
9
43
  const now = /* @__PURE__ */ new Date();
10
44
  const salt = await dailySalt(req.payload, now);
11
45
  const scope = resolveScope ? await resolveScope(req) ?? "" : void 0;
12
46
  const timezone = resolveTimezone ? await resolveTimezone(req, scope ?? null) : void 0;
47
+ const goals = resolveGoals ? await resolveGoals(req, scope ?? null) : void 0;
13
48
  const event = await normalizeEvent({
14
49
  raw,
15
50
  headers: req.headers,
@@ -17,7 +52,8 @@ const makeIngestHandler = (geoResolver, getBuffer = () => null, resolvers = {})
17
52
  salt,
18
53
  now,
19
54
  scope,
20
- timezone
55
+ timezone,
56
+ goals
21
57
  });
22
58
  const buffer = getBuffer();
23
59
  if (buffer) buffer.add(event);
@@ -1 +1 @@
1
- {"version":3,"file":"endpoint.js","names":[],"sources":["../../../src/native/ingest/endpoint.ts"],"sourcesContent":["import type { PayloadHandler, PayloadRequest } from 'payload'\nimport type { GeoResolver } from '../geo/geoResolver'\nimport { flushBatch } from './flushBatch'\nimport { normalizeEvent, type RawEventInput, type StoredEvent } from './normalizeEvent'\nimport { dailySalt } from './salt'\nimport type { WriteBuffer } from './writeBuffer'\n\nexport interface IngestResolvers {\n\tscope?: (req: PayloadRequest) => Promise<string | null>\n\ttimezone?: (req: PayloadRequest, scope?: string | null) => Promise<string>\n}\n\nexport const makeIngestHandler =\n\t(\n\t\tgeoResolver: GeoResolver,\n\t\tgetBuffer: () => WriteBuffer<StoredEvent> | null = () => null,\n\t\tresolvers: IngestResolvers = {}\n\t): PayloadHandler =>\n\tasync (req) => {\n\t\tconst { scope: resolveScope, timezone: resolveTimezone } = resolvers\n\t\tconst ct = req.headers.get('content-type') ?? ''\n\t\tconst raw = (\n\t\t\tct.startsWith('application/json')\n\t\t\t\t? await req.json?.()\n\t\t\t\t: JSON.parse((await req.text?.()) ?? '{}')\n\t\t) as RawEventInput\n\t\tif (!raw?.path || !raw?.hostname || (raw.type !== 'pageview' && raw.type !== 'event')) {\n\t\t\treturn Response.json({ error: 'invalid payload' }, { status: 400 })\n\t\t}\n\t\tconst now = new Date()\n\t\tconst salt = await dailySalt(req.payload, now)\n\t\tconst scope = resolveScope ? ((await resolveScope(req)) ?? '') : undefined\n\t\tconst timezone = resolveTimezone ? await resolveTimezone(req, scope ?? null) : undefined\n\t\tconst event = await normalizeEvent({\n\t\t\traw,\n\t\t\theaders: req.headers,\n\t\t\tgeoResolver,\n\t\t\tsalt,\n\t\t\tnow,\n\t\t\tscope,\n\t\t\ttimezone,\n\t\t})\n\t\tconst buffer = getBuffer()\n\t\tif (buffer) {\n\t\t\tbuffer.add(event)\n\t\t} else {\n\t\t\tawait flushBatch(req.payload, [event])\n\t\t}\n\t\treturn Response.json({ ok: true }, { status: 202 })\n\t}\n"],"mappings":";;;;AAYA,MAAa,qBAEX,aACA,kBAAyD,MACzD,YAA6B,CAAC,MAE/B,OAAO,QAAQ;CACd,MAAM,EAAE,OAAO,cAAc,UAAU,oBAAoB;CAE3D,MAAM,OADK,IAAI,QAAQ,IAAI,cAAc,KAAK,IAE1C,WAAW,kBAAkB,IAC7B,MAAM,IAAI,OAAO,IACjB,KAAK,MAAO,MAAM,IAAI,OAAO,KAAM,IAAI;CAE3C,IAAI,CAAC,KAAK,QAAQ,CAAC,KAAK,YAAa,IAAI,SAAS,cAAc,IAAI,SAAS,SAC5E,OAAO,SAAS,KAAK,EAAE,OAAO,kBAAkB,GAAG,EAAE,QAAQ,IAAI,CAAC;CAEnE,MAAM,sBAAM,IAAI,KAAK;CACrB,MAAM,OAAO,MAAM,UAAU,IAAI,SAAS,GAAG;CAC7C,MAAM,QAAQ,eAAiB,MAAM,aAAa,GAAG,KAAM,KAAM,KAAA;CACjE,MAAM,WAAW,kBAAkB,MAAM,gBAAgB,KAAK,SAAS,IAAI,IAAI,KAAA;CAC/E,MAAM,QAAQ,MAAM,eAAe;EAClC;EACA,SAAS,IAAI;EACb;EACA;EACA;EACA;EACA;CACD,CAAC;CACD,MAAM,SAAS,UAAU;CACzB,IAAI,QACH,OAAO,IAAI,KAAK;MAEhB,MAAM,WAAW,IAAI,SAAS,CAAC,KAAK,CAAC;CAEtC,OAAO,SAAS,KAAK,EAAE,IAAI,KAAK,GAAG,EAAE,QAAQ,IAAI,CAAC;AACnD"}
1
+ {"version":3,"file":"endpoint.js","names":[],"sources":["../../../src/native/ingest/endpoint.ts"],"sourcesContent":["import type { PayloadHandler, PayloadRequest } from 'payload'\nimport { readCappedBody } from '../../capture/requestBody'\nimport type { Goal } from '../../goals/types'\nimport type { GeoResolver } from '../geo/geoResolver'\nimport { flushBatch } from './flushBatch'\nimport {\n\ttype EventType,\n\tnormalizeEvent,\n\ttype RawEventInput,\n\ttype StoredEvent,\n} from './normalizeEvent'\nimport { dailySalt } from './salt'\nimport type { WriteBuffer } from './writeBuffer'\n\nexport interface IngestResolvers {\n\tscope?: (req: PayloadRequest) => Promise<string | null>\n\ttimezone?: (req: PayloadRequest, scope?: string | null) => Promise<string>\n\tgoals?: (req: PayloadRequest, scope?: string | null) => Promise<Goal[]>\n}\n\nconst TYPES: ReadonlySet<string> = new Set<EventType>(['pageview', 'event', 'goal'])\n\n/** One event, not a session replay: far above any legitimate payload, far below a DoS. */\nexport const MAX_INGEST_BODY_BYTES = 64 * 1024\n\n/**\n * Parses the buffered body, answering undefined for anything the validator could not judge:\n * a garbage POST is a 400 like any other bad payload rather than a thrown parse error that\n * escapes into Payload's routeError as a logged 500.\n */\nconst parseBody = (bytes: ArrayBuffer): RawEventInput | undefined => {\n\ttry {\n\t\tconst parsed: unknown = JSON.parse(new TextDecoder().decode(bytes))\n\t\treturn typeof parsed === 'object' && parsed !== null ? (parsed as RawEventInput) : undefined\n\t} catch {\n\t\treturn undefined\n\t}\n}\n\n/**\n * An event is rejected only on its required fields: a known `type`, a `path`, a `hostname`,\n * and a `name` (the event name, or the goal slug on a `goal`) for everything but a pageview.\n * Optional fields are sanitized in `normalizeEvent` and dropped when malformed, so one bad\n * attribute costs an attribute rather than the whole event.\n */\nconst nonEmptyString = (value: unknown): boolean => typeof value === 'string' && value.length > 0\n\nconst isValid = (raw: RawEventInput | undefined): raw is RawEventInput => {\n\t// Types are checked, not just truthiness: a non-string path would otherwise reach goal\n\t// matching and throw there rather than answering 400 here.\n\tif (!raw || !TYPES.has(raw.type) || !nonEmptyString(raw.path) || !nonEmptyString(raw.hostname)) {\n\t\treturn false\n\t}\n\treturn raw.type === 'pageview' || nonEmptyString(raw.name)\n}\n\nexport const makeIngestHandler =\n\t(\n\t\tgeoResolver: GeoResolver,\n\t\tgetBuffer: () => WriteBuffer<StoredEvent> | null = () => null,\n\t\tresolvers: IngestResolvers = {}\n\t): PayloadHandler =>\n\tasync (req) => {\n\t\tconst { scope: resolveScope, timezone: resolveTimezone, goals: resolveGoals } = resolvers\n\t\t// Read like the capture proxy does, and for the same reasons: this is a public,\n\t\t// unauthenticated path, so the body is capped before it is buffered and a body that\n\t\t// dies in transit (a beacon from an unloading tab) answers 400 rather than throwing.\n\t\tconst read = await readCappedBody(req, MAX_INGEST_BODY_BYTES)\n\t\tif (!read.ok) {\n\t\t\treturn Response.json(\n\t\t\t\t{ error: read.reason === 'too-large' ? 'payload too large' : 'invalid payload' },\n\t\t\t\t{ status: read.reason === 'too-large' ? 413 : 400 }\n\t\t\t)\n\t\t}\n\t\tconst raw = parseBody(read.body)\n\t\tif (!isValid(raw)) {\n\t\t\treturn Response.json({ error: 'invalid payload' }, { status: 400 })\n\t\t}\n\t\tconst now = new Date()\n\t\tconst salt = await dailySalt(req.payload, now)\n\t\tconst scope = resolveScope ? ((await resolveScope(req)) ?? '') : undefined\n\t\tconst timezone = resolveTimezone ? await resolveTimezone(req, scope ?? null) : undefined\n\t\t// Goals resolve under the same scope the event is stamped with, so a tenant's event\n\t\t// can only ever complete that tenant's goals.\n\t\tconst goals = resolveGoals ? await resolveGoals(req, scope ?? null) : undefined\n\t\tconst event = await normalizeEvent({\n\t\t\traw,\n\t\t\theaders: req.headers,\n\t\t\tgeoResolver,\n\t\t\tsalt,\n\t\t\tnow,\n\t\t\tscope,\n\t\t\ttimezone,\n\t\t\tgoals,\n\t\t})\n\t\tconst buffer = getBuffer()\n\t\tif (buffer) {\n\t\t\tbuffer.add(event)\n\t\t} else {\n\t\t\tawait flushBatch(req.payload, [event])\n\t\t}\n\t\treturn Response.json({ ok: true }, { status: 202 })\n\t}\n"],"mappings":";;;;;AAoBA,MAAM,QAA6B,IAAI,IAAe;CAAC;CAAY;CAAS;AAAM,CAAC;;AAGnF,MAAa,wBAAwB,KAAK;;;;;;AAO1C,MAAM,aAAa,UAAkD;CACpE,IAAI;EACH,MAAM,SAAkB,KAAK,MAAM,IAAI,YAAY,EAAE,OAAO,KAAK,CAAC;EAClE,OAAO,OAAO,WAAW,YAAY,WAAW,OAAQ,SAA2B,KAAA;CACpF,QAAQ;EACP;CACD;AACD;;;;;;;AAQA,MAAM,kBAAkB,UAA4B,OAAO,UAAU,YAAY,MAAM,SAAS;AAEhG,MAAM,WAAW,QAAy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+13,10 @@ async function flushBatch(payload, events) {
13
13
  for (const event of events) for (const delta of computeRollupDeltas(event)) {
14
14
  const bk = bucketKey(delta.key);
15
15
  const agg = base.get(bk);
16
- if (agg) {
17
- agg.pageviews += delta.inc.pageviews;
18
- agg.events += delta.inc.events;
19
- agg.durationMs += delta.inc.durationMs;
20
- agg.samples += delta.inc.samples;
21
- } else base.set(bk, {
16
+ if (agg) for (const metric of Object.keys(agg.inc)) agg.inc[metric] += delta.inc[metric];
17
+ else base.set(bk, {
22
18
  key: delta.key,
23
- ...delta.inc
19
+ inc: { ...delta.inc }
24
20
  });
25
21
  candidates.set(`${bk}|visitor|${event.visitorHash}`, {
26
22
  key: delta.key,
@@ -33,12 +29,7 @@ async function flushBatch(payload, events) {
33
29
  value: event.sessionId
34
30
  });
35
31
  }
36
- for (const agg of base.values()) await bumpRollup(payload, agg.key, {
37
- pageviews: agg.pageviews,
38
- events: agg.events,
39
- durationMs: agg.durationMs,
40
- samples: agg.samples
41
- });
32
+ for (const agg of base.values()) await bumpRollup(payload, agg.key, agg.inc);
42
33
  const distinct = /* @__PURE__ */ new Map();
43
34
  for (const candidate of candidates.values()) {
44
35
  if (!await insertIfNew(payload, "analytics-seen", {
@@ -1 +1 @@
1
- {"version":3,"file":"flushBatch.js","names":[],"sources":["../../../src/native/ingest/flushBatch.ts"],"sourcesContent":["import type { Payload } from 'payload'\nimport { SEEN_SLUG } from '../collections/seen'\nimport { bucketKey } from '../rollups/applyDistinctDeltas'\nimport { bumpRollup } from '../rollups/bumpRollup'\nimport { computeRollupDeltas, type RollupKey, type RollupMetric } from '../rollups/deltas'\nimport { insertIfNew } from '../rollups/insertIfNew'\nimport type { StoredEvent } from './normalizeEvent'\nimport { writeEvent } from './writeEvent'\n\ninterface BaseAgg {\n\tkey: RollupKey\n\tpageviews: number\n\tevents: number\n\tdurationMs: number\n\tsamples: number\n}\n\ninterface DistinctCandidate {\n\tkey: RollupKey\n\tkind: 'visitor' | 'session'\n\tvalue: string\n}\n\n// Coalesces a batch of events into the fewest writes: events insert concurrently, base\n// rollup increments are summed per bucket into one upsert each, and distinct visitor /\n// session candidates are de-duplicated within the batch before insert-if-new. Correctness\n// matches the per-event path because the seen ledger still de-dupes across batches.\nexport async function flushBatch(payload: Payload, events: StoredEvent[]): Promise<void> {\n\tif (events.length === 0) {\n\t\treturn\n\t}\n\tawait Promise.all(events.map((event) => writeEvent(payload, event)))\n\n\tconst base = new Map<string, BaseAgg>()\n\tconst candidates = new Map<string, DistinctCandidate>()\n\tfor (const event of events) {\n\t\tfor (const delta of computeRollupDeltas(event)) {\n\t\t\tconst bk = bucketKey(delta.key)\n\t\t\tconst agg = base.get(bk)\n\t\t\tif (agg) {\n\t\t\t\tagg.pageviews += delta.inc.pageviews\n\t\t\t\tagg.events += delta.inc.events\n\t\t\t\tagg.durationMs += delta.inc.durationMs\n\t\t\t\tagg.samples += delta.inc.samples\n\t\t\t} else {\n\t\t\t\tbase.set(bk, { key: delta.key, ...delta.inc })\n\t\t\t}\n\t\t\tcandidates.set(`${bk}|visitor|${event.visitorHash}`, {\n\t\t\t\tkey: delta.key,\n\t\t\t\tkind: 'visitor',\n\t\t\t\tvalue: event.visitorHash,\n\t\t\t})\n\t\t\tcandidates.set(`${bk}|session|${event.sessionId}`, {\n\t\t\t\tkey: delta.key,\n\t\t\t\tkind: 'session',\n\t\t\t\tvalue: event.sessionId,\n\t\t\t})\n\t\t}\n\t}\n\n\tfor (const agg of base.values()) {\n\t\tawait bumpRollup(payload, agg.key, {\n\t\t\tpageviews: agg.pageviews,\n\t\t\tevents: agg.events,\n\t\t\tdurationMs: agg.durationMs,\n\t\t\tsamples: agg.samples,\n\t\t})\n\t}\n\n\tconst distinct = new Map<string, { key: RollupKey; metric: RollupMetric; count: number }>()\n\tfor (const candidate of candidates.values()) {\n\t\tconst isNew = await insertIfNew(payload, SEEN_SLUG, {\n\t\t\tbucket: bucketKey(candidate.key),\n\t\t\tkind: candidate.kind,\n\t\t\tvalue: candidate.value,\n\t\t\tperiod: candidate.key.period,\n\t\t})\n\t\tif (!isNew) {\n\t\t\tcontinue\n\t\t}\n\t\tconst metric: RollupMetric = candidate.kind === 'visitor' ? 'visitors' : 'sessions'\n\t\tconst dk = `${bucketKey(candidate.key)}|${metric}`\n\t\tconst entry = distinct.get(dk)\n\t\tif (entry) {\n\t\t\tentry.count += 1\n\t\t} else {\n\t\t\tdistinct.set(dk, { key: candidate.key, metric, count: 1 })\n\t\t}\n\t}\n\n\tfor (const entry of distinct.values()) {\n\t\tconst inc: Partial<Record<RollupMetric, number>> = {}\n\t\tinc[entry.metric] = entry.count\n\t\tawait bumpRollup(payload, entry.key, inc)\n\t}\n}\n"],"mappings":";;;;;;;AA2BA,eAAsB,WAAW,SAAkB,QAAsC;CACxF,IAAI,OAAO,WAAW,GACrB;CAED,MAAM,QAAQ,IAAI,OAAO,KAAK,UAAU,WAAW,SAAS,KAAK,CAAC,CAAC;CAEnE,MAAM,uBAAO,IAAI,IAAqB;CACtC,MAAM,6BAAa,IAAI,IAA+B;CACtD,KAAK,MAAM,SAAS,QACnB,KAAK,MAAM,SAAS,oBAAoB,KAAK,GAAG;EAC/C,MAAM,KAAK,UAAU,MAAM,GAAG;EAC9B,MAAM,MAAM,KAAK,IAAI,EAAE;EACvB,IAAI,KAAK;GACR,IAAI,aAAa,MAAM,IAAI;GAC3B,IAAI,UAAU,MAAM,IAAI;GACxB,IAAI,cAAc,MAAM,IAAI;GAC5B,IAAI,WAAW,MAAM,IAAI;EAC1B,OACC,KAAK,IAAI,IAAI;GAAE,KAAK,MAAM;GAAK,GAAG,MAAM;EAAI,CAAC;EAE9C,WAAW,IAAI,GAAG,GAAG,WAAW,MAAM,eAAe;GACpD,KAAK,MAAM;GACX,MAAM;GACN,OAAO,MAAM;EACd,CAAC;EACD,WAAW,IAAI,GAAG,GAAG,WAAW,MAAM,aAAa;GAClD,KAAK,MAAM;GACX,MAAM;GACN,OAAO,MAAM;EACd,CAAC;CACF;CAGD,KAAK,MAAM,OAAO,KAAK,OAAO,GAC7B,MAAM,WAAW,SAAS,IAAI,KAAK;EAClC,WAAW,IAAI;EACf,QAAQ,IAAI;EACZ,YAAY,IAAI;EAChB,SAAS,IAAI;CACd,CAAC;CAGF,MAAM,2BAAW,IAAI,IAAqE;CAC1F,KAAK,MAAM,aAAa,WAAW,OAAO,GAAG;EAO5C,IAAI,CAAC,MANe,YAAY,SAAA,kBAAoB;GACnD,QAAQ,UAAU,UAAU,GAAG;GAC/B,MAAM,UAAU;GAChB,OAAO,UAAU;GACjB,QAAQ,UAAU,IAAI;EACvB,CAAC,GAEA;EAED,MAAM,SAAuB,UAAU,SAAS,YAAY,aAAa;EACzE,MAAM,KAAK,GAAG,UAAU,UAAU,GAAG,EAAE,GAAG;EAC1C,MAAM,QAAQ,SAAS,IAAI,EAAE;EAC7B,IAAI,OACH,MAAM,SAAS;OAEf,SAAS,IAAI,IAAI;GAAE,KAAK,UAAU;GAAK;GAAQ,OAAO;EAAE,CAAC;CAE3D;CAEA,KAAK,MAAM,SAAS,SAAS,OAAO,GAAG;EACtC,MAAM,MAA6C,CAAC;EACpD,IAAI,MAAM,UAAU,MAAM;EAC1B,MAAM,WAAW,SAAS,MAAM,KAAK,GAAG;CACzC;AACD"}
1
+ {"version":3,"file":"flushBatch.js","names":[],"sources":["../../../src/native/ingest/flushBatch.ts"],"sourcesContent":["import type { Payload } from 'payload'\nimport { SEEN_SLUG } from '../collections/seen'\nimport { bucketKey } from '../rollups/applyDistinctDeltas'\nimport { bumpRollup } from '../rollups/bumpRollup'\nimport {\n\tcomputeRollupDeltas,\n\ttype RollupInc,\n\ttype RollupKey,\n\ttype RollupMetric,\n} from '../rollups/deltas'\nimport { insertIfNew } from '../rollups/insertIfNew'\nimport type { StoredEvent } from './normalizeEvent'\nimport { writeEvent } from './writeEvent'\n\ninterface BaseAgg {\n\tkey: RollupKey\n\tinc: RollupInc\n}\n\ninterface DistinctCandidate {\n\tkey: RollupKey\n\tkind: 'visitor' | 'session'\n\tvalue: string\n}\n\n// Coalesces a batch of events into the fewest writes: events insert concurrently, base\n// rollup increments are summed per bucket into one upsert each, and distinct visitor /\n// session candidates are de-duplicated within the batch before insert-if-new. Correctness\n// matches the per-event path because the seen ledger still de-dupes across batches.\nexport async function flushBatch(payload: Payload, events: StoredEvent[]): Promise<void> {\n\tif (events.length === 0) {\n\t\treturn\n\t}\n\tawait Promise.all(events.map((event) => writeEvent(payload, event)))\n\n\tconst base = new Map<string, BaseAgg>()\n\tconst candidates = new Map<string, DistinctCandidate>()\n\tfor (const event of events) {\n\t\tfor (const delta of computeRollupDeltas(event)) {\n\t\t\tconst bk = bucketKey(delta.key)\n\t\t\tconst agg = base.get(bk)\n\t\t\tif (agg) {\n\t\t\t\tfor (const metric of Object.keys(agg.inc) as Array<keyof RollupInc>) {\n\t\t\t\t\tagg.inc[metric] += delta.inc[metric]\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tbase.set(bk, { key: delta.key, inc: { ...delta.inc } })\n\t\t\t}\n\t\t\tcandidates.set(`${bk}|visitor|${event.visitorHash}`, {\n\t\t\t\tkey: delta.key,\n\t\t\t\tkind: 'visitor',\n\t\t\t\tvalue: event.visitorHash,\n\t\t\t})\n\t\t\tcandidates.set(`${bk}|session|${event.sessionId}`, {\n\t\t\t\tkey: delta.key,\n\t\t\t\tkind: 'session',\n\t\t\t\tvalue: event.sessionId,\n\t\t\t})\n\t\t}\n\t}\n\n\tfor (const agg of base.values()) {\n\t\tawait bumpRollup(payload, agg.key, agg.inc)\n\t}\n\n\tconst distinct = new Map<string, { key: RollupKey; metric: RollupMetric; count: number }>()\n\tfor (const candidate of candidates.values()) {\n\t\tconst isNew = await insertIfNew(payload, SEEN_SLUG, {\n\t\t\tbucket: bucketKey(candidate.key),\n\t\t\tkind: candidate.kind,\n\t\t\tvalue: candidate.value,\n\t\t\tperiod: candidate.key.period,\n\t\t})\n\t\tif (!isNew) {\n\t\t\tcontinue\n\t\t}\n\t\tconst metric: RollupMetric = candidate.kind === 'visitor' ? 'visitors' : 'sessions'\n\t\tconst dk = `${bucketKey(candidate.key)}|${metric}`\n\t\tconst entry = distinct.get(dk)\n\t\tif (entry) {\n\t\t\tentry.count += 1\n\t\t} else {\n\t\t\tdistinct.set(dk, { key: candidate.key, metric, count: 1 })\n\t\t}\n\t}\n\n\tfor (const entry of distinct.values()) {\n\t\tconst inc: Partial<Record<RollupMetric, number>> = {}\n\t\tinc[entry.metric] = entry.count\n\t\tawait bumpRollup(payload, entry.key, inc)\n\t}\n}\n"],"mappings":";;;;;;;AA6BA,eAAsB,WAAW,SAAkB,QAAsC;CACxF,IAAI,OAAO,WAAW,GACrB;CAED,MAAM,QAAQ,IAAI,OAAO,KAAK,UAAU,WAAW,SAAS,KAAK,CAAC,CAAC;CAEnE,MAAM,uBAAO,IAAI,IAAqB;CACtC,MAAM,6BAAa,IAAI,IAA+B;CACtD,KAAK,MAAM,SAAS,QACnB,KAAK,MAAM,SAAS,oBAAoB,KAAK,GAAG;EAC/C,MAAM,KAAK,UAAU,MAAM,GAAG;EAC9B,MAAM,MAAM,KAAK,IAAI,EAAE;EACvB,IAAI,KACH,KAAK,MAAM,UAAU,OAAO,KAAK,IAAI,GAAG,GACvC,IAAI,IAAI,WAAW,MAAM,IAAI;OAG9B,KAAK,IAAI,IAAI;GAAE,KAAK,MAAM;GAAK,KAAK,EAAE,GAAG,MAAM,IAAI;EAAE,CAAC;EAEvD,WAAW,IAAI,GAAG,GAAG,WAAW,MAAM,eAAe;GACpD,KAAK,MAAM;GACX,MAAM;GACN,OAAO,MAAM;EACd,CAAC;EACD,WAAW,IAAI,GAAG,GAAG,WAAW,MAAM,aAAa;GAClD,KAAK,MAAM;GACX,MAAM;GACN,OAAO,MAAM;EACd,CAAC;CACF;CAGD,KAAK,MAAM,OAAO,KAAK,OAAO,GAC7B,MAAM,WAAW,SAAS,IAAI,KAAK,IAAI,GAAG;CAG3C,MAAM,2BAAW,IAAI,IAAqE;CAC1F,KAAK,MAAM,aAAa,WAAW,OAAO,GAAG;EAO5C,IAAI,CAAC,MANe,YAAY,SAAA,kBAAoB;GACnD,QAAQ,UAAU,UAAU,GAAG;GAC/B,MAAM,UAAU;GAChB,OAAO,UAAU;GACjB,QAAQ,UAAU,IAAI;EACvB,CAAC,GAEA;EAED,MAAM,SAAuB,UAAU,SAAS,YAAY,aAAa;EACzE,MAAM,KAAK,GAAG,UAAU,UAAU,GAAG,EAAE,GAAG;EAC1C,MAAM,QAAQ,SAAS,IAAI,EAAE;EAC7B,IAAI,OACH,MAAM,SAAS;OAEf,SAAS,IAAI,IAAI;GAAE,KAAK,UAAU;GAAK;GAAQ,OAAO;EAAE,CAAC;CAE3D;CAEA,KAAK,MAAM,SAAS,SAAS,OAAO,GAAG;EACtC,MAAM,MAA6C,CAAC;EACpD,IAAI,MAAM,UAAU,MAAM;EAC1B,MAAM,WAAW,SAAS,MAAM,KAAK,GAAG;CACzC;AACD"}
@@ -1,34 +1,101 @@
1
+ import { clientIpFromHeaders } from "./clientIp.js";
2
+ import { matchGoals } from "../../goals/match.js";
1
3
  import { classifyDevice } from "./device.js";
2
4
  import { deriveSource } from "./source.js";
3
5
  import { dailyVisitorHash, deriveSessionId } from "./visitorHash.js";
4
6
  //#region src/native/ingest/normalizeEvent.ts
5
- async function normalizeEvent({ raw, headers, geoResolver, salt, now, scope, timezone }) {
7
+ const MAX_PROPS = 20;
8
+ const MAX_KEY_LENGTH = 64;
9
+ const MAX_VALUE_LENGTH = 256;
10
+ const MAX_NAME_LENGTH = 128;
11
+ const MAX_PATH_LENGTH = 512;
12
+ /** Longest legal DNS name. */
13
+ const MAX_HOSTNAME_LENGTH = 253;
14
+ /** 24 hours. A longer duration is a broken clock, not a session. */
15
+ const MAX_DURATION_MS = 864e5;
16
+ const CURRENCY = /^[A-Z]{3}$/;
17
+ /**
18
+ * Optional wire fields are sanitized rather than rejected: a bad `value`, `currency`,
19
+ * `scrollDepth`, `durationMs`, or prop is dropped and the event is still ingested, so one malformed
20
+ * attribute never costs a pageview. Only the required fields (checked in the endpoint)
21
+ * can fail an event.
22
+ */
23
+ const nonNegative = (value) => typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : void 0;
24
+ const currencyCode = (value) => typeof value === "string" && CURRENCY.test(value) ? value : void 0;
25
+ /**
26
+ * Infinity would poison a rollup bucket's average for good, and a string would throw out of
27
+ * the write and take the whole buffered batch with it, so a duration outside the ceiling is
28
+ * dropped like any other malformed attribute.
29
+ */
30
+ const duration = (value) => {
31
+ const n = nonNegative(value);
32
+ return n === void 0 || n > MAX_DURATION_MS ? void 0 : n;
33
+ };
34
+ /** The event name is a rollup bucket key (the `event` dimension), so it is length-capped. */
35
+ const eventName = (value) => typeof value === "string" ? value.slice(0, MAX_NAME_LENGTH) : void 0;
36
+ const depth = (value) => {
37
+ const n = nonNegative(value);
38
+ return n === void 0 ? void 0 : Math.round(Math.min(n, 100));
39
+ };
40
+ /** Flat JSON scalars only: the first 20 short-keyed string/number/boolean entries. */
41
+ const sanitizeProps = (raw) => {
42
+ if (typeof raw !== "object" || raw === null || Array.isArray(raw)) return;
43
+ const out = {};
44
+ let kept = 0;
45
+ for (const [key, value] of Object.entries(raw)) {
46
+ if (kept >= MAX_PROPS || key.length > MAX_KEY_LENGTH) continue;
47
+ if (typeof value === "string") out[key] = value.slice(0, MAX_VALUE_LENGTH);
48
+ else if (typeof value === "boolean" || typeof value === "number" && Number.isFinite(value)) out[key] = value;
49
+ else continue;
50
+ kept++;
51
+ }
52
+ return kept > 0 ? out : void 0;
53
+ };
54
+ async function normalizeEvent({ raw, headers, geoResolver, salt, now, scope, timezone, goals }) {
6
55
  const geo = await geoResolver(headers);
7
- const ip = (headers.get("x-forwarded-for")?.split(",")[0] ?? headers.get("x-real-ip") ?? "").trim();
56
+ const ip = clientIpFromHeaders(headers) ?? "";
8
57
  const ua = headers.get("user-agent") ?? "";
58
+ const path = raw.path.slice(0, MAX_PATH_LENGTH);
59
+ const hostname = raw.hostname.slice(0, MAX_HOSTNAME_LENGTH);
9
60
  const visitorHash = dailyVisitorHash({
10
61
  ip,
11
62
  ua,
12
- site: raw.hostname,
63
+ site: hostname,
13
64
  salt
14
65
  });
15
66
  const hourBucket = now.toISOString().slice(0, 13);
67
+ const props = sanitizeProps(raw.props);
68
+ const value = nonNegative(raw.value);
69
+ const scrollDepth = depth(raw.scrollDepth);
70
+ const durationMs = duration(raw.durationMs);
71
+ const name = eventName(raw.name);
72
+ const completions = goals?.length ? matchGoals({
73
+ type: raw.type,
74
+ name,
75
+ path,
76
+ props,
77
+ value
78
+ }, goals) : [];
16
79
  return {
17
80
  timestamp: now,
18
81
  type: raw.type,
19
- name: raw.name,
20
- path: raw.path,
21
- hostname: raw.hostname,
82
+ name,
83
+ path,
84
+ hostname,
22
85
  referrer: raw.referrer,
23
86
  device: classifyDevice(ua),
24
- source: deriveSource(raw.referrer, raw.hostname),
87
+ source: deriveSource(raw.referrer, hostname),
25
88
  country: geo.country,
26
89
  region: geo.region,
27
90
  city: geo.city,
28
91
  visitorHash,
29
92
  sessionId: deriveSessionId(visitorHash, hourBucket),
30
- durationMs: raw.durationMs,
31
- props: raw.props,
93
+ props,
94
+ ...durationMs !== void 0 ? { durationMs } : {},
95
+ ...value !== void 0 ? { value } : {},
96
+ ...currencyCode(raw.currency) !== void 0 ? { currency: raw.currency } : {},
97
+ ...scrollDepth !== void 0 ? { scrollDepth } : {},
98
+ ...completions.length > 0 ? { goals: completions } : {},
32
99
  ...scope !== void 0 ? { scope } : {},
33
100
  ...timezone !== void 0 ? { timezone } : {}
34
101
  };
@@ -1 +1 @@
1
- {"version":3,"file":"normalizeEvent.js","names":[],"sources":["../../../src/native/ingest/normalizeEvent.ts"],"sourcesContent":["import type { GeoResolver } from '../geo/geoResolver'\nimport { classifyDevice, type DeviceType } from './device'\nimport { deriveSource } from './source'\nimport { dailyVisitorHash, deriveSessionId } from './visitorHash'\n\nexport interface RawEventInput {\n\ttype: 'pageview' | 'event'\n\tname?: string\n\tpath: string\n\thostname: string\n\treferrer?: string\n\tdurationMs?: number\n\tprops?: Record<string, unknown>\n}\n\nexport interface StoredEvent {\n\ttimestamp: Date\n\ttype: 'pageview' | 'event'\n\tname?: string\n\tpath: string\n\thostname: string\n\treferrer?: string\n\tdevice?: DeviceType\n\tsource?: string\n\tcountry?: string\n\tregion?: string\n\tcity?: string\n\tvisitorHash: string\n\tsessionId: string\n\tdurationMs?: number\n\tprops?: Record<string, unknown>\n\t/** Set only in scoped installs; '' is the null scope. */\n\tscope?: string\n\t/**\n\t * Reporting timezone the rollup day bucket is computed in. In-memory only: stripped\n\t * before the event row is persisted (day bucketing happens at rollup time). Absent\n\t * means UTC.\n\t */\n\ttimezone?: string\n}\n\nexport interface NormalizeArgs {\n\traw: RawEventInput\n\theaders: Headers\n\tgeoResolver: GeoResolver\n\tsalt: string\n\tnow: Date\n\tscope?: string\n\ttimezone?: string\n}\n\nexport async function normalizeEvent({\n\traw,\n\theaders,\n\tgeoResolver,\n\tsalt,\n\tnow,\n\tscope,\n\ttimezone,\n}: NormalizeArgs): Promise<StoredEvent> {\n\tconst geo = await geoResolver(headers)\n\tconst ip = (\n\t\theaders.get('x-forwarded-for')?.split(',')[0] ??\n\t\theaders.get('x-real-ip') ??\n\t\t''\n\t).trim()\n\tconst ua = headers.get('user-agent') ?? ''\n\tconst visitorHash = dailyVisitorHash({ ip, ua, site: raw.hostname, salt })\n\tconst hourBucket = now.toISOString().slice(0, 13)\n\treturn {\n\t\ttimestamp: now,\n\t\ttype: raw.type,\n\t\tname: raw.name,\n\t\tpath: raw.path,\n\t\thostname: raw.hostname,\n\t\treferrer: raw.referrer,\n\t\tdevice: classifyDevice(ua),\n\t\tsource: deriveSource(raw.referrer, raw.hostname),\n\t\tcountry: geo.country,\n\t\tregion: geo.region,\n\t\tcity: geo.city,\n\t\tvisitorHash,\n\t\tsessionId: deriveSessionId(visitorHash, hourBucket),\n\t\tdurationMs: raw.durationMs,\n\t\tprops: raw.props,\n\t\t...(scope !== undefined ? { scope } : {}),\n\t\t...(timezone !== undefined ? { timezone } : {}),\n\t}\n}\n"],"mappings":";;;;AAmDA,eAAsB,eAAe,EACpC,KACA,SACA,aACA,MACA,KACA,OACA,YACuC;CACvC,MAAM,MAAM,MAAM,YAAY,OAAO;CACrC,MAAM,MACL,QAAQ,IAAI,iBAAiB,GAAG,MAAM,GAAG,EAAE,MAC3C,QAAQ,IAAI,WAAW,KACvB,IACC,KAAK;CACP,MAAM,KAAK,QAAQ,IAAI,YAAY,KAAK;CACxC,MAAM,cAAc,iBAAiB;EAAE;EAAI;EAAI,MAAM,IAAI;EAAU;CAAK,CAAC;CACzE,MAAM,aAAa,IAAI,YAAY,EAAE,MAAM,GAAG,EAAE;CAChD,OAAO;EACN,WAAW;EACX,MAAM,IAAI;EACV,MAAM,IAAI;EACV,MAAM,IAAI;EACV,UAAU,IAAI;EACd,UAAU,IAAI;EACd,QAAQ,eAAe,EAAE;EACzB,QAAQ,aAAa,IAAI,UAAU,IAAI,QAAQ;EAC/C,SAAS,IAAI;EACb,QAAQ,IAAI;EACZ,MAAM,IAAI;EACV;EACA,WAAW,gBAAgB,aAAa,UAAU;EAClD,YAAY,IAAI;EAChB,OAAO,IAAI;EACX,GAAI,UAAU,KAAA,IAAY,EAAE,MAAM,IAAI,CAAC;EACvC,GAAI,aAAa,KAAA,IAAY,EAAE,SAAS,IAAI,CAAC;CAC9C;AACD"}
1
+ {"version":3,"file":"normalizeEvent.js","names":[],"sources":["../../../src/native/ingest/normalizeEvent.ts"],"sourcesContent":["import { type GoalCompletion, matchGoals } from '../../goals/match'\nimport type { Goal } from '../../goals/types'\nimport type { GeoResolver } from '../geo/geoResolver'\nimport { clientIpFromHeaders } from './clientIp'\nimport { classifyDevice, type DeviceType } from './device'\nimport { deriveSource } from './source'\nimport { dailyVisitorHash, deriveSessionId } from './visitorHash'\n\nexport type EventType = 'pageview' | 'event' | 'goal'\n\nexport interface RawEventInput {\n\ttype: EventType\n\t/** Event name, or the goal slug on a `goal`. Absent on pageviews. */\n\tname?: string\n\tpath: string\n\thostname: string\n\treferrer?: string\n\tdurationMs?: number\n\tprops?: Record<string, unknown>\n\t/** Revenue for a goal completion, in `currency`. */\n\tvalue?: number\n\tcurrency?: string\n\t/** Max scroll depth reached on a pageview, 0-100. */\n\tscrollDepth?: number\n}\n\nexport interface StoredEvent {\n\ttimestamp: Date\n\ttype: EventType\n\tname?: string\n\tpath: string\n\thostname: string\n\treferrer?: string\n\tdevice?: DeviceType\n\tsource?: string\n\tcountry?: string\n\tregion?: string\n\tcity?: string\n\tvisitorHash: string\n\tsessionId: string\n\tdurationMs?: number\n\tprops?: Record<string, unknown>\n\tvalue?: number\n\t/** ISO 4217 code `value` is denominated in. Stored as sent; revenue is never converted. */\n\tcurrency?: string\n\tscrollDepth?: number\n\t/**\n\t * Goals this event completed, resolved against the install's goals at ingest so a later\n\t * goal edit never rewrites history and a raw-event read can re-derive the same numbers\n\t * the rollups hold.\n\t */\n\tgoals?: GoalCompletion[]\n\t/** Set only in scoped installs; '' is the null scope. */\n\tscope?: string\n\t/**\n\t * Reporting timezone the rollup day bucket is computed in. In-memory only: stripped\n\t * before the event row is persisted (day bucketing happens at rollup time). Absent\n\t * means UTC.\n\t */\n\ttimezone?: string\n}\n\nexport interface NormalizeArgs {\n\traw: RawEventInput\n\theaders: Headers\n\tgeoResolver: GeoResolver\n\tsalt: string\n\tnow: Date\n\tscope?: string\n\ttimezone?: string\n\t/** Goals to match this event against; omitted means no goal matching. */\n\tgoals?: Goal[]\n}\n\nconst MAX_PROPS = 20\nconst MAX_KEY_LENGTH = 64\nconst MAX_VALUE_LENGTH = 256\nconst MAX_NAME_LENGTH = 128\nconst MAX_PATH_LENGTH = 512\n/** Longest legal DNS name. */\nconst MAX_HOSTNAME_LENGTH = 253\n/** 24 hours. A longer duration is a broken clock, not a session. */\nconst MAX_DURATION_MS = 86_400_000\nconst CURRENCY = /^[A-Z]{3}$/\n\n/**\n * Optional wire fields are sanitized rather than rejected: a bad `value`, `currency`,\n * `scrollDepth`, `durationMs`, or prop is dropped and the event is still ingested, so one malformed\n * attribute never costs a pageview. Only the required fields (checked in the endpoint)\n * can fail an event.\n */\nconst nonNegative = (value: unknown): number | undefined =>\n\ttypeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : undefined\n\nconst currencyCode = (value: unknown): string | undefined =>\n\ttypeof value === 'string' && CURRENCY.test(value) ? value : undefined\n\n/**\n * Infinity would poison a rollup bucket's average for good, and a string would throw out of\n * the write and take the whole buffered batch with it, so a duration outside the ceiling is\n * dropped like any other malformed attribute.\n */\nconst duration = (value: unknown): number | undefined => {\n\tconst n = nonNegative(value)\n\treturn n === undefined || n > MAX_DURATION_MS ? undefined : n\n}\n\n/** The event name is a rollup bucket key (the `event` dimension), so it is length-capped. */\nconst eventName = (value: unknown): string | undefined =>\n\ttypeof value === 'string' ? value.slice(0, MAX_NAME_LENGTH) : undefined\n\nconst depth = (value: unknown): number | undefined => {\n\tconst n = nonNegative(value)\n\treturn n === undefined ? undefined : Math.round(Math.min(n, 100))\n}\n\n/** Flat JSON scalars only: the first 20 short-keyed string/number/boolean entries. */\nconst sanitizeProps = (raw: unknown): Record<string, unknown> | undefined => {\n\tif (typeof raw !== 'object' || raw === null || Array.isArray(raw)) {\n\t\treturn undefined\n\t}\n\tconst out: Record<string, unknown> = {}\n\tlet kept = 0\n\tfor (const [key, value] of Object.entries(raw)) {\n\t\tif (kept >= MAX_PROPS || key.length > MAX_KEY_LENGTH) {\n\t\t\tcontinue\n\t\t}\n\t\tif (typeof value === 'string') {\n\t\t\tout[key] = value.slice(0, MAX_VALUE_LENGTH)\n\t\t} else if (\n\t\t\ttypeof value === 'boolean' ||\n\t\t\t(typeof value === 'number' && Number.isFinite(value))\n\t\t) {\n\t\t\tout[key] = value\n\t\t} else {\n\t\t\tcontinue\n\t\t}\n\t\tkept++\n\t}\n\treturn kept > 0 ? out : undefined\n}\n\nexport async function normalizeEvent({\n\traw,\n\theaders,\n\tgeoResolver,\n\tsalt,\n\tnow,\n\tscope,\n\ttimezone,\n\tgoals,\n}: NormalizeArgs): Promise<StoredEvent> {\n\tconst geo = await geoResolver(headers)\n\tconst ip = clientIpFromHeaders(headers) ?? ''\n\tconst ua = headers.get('user-agent') ?? ''\n\t// Both are unique-index bucket keys on the rollup and seen rows, so they are capped\n\t// before anything derives from them: an uncapped path exceeds Mongo's index key limit.\n\tconst path = raw.path.slice(0, MAX_PATH_LENGTH)\n\tconst hostname = raw.hostname.slice(0, MAX_HOSTNAME_LENGTH)\n\tconst visitorHash = dailyVisitorHash({ ip, ua, site: hostname, salt })\n\tconst hourBucket = now.toISOString().slice(0, 13)\n\tconst props = sanitizeProps(raw.props)\n\tconst value = nonNegative(raw.value)\n\tconst scrollDepth = depth(raw.scrollDepth)\n\tconst durationMs = duration(raw.durationMs)\n\tconst name = eventName(raw.name)\n\t// Match on the sanitized fields so a rejected value never reaches a goal's revenue.\n\tconst completions = goals?.length\n\t\t? matchGoals({ type: raw.type, name, path, props, value }, goals)\n\t\t: []\n\treturn {\n\t\ttimestamp: now,\n\t\ttype: raw.type,\n\t\tname,\n\t\tpath,\n\t\thostname,\n\t\treferrer: raw.referrer,\n\t\tdevice: classifyDevice(ua),\n\t\tsource: deriveSource(raw.referrer, hostname),\n\t\tcountry: geo.country,\n\t\tregion: geo.region,\n\t\tcity: geo.city,\n\t\tvisitorHash,\n\t\tsessionId: deriveSessionId(visitorHash, hourBucket),\n\t\tprops,\n\t\t...(durationMs !== undefined ? { durationMs } : {}),\n\t\t...(value !== undefined ? { value } : {}),\n\t\t...(currencyCode(raw.currency) !== undefined ? { currency: raw.currency } : {}),\n\t\t...(scrollDepth !== undefined ? { scrollDepth } : {}),\n\t\t...(completions.length > 0 ? { goals: completions } : {}),\n\t\t...(scope !== undefined ? { scope } : {}),\n\t\t...(timezone !== undefined ? { timezone } : {}),\n\t}\n}\n"],"mappings":";;;;;;AA0EA,MAAM,YAAY;AAClB,MAAM,iBAAiB;AACvB,MAAM,mBAAmB;AACzB,MAAM,kBA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,4 @@
1
+ import "../plugin/paths.js";
1
2
  import { composeGeoResolvers } from "./geo/composeGeoResolvers.js";
2
3
  import { platformHeaderResolver } from "./geo/geoResolver.js";
3
4
  import { maxmindResolver } from "./geo/maxmindResolver.js";
@@ -17,14 +18,18 @@ const metrics = new Set([
17
18
  "visitors",
18
19
  "sessions",
19
20
  "events",
20
- "avgDuration"
21
+ "avgDuration",
22
+ "conversions",
23
+ "revenue",
24
+ "scrollDepth"
21
25
  ]);
22
26
  const dimensions = new Set([
23
27
  "page",
24
28
  "country",
25
29
  "source",
26
30
  "device",
27
- "event"
31
+ "event",
32
+ "goal"
28
33
  ]);
29
34
  const REALTIME_EVENT_LIMIT = 5e4;
30
35
  const baseCapabilities = {
@@ -54,6 +59,11 @@ const baseCapabilities = {
54
59
  }
55
60
  };
56
61
  const DAY_MS = 864e5;
62
+ const nativeCapture = {
63
+ proxy: { routes: [] },
64
+ snippet: () => ({ scripts: [] }),
65
+ client: { kind: "native" }
66
+ };
57
67
  /**
58
68
  * Filtered/hour-granularity reads bypass the day-bucketed rollups and aggregate raw
59
69
  * events directly, under the same hard cap as `realtime()`. `retentionDays` clamps how
@@ -129,10 +139,13 @@ function native(options = {}) {
129
139
  });
130
140
  return docs;
131
141
  };
142
+ const ingest = { path: options.ingestPath ?? "/analytics/ingest" };
132
143
  return {
133
144
  id: "native",
134
145
  label: "Native (Payload)",
135
146
  capabilities,
147
+ capture: nativeCapture,
148
+ ingest,
136
149
  isConfigured: () => true,
137
150
  flush: () => buffer?.flush() ?? Promise.resolve(),
138
151
  register(config, context) {
@@ -146,10 +159,11 @@ function native(options = {}) {
146
159
  ];
147
160
  config.endpoints = [...config.endpoints ?? [], {
148
161
  method: "post",
149
- path: options.ingestPath ?? "/analytics/ingest",
162
+ path: ingest.path,
150
163
  handler: makeIngestHandler(geoResolver, () => buffer, {
151
164
  scope: scoped ? context?.resolveScope : void 0,
152
- timezone: context?.resolveTimezone
165
+ timezone: context?.resolveTimezone,
166
+ goals: context?.resolveGoals
153
167
  })
154
168
  }];
155
169
  if (options.retentionDays && options.retentionDays > 0) config.jobs = {
@@ -270,6 +284,8 @@ function native(options = {}) {
270
284
  greater_than_equal: q.dateRange.start.toISOString(),
271
285
  less_than_equal: q.dateRange.end.toISOString()
272
286
  },
287
+ ...q.hostname ? { hostname: { equals: q.hostname } } : {},
288
+ ...q.path ? { path: { equals: q.path } } : {},
273
289
  ...scopeWhere(q)
274
290
  },
275
291
  limit: REALTIME_EVENT_LIMIT,
@@ -1 +1 @@
1
- {"version":3,"file":"nativeAdapter.js","names":["events"],"sources":["../../src/native/nativeAdapter.ts"],"sourcesContent":["import type { Config, Payload } from 'payload'\nimport type {\n\tAdapterContext,\n\tAnalyticsAdapter,\n\tAnalyticsCapabilities,\n\tAnalyticsQuery,\n\tAnalyticsResult,\n\tAnalyticsRow,\n\tDimensionKey,\n\tMetricKey,\n} from '../core/contract'\nimport { EVENTS_SLUG, eventsCollection } from './collections/events'\nimport { ROLLUPS_SLUG, rollupsCollection } from './collections/rollups'\nimport { seenCollection } from './collections/seen'\nimport { composeGeoResolvers } from './geo/composeGeoResolvers'\nimport { type GeoResolver, platformHeaderResolver } from './geo/geoResolver'\nimport { maxmindResolver } from './geo/maxmindResolver'\nimport { makeIngestHandler } from './ingest/endpoint'\nimport { flushBatch } from './ingest/flushBatch'\nimport type { StoredEvent } from './ingest/normalizeEvent'\nimport { createWriteBuffer, type WriteBuffer } from './ingest/writeBuffer'\nimport { aggregateEvents, type EventLike, filtersToWhere } from './query/eventAgg'\nimport { buildRealtime, type RealtimeEvent } from './realtime/buildRealtime'\nimport { pruneEventsTask } from './retention/pruneTask'\nimport {\n\ttype Acc,\n\tadd,\n\temptyAcc,\n\ttype RollupDoc,\n\tselectMetrics,\n\tseriesFromRollups,\n} from './rollupAcc'\n\nexport interface NativeOptions {\n\tgeoResolver?: GeoResolver\n\tgeoDbPath?: string\n\tingestPath?: string\n\tretentionDays?: number\n\t/** Opt-in in-process write batching. `true` uses defaults (maxSize 50, maxAgeMs 2000). */\n\tbuffer?: boolean | { maxSize?: number; maxAgeMs?: number }\n}\n\nexport type NativeAdapter = AnalyticsAdapter & { flush: () => Promise<void> }\n\nconst metrics: ReadonlySet<MetricKey> = new Set([\n\t'pageviews',\n\t'visitors',\n\t'sessions',\n\t'events',\n\t'avgDuration',\n])\nconst dimensions: ReadonlySet<DimensionKey> = new Set([\n\t'page',\n\t'country',\n\t'source',\n\t'device',\n\t'event',\n])\n\nconst REALTIME_EVENT_LIMIT = 50_000\n\nconst baseCapabilities: AnalyticsCapabilities = {\n\tperPageQuery: true,\n\trealtime: true,\n\trealtimeWindowMinutes: 60,\n\tcomparison: true,\n\tminGranularity: 'hour',\n\tmaxLookbackDays: null,\n\tmetrics,\n\tdimensions,\n\tfilters: new Set(['page', 'country', 'region', 'city', 'device', 'source', 'event']),\n\tfilterOperators: new Set(['eq', 'contains']),\n\tbatchPageReport: true,\n\trateLimit: null,\n\trecommendedTtl: { realtime: 10, aggregate: 300 },\n}\n\nconst DAY_MS = 86_400_000\n\ninterface QueryEventsContext {\n\tretentionDays?: number\n\tscopeWhere: (q: AnalyticsQuery) => Record<string, unknown>\n}\n\n/**\n * Filtered/hour-granularity reads bypass the day-bucketed rollups and aggregate raw\n * events directly, under the same hard cap as `realtime()`. `retentionDays` clamps how\n * far back the read can reach when it would otherwise ask for events the prune task has\n * already deleted (or will delete before they're queryable).\n */\nasync function queryEvents(\n\tpayload: Payload,\n\tq: AnalyticsQuery,\n\tctx: QueryEventsContext\n): Promise<AnalyticsResult> {\n\tconst fetchedAt = q.dateRange.end.toISOString()\n\tlet start = q.dateRange.start\n\tlet clamped = false\n\tif (ctx.retentionDays && ctx.retentionDays > 0) {\n\t\tconst floor = new Date(Date.now() - ctx.retentionDays * DAY_MS)\n\t\tif (start < floor) {\n\t\t\tstart = floor\n\t\t\tclamped = true\n\t\t}\n\t}\n\tconst baseWhere: Record<string, unknown> = {\n\t\ttimestamp: {\n\t\t\tgreater_than_equal: start.toISOString(),\n\t\t\tless_than_equal: q.dateRange.end.toISOString(),\n\t\t},\n\t\t...(q.hostname ? { hostname: { equals: q.hostname } } : {}),\n\t\t...(q.path ? { path: { equals: q.path } } : {}),\n\t\t...ctx.scopeWhere(q),\n\t}\n\t// filtersToWhere can also key off 'path' (a page filter); AND rather than spread so\n\t// q.path and a page filter both constrain the query instead of one overwriting the other.\n\tconst filterWhere = filtersToWhere(q.filters ?? [])\n\tconst where: Record<string, unknown> =\n\t\tObject.keys(filterWhere).length > 0 ? { and: [baseWhere, filterWhere] } : baseWhere\n\tconst { docs } = await payload.find({\n\t\tcollection: EVENTS_SLUG as never,\n\t\twhere: where as never,\n\t\t// Newest-first under a hard cap, same tradeoff as realtime(): a very busy site\n\t\t// with more events than the cap in range keeps its most recent activity.\n\t\tlimit: REALTIME_EVENT_LIMIT,\n\t\tpagination: false,\n\t\tdepth: 0,\n\t\tsort: '-timestamp',\n\t})\n\tconst events = docs as unknown as EventLike[]\n\tconst dim = q.dimensions?.find((d) => dimensions.has(d))\n\tconst granularity =\n\t\tq.granularity === 'hour' || q.granularity === 'day' ? q.granularity : undefined\n\tconst { rows, totals } = aggregateEvents(events, {\n\t\tmetrics: q.metrics,\n\t\tdimension: dim,\n\t\tgranularity,\n\t\ttimezone: q.timezone,\n\t\torder: q.order,\n\t\tlimit: q.limit,\n\t})\n\tconst meta: AnalyticsResult['meta'] = { provider: 'native', fetchedAt }\n\tif (clamped) meta.clamped = true\n\tif (events.length >= REALTIME_EVENT_LIMIT) meta.sampled = true\n\treturn { rows, totals, meta }\n}\n\nexport function native(options: NativeOptions = {}): NativeAdapter {\n\tconst geoResolver =\n\t\toptions.geoResolver ??\n\t\t(options.geoDbPath\n\t\t\t? composeGeoResolvers(platformHeaderResolver, maxmindResolver({ dbPath: options.geoDbPath }))\n\t\t\t: platformHeaderResolver)\n\t// Per-instance copy: register() flips scopedQueries when the install is scoped.\n\tconst capabilities: AnalyticsCapabilities = { ...baseCapabilities }\n\tlet payloadRef: Payload | null = null\n\tlet buffer: WriteBuffer<StoredEvent> | null = null\n\tlet scoped = false\n\n\tconst scopeWhere = (q: AnalyticsQuery): Record<string, unknown> =>\n\t\tscoped && q.scope !== undefined ? { scope: { equals: q.scope } } : {}\n\n\tconst readRollups = async (\n\t\tpayload: Payload,\n\t\twhere: Record<string, unknown>\n\t): Promise<RollupDoc[]> => {\n\t\tconst { docs } = await payload.find({\n\t\t\tcollection: ROLLUPS_SLUG as never,\n\t\t\twhere: where as never,\n\t\t\tlimit: 100_000,\n\t\t\tpagination: false,\n\t\t})\n\t\treturn docs as unknown as RollupDoc[]\n\t}\n\n\treturn {\n\t\tid: 'native',\n\t\tlabel: 'Native (Payload)',\n\t\tcapabilities,\n\t\tisConfigured: () => true,\n\t\tflush: () => buffer?.flush() ?? Promise.resolve(),\n\t\tregister(config: Config, context) {\n\t\t\tscoped = context?.scoped ?? false\n\t\t\tif (scoped) {\n\t\t\t\tcapabilities.scopedQueries = true\n\t\t\t}\n\t\t\tconfig.collections = [\n\t\t\t\t...(config.collections ?? []),\n\t\t\t\teventsCollection(scoped),\n\t\t\t\trollupsCollection(scoped),\n\t\t\t\tseenCollection(),\n\t\t\t]\n\t\t\tconfig.endpoints = [\n\t\t\t\t...(config.endpoints ?? []),\n\t\t\t\t{\n\t\t\t\t\tmethod: 'post',\n\t\t\t\t\tpath: options.ingestPath ?? '/analytics/ingest',\n\t\t\t\t\thandler: makeIngestHandler(geoResolver, () => buffer, {\n\t\t\t\t\t\tscope: scoped ? context?.resolveScope : undefined,\n\t\t\t\t\t\ttimezone: context?.resolveTimezone,\n\t\t\t\t\t}),\n\t\t\t\t},\n\t\t\t]\n\t\t\tif (options.retentionDays && options.retentionDays > 0) {\n\t\t\t\tconfig.jobs = {\n\t\t\t\t\t...config.jobs,\n\t\t\t\t\ttasks: [...(config.jobs?.tasks ?? []), pruneEventsTask(options.retentionDays)],\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst prevOnInit = config.onInit\n\t\t\t// payloadRef is set before the app's own onInit so consumer init code can\n\t\t\t// already query the native adapter.\n\t\t\tconfig.onInit = async (p) => {\n\t\t\t\tpayloadRef = p\n\t\t\t\tif (options.buffer) {\n\t\t\t\t\tconst cfg = options.buffer === true ? {} : options.buffer\n\t\t\t\t\tbuffer = createWriteBuffer<StoredEvent>({\n\t\t\t\t\t\tmaxSize: cfg.maxSize ?? 50,\n\t\t\t\t\t\tmaxAgeMs: cfg.maxAgeMs ?? 2000,\n\t\t\t\t\t\tonFlush: (batch) => flushBatch(p, batch),\n\t\t\t\t\t\tonError: (error) => {\n\t\t\t\t\t\t\tp.logger.error({ err: error, msg: 'analytics: write buffer flush failed' })\n\t\t\t\t\t\t},\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t\tawait prevOnInit?.(p)\n\t\t\t}\n\t\t},\n\t\tasync query(q: AnalyticsQuery): Promise<AnalyticsResult> {\n\t\t\tif (!payloadRef) {\n\t\t\t\tthrow new Error('analytics: native adapter queried before init')\n\t\t\t}\n\t\t\tif ((q.filters && q.filters.length > 0) || q.granularity === 'hour') {\n\t\t\t\treturn queryEvents(payloadRef, q, { retentionDays: options.retentionDays, scopeWhere })\n\t\t\t}\n\t\t\tconst fetchedAt = q.dateRange.end.toISOString()\n\t\t\tconst periodWhere = {\n\t\t\t\tgreater_than_equal: q.dateRange.start.toISOString(),\n\t\t\t\tless_than_equal: q.dateRange.end.toISOString(),\n\t\t\t}\n\t\t\tconst dim = q.dimensions?.find((d) => dimensions.has(d))\n\n\t\t\t// Distinct metrics (visitors/sessions) must not be summed across breakdown rows.\n\t\t\t// Totals always come from the non-dimensioned aggregate row. A dimensioned query\n\t\t\t// reports site-wide totals (path=''), a plain query uses the path-scoped row.\n\t\t\tconst totalsPath = dim ? '' : (q.path ?? '')\n\t\t\tconst totalsDocs = await readRollups(payloadRef, {\n\t\t\t\tgranularity: { equals: 'day' },\n\t\t\t\tdimension: { equals: '' },\n\t\t\t\tpath: { equals: totalsPath },\n\t\t\t\thostname: { equals: q.hostname ?? '' },\n\t\t\t\tperiod: periodWhere,\n\t\t\t\t...scopeWhere(q),\n\t\t\t})\n\t\t\tconst totalsAcc = emptyAcc()\n\t\t\tfor (const d of totalsDocs) {\n\t\t\t\tadd(totalsAcc, d)\n\t\t\t}\n\t\t\tconst totals = selectMetrics(totalsAcc, q.metrics)\n\n\t\t\tif (!dim) {\n\t\t\t\tif (q.granularity === 'day') {\n\t\t\t\t\treturn {\n\t\t\t\t\t\trows: seriesFromRollups(totalsDocs, q.metrics, q.timezone),\n\t\t\t\t\t\ttotals,\n\t\t\t\t\t\tmeta: { provider: 'native', fetchedAt },\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn { rows: [{ metrics: totals }], totals, meta: { provider: 'native', fetchedAt } }\n\t\t\t}\n\n\t\t\tconst breakdownWhere =\n\t\t\t\tdim === 'page'\n\t\t\t\t\t? {\n\t\t\t\t\t\t\tgranularity: { equals: 'day' },\n\t\t\t\t\t\t\tdimension: { equals: '' },\n\t\t\t\t\t\t\tpath: { not_equals: '' },\n\t\t\t\t\t\t\thostname: { equals: q.hostname ?? '' },\n\t\t\t\t\t\t\tperiod: periodWhere,\n\t\t\t\t\t\t\t...scopeWhere(q),\n\t\t\t\t\t\t}\n\t\t\t\t\t: {\n\t\t\t\t\t\t\tgranularity: { equals: 'day' },\n\t\t\t\t\t\t\tdimension: { equals: dim },\n\t\t\t\t\t\t\tpath: { equals: '' },\n\t\t\t\t\t\t\thostname: { equals: q.hostname ?? '' },\n\t\t\t\t\t\t\tperiod: periodWhere,\n\t\t\t\t\t\t\t...scopeWhere(q),\n\t\t\t\t\t\t}\n\t\t\tconst groups = new Map<string, Acc>()\n\t\t\tfor (const d of await readRollups(payloadRef, breakdownWhere)) {\n\t\t\t\tconst value = dim === 'page' ? d.path : d.dimvalue\n\t\t\t\tlet acc = groups.get(value)\n\t\t\t\tif (!acc) {\n\t\t\t\t\tacc = emptyAcc()\n\t\t\t\t\tgroups.set(value, acc)\n\t\t\t\t}\n\t\t\t\tadd(acc, d)\n\t\t\t}\n\t\t\tlet rows: AnalyticsRow[] = [...groups].map(([value, acc]) => ({\n\t\t\t\tdimensions: { [dim]: value } as Partial<Record<DimensionKey, string>>,\n\t\t\t\tmetrics: selectMetrics(acc, q.metrics),\n\t\t\t}))\n\t\t\tconst sortMetric = q.order?.metric ?? 'pageviews'\n\t\t\tconst direction = (q.order?.direction ?? 'desc') === 'asc' ? 1 : -1\n\t\t\trows.sort((a, b) => ((a.metrics[sortMetric] ?? 0) - (b.metrics[sortMetric] ?? 0)) * direction)\n\t\t\tif (q.limit) {\n\t\t\t\trows = rows.slice(0, q.limit)\n\t\t\t}\n\t\t\treturn { rows, totals, meta: { provider: 'native', fetchedAt } }\n\t\t},\n\t\tasync realtime(q: AnalyticsQuery, _ctx: AdapterContext): Promise<AnalyticsResult> {\n\t\t\tif (!payloadRef) {\n\t\t\t\tthrow new Error('analytics: native adapter queried before init')\n\t\t\t}\n\t\t\tconst fetchedAt = q.dateRange.end.toISOString()\n\t\t\tconst { docs } = await payloadRef.find({\n\t\t\t\tcollection: EVENTS_SLUG as never,\n\t\t\t\twhere: {\n\t\t\t\t\ttimestamp: {\n\t\t\t\t\t\tgreater_than_equal: q.dateRange.start.toISOString(),\n\t\t\t\t\t\tless_than_equal: q.dateRange.end.toISOString(),\n\t\t\t\t\t},\n\t\t\t\t\t...scopeWhere(q),\n\t\t\t\t} as never,\n\t\t\t\t// Newest-first under a hard cap: if a very busy site has more events than the\n\t\t\t\t// cap in the window, keep the most recent activity rather than the oldest.\n\t\t\t\t// Unbounded accuracy via DB-side aggregation is deferred to the hardening spec.\n\t\t\t\tlimit: REALTIME_EVENT_LIMIT,\n\t\t\t\tpagination: false,\n\t\t\t\tdepth: 0,\n\t\t\t\tsort: '-timestamp',\n\t\t\t})\n\t\t\tconst events = docs as unknown as RealtimeEvent[]\n\t\t\tconst { rows, totals } = buildRealtime(events, q.dateRange, q.metrics)\n\t\t\treturn { rows,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;EAChE;CACD;AACD"}
1
+ {"version":3,"file":"nativeAdapter.js","names":["events"],"sources":["../../src/native/nativeAdapter.ts"],"sourcesContent":["import type { Config, Payload } from 'payload'\nimport type { CaptureSupport } from '../core/capture'\nimport type {\n\tAdapterContext,\n\tAnalyticsAdapter,\n\tAnalyticsCapabilities,\n\tAnalyticsQuery,\n\tAnalyticsResult,\n\tAnalyticsRow,\n\tDimensionKey,\n\tMetricKey,\n} from '../core/contract'\nimport { INGEST_PATH } from '../plugin/paths'\nimport { EVENTS_SLUG, eventsCollection } from './collections/events'\nimport { ROLLUPS_SLUG, rollupsCollection } from './collections/rollups'\nimport { seenCollection } from './collections/seen'\nimport { composeGeoResolvers } from './geo/composeGeoResolvers'\nimport { type GeoResolver, platformHeaderResolver } from './geo/geoResolver'\nimport { maxmindResolver } from './geo/maxmindResolver'\nimport { makeIngestHandler } from './ingest/endpoint'\nimport { flushBatch } from './ingest/flushBatch'\nimport type { StoredEvent } from './ingest/normalizeEvent'\nimport { createWriteBuffer, type WriteBuffer } from './ingest/writeBuffer'\nimport { aggregateEvents, type EventLike, filtersToWhere } from './query/eventAgg'\nimport { buildRealtime, type RealtimeEvent } from './realtime/buildRealtime'\nimport { pruneEventsTask } from './retention/pruneTask'\nimport {\n\ttype Acc,\n\tadd,\n\temptyAcc,\n\ttype RollupDoc,\n\tselectMetrics,\n\tseriesFromRollups,\n} from './rollupAcc'\n\nexport interface NativeOptions {\n\tgeoResolver?: GeoResolver\n\tgeoDbPath?: string\n\tingestPath?: string\n\tretentionDays?: number\n\t/** Opt-in in-process write batching. `true` uses defaults (maxSize 50, maxAgeMs 2000). */\n\tbuffer?: boolean | { maxSize?: number; maxAgeMs?: number }\n}\n\nexport type NativeAdapter = AnalyticsAdapter & { flush: () => Promise<void> }\n\nconst metrics: ReadonlySet<MetricKey> = new Set([\n\t'pageviews',\n\t'visitors',\n\t'sessions',\n\t'events',\n\t'avgDuration',\n\t'conversions',\n\t'revenue',\n\t'scrollDepth',\n])\nconst dimensions: ReadonlySet<DimensionKey> = new Set([\n\t'page',\n\t'country',\n\t'source',\n\t'device',\n\t'event',\n\t'goal',\n])\n\nconst REALTIME_EVENT_LIMIT = 50_000\n\nconst baseCapabilities: AnalyticsCapabilities = {\n\tperPageQuery: true,\n\trealtime: true,\n\trealtimeWindowMinutes: 60,\n\tcomparison: true,\n\tminGranularity: 'hour',\n\tmaxLookbackDays: null,\n\tmetrics,\n\tdimensions,\n\tfilters: new Set(['page', 'country', 'region', 'city', 'device', 'source', 'event']),\n\tfilterOperators: new Set(['eq', 'contains']),\n\tbatchPageReport: true,\n\trateLimit: null,\n\trecommendedTtl: { realtime: 10, aggregate: 300 },\n}\n\nconst DAY_MS = 86_400_000\n\n// The tracker itself is the client (an RSC renders TrackerBoot), so there is nothing to\n// proxy or inject; the ingest path is carried by the tracker config resolver instead.\nconst nativeCapture: CaptureSupport = {\n\tproxy: { routes: [] },\n\tsnippet: () => ({ scripts: [] }),\n\tclient: { kind: 'native' },\n}\n\ninterface QueryEventsContext {\n\tretentionDays?: number\n\tscopeWhere: (q: AnalyticsQuery) => Record<string, unknown>\n}\n\n/**\n * Filtered/hour-granularity reads bypass the day-bucketed rollups and aggregate raw\n * events directly, under the same hard cap as `realtime()`. `retentionDays` clamps how\n * far back the read can reach when it would otherwise ask for events the prune task has\n * already deleted (or will delete before they're queryable).\n */\nasync function queryEvents(\n\tpayload: Payload,\n\tq: AnalyticsQuery,\n\tctx: QueryEventsContext\n): Promise<AnalyticsResult> {\n\tconst fetchedAt = q.dateRange.end.toISOString()\n\tlet start = q.dateRange.start\n\tlet clamped = false\n\tif (ctx.retentionDays && ctx.retentionDays > 0) {\n\t\tconst floor = new Date(Date.now() - ctx.retentionDays * DAY_MS)\n\t\tif (start < floor) {\n\t\t\tstart = floor\n\t\t\tclamped = true\n\t\t}\n\t}\n\tconst baseWhere: Record<string, unknown> = {\n\t\ttimestamp: {\n\t\t\tgreater_than_equal: start.toISOString(),\n\t\t\tless_than_equal: q.dateRange.end.toISOString(),\n\t\t},\n\t\t...(q.hostname ? { hostname: { equals: q.hostname } } : {}),\n\t\t...(q.path ? { path: { equals: q.path } } : {}),\n\t\t...ctx.scopeWhere(q),\n\t}\n\t// filtersToWhere can also key off 'path' (a page filter); AND rather than spread so\n\t// q.path and a page filter both constrain the query instead of one overwriting the other.\n\tconst filterWhere = filtersToWhere(q.filters ?? [])\n\tconst where: Record<string, unknown> =\n\t\tObject.keys(filterWhere).length > 0 ? { and: [baseWhere, filterWhere] } : baseWhere\n\tconst { docs } = await payload.find({\n\t\tcollection: EVENTS_SLUG as never,\n\t\twhere: where as never,\n\t\t// Newest-first under a hard cap, same tradeoff as realtime(): a very busy site\n\t\t// with more events than the cap in range keeps its most recent activity.\n\t\tlimit: REALTIME_EVENT_LIMIT,\n\t\tpagination: false,\n\t\tdepth: 0,\n\t\tsort: '-timestamp',\n\t})\n\tconst events = docs as unknown as EventLike[]\n\tconst dim = q.dimensions?.find((d) => dimensions.has(d))\n\tconst granularity =\n\t\tq.granularity === 'hour' || q.granularity === 'day' ? q.granularity : undefined\n\tconst { rows, totals } = aggregateEvents(events, {\n\t\tmetrics: q.metrics,\n\t\tdimension: dim,\n\t\tgranularity,\n\t\ttimezone: q.timezone,\n\t\torder: q.order,\n\t\tlimit: q.limit,\n\t})\n\tconst meta: AnalyticsResult['meta'] = { provider: 'native', fetchedAt }\n\tif (clamped) meta.clamped = true\n\tif (events.length >= REALTIME_EVENT_LIMIT) meta.sampled = true\n\treturn { rows, totals, meta }\n}\n\nexport function native(options: NativeOptions = {}): NativeAdapter {\n\tconst geoResolver =\n\t\toptions.geoResolver ??\n\t\t(options.geoDbPath\n\t\t\t? composeGeoResolvers(platformHeaderResolver, maxmindResolver({ dbPath: options.geoDbPath }))\n\t\t\t: platformHeaderResolver)\n\t// Per-instance copy: register() flips scopedQueries when the install is scoped.\n\tconst capabilities: AnalyticsCapabilities = { ...baseCapabilities }\n\tlet payloadRef: Payload | null = null\n\tlet buffer: WriteBuffer<StoredEvent> | null = null\n\tlet scoped = false\n\n\tconst scopeWhere = (q: AnalyticsQuery): Record<string, unknown> =>\n\t\tscoped && q.scope !== undefined ? { scope: { equals: q.scope } } : {}\n\n\tconst readRollups = async (\n\t\tpayload: Payload,\n\t\twhere: Record<string, unknown>\n\t): Promise<RollupDoc[]> => {\n\t\tconst { docs } = await payload.find({\n\t\t\tcollection: ROLLUPS_SLUG as never,\n\t\t\twhere: where as never,\n\t\t\tlimit: 100_000,\n\t\t\tpagination: false,\n\t\t})\n\t\treturn docs as unknown as RollupDoc[]\n\t}\n\n\tconst ingest = { path: options.ingestPath ?? INGEST_PATH }\n\n\treturn {\n\t\tid: 'native',\n\t\tlabel: 'Native (Payload)',\n\t\tcapabilities,\n\t\tcapture: nativeCapture,\n\t\tingest,\n\t\tisConfigured: () => true,\n\t\tflush: () => buffer?.flush() ?? Promise.resolve(),\n\t\tregister(config: Config, context) {\n\t\t\tscoped = context?.scoped ?? false\n\t\t\tif (scoped) {\n\t\t\t\tcapabilities.scopedQueries = true\n\t\t\t}\n\t\t\tconfig.collections = [\n\t\t\t\t...(config.collections ?? []),\n\t\t\t\teventsCollection(scoped),\n\t\t\t\trollupsCollection(scoped),\n\t\t\t\tseenCollection(),\n\t\t\t]\n\t\t\tconfig.endpoints = [\n\t\t\t\t...(config.endpoints ?? []),\n\t\t\t\t{\n\t\t\t\t\tmethod: 'post',\n\t\t\t\t\tpath: ingest.path,\n\t\t\t\t\thandler: makeIngestHandler(geoResolver, () => buffer, {\n\t\t\t\t\t\tscope: scoped ? context?.resolveScope : undefined,\n\t\t\t\t\t\ttimezone: context?.resolveTimezone,\n\t\t\t\t\t\tgoals: context?.resolveGoals,\n\t\t\t\t\t}),\n\t\t\t\t},\n\t\t\t]\n\t\t\tif (options.retentionDays && options.retentionDays > 0) {\n\t\t\t\tconfig.jobs = {\n\t\t\t\t\t...config.jobs,\n\t\t\t\t\ttasks: [...(config.jobs?.tasks ?? []), pruneEventsTask(options.retentionDays)],\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst prevOnInit = config.onInit\n\t\t\t// payloadRef is set before the app's own onInit so consumer init code can\n\t\t\t// already query the native adapter.\n\t\t\tconfig.onInit = async (p) => {\n\t\t\t\tpayloadRef = p\n\t\t\t\tif (options.buffer) {\n\t\t\t\t\tconst cfg = options.buffer === true ? {} : options.buffer\n\t\t\t\t\tbuffer = createWriteBuffer<StoredEvent>({\n\t\t\t\t\t\tmaxSize: cfg.maxSize ?? 50,\n\t\t\t\t\t\tmaxAgeMs: cfg.maxAgeMs ?? 2000,\n\t\t\t\t\t\tonFlush: (batch) => flushBatch(p, batch),\n\t\t\t\t\t\tonError: (error) => {\n\t\t\t\t\t\t\tp.logger.error({ err: error, msg: 'analytics: write buffer flush failed' })\n\t\t\t\t\t\t},\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t\tawait prevOnInit?.(p)\n\t\t\t}\n\t\t},\n\t\tasync query(q: AnalyticsQuery): Promise<AnalyticsResult> {\n\t\t\tif (!payloadRef) {\n\t\t\t\tthrow new Error('analytics: native adapter queried before init')\n\t\t\t}\n\t\t\tif ((q.filters && q.filters.length > 0) || q.granularity === 'hour') {\n\t\t\t\treturn queryEvents(payloadRef, q, { retentionDays: options.retentionDays, scopeWhere })\n\t\t\t}\n\t\t\tconst fetchedAt = q.dateRange.end.toISOString()\n\t\t\tconst periodWhere = {\n\t\t\t\tgreater_than_equal: q.dateRange.start.toISOString(),\n\t\t\t\tless_than_equal: q.dateRange.end.toISOString(),\n\t\t\t}\n\t\t\tconst dim = q.dimensions?.find((d) => dimensions.has(d))\n\n\t\t\t// Distinct metrics (visitors/sessions) must not be summed across breakdown rows.\n\t\t\t// Totals always come from the non-dimensioned aggregate row. A dimensioned query\n\t\t\t// reports site-wide totals (path=''), a plain query uses the path-scoped row.\n\t\t\tconst totalsPath = dim ? '' : (q.path ?? '')\n\t\t\tconst totalsDocs = await readRollups(payloadRef, {\n\t\t\t\tgranularity: { equals: 'day' },\n\t\t\t\tdimension: { equals: '' },\n\t\t\t\tpath: { equals: totalsPath },\n\t\t\t\thostname: { equals: q.hostname ?? '' },\n\t\t\t\tperiod: periodWhere,\n\t\t\t\t...scopeWhere(q),\n\t\t\t})\n\t\t\tconst totalsAcc = emptyAcc()\n\t\t\tfor (const d of totalsDocs) {\n\t\t\t\tadd(totalsAcc, d)\n\t\t\t}\n\t\t\tconst totals = selectMetrics(totalsAcc, q.metrics)\n\n\t\t\tif (!dim) {\n\t\t\t\tif (q.granularity === 'day') {\n\t\t\t\t\treturn {\n\t\t\t\t\t\trows: seriesFromRollups(totalsDocs, q.metrics, q.timezone),\n\t\t\t\t\t\ttotals,\n\t\t\t\t\t\tmeta: { provider: 'native', fetchedAt },\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn { rows: [{ metrics: totals }], totals, meta: { provider: 'native', fetchedAt } }\n\t\t\t}\n\n\t\t\tconst breakdownWhere =\n\t\t\t\tdim === 'page'\n\t\t\t\t\t? {\n\t\t\t\t\t\t\tgranularity: { equals: 'day' },\n\t\t\t\t\t\t\tdimension: { equals: '' },\n\t\t\t\t\t\t\tpath: { not_equals: '' },\n\t\t\t\t\t\t\thostname: { equals: q.hostname ?? '' },\n\t\t\t\t\t\t\tperiod: periodWhere,\n\t\t\t\t\t\t\t...scopeWhere(q),\n\t\t\t\t\t\t}\n\t\t\t\t\t: {\n\t\t\t\t\t\t\tgranularity: { equals: 'day' },\n\t\t\t\t\t\t\tdimension: { equals: dim },\n\t\t\t\t\t\t\tpath: { equals: '' },\n\t\t\t\t\t\t\thostname: { equals: q.hostname ?? '' },\n\t\t\t\t\t\t\tperiod: periodWhere,\n\t\t\t\t\t\t\t...scopeWhere(q),\n\t\t\t\t\t\t}\n\t\t\tconst groups = new Map<string, Acc>()\n\t\t\tfor (const d of await readRollups(payloadRef, breakdownWhere)) {\n\t\t\t\tconst value = dim === 'page' ? d.path : d.dimvalue\n\t\t\t\tlet acc = groups.get(value)\n\t\t\t\tif (!acc) {\n\t\t\t\t\tacc = emptyAcc()\n\t\t\t\t\tgroups.set(value, acc)\n\t\t\t\t}\n\t\t\t\tadd(acc, d)\n\t\t\t}\n\t\t\tlet rows: AnalyticsRow[] = [...groups].map(([value, acc]) => ({\n\t\t\t\tdimensions: { [dim]: value } as Partial<Record<DimensionKey, string>>,\n\t\t\t\tmetrics: selectMetrics(acc, q.metrics),\n\t\t\t}))\n\t\t\tconst sortMetric = q.order?.metric ?? 'pageviews'\n\t\t\tconst direction = (q.order?.direction ?? 'desc') === 'asc' ? 1 : -1\n\t\t\trows.sort((a, b) => ((a.metrics[sortMetric] ?? 0) - (b.metrics[sortMetric] ?? 0)) * direction)\n\t\t\tif (q.limit) {\n\t\t\t\trows = rows.slice(0, q.limit)\n\t\t\t}\n\t\t\treturn { rows, totals, meta: { provider: 'native', fetchedAt } }\n\t\t},\n\t\tasync realtime(q: AnalyticsQuery, _ctx: AdapterContext): Promise<AnalyticsResult> {\n\t\t\tif (!payloadRef) {\n\t\t\t\tthrow new Error('analytics: native adapter queried before init')\n\t\t\t}\n\t\t\tconst fetchedAt = q.dateRange.end.toISOString()\n\t\t\tconst { docs } = await payloadRef.find({\n\t\t\t\tcollection: EVENTS_SLUG as never,\n\t\t\t\twhere: {\n\t\t\t\t\ttimestamp: {\n\t\t\t\t\t\tgreater_than_equal: q.dateRange.start.toISOString(),\n\t\t\t\t\t\tless_than_equal: q.dateRange.end.toISOString(),\n\t\t\t\t\t},\n\t\t\t\t\t...(q.hostname ? { hostname: { equals: q.hostname } } : {}),\n\t\t\t\t\t...(q.path ? { path: { equals: q.path } } : {}),\n\t\t\t\t\t...scopeWhere(q),\n\t\t\t\t} as never,\n\t\t\t\t// Newest-first under a hard cap: if a very busy site has more events than the\n\t\t\t\t// cap in the window, keep the most recent activity rather than the oldest.\n\t\t\t\t// Unbounded accuracy via DB-side aggregation is deferred to the hardening spec.\n\t\t\t\tlimit: REALTIME_EVENT_LIMIT,\n\t\t\t\tpagination: false,\n\t\t\t\tdepth: 0,\n\t\t\t\tsort: '-timestamp',\n\t\t\t})\n\t\t\tconst events = docs as unknown as RealtimeEvent[]\n\t\t\tconst { rows, totals } = buildRealtime(events, q.dateRange, q.metrics)\n\t\t\treturn { rows, totals, meta: { provider: 'native', fetchedAt } }\n\t\t},\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;AA8CA,MAAM,UAAkC,IAAI,IAAI;CAC/C;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACD,CAAC;AACD,MAAM,aAAwC,IAAI,IAAI;CACrD;CACA;CACA;CACA;CACA;CACA;AACD,CAAC;AAED,MAAM,uBAAuB;AAE7B,MAAM,mBAA0C;CAC/C,cAAc;CACd,UAAU;CACV,uBAAuB;CACvB,YAAY;CACZ,gBAAgB;CAChB,iBAAiB;CACjB;CACA;CACA,SAAS,IAAI,IAAI;EAAC;EAAQ;EAAW;EAAU;EAAQ;EAAU;EAAU;CAAO,CAAC;CACnF,iBAAiB,IAAI,IAAI,CAAC,MAAM,UAAU,CAAC;CAC3C,iBAAiB;CACjB,WAAW;CACX,gBAAgB;EAAE,UAAU;EAAI,WAAW;CAAI;AAChD;AAEA,MAAM,SAAS;AAIf,MAAM,gBAAgC;CACrC,OAAO,EAAE,QAAQ,CAAC,EAAE;CACpB,gBAAgB,EAAE,SAAS,CAAC,EAAE;CAC9B,QAAQ,EAAE,MAAM,SAAS;AAC1B;;;;;;;AAaA,eAAe,YACd,SACA,GACA,KAC2B;CAC3B,MAAM,YAAY,EAAE,UAAU,IAAI,YAAY;CAC9C,IAAI,QAAQ,EAAE,UAAU;CACxB,IAAI,UAAU;CACd,IAAI,IAAI,iBAAiB,IAAI,gBAAgB,GAAG;EAC/C,MAAM,wBAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,IAAI,gBAAgB,MAAM;EAC9D,IAAI,QAAQ,OAAO;GAClB,QAAQ;GACR,UAAU;EACX;CACD;CACA,MAAM,YAAqC;EAC1C,WAAW;GACV,oBAAoB,MAAM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AY;GAC9C,MAAM,EAAE,SAAS,MAAM,WAAW,KAAK;IACtC,YAAY;IACZ,OAAO;KACN,WAAW;MACV,oBAAoB,EAAE,UAAU,MAAM,YAAY;MAClD,iBAAiB,EAAE,UAAU,IAAI,YAAY;KAC9C;KACA,GAAI,EAAE,WAAW,EAAE,UAAU,EAAE,QAAQ,EAAE,SAAS,EAAE,IAAI,CAAC;KACzD,GAAI,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,IAAI,CAAC;KAC7C,GAAG,WAAW,CAAC;IAChB;IAIA,OAAO;IACP,YAAY;IACZ,OAAO;IACP,MAAM;GACP,CAAC;GAED,MAAM,EAAE,MAAM,WAAW,cAAcA,MAAQ,EAAE,WAAW,EAAE,OAAO;GACrE,OAAO;IAAE;IAAM;IAAQ,MAAM;KAAE,UAAU;KAAU;IAAU;GAAE;EAChE;CACD;AACD"}
@@ -21,33 +21,48 @@ const FILTER_OPERATOR = {
21
21
  * this is just the safety net so a stray unsupported filter never throws.
22
22
  * `contains` is case-insensitive on both DBs; on Postgres its value is a raw ILIKE
23
23
  * pattern (`%`/`_` act as wildcards), while Mongo regex-escapes the value first.
24
+ * One fragment per filter under `and`, not merged by key: two filters on the same field
25
+ * (e.g. two `eq` values) must both constrain the query rather than the later one silently
26
+ * overwriting the earlier one.
24
27
  */
25
28
  const filtersToWhere = (filters) => {
26
- const where = {};
29
+ const fragments = [];
27
30
  for (const filter of filters) {
28
31
  const field = EVENT_FIELD[filter.dimension];
29
32
  if (!field || filter.operator === "matches") continue;
30
33
  const op = FILTER_OPERATOR[filter.operator];
31
34
  if (!op) continue;
32
- where[field] = {
33
- ...where[field],
34
- [op]: filter.value
35
- };
35
+ fragments.push({ [field]: { [op]: filter.value } });
36
36
  }
37
- return where;
37
+ return fragments.length > 0 ? { and: fragments } : {};
38
38
  };
39
39
  const emptyBucket = () => ({
40
40
  pageviews: 0,
41
41
  events: 0,
42
42
  durationMs: 0,
43
43
  visitors: /* @__PURE__ */ new Set(),
44
- sessions: /* @__PURE__ */ new Set()
44
+ sessions: /* @__PURE__ */ new Set(),
45
+ conversions: 0,
46
+ revenue: 0,
47
+ scrollDepthSum: 0,
48
+ scrollSamples: 0
45
49
  });
46
- const addEvent = (bucket, event) => {
50
+ /**
51
+ * `completions` narrows what the event contributes to conversions/revenue: a goal breakdown
52
+ * row counts only its own goal, every other bucket counts all of them. Mirrors how
53
+ * computeRollupDeltas fills a `goal` bucket versus a site or page bucket.
54
+ */
55
+ const addEvent = (bucket, event, completions = event.goals ?? []) => {
47
56
  if (event.type === "pageview") {
48
57
  bucket.pageviews++;
49
58
  bucket.durationMs += event.durationMs ?? 0;
50
59
  } else bucket.events++;
60
+ if (event.scrollDepth !== void 0) {
61
+ bucket.scrollDepthSum += event.scrollDepth;
62
+ bucket.scrollSamples++;
63
+ }
64
+ bucket.conversions += completions.length;
65
+ for (const completion of completions) bucket.revenue += completion.value;
51
66
  bucket.visitors.add(event.visitorHash);
52
67
  bucket.sessions.add(event.sessionId);
53
68
  };
@@ -57,7 +72,10 @@ const selectMetrics = (bucket, wanted) => {
57
72
  if (wanted.includes("events")) out.events = bucket.events;
58
73
  if (wanted.includes("visitors")) out.visitors = bucket.visitors.size;
59
74
  if (wanted.includes("sessions")) out.sessions = bucket.sessions.size;
75
+ if (wanted.includes("conversions")) out.conversions = bucket.conversions;
76
+ if (wanted.includes("revenue")) out.revenue = bucket.revenue;
60
77
  if (wanted.includes("avgDuration")) out.avgDuration = bucket.pageviews > 0 ? Math.round(bucket.durationMs / bucket.pageviews) : 0;
78
+ if (wanted.includes("scrollDepth")) out.scrollDepth = bucket.scrollSamples > 0 ? Math.round(bucket.scrollDepthSum / bucket.scrollSamples) : 0;
61
79
  return out;
62
80
  };
63
81
  /**
@@ -80,17 +98,25 @@ const aggregateEvents = (events, { metrics, dimension, granularity, timezone = "
80
98
  for (const event of events) addEvent(totalsBucket, event);
81
99
  const totals = selectMetrics(totalsBucket, metrics);
82
100
  const field = dimension ? EVENT_FIELD[dimension] : void 0;
83
- if (dimension && field) {
101
+ if (dimension && (field || dimension === "goal")) {
84
102
  const groups = /* @__PURE__ */ new Map();
85
- for (const event of events) {
86
- const value = event[field];
87
- if (!value) continue;
103
+ const bucketFor = (value) => {
88
104
  let bucket = groups.get(value);
89
105
  if (!bucket) {
90
106
  bucket = emptyBucket();
91
107
  groups.set(value, bucket);
92
108
  }
93
- addEvent(bucket, event);
109
+ return bucket;
110
+ };
111
+ for (const event of events) {
112
+ if (dimension === "goal") {
113
+ for (const completion of event.goals ?? []) addEvent(bucketFor(completion.slug), event, [completion]);
114
+ continue;
115
+ }
116
+ if (!field || field === "name" && event.type !== "event") continue;
117
+ const value = event[field];
118
+ if (!value) continue;
119
+ addEvent(bucketFor(value), event);
94
120
  }
95
121
  let rows = [...groups].map(([value, bucket]) => ({
96
122
  dimensions: { [dimension]: value },
@@ -1 +1 @@
1
- {"version":3,"file":"eventAgg.js","names":[],"sources":["../../../src/native/query/eventAgg.ts"],"sourcesContent":["import type { AnalyticsFilter, AnalyticsRow, DimensionKey, MetricKey } from '../../core/contract'\nimport { DEFAULT_TIMEZONE, startOfDayInTz } from '../../timeframe/tz'\n\n/** Raw event shape aggregateEvents/filtersToWhere operate on; matches StoredEvent as read back from the events collection (timestamp comes back as an ISO string). */\nexport interface EventLike {\n\ttimestamp: string | Date\n\ttype: 'pageview' | 'event'\n\tname?: string\n\tpath: string\n\tdevice?: string\n\tsource?: string\n\tcountry?: string\n\tregion?: string\n\tcity?: string\n\tvisitorHash: string\n\tsessionId: string\n\tdurationMs?: number\n}\n\nconst HOUR_MS = 3_600_000\n\ntype EventStringField = 'path' | 'country' | 'region' | 'city' | 'device' | 'source' | 'name'\n\n/** Dimensions native can filter/group on directly against the events collection. */\nconst EVENT_FIELD: Partial<Record<DimensionKey, EventStringField>> = {\n\tpage: 'path',\n\tcountry: 'country',\n\tregion: 'region',\n\tcity: 'city',\n\tdevice: 'device',\n\tsource: 'source',\n\tevent: 'name',\n}\n\nconst FILTER_OPERATOR: Record<'eq' | 'contains', string> = { eq: 'equals', contains: 'contains' }\n\n/**\n * Payload where fragment for supported filters; unsupported dimensions/operators are\n * dropped. Capability gating (`filters`/`filterOperators`) is the real contract upstream,\n * this is just the safety net so a stray unsupported filter never throws.\n * `contains` is case-insensitive on both DBs; on Postgres its value is a raw ILIKE\n * pattern (`%`/`_` act as wildcards), while Mongo regex-escapes the value first.\n */\nexport const filtersToWhere = (filters: AnalyticsFilter[]): Record<string, unknown> => {\n\tconst where: Record<string, unknown> = {}\n\tfor (const filter of filters) {\n\t\tconst field = EVENT_FIELD[filter.dimension]\n\t\tif (!field || filter.operator === 'matches') {\n\t\t\tcontinue\n\t\t}\n\t\tconst op = FILTER_OPERATOR[filter.operator]\n\t\tif (!op) {\n\t\t\tcontinue\n\t\t}\n\t\twhere[field] = { ...(where[field] as object | undefined), [op]: filter.value }\n\t}\n\treturn where\n}\n\ninterface Bucket {\n\tpageviews: number\n\tevents: number\n\tdurationMs: number\n\tvisitors: Set<string>\n\tsessions: Set<string>\n}\n\nconst emptyBucket = (): Bucket => ({\n\tpageviews: 0,\n\tevents: 0,\n\tdurationMs: 0,\n\tvisitors: new Set(),\n\tsessions: new Set(),\n})\n\nconst addEvent = (bucket: Bucket, event: EventLike): void => {\n\tif (event.type === 'pageview') {\n\t\tbucket.pageviews++\n\t\tbucket.durationMs += event.durationMs ?? 0\n\t} else {\n\t\tbucket.events++\n\t}\n\tbucket.visitors.add(event.visitorHash)\n\tbucket.sessions.add(event.sessionId)\n}\n\nconst selectMetrics = (bucket: Bucket, wanted: MetricKey[]): Partial<Record<MetricKey, number>> => {\n\tconst out: Partial<Record<MetricKey, number>> = {}\n\tif (wanted.includes('pageviews')) out.pageviews = bucket.pageviews\n\tif (wanted.includes('events')) out.events = bucket.events\n\tif (wanted.includes('visitors')) out.visitors = bucket.visitors.size\n\tif (wanted.includes('sessions')) out.sessions = bucket.sessions.size\n\tif (wanted.includes('avgDuration')) {\n\t\tout.avgDuration = bucket.pageviews > 0 ? Math.round(bucket.durationMs / bucket.pageviews) : 0\n\t}\n\treturn out\n}\n\nexport interface AggregateEventsArgs {\n\tmetrics: MetricKey[]\n\tdimension?: DimensionKey\n\tgranularity?: 'hour' | 'day'\n\ttimezone?: string\n\t/** Sort for dimension breakdown rows; defaults to pageviews desc, same as the rollup path. */\n\torder?: { metric: MetricKey; direction: 'asc' | 'desc' }\n\t/** Row cap for dimension breakdown rows, applied after sorting. */\n\tlimit?: number\n}\n\nexport interface AggregateEventsResult {\n\trows: AnalyticsRow[]\n\ttotals: Partial<Record<MetricKey, number>>\n}\n\n/**\n * Hour buckets are UTC-hour floors regardless of `timezone`; sub-day bucketing ignoring\n * the reporting timezone is a documented v1 limitation. Day buckets floor in `timezone`\n * to stay aligned with native rollup day boundaries.\n */\nconst bucketKeyFor = (event: EventLike, granularity: 'hour' | 'day', timezone: string): string => {\n\tconst ts = new Date(event.timestamp)\n\tif (granularity === 'hour') {\n\t\treturn new Date(Math.floor(ts.getTime() / HOUR_MS) * HOUR_MS).toISOString()\n\t}\n\treturn startOfDayInTz(ts, timezone).toISOString()\n}\n\n/**\n * Aggregate raw events into totals, plus one of: dimension breakdown rows or hour/day\n * series rows (dimension wins if both are requested). Totals always cover every event\n * passed in, independent of which rows a breakdown or series produces.\n */\nexport const aggregateEvents = (\n\tevents: EventLike[],\n\t{\n\t\tmetrics,\n\t\tdimension,\n\t\tgranularity,\n\t\ttimezone = DEFAULT_TIMEZONE,\n\t\torder,\n\t\tlimit,\n\t}: AggregateEventsArgs\n): AggregateEventsResult => {\n\tconst totalsBucket = emptyBucket()\n\tfor (const event of events) {\n\t\taddEvent(totalsBucket, event)\n\t}\n\tconst totals = selectMetrics(totalsBucket, metrics)\n\n\tconst field = dimension ? EVENT_FIELD[dimension] : undefined\n\tif (dimension && field) {\n\t\tconst groups = new Map<string, Bucket>()\n\t\tfor (const event of events) {\n\t\t\tconst value = event[field]\n\t\t\tif (!value) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tlet bucket = groups.get(value)\n\t\t\tif (!bucket) {\n\t\t\t\tbucket = emptyBucket()\n\t\t\t\tgroups.set(value, bucket)\n\t\t\t}\n\t\t\taddEvent(bucket, event)\n\t\t}\n\t\tlet rows: AnalyticsRow[] = [...groups].map(([value, bucket]) => ({\n\t\t\tdimensions: { [dimension]: value } as Partial<Record<DimensionKey, string>>,\n\t\t\tmetrics: selectMetrics(bucket, metrics),\n\t\t}))\n\t\tconst sortMetric = order?.metric ?? 'pageviews'\n\t\tconst direction = (order?.direction ?? 'desc') === 'asc' ? 1 : -1\n\t\trows.sort((a, b) => ((a.metrics[sortMetric] ?? 0) - (b.metrics[sortMetric] ?? 0)) * direction)\n\t\tif (limit) {\n\t\t\trows = rows.slice(0, limit)\n\t\t}\n\t\treturn { rows, totals }\n\t}\n\n\tif (granularity) {\n\t\tconst buckets = new Map<string, Bucket>()\n\t\tfor (const event of events) {\n\t\t\tconst key = bucketKeyFor(event, granularity, timezone)\n\t\t\tlet bucket = buckets.get(key)\n\t\t\tif (!bucket) {\n\t\t\t\tbucket = emptyBucket()\n\t\t\t\tbuckets.set(key, bucket)\n\t\t\t}\n\t\t\taddEvent(bucket, event)\n\t\t}\n\t\tconst rows: AnalyticsRow[] = [...buckets.entries()]\n\t\t\t.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))\n\t\t\t.map(([timestamp, bucket]) => ({ timestamp, metrics: selectMetrics(bucket, metrics) }))\n\t\treturn { rows, totals }\n\t}\n\n\treturn { rows: [{ metrics: totals }], totals }\n}\n"],"mappings":";;AAmBA,MAAM,UAAU;;AAKhB,MAAM,cAA+D;CACpE,MAAM;CACN,SAAS;CACT,QAAQ;CACR,MAAM;CACN,QAAQ;CACR,QAAQ;CACR,OAAO;AACR;AAEA,MAAM,kBAAqD;CAAE,IAAI;CAAU,UAAU;AAAW;;;;;;;;AAShG,MAAa,kBAAkB,YAAwD;CACtF,MAAM,QAAiC,CAAC;C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AM,CAAC,IAAI,CAAC,OAAQ,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI,CAAE,EAC/C,KAAK,CAAC,WAAW,aAAa;IAAE;IAAW,SAAS,cAAc,QAAQ,OAAO;GAAE,EACzE;GAAG;EAAO;CACvB;CAEA,OAAO;EAAE,MAAM,CAAC,EAAE,SAAS,OAAO,CAAC;EAAG;CAAO;AAC9C"}
1
+ {"version":3,"file":"eventAgg.js","names":[],"sources":["../../../src/native/query/eventAgg.ts"],"sourcesContent":["import type { AnalyticsFilter, AnalyticsRow, DimensionKey, MetricKey } from '../../core/contract'\nimport { DEFAULT_TIMEZONE, startOfDayInTz } from '../../timeframe/tz'\n\n/** Raw event shape aggregateEvents/filtersToWhere operate on; matches StoredEvent as read back from the events collection (timestamp comes back as an ISO string). */\nexport interface EventLike {\n\ttimestamp: string | Date\n\ttype: 'pageview' | 'event' | 'goal'\n\tname?: string\n\tpath: string\n\tdevice?: string\n\tsource?: string\n\tcountry?: string\n\tregion?: string\n\tcity?: string\n\tvisitorHash: string\n\tsessionId: string\n\tdurationMs?: number\n\tscrollDepth?: number\n\t/** Goal completions stamped at ingest; the source of conversions/revenue on this path. */\n\tgoals?: Array<{ slug: string; value: number }>\n}\n\nconst HOUR_MS = 3_600_000\n\ntype EventStringField = 'path' | 'country' | 'region' | 'city' | 'device' | 'source' | 'name'\n\n/** Dimensions native can filter/group on directly against the events collection. */\nconst EVENT_FIELD: Partial<Record<DimensionKey, EventStringField>> = {\n\tpage: 'path',\n\tcountry: 'country',\n\tregion: 'region',\n\tcity: 'city',\n\tdevice: 'device',\n\tsource: 'source',\n\tevent: 'name',\n}\n\nconst FILTER_OPERATOR: Record<'eq' | 'contains', string> = { eq: 'equals', contains: 'contains' }\n\n/**\n * Payload where fragment for supported filters; unsupported dimensions/operators are\n * dropped. Capability gating (`filters`/`filterOperators`) is the real contract upstream,\n * this is just the safety net so a stray unsupported filter never throws.\n * `contains` is case-insensitive on both DBs; on Postgres its value is a raw ILIKE\n * pattern (`%`/`_` act as wildcards), while Mongo regex-escapes the value first.\n * One fragment per filter under `and`, not merged by key: two filters on the same field\n * (e.g. two `eq` values) must both constrain the query rather than the later one silently\n * overwriting the earlier one.\n */\nexport const filtersToWhere = (filters: AnalyticsFilter[]): Record<string, unknown> => {\n\tconst fragments: Record<string, unknown>[] = []\n\tfor (const filter of filters) {\n\t\tconst field = EVENT_FIELD[filter.dimension]\n\t\tif (!field || filter.operator === 'matches') {\n\t\t\tcontinue\n\t\t}\n\t\tconst op = FILTER_OPERATOR[filter.operator]\n\t\tif (!op) {\n\t\t\tcontinue\n\t\t}\n\t\tfragments.push({ [field]: { [op]: filter.value } })\n\t}\n\treturn fragments.length > 0 ? { and: fragments } : {}\n}\n\ninterface Bucket {\n\tpageviews: number\n\tevents: number\n\tdurationMs: number\n\tvisitors: Set<string>\n\tsessions: Set<string>\n\tconversions: number\n\trevenue: number\n\tscrollDepthSum: number\n\tscrollSamples: number\n}\n\nconst emptyBucket = (): Bucket => ({\n\tpageviews: 0,\n\tevents: 0,\n\tdurationMs: 0,\n\tvisitors: new Set(),\n\tsessions: new Set(),\n\tconversions: 0,\n\trevenue: 0,\n\tscrollDepthSum: 0,\n\tscrollSamples: 0,\n})\n\n/**\n * `completions` narrows what the event contributes to conversions/revenue: a goal breakdown\n * row counts only its own goal, every other bucket counts all of them. Mirrors how\n * computeRollupDeltas fills a `goal` bucket versus a site or page bucket.\n */\nconst addEvent = (bucket: Bucket, event: EventLike, completions = event.goals ?? []): void => {\n\tif (event.type === 'pageview') {\n\t\tbucket.pageviews++\n\t\tbucket.durationMs += event.durationMs ?? 0\n\t} else {\n\t\tbucket.events++\n\t}\n\tif (event.scrollDepth !== undefined) {\n\t\tbucket.scrollDepthSum += event.scrollDepth\n\t\tbucket.scrollSamples++\n\t}\n\tbucket.conversions += completions.length\n\tfor (const completion of completions) {\n\t\tbucket.revenue += completion.value\n\t}\n\tbucket.visitors.add(event.visitorHash)\n\tbucket.sessions.add(event.sessionId)\n}\n\nconst selectMetrics = (bucket: Bucket, wanted: MetricKey[]): Partial<Record<MetricKey, number>> => {\n\tconst out: Partial<Record<MetricKey, number>> = {}\n\tif (wanted.includes('pageviews')) out.pageviews = bucket.pageviews\n\tif (wanted.includes('events')) out.events = bucket.events\n\tif (wanted.includes('visitors')) out.visitors = bucket.visitors.size\n\tif (wanted.includes('sessions')) out.sessions = bucket.sessions.size\n\tif (wanted.includes('conversions')) out.conversions = bucket.conversions\n\tif (wanted.includes('revenue')) out.revenue = bucket.revenue\n\tif (wanted.includes('avgDuration')) {\n\t\tout.avgDuration = bucket.pageviews > 0 ? Math.round(bucket.durationMs / bucket.pageviews) : 0\n\t}\n\tif (wanted.includes('scrollDepth')) {\n\t\tout.scrollDepth =\n\t\t\tbucket.scrollSamples > 0 ? Math.round(bucket.scrollDepthSum / bucket.scrollSamples) : 0\n\t}\n\treturn out\n}\n\nexport interface AggregateEventsArgs {\n\tmetrics: MetricKey[]\n\tdimension?: DimensionKey\n\tgranularity?: 'hour' | 'day'\n\ttimezone?: string\n\t/** Sort for dimension breakdown rows; defaults to pageviews desc, same as the rollup path. */\n\torder?: { metric: MetricKey; direction: 'asc' | 'desc' }\n\t/** Row cap for dimension breakdown rows, applied after sorting. */\n\tlimit?: number\n}\n\nexport interface AggregateEventsResult {\n\trows: AnalyticsRow[]\n\ttotals: Partial<Record<MetricKey, number>>\n}\n\n/**\n * Hour buckets are UTC-hour floors regardless of `timezone`; sub-day bucketing ignoring\n * the reporting timezone is a documented v1 limitation. Day buckets floor in `timezone`\n * to stay aligned with native rollup day boundaries.\n */\nconst bucketKeyFor = (event: EventLike, granularity: 'hour' | 'day', timezone: string): string => {\n\tconst ts = new Date(event.timestamp)\n\tif (granularity === 'hour') {\n\t\treturn new Date(Math.floor(ts.getTime() / HOUR_MS) * HOUR_MS).toISOString()\n\t}\n\treturn startOfDayInTz(ts, timezone).toISOString()\n}\n\n/**\n * Aggregate raw events into totals, plus one of: dimension breakdown rows or hour/day\n * series rows (dimension wins if both are requested). Totals always cover every event\n * passed in, independent of which rows a breakdown or series produces.\n */\nexport const aggregateEvents = (\n\tevents: EventLike[],\n\t{\n\t\tmetrics,\n\t\tdimension,\n\t\tgranularity,\n\t\ttimezone = DEFAULT_TIMEZONE,\n\t\torder,\n\t\tlimit,\n\t}: AggregateEventsArgs\n): AggregateEventsResult => {\n\tconst totalsBucket = emptyBucket()\n\tfor (const event of events) {\n\t\taddEvent(totalsBucket, event)\n\t}\n\tconst totals = selectMetrics(totalsBucket, metrics)\n\n\tconst field = dimension ? EVENT_FIELD[dimension] : undefined\n\tif (dimension && (field || dimension === 'goal')) {\n\t\tconst groups = new Map<string, Bucket>()\n\t\tconst bucketFor = (value: string): Bucket => {\n\t\t\tlet bucket = groups.get(value)\n\t\t\tif (!bucket) {\n\t\t\t\tbucket = emptyBucket()\n\t\t\t\tgroups.set(value, bucket)\n\t\t\t}\n\t\t\treturn bucket\n\t\t}\n\t\tfor (const event of events) {\n\t\t\tif (dimension === 'goal') {\n\t\t\t\tfor (const completion of event.goals ?? []) {\n\t\t\t\t\taddEvent(bucketFor(completion.slug), event, [completion])\n\t\t\t\t}\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\t// The event dimension reports named custom events only: a goal event's name is a\n\t\t\t// goal slug and belongs to the goal dimension instead.\n\t\t\tif (!field || (field === 'name' && event.type !== 'event')) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tconst value = event[field]\n\t\t\tif (!value) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\taddEvent(bucketFor(value), event)\n\t\t}\n\t\tlet rows: AnalyticsRow[] = [...groups].map(([value, bucket]) => ({\n\t\t\tdimensions: { [dimension]: value } as Partial<Record<DimensionKey, string>>,\n\t\t\tmetrics: selectMetrics(bucket, metrics),\n\t\t}))\n\t\tconst sortMetric = order?.metric ?? 'pageviews'\n\t\tconst direction = (order?.direction ?? 'desc') === 'asc' ? 1 : -1\n\t\trows.sort((a, b) => ((a.metrics[sortMetric] ?? 0) - (b.metrics[sortMetric] ?? 0)) * direction)\n\t\tif (limit) {\n\t\t\trows = rows.slice(0, limit)\n\t\t}\n\t\treturn { rows, totals }\n\t}\n\n\tif (granularity) {\n\t\tconst buckets = new Map<string, Bucket>()\n\t\tfor (const event of events) {\n\t\t\tconst key = bucketKeyFor(event, granularity, timezone)\n\t\t\tlet bucket = buckets.get(key)\n\t\t\tif (!bucket) {\n\t\t\t\tbucket = emptyBucket()\n\t\t\t\tbuckets.set(key, bucket)\n\t\t\t}\n\t\t\taddEvent(bucket, event)\n\t\t}\n\t\tconst rows: AnalyticsRow[] = [...buckets.entries()]\n\t\t\t.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))\n\t\t\t.map(([timestamp, bucket]) => ({ timestamp, metrics: selectMetrics(bucket, metrics) }))\n\t\treturn { rows, totals }\n\t}\n\n\treturn { rows: [{ metrics: totals }], totals }\n}\n"],"mappings":";;AAsBA,MAAM,UAAU;;AAKhB,MAAM,cAA+D;CACpE,MAAM;CACN,SAAS;CACT,QAAQ;CACR,MAAM;CACN,QAAQ;CACR,QAAQ;CACR,OAAO;AACR;AAEA,MAAM,kBAAqD;CAAE,IAAI;CAAU,UAAU;AAAW;;;;;;;;;;;AAYhG,MAAa,kBAAkB,YAAwD;CACtF,MAAM,YAAuC,CAAC;CAC9C,KAAK,MAAM,UAAU,SAAS;EAC7B,MAAM,QAAQ,YAAY,OAAO;EACjC,IAAI,CAAC,SAAS,OAAO,aAAa,WACjC;EAED,MAAM,KAAK,gBAAgB,OAAO;EAClC,IAAI,CAAC,IACJ;EAED,UAAU,KAAK,GAAG,QAAQ,GAAG,KAAK,OAAO,MAAM,EAAE,CAAC;CACnD;CACA,OAAO,UAAU,SAAS,IAAI,EAAE,KAAK,UAAU,IAAI,CAAC;AACrD;AAcA,MAAM,qBAA6B;CAClC,WAAW;CACX,QAAQ;CACR,YAAY;CACZ,0BAAU,IAAI,IAAI;CAClB,0BAAU,IAAI,IAAI;CAClB,a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