@10x-media/analytics 1.0.0-beta.8 → 1.0.0-beta.9

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 (239) hide show
  1. package/CHANGELOG.md +70 -0
  2. package/LICENSE +21 -21
  3. package/README.md +3 -3
  4. package/dist/adapters/ga4/eventName.d.ts +15 -0
  5. package/dist/adapters/ga4/eventName.js +19 -0
  6. package/dist/adapters/ga4/eventName.js.map +1 -0
  7. package/dist/adapters/ga4/ga4.d.ts +5 -0
  8. package/dist/adapters/ga4/ga4.js +178 -70
  9. package/dist/adapters/ga4/ga4.js.map +1 -1
  10. package/dist/adapters/ga4/measurementId.js +15 -0
  11. package/dist/adapters/ga4/measurementId.js.map +1 -0
  12. package/dist/adapters/goalRead.js +126 -0
  13. package/dist/adapters/goalRead.js.map +1 -0
  14. package/dist/adapters/plausible/plausible.d.ts +15 -0
  15. package/dist/adapters/plausible/plausible.js +110 -42
  16. package/dist/adapters/plausible/plausible.js.map +1 -1
  17. package/dist/adapters/posthog/posthog.js +47 -25
  18. package/dist/adapters/posthog/posthog.js.map +1 -1
  19. package/dist/adapters/umami/umami.d.ts +8 -0
  20. package/dist/adapters/umami/umami.js +237 -28
  21. package/dist/adapters/umami/umami.js.map +1 -1
  22. package/dist/charts/BarList.js +8 -2
  23. package/dist/charts/BarList.js.map +1 -1
  24. package/dist/core/cacheKey.js +11 -1
  25. package/dist/core/cacheKey.js.map +1 -1
  26. package/dist/core/capabilities.js +7 -2
  27. package/dist/core/capabilities.js.map +1 -1
  28. package/dist/core/capture.d.ts +1 -1
  29. package/dist/core/contract.d.ts +32 -2
  30. package/dist/core/contract.js.map +1 -1
  31. package/dist/core/options.d.ts +8 -2
  32. package/dist/core/options.js +4 -2
  33. package/dist/core/options.js.map +1 -1
  34. package/dist/core/serverEvent.d.ts +4 -2
  35. package/dist/core/serverEvent.js.map +1 -1
  36. package/dist/exports/adapters/ga4.d.ts +2 -1
  37. package/dist/exports/adapters/ga4.js +2 -1
  38. package/dist/exports/client.d.ts +5 -2
  39. package/dist/exports/client.js +4 -1
  40. package/dist/exports/rsc.d.ts +2 -1
  41. package/dist/exports/tracker.d.ts +3 -2
  42. package/dist/exports/tracker.js +2 -1
  43. package/dist/exports/types.d.ts +2 -1
  44. package/dist/exports/types.js +2 -1
  45. package/dist/fields/config/FilterDimensionSelectField.d.ts +14 -0
  46. package/dist/fields/config/FilterDimensionSelectField.js +77 -0
  47. package/dist/fields/config/FilterDimensionSelectField.js.map +1 -0
  48. package/dist/fields/config/FilterOperatorSelectField.d.ts +14 -0
  49. package/dist/fields/config/FilterOperatorSelectField.js +75 -0
  50. package/dist/fields/config/FilterOperatorSelectField.js.map +1 -0
  51. package/dist/fields/config/MetricSelectField.js +1 -1
  52. package/dist/fields/config/SourceSelectField.js +1 -1
  53. package/dist/fields/config/sourcesState.js +19 -3
  54. package/dist/fields/config/sourcesState.js.map +1 -1
  55. package/dist/fields/config/useAnalyticsSources.d.ts +8 -3
  56. package/dist/fields/config/useAnalyticsSources.js +9 -6
  57. package/dist/fields/config/useAnalyticsSources.js.map +1 -1
  58. package/dist/fields/config/useFilterCapabilities.d.ts +28 -0
  59. package/dist/fields/config/useFilterCapabilities.js +52 -0
  60. package/dist/fields/config/useFilterCapabilities.js.map +1 -0
  61. package/dist/fields/readForDocument.js +16 -4
  62. package/dist/fields/readForDocument.js.map +1 -1
  63. package/dist/index.d.ts +3 -1
  64. package/dist/index.js +2 -1
  65. package/dist/index.js.map +1 -1
  66. package/dist/native/collections/events.js +36 -0
  67. package/dist/native/collections/events.js.map +1 -1
  68. package/dist/native/eventScan.js +25 -0
  69. package/dist/native/eventScan.js.map +1 -0
  70. package/dist/native/ingest/browser.js +47 -0
  71. package/dist/native/ingest/browser.js.map +1 -0
  72. package/dist/native/ingest/flushBatch.js +26 -26
  73. package/dist/native/ingest/flushBatch.js.map +1 -1
  74. package/dist/native/ingest/language.js +14 -0
  75. package/dist/native/ingest/language.js.map +1 -0
  76. package/dist/native/ingest/normalizeEvent.d.ts +6 -0
  77. package/dist/native/ingest/normalizeEvent.js +32 -5
  78. package/dist/native/ingest/normalizeEvent.js.map +1 -1
  79. package/dist/native/ingest/referrer.js +48 -0
  80. package/dist/native/ingest/referrer.js.map +1 -0
  81. package/dist/native/ingest/serverTrack.js +1 -0
  82. package/dist/native/ingest/serverTrack.js.map +1 -1
  83. package/dist/native/ingest/source.d.ts +7 -0
  84. package/dist/native/ingest/source.js +139 -14
  85. package/dist/native/ingest/source.js.map +1 -1
  86. package/dist/native/ingest/utm.js +33 -0
  87. package/dist/native/ingest/utm.js.map +1 -0
  88. package/dist/native/nativeAdapter.js +32 -26
  89. package/dist/native/nativeAdapter.js.map +1 -1
  90. package/dist/native/query/eventAgg.js +14 -1
  91. package/dist/native/query/eventAgg.js.map +1 -1
  92. package/dist/native/rollups/bucketKey.js +15 -0
  93. package/dist/native/rollups/bucketKey.js.map +1 -0
  94. package/dist/native/rollups/bumpRollups.js +116 -0
  95. package/dist/native/rollups/bumpRollups.js.map +1 -0
  96. package/dist/native/rollups/deltas.js +30 -15
  97. package/dist/native/rollups/deltas.js.map +1 -1
  98. package/dist/native/rollups/insertManyIfNew.js +93 -0
  99. package/dist/native/rollups/insertManyIfNew.js.map +1 -0
  100. package/dist/next/captureRewrites.d.ts +3 -2
  101. package/dist/next/captureRewrites.js +3 -2
  102. package/dist/next/captureRewrites.js.map +1 -1
  103. package/dist/plugin/documentEndpoint.js +26 -7
  104. package/dist/plugin/documentEndpoint.js.map +1 -1
  105. package/dist/plugin/goalHint.d.ts +1 -0
  106. package/dist/plugin/goalHint.js +24 -0
  107. package/dist/plugin/goalHint.js.map +1 -0
  108. package/dist/plugin/queryEndpoint.js +19 -12
  109. package/dist/plugin/queryEndpoint.js.map +1 -1
  110. package/dist/plugin/realtimeEndpoint.js +40 -15
  111. package/dist/plugin/realtimeEndpoint.js.map +1 -1
  112. package/dist/plugin/responses.js +17 -0
  113. package/dist/plugin/responses.js.map +1 -0
  114. package/dist/plugin/sourcesEndpoint.js +10 -3
  115. package/dist/plugin/sourcesEndpoint.js.map +1 -1
  116. package/dist/providers/collection.js +14 -4
  117. package/dist/providers/collection.js.map +1 -1
  118. package/dist/providers/factory.js +4 -2
  119. package/dist/providers/factory.js.map +1 -1
  120. package/dist/query/response.d.ts +4 -1
  121. package/dist/query/response.js +11 -0
  122. package/dist/query/response.js.map +1 -0
  123. package/dist/surfacing/engine.js +1 -1
  124. package/dist/surfacing/engine.js.map +1 -1
  125. package/dist/testing/memoryAdapter.js +0 -1
  126. package/dist/testing/memoryAdapter.js.map +1 -1
  127. package/dist/tracker/createTracker.js +13 -1
  128. package/dist/tracker/createTracker.js.map +1 -1
  129. package/dist/tracker/sinks/ga4.d.ts +21 -0
  130. package/dist/tracker/sinks/ga4.js +26 -0
  131. package/dist/tracker/sinks/ga4.js.map +1 -0
  132. package/dist/tracker/types.d.ts +5 -0
  133. package/dist/translations/ar.js +30 -1
  134. package/dist/translations/ar.js.map +1 -1
  135. package/dist/translations/de.js +30 -1
  136. package/dist/translations/de.js.map +1 -1
  137. package/dist/translations/en.js +30 -1
  138. package/dist/translations/en.js.map +1 -1
  139. package/dist/translations/es.js +30 -1
  140. package/dist/translations/es.js.map +1 -1
  141. package/dist/translations/fr.js +30 -1
  142. package/dist/translations/fr.js.map +1 -1
  143. package/dist/translations/id.js +30 -1
  144. package/dist/translations/id.js.map +1 -1
  145. package/dist/translations/keys.d.ts +29 -0
  146. package/dist/translations/keys.js +30 -1
  147. package/dist/translations/keys.js.map +1 -1
  148. package/dist/translations/ko.js +30 -1
  149. package/dist/translations/ko.js.map +1 -1
  150. package/dist/translations/metricKeys.js +7 -1
  151. package/dist/translations/metricKeys.js.map +1 -1
  152. package/dist/translations/pt.js +30 -1
  153. package/dist/translations/pt.js.map +1 -1
  154. package/dist/translations/ru.js +30 -1
  155. package/dist/translations/ru.js.map +1 -1
  156. package/dist/translations/server.js.map +1 -1
  157. package/dist/translations/uk.js +30 -1
  158. package/dist/translations/uk.js.map +1 -1
  159. package/dist/translations/zh.js +30 -1
  160. package/dist/translations/zh.js.map +1 -1
  161. package/dist/view/AnalyticsViewClient.js +13 -2
  162. package/dist/view/AnalyticsViewClient.js.map +1 -1
  163. package/dist/view/gating.js +40 -3
  164. package/dist/view/gating.js.map +1 -1
  165. package/dist/view/labels.js +21 -1
  166. package/dist/view/labels.js.map +1 -1
  167. package/dist/view/sections/Breakdowns.js +78 -38
  168. package/dist/view/sections/Breakdowns.js.map +1 -1
  169. package/dist/view/sections/FilterChips.js +8 -3
  170. package/dist/view/sections/FilterChips.js.map +1 -1
  171. package/dist/view/sections/GoalsPanel.js +2 -1
  172. package/dist/view/sections/GoalsPanel.js.map +1 -1
  173. package/dist/view/sections/RealtimeStrip.js +4 -1
  174. package/dist/view/sections/RealtimeStrip.js.map +1 -1
  175. package/dist/view/sections/Toolbar.js +10 -3
  176. package/dist/view/sections/Toolbar.js.map +1 -1
  177. package/dist/view/sections/Trend.js +8 -2
  178. package/dist/view/sections/Trend.js.map +1 -1
  179. package/dist/view/state.js +7 -2
  180. package/dist/view/state.js.map +1 -1
  181. package/dist/view/useViewQueries.js +6 -3
  182. package/dist/view/useViewQueries.js.map +1 -1
  183. package/dist/widgets/AnalyticsBreakdownWidget.js +59 -10
  184. package/dist/widgets/AnalyticsBreakdownWidget.js.map +1 -1
  185. package/dist/widgets/AnalyticsGoalsWidget.js +10 -2
  186. package/dist/widgets/AnalyticsGoalsWidget.js.map +1 -1
  187. package/dist/widgets/AnalyticsMetricWidget.js +30 -3
  188. package/dist/widgets/AnalyticsMetricWidget.js.map +1 -1
  189. package/dist/widgets/AnalyticsRealtimeWidget.js +3 -1
  190. package/dist/widgets/AnalyticsRealtimeWidget.js.map +1 -1
  191. package/dist/widgets/AnalyticsTrendWidget.js +30 -3
  192. package/dist/widgets/AnalyticsTrendWidget.js.map +1 -1
  193. package/dist/widgets/RealtimeCounter.d.ts +4 -0
  194. package/dist/widgets/RealtimeCounter.js +10 -1
  195. package/dist/widgets/RealtimeCounter.js.map +1 -1
  196. package/dist/widgets/breakdownTypes.js.map +1 -1
  197. package/dist/widgets/comparison.js +15 -1
  198. package/dist/widgets/comparison.js.map +1 -1
  199. package/dist/widgets/defaults.d.ts +6 -2
  200. package/dist/widgets/defaults.js +72 -25
  201. package/dist/widgets/defaults.js.map +1 -1
  202. package/dist/widgets/filterCaption.js +27 -0
  203. package/dist/widgets/filterCaption.js.map +1 -0
  204. package/dist/widgets/filterField.d.ts +19 -0
  205. package/dist/widgets/filterField.js +75 -0
  206. package/dist/widgets/filterField.js.map +1 -0
  207. package/dist/widgets/prepareWidgetRead.d.ts +7 -0
  208. package/dist/widgets/prepareWidgetRead.js +74 -0
  209. package/dist/widgets/prepareWidgetRead.js.map +1 -0
  210. package/dist/widgets/readForWidget.d.ts +12 -2
  211. package/dist/widgets/readForWidget.js +34 -49
  212. package/dist/widgets/readForWidget.js.map +1 -1
  213. package/dist/widgets/readForWidgetBreakdown.d.ts +17 -1
  214. package/dist/widgets/readForWidgetBreakdown.js +29 -42
  215. package/dist/widgets/readForWidgetBreakdown.js.map +1 -1
  216. package/dist/widgets/readForWidgetGoals.d.ts +11 -1
  217. package/dist/widgets/readForWidgetGoals.js +44 -30
  218. package/dist/widgets/readForWidgetGoals.js.map +1 -1
  219. package/dist/widgets/readForWidgetRealtime.d.ts +8 -0
  220. package/dist/widgets/readForWidgetRealtime.js +21 -20
  221. package/dist/widgets/readForWidgetRealtime.js.map +1 -1
  222. package/dist/widgets/readForWidgetSeries.d.ts +11 -1
  223. package/dist/widgets/readForWidgetSeries.js +38 -49
  224. package/dist/widgets/readForWidgetSeries.js.map +1 -1
  225. package/dist/widgets/readMeta.js +18 -0
  226. package/dist/widgets/readMeta.js.map +1 -0
  227. package/dist/widgets/registerWidgets.js +5 -2
  228. package/dist/widgets/registerWidgets.js.map +1 -1
  229. package/dist/widgets/types.d.ts +15 -0
  230. package/dist/widgets/types.js.map +1 -1
  231. package/dist/widgets/viewLink.js +5 -2
  232. package/dist/widgets/viewLink.js.map +1 -1
  233. package/package.json +5 -5
  234. package/dist/native/rollups/applyDistinctDeltas.js +0 -9
  235. package/dist/native/rollups/applyDistinctDeltas.js.map +0 -1
  236. package/dist/native/rollups/bumpRollup.js +0 -73
  237. package/dist/native/rollups/bumpRollup.js.map +0 -1
  238. package/dist/native/rollups/insertIfNew.js +0 -53
  239. package/dist/native/rollups/insertIfNew.js.map +0 -1
@@ -1 +1 @@
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\tconst device = classifyDevice(ua)\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\t...(device ? { device } : {}),\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,kBAAkB;AACxB,MAAM,kBAAkB;;AAExB,MAAM,sBAAsB;;AAE5B,MAAM,kBAAkB;AACxB,MAAM,WAAW;;;;;;;AAQjB,MAAM,eAAe,UACpB,OAAO,UAAU,YAAY,OAAO,SAAS,KAAK,KAAK,SAAS,IAAI,QAAQ,KAAA;AAE7E,MAAM,gBAAgB,UACrB,OAAO,UAAU,YAAY,SAAS,KAAK,KAAK,IAAI,QAAQ,KAAA;;;;;;AAO7D,MAAM,YAAY,UAAuC;CACxD,MAAM,IAAI,YAAY,KAAK;CAC3B,OAAO,MAAM,KAAA,KAAa,IAAI,kBAAkB,KAAA,IAAY;AAC7D;;AAGA,MAAM,aAAa,UAClB,OAAO,UAAU,WAAW,MAAM,MAAM,GAAG,eAAe,IAAI,KAAA;AAE/D,MAAM,SAAS,UAAuC;CACrD,MAAM,IAAI,YAAY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AAI;EAAM;EAAM;EAAM;EAAO;CAAM,GAAG,KAAK,IAC9D,CAAC;CACJ,OAAO;EACN,WAAW;EACX,MAAM,IAAI;EACV;EACA;EACA;EACA,UAAU,IAAI;EACd,GAAI,SAAS,EAAE,OAAO,IAAI,CAAC;EAC3B,QAAQ,aAAa,IAAI,UAAU,QAAQ;EAC3C,SAAS,IAAI;EACb,QAAQ,IAAI;EACZ,MAAM,IAAI;EACV;EACA,WAAW,gBAAgB,aAAa,UAAU;EAClD;EACA,GAAI,eAAe,KAAA,IAAY,EAAE,WAAW,IAAI,CAAC;EACjD,GAAI,UAAU,KAAA,IAAY,EAAE,MAAM,IAAI,CAAC;EACvC,GAAI,aAAa,IAAI,QAAQ,MAAM,KAAA,IAAY,EAAE,UAAU,IAAI,SAAS,IAAI,CAAC;EAC7E,GAAI,gBAAgB,KAAA,IAAY,EAAE,YAAY,IAAI,CAAC;EACnD,GAAI,YAAY,SAAS,IAAI,EAAE,OAAO,YAAY,IAAI,CAAC;EACvD,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 { MAX_GEO_LENGTH, MAX_QUERY_LENGTH, MAX_REFERRER_LENGTH } from '../../query/limits'\nimport type { GeoResolver } from '../geo/geoResolver'\nimport { type BrowserName, classifyBrowser, classifyOs, type OsName } from './browser'\nimport { clientIpFromHeaders } from './clientIp'\nimport { classifyDevice, type DeviceType } from './device'\nimport { primaryLanguage } from './language'\nimport { referrerHost, storedReferrer } from './referrer'\nimport { deriveSource } from './source'\nimport { extractUtm } from './utm'\nimport { dailyVisitorHash, deriveSessionId } from './visitorHash'\n\n/** The wire caps, re-exported beside the sanitizers that apply them. */\nexport { MAX_GEO_LENGTH, MAX_QUERY_LENGTH, MAX_REFERRER_LENGTH }\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\t/**\n\t * The page's query string, without its leading `?`. Read for its utm keys and then\n\t * discarded: it is never stored, so a session token or an email address that happens to\n\t * be in the URL never lands in the events collection.\n\t */\n\tquery?: 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\t/** Origin and path only: the query string and fragment are stripped before storage. */\n\treferrer?: string\n\t/**\n\t * The referrer's bare host, derived at ingest because a `where` cannot derive it at read\n\t * time: it is what the `referrer` dimension buckets and filters on.\n\t */\n\treferrerHost?: string\n\tdevice?: DeviceType\n\tbrowser?: BrowserName\n\tos?: OsName\n\t/**\n\t * The hit's traffic channel (`TrafficChannel`). Typed as a string because rows written\n\t * before the channel classifier hold a referrer host here instead, and they are never\n\t * rewritten.\n\t */\n\tsource?: string\n\t/** The five campaign keys extracted from the wire `query`; absent when it carried none. */\n\tutmSource?: string\n\tutmMedium?: string\n\tutmCampaign?: string\n\tutmContent?: string\n\tutmTerm?: string\n\tcountry?: string\n\tregion?: string\n\tcity?: string\n\t/** Primary `Accept-Language` tag, lowercased (`de-de`). */\n\tlanguage?: 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\n/**\n * A geo value as stored. The resolver reads request headers a client can set, and each value\n * becomes a rollup dimvalue and part of a seen-ledger key, so it is capped here rather than\n * trusted. An empty value stays absent: '' would be a bucket of its own.\n */\nconst geoValue = (value: string | undefined): string | undefined =>\n\tvalue ? value.slice(0, MAX_GEO_LENGTH) : undefined\n\n/** Capped before it is parsed, so a hostile query cannot make ingest do unbounded work. */\nconst queryString = (value: unknown): string | undefined =>\n\ttypeof value === 'string' ? value.slice(0, MAX_QUERY_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// Everything that becomes a rollup dimvalue or a seen-ledger key is capped before anything\n\t// derives from it: an uncapped value exceeds Mongo's index key limit, and on Postgres a\n\t// btree key over 2704 bytes fails the write outright. That covers path and hostname here,\n\t// the geo values below (a client can set the headers a resolver reads), and the event name,\n\t// query and referrer in their own sanitizers.\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\tconst device = classifyDevice(ua)\n\tconst browser = classifyBrowser(ua)\n\tconst os = classifyOs(ua)\n\tconst language = primaryLanguage(headers.get('accept-language'))\n\tconst refHost = referrerHost(raw.referrer, hostname)\n\tconst query = queryString(raw.query)\n\tconst utm = extractUtm(query)\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: storedReferrer(raw.referrer),\n\t\t...(refHost ? { referrerHost: refHost } : {}),\n\t\t...(device ? { device } : {}),\n\t\t...(browser ? { browser } : {}),\n\t\t...(os ? { os } : {}),\n\t\tsource: deriveSource({ referrerHost: refHost, utmMedium: utm.utmMedium, query }),\n\t\t...utm,\n\t\tcountry: geoValue(geo.country),\n\t\tregion: geoValue(geo.region),\n\t\tcity: geoValue(geo.city),\n\t\t...(language ? { language } : {}),\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":";;;;;;;;;;AA6GA,MAAM,YAAY;AAClB,MAAM,iBAAiB;AACvB,MAAM,mBAAmB;AACzB,MAAM,kBAAkB;AACxB,MAAM,kBAAkB;;AAExB,MAAM,sBAAsB;;AAE5B,MAAM,kBAAkB;AACxB,MAAM,WAAW;;;;;;;AAQjB,MAAM,eAAe,UACpB,OAAO,UAAU,YAAY,OAAO,SAAS,KAAK,KAAK,SAAS,IAAI,QAAQ,KAAA;AAE7E,MAAM,gBAAgB,UACrB,OAAO,UAAU,YAAY,SAAS,KAAK,KAAK,IAAI,QAAQ,KAAA;;;;;;AAO7D,MAAM,YAAY,UAAuC;CACxD,MAAM,IAAI,YAAY,KAAK;CAC3B,OAAO,MAAM,KAAA,KAAa,IAAI,kBAAkB,KAAA,IAAY;AAC7D;;AAGA,MAAM,aAAa,UAClB,OAAO,UAAU,WAAW,MAAM,MAAM,GAAG,eAAe,IAAI,KAAA;;;;;;AAO/D,MAAM,YAAY,UACjB,QAAQ,MAAM,MAAM,GAAA,GAAiB,IAAI,KAAA;;AAG1C,MAAM,eAAe,UACpB,OAAO,UAAU,WAAW,MAAM,MAAM,GAAA,GAAmB,IAAI,KAAA;AAEhE,MAAM,SAAS,UAAuC;CACrD,MAAM,IAAI,YAAY,KAAK;CAC3B,OAAO,MAAM,KAAA,IAAY,KAAA,IAAY,KAAK,MAAM,KAAK,IAAI,GAAG,GAAG,CAAC;AACjE;;AAGA,MAAM,iBAAiB,QAAsD;CAC5E,IAAI,OAAO,QAAQ,YAAY,QAAQ,QAAQ,MAAM,QAAQ,GAAG,GAC/D;CAED,MAAM,MAA+B,CAAC;CACtC,IAAI,OAAO;CACX,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,GAAG,GAAG;EAC/C,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;EACX,MAAM,IAAI;EACV;EACA;EACA;EACA,UAAU,eAAe,IAAI,QAAQ;EACrC,GAAI,UAAU,EAAE,cAAc,QAAQ,IAAI,CAAC;EAC3C,GAAI,SAAS,EAAE,OAAO,IAAI,CAAC;EAC3B,GAAI,UAAU,EAAE,QAAQ,IAAI,CAAC;EAC7B,GAAI,KAAK,EAAE,GAAG,IAAI,CAAC;EACnB,QAAQ,aAAa;GAAE,cAAc;GAAS,WAAW,IAAI;GAAW;EAAM,CAAC;EAC/E,GAAG;EACH,SAAS,SAAS,IAAI,OAAO;EAC7B,QAAQ,SAAS,IAAI,MAAM;EAC3B,MAAM,SAAS,IAAI,IAAI;EACvB,GAAI,WAAW,EAAE,SAAS,IAAI,CAAC;EAC/B;EACA,WAAW,gBAAgB,aAAa,UAAU;EAClD;EACA,GAAI,eAAe,KAAA,IAAY,EAAE,WAAW,IAAI,CAAC;EACjD,GAAI,UAAU,KAAA,IAAY,EAAE,MAAM,IAAI,CAAC;EACvC,GAAI,aAAa,IAAI,QAAQ,MAAM,KAAA,IAAY,EAAE,UAAU,IAAI,SAAS,IAAI,CAAC;EAC7E,GAAI,gBAAgB,KAAA,IAAY,EAAE,YAAY,IAAI,CAAC;EACnD,GAAI,YAAY,SAAS,IAAI,EAAE,OAAO,YAAY,IAAI,CAAC;EACvD,GAAI,UAAU,KAAA,IAAY,EAAE,MAAM,IAAI,CAAC;EACvC,GAAI,aAAa,KAAA,IAAY,EAAE,SAAS,IAAI,CAAC;CAC9C;AACD"}
@@ -0,0 +1,48 @@
1
+ //#region src/native/ingest/referrer.ts
2
+ /** A hostname as it is compared: lowercased, without a leading `www.`. */
3
+ const bareHost = (host) => host.toLowerCase().replace(/^www\./, "");
4
+ /**
5
+ * The referrer as it is stored: everything from the first `#` or `?` onwards removed, then
6
+ * capped. A same-origin navigation puts the previous page's whole URL in `document.referrer`,
7
+ * so the query string routinely carries a reset token, a session id or an email address that
8
+ * has no business in an analytics row. Origin and path are kept verbatim, case included,
9
+ * because they are what makes a referrer readable. The fragment goes first: a `?` after a
10
+ * `#` belongs to the fragment, not to a query.
11
+ *
12
+ * An unparseable referrer is stripped on the same terms rather than trusted, and a value that
13
+ * is not a string at all is dropped: the wire field is public and unvalidated, so a body
14
+ * carrying `"referrer": {}` must cost the event its referrer, not the request its response.
15
+ */
16
+ const storedReferrer = (raw) => {
17
+ if (typeof raw !== "string") return;
18
+ const stripped = raw.split("#")[0]?.split("?")[0];
19
+ return stripped ? stripped.slice(0, 512) : void 0;
20
+ };
21
+ /**
22
+ * The referrer's bare host, which is what the `referrer` dimension buckets and reads by:
23
+ * lowercased, without scheme, port, path, query or a leading `www.`. Nothing is reported for
24
+ * a missing, non-string, hostless or unparseable referrer, so the event contributes no
25
+ * referrer bucket.
26
+ *
27
+ * A self-referral reports nothing either: internal navigation is the bulk of any site's
28
+ * traffic, and counting it would put the site's own domain at the top of its referrers
29
+ * breakdown forever. `selfHostname` is the event's own hostname, compared on the same terms
30
+ * (case-insensitively, `www.` stripped). The `source` dimension reads the same absence as its
31
+ * `direct` channel.
32
+ */
33
+ const referrerHost = (raw, selfHostname) => {
34
+ if (typeof raw !== "string" || !raw) return;
35
+ let host;
36
+ try {
37
+ host = new URL(raw).hostname;
38
+ } catch {
39
+ return;
40
+ }
41
+ const bare = bareHost(host);
42
+ if (!bare || bare === bareHost(selfHostname)) return;
43
+ return bare.slice(0, 253);
44
+ };
45
+ //#endregion
46
+ export { referrerHost, storedReferrer };
47
+
48
+ //# sourceMappingURL=referrer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"referrer.js","names":[],"sources":["../../../src/native/ingest/referrer.ts"],"sourcesContent":["import { MAX_QUERY_HOSTNAME_LENGTH, MAX_REFERRER_LENGTH } from '../../query/limits'\n\n/** A hostname as it is compared: lowercased, without a leading `www.`. */\nconst bareHost = (host: string): string => host.toLowerCase().replace(/^www\\./, '')\n\n/**\n * The referrer as it is stored: everything from the first `#` or `?` onwards removed, then\n * capped. A same-origin navigation puts the previous page's whole URL in `document.referrer`,\n * so the query string routinely carries a reset token, a session id or an email address that\n * has no business in an analytics row. Origin and path are kept verbatim, case included,\n * because they are what makes a referrer readable. The fragment goes first: a `?` after a\n * `#` belongs to the fragment, not to a query.\n *\n * An unparseable referrer is stripped on the same terms rather than trusted, and a value that\n * is not a string at all is dropped: the wire field is public and unvalidated, so a body\n * carrying `\"referrer\": {}` must cost the event its referrer, not the request its response.\n */\nexport const storedReferrer = (raw: unknown): string | undefined => {\n\tif (typeof raw !== 'string') {\n\t\treturn undefined\n\t}\n\tconst stripped = raw.split('#')[0]?.split('?')[0]\n\treturn stripped ? stripped.slice(0, MAX_REFERRER_LENGTH) : undefined\n}\n\n/**\n * The referrer's bare host, which is what the `referrer` dimension buckets and reads by:\n * lowercased, without scheme, port, path, query or a leading `www.`. Nothing is reported for\n * a missing, non-string, hostless or unparseable referrer, so the event contributes no\n * referrer bucket.\n *\n * A self-referral reports nothing either: internal navigation is the bulk of any site's\n * traffic, and counting it would put the site's own domain at the top of its referrers\n * breakdown forever. `selfHostname` is the event's own hostname, compared on the same terms\n * (case-insensitively, `www.` stripped). The `source` dimension reads the same absence as its\n * `direct` channel.\n */\nexport const referrerHost = (raw: unknown, selfHostname: string): string | undefined => {\n\tif (typeof raw !== 'string' || !raw) {\n\t\treturn undefined\n\t}\n\tlet host: string\n\ttry {\n\t\thost = new URL(raw).hostname\n\t} catch {\n\t\treturn undefined\n\t}\n\tconst bare = bareHost(host)\n\tif (!bare || bare === bareHost(selfHostname)) {\n\t\treturn undefined\n\t}\n\treturn bare.slice(0, MAX_QUERY_HOSTNAME_LENGTH)\n}\n"],"mappings":";;AAGA,MAAM,YAAY,SAAyB,KAAK,YAAY,EAAE,QAAQ,UAAU,EAAE;;;;;;;;;;;;;AAclF,MAAa,kBAAkB,QAAqC;CACnE,IAAI,OAAO,QAAQ,UAClB;CAED,MAAM,WAAW,IAAI,MAAM,GAAG,EAAE,IAAI,MAAM,GAAG,EAAE;CAC/C,OAAO,WAAW,SAAS,MAAM,GAAA,GAAsB,IAAI,KAAA;AAC5D;;;;;;;;;;;;;AAcA,MAAa,gBAAgB,KAAc,iBAA6C;CACvF,IAAI,OAAO,QAAQ,YAAY,CAAC,KAC/B;CAED,IAAI;CACJ,IAAI;EACH,OAAO,IAAI,IAAI,GAAG,EAAE;CACrB,QAAQ;EACP;CACD;CACA,MAAM,OAAO,SAAS,IAAI;CAC1B,IAAI,CAAC,QAAQ,SAAS,SAAS,YAAY,GAC1C;CAED,OAAO,KAAK,MAAM,GAAA,GAA4B;AAC/C"}
@@ -56,6 +56,7 @@ const makeServerTrack = (deps) => async (event, opts) => {
56
56
  path: event.path,
57
57
  hostname: event.hostname,
58
58
  referrer: event.referrer,
59
+ query: event.query,
59
60
  props: event.props,
60
61
  value: event.value,
61
62
  currency: event.currency
@@ -1 +1 @@
1
- {"version":3,"file":"serverTrack.js","names":[],"sources":["../../../src/native/ingest/serverTrack.ts"],"sourcesContent":["import { createLocalReq, type Payload, type PayloadRequest } from 'payload'\nimport {\n\tAnalyticsTrackError,\n\ttype ServerEventInput,\n\ttype ServerTrack,\n\ttype ServerTrackOptions,\n} from '../../core/serverEvent'\nimport { getRuntime } from '../../plugin/runtime'\nimport type { GeoResolver } from '../geo/geoResolver'\nimport { SERVER_USER_AGENT } from './device'\nimport type { IngestResolvers } from './endpoint'\nimport { flushBatch } from './flushBatch'\nimport { normalizeEvent, type StoredEvent } from './normalizeEvent'\nimport { dailySalt } from './salt'\nimport { rawEventError } from './validate'\nimport type { WriteBuffer } from './writeBuffer'\n\nexport interface ServerTrackDeps {\n\t/** Null until the adapter's onInit has run. */\n\tgetPayload: () => Payload | null\n\tgeoResolver: GeoResolver\n\tgetBuffer: () => WriteBuffer<StoredEvent> | null\n\t/** Read late: the register context that carries them arrives after the adapter is built. */\n\tgetResolvers: () => IngestResolvers\n}\n\n/**\n * The originating request's own headers, so a server event attributes exactly like the\n * browser event it stands in for: the platform's geo headers reach the geo resolver and the\n * visitor hashes to the same person. Explicit `ip` / `userAgent` overlay them.\n *\n * With no request and no user agent the synthetic server agent stands in, which hashes every\n * such event in a day to one visitor per site and reports no device at all.\n */\nconst attributionHeaders = (event: ServerEventInput, req?: PayloadRequest): Headers => {\n\tconst headers = new Headers(req?.headers)\n\tif (event.ip) {\n\t\theaders.set('x-forwarded-for', event.ip)\n\t}\n\tif (event.userAgent) {\n\t\theaders.set('user-agent', event.userAgent)\n\t}\n\tif (!headers.get('user-agent')) {\n\t\theaders.set('user-agent', SERVER_USER_AGENT)\n\t}\n\treturn headers\n}\n\nconst resolveScope = async (\n\tevent: ServerEventInput,\n\topts: ServerTrackOptions | undefined,\n\tresolvers: IngestResolvers\n): Promise<string | undefined> => {\n\t// An unscoped install has no scope resolver and no scope field on its rows, so a scope\n\t// declared by the caller is ignored exactly as the endpoint ignores one on the wire.\n\tif (!resolvers.scope) {\n\t\treturn undefined\n\t}\n\tif (event.scope !== undefined) {\n\t\treturn event.scope ?? ''\n\t}\n\tif (!opts?.req) {\n\t\treturn ''\n\t}\n\treturn (await resolvers.scope(opts.req)) ?? ''\n}\n\n/**\n * The native adapter's server-side ingestion, exposed as `adapter.ingest.track`. Same\n * normalization, goal matching and write path the HTTP endpoint uses; only attribution and\n * scope resolution differ, since there may be no request behind the event at all.\n */\nexport const makeServerTrack =\n\t(deps: ServerTrackDeps): ServerTrack =>\n\tasync (event, opts) => {\n\t\tconst payload = deps.getPayload()\n\t\tif (!payload) {\n\t\t\tthrow new AnalyticsTrackError('analytics: trackServerEvent called before init')\n\t\t}\n\t\tconst invalid = rawEventError(event)\n\t\tif (invalid) {\n\t\t\tthrow new AnalyticsTrackError(`analytics: trackServerEvent needs a valid \"${invalid}\"`)\n\t\t}\n\t\tconst resolvers = deps.getResolvers()\n\t\tconst scope = await resolveScope(event, opts, resolvers)\n\t\t// createLocalReq mutates the request it is given, so a caller's own req is passed\n\t\t// through untouched and a fresh one is built only when a resolver needs it.\n\t\tlet local: PayloadRequest | undefined\n\t\tconst requestFor = async (): Promise<PayloadRequest> => {\n\t\t\tif (opts?.req) {\n\t\t\t\treturn opts.req\n\t\t\t}\n\t\t\tlocal ??= await createLocalReq({}, payload)\n\t\t\treturn local\n\t\t}\n\t\tconst timezone =\n\t\t\tevent.timezone ??\n\t\t\t(resolvers.timezone ? await resolvers.timezone(await requestFor(), scope ?? null) : undefined)\n\t\t// Goals resolve under the scope the event is stamped with, so a tenant's server event\n\t\t// can only ever complete that tenant's goals.\n\t\tconst goals = resolvers.goals\n\t\t\t? await resolvers.goals(await requestFor(), scope ?? null)\n\t\t\t: undefined\n\t\tconst now = event.now ?? new Date()\n\t\tconst stored = await normalizeEvent({\n\t\t\traw: {\n\t\t\t\ttype: event.type,\n\t\t\t\tname: event.name,\n\t\t\t\tpath: event.path,\n\t\t\t\thostname: event.hostname,\n\t\t\t\treferrer: event.referrer,\n\t\t\t\tprops: event.props,\n\t\t\t\tvalue: event.value,\n\t\t\t\tcurrency: event.currency,\n\t\t\t},\n\t\t\theaders: attributionHeaders(event, opts?.req),\n\t\t\tgeoResolver: deps.geoResolver,\n\t\t\tsalt: await dailySalt(payload, now),\n\t\t\tnow,\n\t\t\tscope,\n\t\t\ttimezone,\n\t\t\tgoals,\n\t\t})\n\t\tconst buffer = deps.getBuffer()\n\t\tif (!buffer) {\n\t\t\tawait flushBatch(payload, [stored])\n\t\t\treturn\n\t\t}\n\t\tbuffer.add(stored)\n\t\tif (opts?.flush) {\n\t\t\tawait buffer.flush()\n\t\t}\n\t}\n\n/**\n * Records an analytics event from server code: a webhook, a job, a server action. The event\n * goes through the same normalization, sanitization and goal matching a browser event does.\n * Pass `opts.req` whenever there is a request behind the event, so it inherits that request's\n * attribution, scope and reporting timezone.\n *\n * `event.scope` overrides the request's scope; it is ignored on an unscoped install and is\n * never checked against the scopes that exist, so the caller owns what it stamps.\n *\n * Needs the native adapter: a provider slot (Plausible, GA4) has no server ingestion seam\n * here yet, so an install without the native adapter throws rather than dropping the event.\n */\nexport const trackServerEvent = async (\n\tpayload: Payload,\n\tevent: ServerEventInput,\n\topts?: ServerTrackOptions\n): Promise<void> => {\n\tconst track = getRuntime(payload)\n\t\t?.registry.all()\n\t\t.find((adapter) => adapter.ingest?.track)?.ingest?.track\n\tif (!track) {\n\t\tthrow new AnalyticsTrackError(\n\t\t\t'analytics: trackServerEvent needs the native adapter; provider-slot server tracking is not supported yet'\n\t\t)\n\t}\n\tawait track(event, opts)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAkCA,MAAM,sBAAsB,OAAyB,QAAkC;CACtF,MAAM,UAAU,IAAI,QAAQ,KAAK,OAAO;CACxC,IAAI,MAAM,IACT,QAAQ,IAAI,mBAAmB,MAAM,EAAE;CAExC,IAAI,MAAM,WACT,QAAQ,IAAI,cAAc,MAAM,SAAS;CAE1C,IAAI,CAAC,QAAQ,IAAI,YAAY,GAC5B,QAAQ,IAAI,cAAc,iBAAiB;CAE5C,OAAO;AACR;AAEA,MAAM,eAAe,OACpB,OACA,MACA,cACiC;CAGjC,IAAI,CAAC,UAAU,OACd;CAED,IAAI,MAAM,UAAU,KAAA,GACnB,OAAO,MAAM,SAAS;CAEvB,IAAI,CAAC,MAAM,KACV,OAAO;CAER,OAAQ,MAAM,UAAU,MAAM,KAAK,GAAG,KAAM;AAC7C;;;;;;AAOA,MAAa,mBACX,SACD,OAAO,OAAO,SAAS;CACtB,MAAM,UAAU,KAAK,WAAW;CAChC,IAAI,CAAC,SACJ,MAAM,IAAI,oBAAoB,gDAAgD;CAE/E,MAAM,UAAU,cAAc,KAAK;CACnC,IAAI,SACH,MAAM,IAAI,oBAAoB,8CAA8C,QAAQ,EAAE;CAEvF,MAAM,YAAY,KAAK,aAAa;CACpC,MAAM,QAAQ,MAAM,aAAa,OAAO,MAAM,SAAS;CAGvD,IAAI;CACJ,MAAM,aAAa,YAAqC;EACvD,IAAI,MAAM,KACT,OAAO,KAAK;EAEb,UAAU,MAAM,eAAe,CAAC,GAAG,OAAO;EAC1C,OAAO;CACR;CACA,MAAM,WACL,MAAM,aACL,UAAU,WAAW,MAAM,UAAU,SAAS,MAAM,WAAW,GAAG,SAAS,IAAI,IAAI,KAAA;CAGrF,MAAM,QAAQ,UAAU,QACrB,MAAM,UAAU,MAAM,MAAM,WAAW,GAAG,SAAS,IAAI,IACvD,KAAA;CACH,MAAM,MAAM,MAAM,uBAAO,IAAI,KAAK;CAClC,MAAM,SAAS,MAAM,eAAe;EACnC,KAAK;GACJ,MAAM,MAAM;GACZ,MAAM,MAAM;GACZ,MAAM,MAAM;GACZ,UAAU,MAAM;GAChB,UAAU,MAAM;GAChB,OAAO,MAAM;GACb,OAAO,MAAM;GACb,UAAU,MAAM;EACjB;EACA,SAAS,mBAAmB,OAAO,MAAM,GAAG;EAC5C,aAAa,KAAK;EAClB,MAAM,MAAM,UAAU,SAAS,GAAG;EAClC;EACA;EACA;EACA;CACD,CAAC;CACD,MAAM,SAAS,KAAK,UAAU;CAC9B,IAAI,CAAC,QAAQ;EACZ,MAAM,WAAW,SAAS,CAAC,MAAM,CAAC;EAClC;CACD;CACA,OAAO,IAAI,MAAM;CACjB,IAAI,MAAM,OACT,MAAM,OAAO,MAAM;AAErB;;;;;;;;;;;;;AAcD,MAAa,mBAAmB,OAC/B,SACA,OACA,SACmB;CACnB,MAAM,QAAQ,WAAW,OAAO,GAC7B,SAAS,IAAI,EACd,MAAM,YAAY,QAAQ,QAAQ,KAAK,GAAG,QAAQ;CACpD,IAAI,CAAC,OACJ,MAAM,IAAI,oBACT,0GACD;CAED,MAAM,MAAM,OAAO,IAAI;AACxB"}
1
+ {"version":3,"file":"serverTrack.js","names":[],"sources":["../../../src/native/ingest/serverTrack.ts"],"sourcesContent":["import { createLocalReq, type Payload, type PayloadRequest } from 'payload'\nimport {\n\tAnalyticsTrackError,\n\ttype ServerEventInput,\n\ttype ServerTrack,\n\ttype ServerTrackOptions,\n} from '../../core/serverEvent'\nimport { getRuntime } from '../../plugin/runtime'\nimport type { GeoResolver } from '../geo/geoResolver'\nimport { SERVER_USER_AGENT } from './device'\nimport type { IngestResolvers } from './endpoint'\nimport { flushBatch } from './flushBatch'\nimport { normalizeEvent, type StoredEvent } from './normalizeEvent'\nimport { dailySalt } from './salt'\nimport { rawEventError } from './validate'\nimport type { WriteBuffer } from './writeBuffer'\n\nexport interface ServerTrackDeps {\n\t/** Null until the adapter's onInit has run. */\n\tgetPayload: () => Payload | null\n\tgeoResolver: GeoResolver\n\tgetBuffer: () => WriteBuffer<StoredEvent> | null\n\t/** Read late: the register context that carries them arrives after the adapter is built. */\n\tgetResolvers: () => IngestResolvers\n}\n\n/**\n * The originating request's own headers, so a server event attributes exactly like the\n * browser event it stands in for: the platform's geo headers reach the geo resolver and the\n * visitor hashes to the same person. Explicit `ip` / `userAgent` overlay them.\n *\n * With no request and no user agent the synthetic server agent stands in, which hashes every\n * such event in a day to one visitor per site and reports no device at all.\n */\nconst attributionHeaders = (event: ServerEventInput, req?: PayloadRequest): Headers => {\n\tconst headers = new Headers(req?.headers)\n\tif (event.ip) {\n\t\theaders.set('x-forwarded-for', event.ip)\n\t}\n\tif (event.userAgent) {\n\t\theaders.set('user-agent', event.userAgent)\n\t}\n\tif (!headers.get('user-agent')) {\n\t\theaders.set('user-agent', SERVER_USER_AGENT)\n\t}\n\treturn headers\n}\n\nconst resolveScope = async (\n\tevent: ServerEventInput,\n\topts: ServerTrackOptions | undefined,\n\tresolvers: IngestResolvers\n): Promise<string | undefined> => {\n\t// An unscoped install has no scope resolver and no scope field on its rows, so a scope\n\t// declared by the caller is ignored exactly as the endpoint ignores one on the wire.\n\tif (!resolvers.scope) {\n\t\treturn undefined\n\t}\n\tif (event.scope !== undefined) {\n\t\treturn event.scope ?? ''\n\t}\n\tif (!opts?.req) {\n\t\treturn ''\n\t}\n\treturn (await resolvers.scope(opts.req)) ?? ''\n}\n\n/**\n * The native adapter's server-side ingestion, exposed as `adapter.ingest.track`. Same\n * normalization, goal matching and write path the HTTP endpoint uses; only attribution and\n * scope resolution differ, since there may be no request behind the event at all.\n */\nexport const makeServerTrack =\n\t(deps: ServerTrackDeps): ServerTrack =>\n\tasync (event, opts) => {\n\t\tconst payload = deps.getPayload()\n\t\tif (!payload) {\n\t\t\tthrow new AnalyticsTrackError('analytics: trackServerEvent called before init')\n\t\t}\n\t\tconst invalid = rawEventError(event)\n\t\tif (invalid) {\n\t\t\tthrow new AnalyticsTrackError(`analytics: trackServerEvent needs a valid \"${invalid}\"`)\n\t\t}\n\t\tconst resolvers = deps.getResolvers()\n\t\tconst scope = await resolveScope(event, opts, resolvers)\n\t\t// createLocalReq mutates the request it is given, so a caller's own req is passed\n\t\t// through untouched and a fresh one is built only when a resolver needs it.\n\t\tlet local: PayloadRequest | undefined\n\t\tconst requestFor = async (): Promise<PayloadRequest> => {\n\t\t\tif (opts?.req) {\n\t\t\t\treturn opts.req\n\t\t\t}\n\t\t\tlocal ??= await createLocalReq({}, payload)\n\t\t\treturn local\n\t\t}\n\t\tconst timezone =\n\t\t\tevent.timezone ??\n\t\t\t(resolvers.timezone ? await resolvers.timezone(await requestFor(), scope ?? null) : undefined)\n\t\t// Goals resolve under the scope the event is stamped with, so a tenant's server event\n\t\t// can only ever complete that tenant's goals.\n\t\tconst goals = resolvers.goals\n\t\t\t? await resolvers.goals(await requestFor(), scope ?? null)\n\t\t\t: undefined\n\t\tconst now = event.now ?? new Date()\n\t\tconst stored = await normalizeEvent({\n\t\t\traw: {\n\t\t\t\ttype: event.type,\n\t\t\t\tname: event.name,\n\t\t\t\tpath: event.path,\n\t\t\t\thostname: event.hostname,\n\t\t\t\treferrer: event.referrer,\n\t\t\t\tquery: event.query,\n\t\t\t\tprops: event.props,\n\t\t\t\tvalue: event.value,\n\t\t\t\tcurrency: event.currency,\n\t\t\t},\n\t\t\theaders: attributionHeaders(event, opts?.req),\n\t\t\tgeoResolver: deps.geoResolver,\n\t\t\tsalt: await dailySalt(payload, now),\n\t\t\tnow,\n\t\t\tscope,\n\t\t\ttimezone,\n\t\t\tgoals,\n\t\t})\n\t\tconst buffer = deps.getBuffer()\n\t\tif (!buffer) {\n\t\t\tawait flushBatch(payload, [stored])\n\t\t\treturn\n\t\t}\n\t\tbuffer.add(stored)\n\t\tif (opts?.flush) {\n\t\t\tawait buffer.flush()\n\t\t}\n\t}\n\n/**\n * Records an analytics event from server code: a webhook, a job, a server action. The event\n * goes through the same normalization, sanitization and goal matching a browser event does.\n * Pass `opts.req` whenever there is a request behind the event, so it inherits that request's\n * attribution, scope and reporting timezone.\n *\n * `event.scope` overrides the request's scope; it is ignored on an unscoped install and is\n * never checked against the scopes that exist, so the caller owns what it stamps.\n *\n * Needs the native adapter: a provider slot (Plausible, GA4) has no server ingestion seam\n * here yet, so an install without the native adapter throws rather than dropping the event.\n */\nexport const trackServerEvent = async (\n\tpayload: Payload,\n\tevent: ServerEventInput,\n\topts?: ServerTrackOptions\n): Promise<void> => {\n\tconst track = getRuntime(payload)\n\t\t?.registry.all()\n\t\t.find((adapter) => adapter.ingest?.track)?.ingest?.track\n\tif (!track) {\n\t\tthrow new AnalyticsTrackError(\n\t\t\t'analytics: trackServerEvent needs the native adapter; provider-slot server tracking is not supported yet'\n\t\t)\n\t}\n\tawait track(event, opts)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAkCA,MAAM,sBAAsB,OAAyB,QAAkC;CACtF,MAAM,UAAU,IAAI,QAAQ,KAAK,OAAO;CACxC,IAAI,MAAM,IACT,QAAQ,IAAI,mBAAmB,MAAM,EAAE;CAExC,IAAI,MAAM,WACT,QAAQ,IAAI,cAAc,MAAM,SAAS;CAE1C,IAAI,CAAC,QAAQ,IAAI,YAAY,GAC5B,QAAQ,IAAI,cAAc,iBAAiB;CAE5C,OAAO;AACR;AAEA,MAAM,eAAe,OACpB,OACA,MACA,cACiC;CAGjC,IAAI,CAAC,UAAU,OACd;CAED,IAAI,MAAM,UAAU,KAAA,GACnB,OAAO,MAAM,SAAS;CAEvB,IAAI,CAAC,MAAM,KACV,OAAO;CAER,OAAQ,MAAM,UAAU,MAAM,K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mB,OAC/B,SACA,OACA,SACmB;CACnB,MAAM,QAAQ,WAAW,OAAO,GAC7B,SAAS,IAAI,EACd,MAAM,YAAY,QAAQ,QAAQ,KAAK,GAAG,QAAQ;CACpD,IAAI,CAAC,OACJ,MAAM,IAAI,oBACT,0GACD;CAED,MAAM,MAAM,OAAO,IAAI;AACxB"}
@@ -0,0 +1,7 @@
1
+ //#region src/native/ingest/source.d.ts
2
+ /** Every bucket the `source` dimension can hold, lowercase as they are stored. */
3
+ declare const TRAFFIC_CHANNELS: readonly ["direct", "search", "social", "email", "paid", "referral"];
4
+ type TrafficChannel = (typeof TRAFFIC_CHANNELS)[number];
5
+ //#endregion
6
+ export { TRAFFIC_CHANNELS, TrafficChannel };
7
+ //# sourceMappingURL=source.d.ts.map
@@ -1,22 +1,147 @@
1
1
  //#region src/native/ingest/source.ts
2
- const stripWww = (host) => host.replace(/^www\./, "");
2
+ /** Every bucket the `source` dimension can hold, lowercase as they are stored. */
3
+ const TRAFFIC_CHANNELS = [
4
+ "direct",
5
+ "search",
6
+ "social",
7
+ "email",
8
+ "paid",
9
+ "referral"
10
+ ];
3
11
  /**
4
- * Reduce a raw referrer URL to a coarse traffic source: the bare referrer host
5
- * (without `www.`), or `Direct` when there is no referrer, it is unparseable, or it
6
- * points back at the site itself (internal navigation is not a traffic source).
12
+ * A `Map` rather than an object literal: a lookup keyed by whatever `utm_medium` carries must
13
+ * answer nothing for `constructor` or `__proto__`, where an object literal answers an
14
+ * inherited member and puts a function in the `source` column.
15
+ *
16
+ * `referral` is deliberately absent. It names the channel the host branch already decides, and
17
+ * pinning it here would turn a hit with no referrer at all into `referral` rather than `direct`.
7
18
  */
8
- const deriveSource = (referrer, selfHostname) => {
9
- if (!referrer) return "Direct";
10
- let host;
11
- try {
12
- host = new URL(referrer).hostname;
13
- } catch {
14
- return "Direct";
19
+ const MEDIUM_CHANNELS = new Map([
20
+ ["cpc", "paid"],
21
+ ["ppc", "paid"],
22
+ ["display", "paid"],
23
+ ["retargeting", "paid"],
24
+ ["email", "email"],
25
+ ["newsletter", "email"],
26
+ ["social", "social"],
27
+ ["social-media", "social"],
28
+ ["sm", "social"],
29
+ ["organic", "search"],
30
+ ["search", "search"]
31
+ ]);
32
+ /** A click id only an ad click carries, whatever the referrer says. */
33
+ const CLICK_IDS = new Set([
34
+ "gclid",
35
+ "fbclid",
36
+ "msclkid",
37
+ "ttclid"
38
+ ]);
39
+ /**
40
+ * Hosts whose channel the lists below would get wrong, checked first. Webmail is `referral`
41
+ * rather than `email`: a referrer only proves a link was opened in a mail client, never that
42
+ * the mail was the newsletter an `email` bucket claims credit for.
43
+ */
44
+ const HOST_OVERRIDES = new Map([
45
+ ["mail.google.com", "referral"],
46
+ ["mail.yahoo.com", "referral"],
47
+ ["outlook.live.com", "referral"],
48
+ ["outlook.office.com", "referral"],
49
+ ["mail.proton.me", "referral"]
50
+ ]);
51
+ const SEARCH_HOSTS = [
52
+ "bing.com",
53
+ "duckduckgo.com",
54
+ "baidu.com",
55
+ "ecosia.org",
56
+ "search.brave.com",
57
+ "qwant.com",
58
+ "startpage.com",
59
+ "naver.com",
60
+ "seznam.cz",
61
+ "sogou.com"
62
+ ];
63
+ const SOCIAL_HOSTS = [
64
+ "facebook.com",
65
+ "fb.com",
66
+ "instagram.com",
67
+ "twitter.com",
68
+ "x.com",
69
+ "t.co",
70
+ "linkedin.com",
71
+ "lnkd.in",
72
+ "reddit.com",
73
+ "tiktok.com",
74
+ "youtube.com",
75
+ "youtu.be",
76
+ "threads.net",
77
+ "mastodon.social",
78
+ "bsky.app",
79
+ "web.telegram.org",
80
+ "t.me",
81
+ "wa.me",
82
+ "whatsapp.com"
83
+ ];
84
+ /** Brands running one site per country, matched on the label rather than a fixed tld. */
85
+ const SEARCH_BRANDS = [
86
+ "google",
87
+ "yahoo",
88
+ "yandex"
89
+ ];
90
+ const SOCIAL_BRANDS = ["pinterest"];
91
+ /**
92
+ * Second-level suffixes a brand's country site sits under (`google.co.uk`, `yahoo.com.au`).
93
+ * Without this list a two-label tail would also match `google.evil.com`, handing any host that
94
+ * cares to name itself after a search engine the search channel.
95
+ */
96
+ const SECOND_LEVEL = new Set([
97
+ "co",
98
+ "com",
99
+ "net",
100
+ "org",
101
+ "ac",
102
+ "gov",
103
+ "edu"
104
+ ]);
105
+ const matchesHost = (host, domain) => host === domain || host.endsWith(`.${domain}`);
106
+ const matchesBrand = (host, brand) => {
107
+ const labels = host.split(".");
108
+ for (let i = 0; i < labels.length; i++) {
109
+ if (labels[i] !== brand) continue;
110
+ const tail = labels.length - i - 1;
111
+ if (tail === 1 || tail === 2 && SECOND_LEVEL.has(labels[i + 1] ?? "")) return true;
112
+ }
113
+ return false;
114
+ };
115
+ const hostChannel = (host) => {
116
+ for (const [domain, channel] of HOST_OVERRIDES) if (matchesHost(host, domain)) return channel;
117
+ if (SEARCH_HOSTS.some((domain) => matchesHost(host, domain)) || SEARCH_BRANDS.some((brand) => matchesBrand(host, brand))) return "search";
118
+ if (SOCIAL_HOSTS.some((domain) => matchesHost(host, domain)) || SOCIAL_BRANDS.some((brand) => matchesBrand(host, brand))) return "social";
119
+ return "referral";
120
+ };
121
+ const hasClickId = (query) => {
122
+ for (const [key, value] of new URLSearchParams(query)) if (value && CLICK_IDS.has(key.toLowerCase())) return true;
123
+ return false;
124
+ };
125
+ /**
126
+ * The `source` dimension's bucket: the traffic channel a hit arrived through, which is what a
127
+ * report is asked for ("how much of last month was paid?"), where the host it came from is the
128
+ * `referrer` dimension's job.
129
+ *
130
+ * `utm_medium` wins outright, because a campaign states its own channel and the referrer host
131
+ * is then only the last hop; a paid click id is next, since an ad click carries one whatever
132
+ * host it bounced through; the built-in search and social lists decide the rest. Anything else
133
+ * with a host is `referral`, and a hit with no usable host at all is `direct`.
134
+ */
135
+ const deriveSource = ({ referrerHost, utmMedium, query }) => {
136
+ const medium = utmMedium?.trim().toLowerCase();
137
+ if (medium) {
138
+ const channel = medium.startsWith("paid") ? "paid" : MEDIUM_CHANNELS.get(medium);
139
+ if (channel) return channel;
15
140
  }
16
- if (!host || stripWww(host) === stripWww(selfHostname)) return "Direct";
17
- return stripWww(host);
141
+ if (query && hasClickId(query)) return "paid";
142
+ return referrerHost ? hostChannel(referrerHost) : "direct";
18
143
  };
19
144
  //#endregion
20
- export { deriveSource };
145
+ export { TRAFFIC_CHANNELS, deriveSource };
21
146
 
22
147
  //# sourceMappingURL=source.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"source.js","names":[],"sources":["../../../src/native/ingest/source.ts"],"sourcesContent":["const stripWww = (host: string): string => host.replace(/^www\\./, '')\n\n/**\n * Reduce a raw referrer URL to a coarse traffic source: the bare referrer host\n * (without `www.`), or `Direct` when there is no referrer, it is unparseable, or it\n * points back at the site itself (internal navigation is not a traffic source).\n */\nexport const deriveSource = (referrer: string | undefined, selfHostname: string): string => {\n\tif (!referrer) {\n\t\treturn 'Direct'\n\t}\n\tlet host: string\n\ttry {\n\t\thost = new URL(referrer).hostname\n\t} catch {\n\t\treturn 'Direct'\n\t}\n\tif (!host || stripWww(host) === stripWww(selfHostname)) {\n\t\treturn 'Direct'\n\t}\n\treturn stripWww(host)\n}\n"],"mappings":";AAAA,MAAM,YAAY,SAAyB,KAAK,QAAQ,UAAU,EAAE;;;;;;AAOpE,MAAa,gBAAgB,UAA8B,iBAAiC;CAC3F,IAAI,CAAC,UACJ,OAAO;CAER,IAAI;CACJ,IAAI;EACH,OAAO,IAAI,IAAI,QAAQ,EAAE;CAC1B,QAAQ;EACP,OAAO;CACR;CACA,IAAI,CAAC,QAAQ,SAAS,IAAI,MAAM,SAAS,YAAY,GACpD,OAAO;CAER,OAAO,SAAS,IAAI;AACrB"}
1
+ {"version":3,"file":"source.js","names":[],"sources":["../../../src/native/ingest/source.ts"],"sourcesContent":["/** Every bucket the `source` dimension can hold, lowercase as they are stored. */\nexport const TRAFFIC_CHANNELS = ['direct', 'search', 'social', 'email', 'paid', 'referral'] as const\n\nexport type TrafficChannel = (typeof TRAFFIC_CHANNELS)[number]\n\nexport interface SourceInput {\n\t/** The referrer's bare host from `referrerHost`, already absent for a same-site hit. */\n\treferrerHost?: string\n\tutmMedium?: string\n\t/** The page's query string, read here only for the ad networks' click ids. */\n\tquery?: string\n}\n\n/**\n * A `Map` rather than an object literal: a lookup keyed by whatever `utm_medium` carries must\n * answer nothing for `constructor` or `__proto__`, where an object literal answers an\n * inherited member and puts a function in the `source` column.\n *\n * `referral` is deliberately absent. It names the channel the host branch already decides, and\n * pinning it here would turn a hit with no referrer at all into `referral` rather than `direct`.\n */\nconst MEDIUM_CHANNELS: ReadonlyMap<string, TrafficChannel> = new Map<string, TrafficChannel>([\n\t['cpc', 'paid'],\n\t['ppc', 'paid'],\n\t['display', 'paid'],\n\t['retargeting', 'paid'],\n\t['email', 'email'],\n\t['newsletter', 'email'],\n\t['social', 'social'],\n\t['social-media', 'social'],\n\t['sm', 'social'],\n\t['organic', 'search'],\n\t['search', 'search'],\n])\n\n/** A click id only an ad click carries, whatever the referrer says. */\nconst CLICK_IDS: ReadonlySet<string> = new Set(['gclid', 'fbclid', 'msclkid', 'ttclid'])\n\n/**\n * Hosts whose channel the lists below would get wrong, checked first. Webmail is `referral`\n * rather than `email`: a referrer only proves a link was opened in a mail client, never that\n * the mail was the newsletter an `email` bucket claims credit for.\n */\nconst HOST_OVERRIDES: ReadonlyMap<string, TrafficChannel> = new Map<string, TrafficChannel>([\n\t['mail.google.com', 'referral'],\n\t['mail.yahoo.com', 'referral'],\n\t['outlook.live.com', 'referral'],\n\t['outlook.office.com', 'referral'],\n\t['mail.proton.me', 'referral'],\n])\n\nconst SEARCH_HOSTS = [\n\t'bing.com',\n\t'duckduckgo.com',\n\t'baidu.com',\n\t'ecosia.org',\n\t'search.brave.com',\n\t'qwant.com',\n\t'startpage.com',\n\t'naver.com',\n\t'seznam.cz',\n\t'sogou.com',\n]\nconst SOCIAL_HOSTS = [\n\t'facebook.com',\n\t'fb.com',\n\t'instagram.com',\n\t'twitter.com',\n\t'x.com',\n\t't.co',\n\t'linkedin.com',\n\t'lnkd.in',\n\t'reddit.com',\n\t'tiktok.com',\n\t'youtube.com',\n\t'youtu.be',\n\t'threads.net',\n\t'mastodon.social',\n\t'bsky.app',\n\t'web.telegram.org',\n\t't.me',\n\t'wa.me',\n\t'whatsapp.com',\n]\n\n/** Brands running one site per country, matched on the label rather than a fixed tld. */\nconst SEARCH_BRANDS = ['google', 'yahoo', 'yandex']\nconst SOCIAL_BRANDS = ['pinterest']\n\n/**\n * Second-level suffixes a brand's country site sits under (`google.co.uk`, `yahoo.com.au`).\n * Without this list a two-label tail would also match `google.evil.com`, handing any host that\n * cares to name itself after a search engine the search channel.\n */\nconst SECOND_LEVEL: ReadonlySet<string> = new Set(['co', 'com', 'net', 'org', 'ac', 'gov', 'edu'])\n\nconst matchesHost = (host: string, domain: string): boolean =>\n\thost === domain || host.endsWith(`.${domain}`)\n\nconst matchesBrand = (host: string, brand: string): boolean => {\n\tconst labels = host.split('.')\n\tfor (let i = 0; i < labels.length; i++) {\n\t\tif (labels[i] !== brand) {\n\t\t\tcontinue\n\t\t}\n\t\tconst tail = labels.length - i - 1\n\t\tif (tail === 1 || (tail === 2 && SECOND_LEVEL.has(labels[i + 1] ?? ''))) {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n\nconst hostChannel = (host: string): TrafficChannel => {\n\tfor (const [domain, channel] of HOST_OVERRIDES) {\n\t\tif (matchesHost(host, domain)) {\n\t\t\treturn channel\n\t\t}\n\t}\n\tif (\n\t\tSEARCH_HOSTS.some((domain) => matchesHost(host, domain)) ||\n\t\tSEARCH_BRANDS.some((brand) => matchesBrand(host, brand))\n\t) {\n\t\treturn 'search'\n\t}\n\tif (\n\t\tSOCIAL_HOSTS.some((domain) => matchesHost(host, domain)) ||\n\t\tSOCIAL_BRANDS.some((brand) => matchesBrand(host, brand))\n\t) {\n\t\treturn 'social'\n\t}\n\treturn 'referral'\n}\n\nconst hasClickId = (query: string): boolean => {\n\tfor (const [key, value] of new URLSearchParams(query)) {\n\t\tif (value && CLICK_IDS.has(key.toLowerCase())) {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n\n/**\n * The `source` dimension's bucket: the traffic channel a hit arrived through, which is what a\n * report is asked for (\"how much of last month was paid?\"), where the host it came from is the\n * `referrer` dimension's job.\n *\n * `utm_medium` wins outright, because a campaign states its own channel and the referrer host\n * is then only the last hop; a paid click id is next, since an ad click carries one whatever\n * host it bounced through; the built-in search and social lists decide the rest. Anything else\n * with a host is `referral`, and a hit with no usable host at all is `direct`.\n */\nexport const deriveSource = ({ referrerHost, utmMedium, query }: SourceInput): TrafficChannel => {\n\tconst medium = utmMedium?.trim().toLowerCase()\n\tif (medium) {\n\t\tconst channel = medium.startsWith('paid') ? 'paid' : MEDIUM_CHANNELS.get(medium)\n\t\tif (channel) {\n\t\t\treturn channel\n\t\t}\n\t}\n\tif (query && hasClickId(query)) {\n\t\treturn 'paid'\n\t}\n\treturn referrerHost ? hostChannel(referrerHost) : 'direct'\n}\n"],"mappings":";;AACA,MAAa,mBAAmB;CAAC;CAAU;CAAU;CAAU;CAAS;CAAQ;AAAU;;;;;;;;;AAoB1F,MAAM,kBAAuD,IAAI,IAA4B;CAC5F,CAAC,OAAO,MAAM;CACd,CAAC,OAAO,MAAM;CACd,CAAC,WAAW,MAAM;CAClB,CAAC,eAAe,MAAM;CACtB,CAAC,SAAS,OAAO;CACjB,CAAC,cAAc,OAAO;CACtB,CAAC,UAAU,QAAQ;CACnB,CAAC,gBAAgB,QAAQ;CACzB,CAAC,MAAM,QAAQ;CACf,CAAC,WAAW,QAAQ;CACpB,CAAC,UAAU,QAAQ;AACpB,CAAC;;AAGD,MAAM,YAAiC,IAAI,IAAI;CAAC;CAAS;CAAU;CAAW;AAAQ,CAAC;;;;;;AAOvF,MAAM,iBAAsD,IAAI,IAA4B;CAC3F,CAAC,mBAAmB,UAAU;CAC9B,CAAC,kBAAkB,UAAU;CAC7B,CAAC,oBAAoB,UAAU;CAC/B,CAAC,sBAAsB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AI,IAAI,YAAY,CAAC,GAC3C,OAAO;CAGT,OAAO;AACR;;;;;;;;;;;AAYA,MAAa,gBAAgB,EAAE,cAAc,WAAW,YAAyC;CAChG,MAAM,SAAS,WAAW,KAAK,EAAE,YAAY;CAC7C,IAAI,QAAQ;EACX,MAAM,UAAU,OAAO,WAAW,MAAM,IAAI,SAAS,gBAAgB,IAAI,MAAM;EAC/E,IAAI,SACH,OAAO;CAET;CACA,IAAI,SAAS,WAAW,KAAK,GAC5B,OAAO;CAER,OAAO,eAAe,YAAY,YAAY,IAAI;AACnD"}
@@ -0,0 +1,33 @@
1
+ /**
2
+ * A `Map` rather than an object literal: the lookup is keyed by a query key an attacker
3
+ * writes, and an object literal answers its inherited members, so `?constructor=x` would
4
+ * otherwise reach storage under a key `UtmParams` does not declare.
5
+ */
6
+ const UTM_KEYS = new Map([
7
+ ["utm_source", "utmSource"],
8
+ ["utm_medium", "utmMedium"],
9
+ ["utm_campaign", "utmCampaign"],
10
+ ["utm_content", "utmContent"],
11
+ ["utm_term", "utmTerm"]
12
+ ]);
13
+ /**
14
+ * The five utm keys carried by a page's query string, decoded and capped. Keys are matched
15
+ * case-insensitively and a repeated key keeps its first value, matching what
16
+ * `URLSearchParams.get` would have answered. Everything else in the query is discarded here
17
+ * and never reaches storage.
18
+ */
19
+ const extractUtm = (query) => {
20
+ if (!query) return {};
21
+ const out = {};
22
+ for (const [rawKey, rawValue] of new URLSearchParams(query)) {
23
+ const key = UTM_KEYS.get(rawKey.toLowerCase());
24
+ if (!key || out[key] !== void 0) continue;
25
+ const value = rawValue.trim().slice(0, 128);
26
+ if (value) out[key] = value;
27
+ }
28
+ return out;
29
+ };
30
+ //#endregion
31
+ export { extractUtm };
32
+
33
+ //# sourceMappingURL=utm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utm.js","names":[],"sources":["../../../src/native/ingest/utm.ts"],"sourcesContent":["export interface UtmParams {\n\tutmSource?: string\n\tutmMedium?: string\n\tutmCampaign?: string\n\tutmContent?: string\n\tutmTerm?: string\n}\n\n/** Each extracted value is a rollup bucket key, so it is capped like every other one. */\nexport const MAX_UTM_LENGTH = 128\n\n/**\n * A `Map` rather than an object literal: the lookup is keyed by a query key an attacker\n * writes, and an object literal answers its inherited members, so `?constructor=x` would\n * otherwise reach storage under a key `UtmParams` does not declare.\n */\nconst UTM_KEYS: ReadonlyMap<string, keyof UtmParams> = new Map<string, keyof UtmParams>([\n\t['utm_source', 'utmSource'],\n\t['utm_medium', 'utmMedium'],\n\t['utm_campaign', 'utmCampaign'],\n\t['utm_content', 'utmContent'],\n\t['utm_term', 'utmTerm'],\n])\n\n/**\n * The five utm keys carried by a page's query string, decoded and capped. Keys are matched\n * case-insensitively and a repeated key keeps its first value, matching what\n * `URLSearchParams.get` would have answered. Everything else in the query is discarded here\n * and never reaches storage.\n */\nexport const extractUtm = (query: string | undefined): UtmParams => {\n\tif (!query) {\n\t\treturn {}\n\t}\n\tconst out: UtmParams = {}\n\tfor (const [rawKey, rawValue] of new URLSearchParams(query)) {\n\t\tconst key = UTM_KEYS.get(rawKey.toLowerCase())\n\t\tif (!key || out[key] !== undefined) {\n\t\t\tcontinue\n\t\t}\n\t\tconst value = rawValue.trim().slice(0, MAX_UTM_LENGTH)\n\t\tif (value) {\n\t\t\tout[key] = value\n\t\t}\n\t}\n\treturn out\n}\n"],"mappings":";;;;;AAgBA,MAAM,WAAiD,IAAI,IAA6B;CACvF,CAAC,cAAc,WAAW;CAC1B,CAAC,cAAc,WAAW;CAC1B,CAAC,gBAAgB,aAAa;CAC9B,CAAC,eAAe,YAAY;CAC5B,CAAC,YAAY,SAAS;AACvB,CAAC;;;;;;;AAQD,MAAa,cAAc,UAAyC;CACnE,IAAI,CAAC,OACJ,OAAO,CAAC;CAET,MAAM,MAAiB,CAAC;CACxB,KAAK,MAAM,CAAC,QAAQ,aAAa,IAAI,gBAAgB,KAAK,GAAG;EAC5D,MAAM,MAAM,SAAS,IAAI,OAAO,YAAY,CAAC;EAC7C,IAAI,CAAC,OAAO,IAAI,SAAS,KAAA,GACxB;EAED,MAAM,QAAQ,SAAS,KAAK,EAAE,MAAM,GAAA,GAAiB;EACrD,IAAI,OACH,IAAI,OAAO;CAEb;CACA,OAAO;AACR"}
@@ -7,6 +7,7 @@ import { makeServerTrack } from "./ingest/serverTrack.js";
7
7
  import { composeGeoResolvers } from "./geo/composeGeoResolvers.js";
8
8
  import { platformHeaderResolver } from "./geo/geoResolver.js";
9
9
  import { maxmindResolver } from "./geo/maxmindResolver.js";
10
+ import { EVENT_SCAN_LIMIT, eventScanMeta } from "./eventScan.js";
10
11
  import { makeIngestHandler } from "./ingest/endpoint.js";
11
12
  import { createWriteBuffer } from "./ingest/writeBuffer.js";
12
13
  import { aggregateEvents, filtersToWhere } from "./query/eventAgg.js";
@@ -24,33 +25,39 @@ const metrics = new Set([
24
25
  "revenue",
25
26
  "scrollDepth"
26
27
  ]);
28
+ /**
29
+ * Everything the tracker can know about a hit. `source` is the traffic channel (`direct`,
30
+ * `search`, `social`, `email`, `paid`, `referral`) and `referrer` the host it came from, so a
31
+ * report can ask how much of the month was paid without reading a list of hostnames.
32
+ */
27
33
  const dimensions = new Set([
28
34
  "page",
29
- "country",
35
+ "referrer",
30
36
  "source",
37
+ "utmSource",
38
+ "utmMedium",
39
+ "utmCampaign",
40
+ "utmContent",
41
+ "utmTerm",
31
42
  "device",
43
+ "browser",
44
+ "os",
45
+ "country",
46
+ "region",
47
+ "city",
48
+ "language",
32
49
  "event",
33
50
  "goal"
34
51
  ]);
35
- const REALTIME_EVENT_LIMIT = 5e4;
36
52
  const baseCapabilities = {
37
53
  perPageQuery: true,
38
54
  realtime: true,
39
55
  realtimeWindowMinutes: 60,
40
- comparison: true,
41
56
  minGranularity: "hour",
42
57
  maxLookbackDays: null,
43
58
  metrics,
44
59
  dimensions,
45
- filters: new Set([
46
- "page",
47
- "country",
48
- "region",
49
- "city",
50
- "device",
51
- "source",
52
- "event"
53
- ]),
60
+ filters: new Set([...dimensions].filter((dimension) => dimension !== "goal")),
54
61
  filterOperators: new Set(["eq", "contains"]),
55
62
  batchPageReport: true,
56
63
  rateLimit: null,
@@ -96,7 +103,7 @@ async function queryEvents(payload, q, ctx) {
96
103
  const { docs } = await payload.find({
97
104
  collection: EVENTS_SLUG,
98
105
  where,
99
- limit: REALTIME_EVENT_LIMIT,
106
+ limit: EVENT_SCAN_LIMIT,
100
107
  pagination: false,
101
108
  depth: 0,
102
109
  sort: "-timestamp"
@@ -112,16 +119,14 @@ async function queryEvents(payload, q, ctx) {
112
119
  order: q.order,
113
120
  limit: q.limit
114
121
  });
115
- const meta = {
116
- provider: "native",
117
- fetchedAt
118
- };
119
- if (clamped) meta.clamped = true;
120
- if (events.length >= REALTIME_EVENT_LIMIT) meta.sampled = true;
121
122
  return {
122
123
  rows,
123
124
  totals,
124
- meta
125
+ meta: eventScanMeta({
126
+ fetchedAt,
127
+ eventCount: events.length,
128
+ clamped
129
+ })
125
130
  };
126
131
  }
127
132
  function native(options = {}) {
@@ -299,19 +304,20 @@ function native(options = {}) {
299
304
  ...q.path ? { path: { equals: q.path } } : {},
300
305
  ...scopeWhere(q)
301
306
  },
302
- limit: REALTIME_EVENT_LIMIT,
307
+ limit: EVENT_SCAN_LIMIT,
303
308
  pagination: false,
304
309
  depth: 0,
305
310
  sort: "-timestamp"
306
311
  });
307
- const { rows, totals } = buildRealtime(docs, q.dateRange, q.metrics);
312
+ const events = docs;
313
+ const { rows, totals } = buildRealtime(events, q.dateRange, q.metrics);
308
314
  return {
309
315
  rows,
310
316
  totals,
311
- meta: {
312
- provider: "native",
313
- fetchedAt
314
- }
317
+ meta: eventScanMeta({
318
+ fetchedAt,
319
+ eventCount: events.length
320
+ })
315
321
  };
316
322
  }
317
323
  };
@@ -1 +1 @@
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 { type IngestResolvers, makeIngestHandler } from './ingest/endpoint'\nimport { flushBatch } from './ingest/flushBatch'\nimport type { StoredEvent } from './ingest/normalizeEvent'\nimport { makeServerTrack } from './ingest/serverTrack'\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\t// The plugin's resolvers arrive in register(); server tracking reads them late so it\n\t// resolves the same scope, timezone and goals the endpoint does.\n\tlet resolvers: IngestResolvers = {}\n\n\tconst ingest = {\n\t\tpath: options.ingestPath ?? INGEST_PATH,\n\t\ttrack: makeServerTrack({\n\t\t\tgetPayload: () => payloadRef,\n\t\t\tgeoResolver,\n\t\t\tgetBuffer: () => buffer,\n\t\t\tgetResolvers: () => resolvers,\n\t\t}),\n\t}\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\tresolvers = {\n\t\t\t\tscope: scoped ? context?.resolveScope : undefined,\n\t\t\t\ttimezone: context?.resolveTimezone,\n\t\t\t\tgoals: context?.resolveGoals,\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, resolvers),\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":";;;;;;;;;;;;;;;;AA+CA,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,IAA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ACL,OAAO,KAAK,MAAM,GAAG,EAAE,KAAK;GAE7B,OAAO;IAAE;IAAM;IAAQ,MAAM;KAAE,UAAU;KAAU;IAAU;GAAE;EAChE;EACA,MAAM,SAAS,GAAmB,MAAgD;GACjF,IAAI,CAAC,YACJ,MAAM,IAAI,MAAM,+CAA+C;GAEhE,MAAM,YAAY,EAAE,UAAU,IAAI,YAAY;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"}
1
+ {"version":3,"file":"nativeAdapter.js","names":[],"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 { EVENT_SCAN_LIMIT, eventScanMeta } from './eventScan'\nimport { composeGeoResolvers } from './geo/composeGeoResolvers'\nimport { type GeoResolver, platformHeaderResolver } from './geo/geoResolver'\nimport { maxmindResolver } from './geo/maxmindResolver'\nimport { type IngestResolvers, makeIngestHandler } from './ingest/endpoint'\nimport { flushBatch } from './ingest/flushBatch'\nimport type { StoredEvent } from './ingest/normalizeEvent'\nimport { makeServerTrack } from './ingest/serverTrack'\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])\n/**\n * Everything the tracker can know about a hit. `source` is the traffic channel (`direct`,\n * `search`, `social`, `email`, `paid`, `referral`) and `referrer` the host it came from, so a\n * report can ask how much of the month was paid without reading a list of hostnames.\n */\nconst dimensions: ReadonlySet<DimensionKey> = new Set([\n\t'page',\n\t'referrer',\n\t'source',\n\t'utmSource',\n\t'utmMedium',\n\t'utmCampaign',\n\t'utmContent',\n\t'utmTerm',\n\t'device',\n\t'browser',\n\t'os',\n\t'country',\n\t'region',\n\t'city',\n\t'language',\n\t'event',\n\t'goal',\n])\n\nconst baseCapabilities: AnalyticsCapabilities = {\n\tperPageQuery: true,\n\trealtime: true,\n\trealtimeWindowMinutes: 60,\n\tminGranularity: 'hour',\n\tmaxLookbackDays: null,\n\tmetrics,\n\tdimensions,\n\t// Every dimension except `goal`, whose completions live in a json column rather than a\n\t// field a `where` can compare.\n\tfilters: new Set([...dimensions].filter((dimension) => dimension !== 'goal')),\n\t// No `matches`: every regex flavor the providers offer differs, and the two database\n\t// adapters have no portable one to match them with.\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: EVENT_SCAN_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\treturn { rows, totals, meta: eventScanMeta({ fetchedAt, eventCount: events.length, clamped }) }\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\t// The plugin's resolvers arrive in register(); server tracking reads them late so it\n\t// resolves the same scope, timezone and goals the endpoint does.\n\tlet resolvers: IngestResolvers = {}\n\n\tconst ingest = {\n\t\tpath: options.ingestPath ?? INGEST_PATH,\n\t\ttrack: makeServerTrack({\n\t\t\tgetPayload: () => payloadRef,\n\t\t\tgeoResolver,\n\t\t\tgetBuffer: () => buffer,\n\t\t\tgetResolvers: () => resolvers,\n\t\t}),\n\t}\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\tresolvers = {\n\t\t\t\tscope: scoped ? context?.resolveScope : undefined,\n\t\t\t\ttimezone: context?.resolveTimezone,\n\t\t\t\tgoals: context?.resolveGoals,\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, resolvers),\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: EVENT_SCAN_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: eventScanMeta({ fetchedAt, eventCount: events.length }) }\n\t\t},\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAgDA,MAAM,UAAkC,IAAI,IAAI;CAC/C;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACD,CAAC;;;;;;AAMD,MAAM,aAAwC,IAAI,IAAI;CACrD;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACD,CAAC;AAED,MAAM,mBAA0C;CAC/C,cAAc;CACd,UAAU;CACV,uBAAuB;CACvB,gBAAgB;CAChB,iBAAiB;CACjB;CACA;CAGA,SAAS,IAAI,IAAI,CAAC,GAAG,UAAU,EAAE,QAAQ,cAAc,cAAc,MAAM,CAAC;CAG5E,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,I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