@10x-media/analytics 0.1.0-beta.5 → 1.0.0-beta.10

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 (515) hide show
  1. package/CHANGELOG.md +270 -0
  2. package/LICENSE +21 -21
  3. package/README.md +15 -4
  4. package/dist/adapters/ga4/disableKey.d.ts +9 -0
  5. package/dist/adapters/ga4/disableKey.js +21 -0
  6. package/dist/adapters/ga4/disableKey.js.map +1 -0
  7. package/dist/adapters/ga4/eventName.d.ts +15 -0
  8. package/dist/adapters/ga4/eventName.js +19 -0
  9. package/dist/adapters/ga4/eventName.js.map +1 -0
  10. package/dist/adapters/ga4/ga4.d.ts +5 -0
  11. package/dist/adapters/ga4/ga4.js +186 -58
  12. package/dist/adapters/ga4/ga4.js.map +1 -1
  13. package/dist/adapters/ga4/measurementId.js +15 -0
  14. package/dist/adapters/ga4/measurementId.js.map +1 -0
  15. package/dist/adapters/goalRead.js +126 -0
  16. package/dist/adapters/goalRead.js.map +1 -0
  17. package/dist/adapters/http/fetchJson.js +1 -1
  18. package/dist/adapters/plausible/plausible.d.ts +25 -0
  19. package/dist/adapters/plausible/plausible.js +159 -38
  20. package/dist/adapters/plausible/plausible.js.map +1 -1
  21. package/dist/adapters/posthog/posthog.d.ts +10 -1
  22. package/dist/adapters/posthog/posthog.js +130 -21
  23. package/dist/adapters/posthog/posthog.js.map +1 -1
  24. package/dist/adapters/series.js +11 -1
  25. package/dist/adapters/series.js.map +1 -1
  26. package/dist/adapters/umami/umami.d.ts +13 -1
  27. package/dist/adapters/umami/umami.js +278 -26
  28. package/dist/adapters/umami/umami.js.map +1 -1
  29. package/dist/binding/resolvePath.js +18 -2
  30. package/dist/binding/resolvePath.js.map +1 -1
  31. package/dist/capture/getTrackerConfig.d.ts +20 -0
  32. package/dist/capture/getTrackerConfig.js +31 -0
  33. package/dist/capture/getTrackerConfig.js.map +1 -0
  34. package/dist/capture/mountPath.js +14 -0
  35. package/dist/capture/mountPath.js.map +1 -0
  36. package/dist/capture/proxyEndpoint.js +127 -0
  37. package/dist/capture/proxyEndpoint.js.map +1 -0
  38. package/dist/capture/proxyRoutes.js +108 -0
  39. package/dist/capture/proxyRoutes.js.map +1 -0
  40. package/dist/capture/requestBody.js +68 -0
  41. package/dist/capture/requestBody.js.map +1 -0
  42. package/dist/capture/slots.d.ts +8 -0
  43. package/dist/capture/slots.js +54 -0
  44. package/dist/capture/slots.js.map +1 -0
  45. package/dist/capture/trackerConfig.d.ts +33 -0
  46. package/dist/capture/trackerConfig.js +95 -0
  47. package/dist/capture/trackerConfig.js.map +1 -0
  48. package/dist/capture/trackerEndpoint.js +35 -0
  49. package/dist/capture/trackerEndpoint.js.map +1 -0
  50. package/dist/charts/BarList.d.ts +16 -1
  51. package/dist/charts/BarList.js +41 -19
  52. package/dist/charts/BarList.js.map +1 -1
  53. package/dist/charts/TrendChart.d.ts +16 -3
  54. package/dist/charts/TrendChart.js +172 -106
  55. package/dist/charts/TrendChart.js.map +1 -1
  56. package/dist/charts/bars.d.ts +4 -0
  57. package/dist/charts/bars.js +3 -1
  58. package/dist/charts/bars.js.map +1 -1
  59. package/dist/charts/bucket.js +10 -1
  60. package/dist/charts/bucket.js.map +1 -1
  61. package/dist/charts/spline.js +2 -2
  62. package/dist/charts/spline.js.map +1 -1
  63. package/dist/charts/styles.js +18 -1
  64. package/dist/charts/styles.js.map +1 -1
  65. package/dist/core/cacheKey.js +26 -3
  66. package/dist/core/cacheKey.js.map +1 -1
  67. package/dist/core/capabilities.d.ts +5 -1
  68. package/dist/core/capabilities.js +11 -2
  69. package/dist/core/capabilities.js.map +1 -1
  70. package/dist/core/capture.d.ts +49 -0
  71. package/dist/core/contract.d.ts +74 -5
  72. package/dist/core/contract.js +30 -1
  73. package/dist/core/contract.js.map +1 -1
  74. package/dist/core/granularity.js +1 -1
  75. package/dist/core/options.d.ts +164 -6
  76. package/dist/core/options.js +126 -3
  77. package/dist/core/options.js.map +1 -1
  78. package/dist/core/registry.d.ts +1 -0
  79. package/dist/core/scopeList.js +32 -0
  80. package/dist/core/scopeList.js.map +1 -0
  81. package/dist/core/scopedRead.js +101 -30
  82. package/dist/core/scopedRead.js.map +1 -1
  83. package/dist/core/serverEvent.d.ts +60 -0
  84. package/dist/core/serverEvent.js +12 -0
  85. package/dist/core/serverEvent.js.map +1 -0
  86. package/dist/exports/adapters/ga4.d.ts +2 -1
  87. package/dist/exports/adapters/ga4.js +2 -1
  88. package/dist/exports/adapters/native.d.ts +3 -1
  89. package/dist/exports/adapters/native.js +2 -1
  90. package/dist/exports/client.d.ts +13 -3
  91. package/dist/exports/client.js +10 -1
  92. package/dist/exports/geo.d.ts +2 -2
  93. package/dist/exports/next.d.ts +2 -1
  94. package/dist/exports/next.js +2 -1
  95. package/dist/exports/react.d.ts +4 -0
  96. package/dist/exports/react.js +5 -0
  97. package/dist/exports/rsc.d.ts +11 -3
  98. package/dist/exports/rsc.js +7 -2
  99. package/dist/exports/tracker.d.ts +21 -0
  100. package/dist/exports/tracker.js +15 -0
  101. package/dist/exports/types.d.ts +16 -3
  102. package/dist/exports/types.js +4 -2
  103. package/dist/fields/AnalyticsPanelClient.js +2 -2
  104. package/dist/fields/AnalyticsPanelField.js +1 -1
  105. package/dist/fields/AnalyticsStatField.js +2 -2
  106. package/dist/fields/config/FilterDimensionSelectField.d.ts +14 -0
  107. package/dist/fields/config/FilterDimensionSelectField.js +77 -0
  108. package/dist/fields/config/FilterDimensionSelectField.js.map +1 -0
  109. package/dist/fields/config/FilterOperatorSelectField.d.ts +14 -0
  110. package/dist/fields/config/FilterOperatorSelectField.js +75 -0
  111. package/dist/fields/config/FilterOperatorSelectField.js.map +1 -0
  112. package/dist/fields/config/MetricSelectField.js +3 -3
  113. package/dist/fields/config/MetricSelectField.js.map +1 -1
  114. package/dist/fields/config/SourceSelectField.js +3 -3
  115. package/dist/fields/config/SourceSelectField.js.map +1 -1
  116. package/dist/fields/config/fetchSources.d.ts +5 -1
  117. package/dist/fields/config/fetchSources.js +7 -3
  118. package/dist/fields/config/fetchSources.js.map +1 -1
  119. package/dist/fields/config/narrow.js +2 -0
  120. package/dist/fields/config/narrow.js.map +1 -1
  121. package/dist/fields/config/sourcesState.js +19 -3
  122. package/dist/fields/config/sourcesState.js.map +1 -1
  123. package/dist/fields/config/useAnalyticsSources.d.ts +8 -3
  124. package/dist/fields/config/useAnalyticsSources.js +9 -6
  125. package/dist/fields/config/useAnalyticsSources.js.map +1 -1
  126. package/dist/fields/config/useFilterCapabilities.d.ts +28 -0
  127. package/dist/fields/config/useFilterCapabilities.js +52 -0
  128. package/dist/fields/config/useFilterCapabilities.js.map +1 -0
  129. package/dist/fields/readForDocument.d.ts +5 -0
  130. package/dist/fields/readForDocument.js +22 -10
  131. package/dist/fields/readForDocument.js.map +1 -1
  132. package/dist/goals/GoalSelectField.d.ts +7 -0
  133. package/dist/goals/GoalSelectField.js +77 -0
  134. package/dist/goals/GoalSelectField.js.map +1 -0
  135. package/dist/goals/collection.js +254 -0
  136. package/dist/goals/collection.js.map +1 -0
  137. package/dist/goals/currency.js +14 -0
  138. package/dist/goals/currency.js.map +1 -0
  139. package/dist/goals/fetchGoals.d.ts +18 -0
  140. package/dist/goals/fetchGoals.js +33 -0
  141. package/dist/goals/fetchGoals.js.map +1 -0
  142. package/dist/goals/goalField.d.ts +31 -0
  143. package/dist/goals/goalField.js +56 -0
  144. package/dist/goals/goalField.js.map +1 -0
  145. package/dist/goals/goalsEndpoint.d.ts +1 -0
  146. package/dist/goals/goalsEndpoint.js +58 -0
  147. package/dist/goals/goalsEndpoint.js.map +1 -0
  148. package/dist/goals/match.js +64 -0
  149. package/dist/goals/match.js.map +1 -0
  150. package/dist/goals/resolver.d.ts +8 -0
  151. package/dist/goals/resolver.js +147 -0
  152. package/dist/goals/resolver.js.map +1 -0
  153. package/dist/goals/trackGoalAction.d.ts +76 -0
  154. package/dist/goals/trackGoalAction.js +158 -0
  155. package/dist/goals/trackGoalAction.js.map +1 -0
  156. package/dist/goals/types.d.ts +33 -0
  157. package/dist/goals/types.js +7 -0
  158. package/dist/goals/types.js.map +1 -0
  159. package/dist/goals/useAnalyticsGoals.d.ts +20 -0
  160. package/dist/goals/useAnalyticsGoals.js +67 -0
  161. package/dist/goals/useAnalyticsGoals.js.map +1 -0
  162. package/dist/index.d.ts +14 -3
  163. package/dist/index.js +174 -19
  164. package/dist/index.js.map +1 -1
  165. package/dist/native/collections/events.js +65 -1
  166. package/dist/native/collections/events.js.map +1 -1
  167. package/dist/native/collections/rollups.js +39 -3
  168. package/dist/native/collections/rollups.js.map +1 -1
  169. package/dist/native/eventScan.js +25 -0
  170. package/dist/native/eventScan.js.map +1 -0
  171. package/dist/native/geo/composeGeoResolvers.js +2 -2
  172. package/dist/native/geo/composeGeoResolvers.js.map +1 -1
  173. package/dist/native/geo/geoResolver.d.ts +9 -2
  174. package/dist/native/geo/geoResolver.js.map +1 -1
  175. package/dist/native/geo/maxmindResolver.js +3 -5
  176. package/dist/native/geo/maxmindResolver.js.map +1 -1
  177. package/dist/native/ingest/bots.d.ts +11 -0
  178. package/dist/native/ingest/bots.js +71 -0
  179. package/dist/native/ingest/bots.js.map +1 -0
  180. package/dist/native/ingest/browser.js +47 -0
  181. package/dist/native/ingest/browser.js.map +1 -0
  182. package/dist/native/ingest/clientIp.js +20 -0
  183. package/dist/native/ingest/clientIp.js.map +1 -0
  184. package/dist/native/ingest/device.js +11 -3
  185. package/dist/native/ingest/device.js.map +1 -1
  186. package/dist/native/ingest/endpoint.d.ts +1 -0
  187. package/dist/native/ingest/endpoint.js +107 -21
  188. package/dist/native/ingest/endpoint.js.map +1 -1
  189. package/dist/native/ingest/flushBatch.js +29 -38
  190. package/dist/native/ingest/flushBatch.js.map +1 -1
  191. package/dist/native/ingest/language.js +14 -0
  192. package/dist/native/ingest/language.js.map +1 -0
  193. package/dist/native/ingest/normalizeEvent.d.ts +30 -0
  194. package/dist/native/ingest/normalizeEvent.js +115 -16
  195. package/dist/native/ingest/normalizeEvent.js.map +1 -1
  196. package/dist/native/ingest/referrer.js +48 -0
  197. package/dist/native/ingest/referrer.js.map +1 -0
  198. package/dist/native/ingest/requestHost.js +57 -0
  199. package/dist/native/ingest/requestHost.js.map +1 -0
  200. package/dist/native/ingest/resolveHostname.d.ts +29 -0
  201. package/dist/native/ingest/resolveHostname.js +52 -0
  202. package/dist/native/ingest/resolveHostname.js.map +1 -0
  203. package/dist/native/ingest/salt.js +27 -2
  204. package/dist/native/ingest/salt.js.map +1 -1
  205. package/dist/native/ingest/serverTrack.d.ts +20 -0
  206. package/dist/native/ingest/serverTrack.js +109 -0
  207. package/dist/native/ingest/serverTrack.js.map +1 -0
  208. package/dist/native/ingest/source.d.ts +11 -0
  209. package/dist/native/ingest/source.js +264 -16
  210. package/dist/native/ingest/source.js.map +1 -1
  211. package/dist/native/ingest/utm.js +33 -0
  212. package/dist/native/ingest/utm.js.map +1 -0
  213. package/dist/native/ingest/validate.js +26 -0
  214. package/dist/native/ingest/validate.js.map +1 -0
  215. package/dist/native/nativeAdapter.d.ts +37 -1
  216. package/dist/native/nativeAdapter.js +175 -22
  217. package/dist/native/nativeAdapter.js.map +1 -1
  218. package/dist/native/query/eventAgg.js +175 -0
  219. package/dist/native/query/eventAgg.js.map +1 -0
  220. package/dist/native/retention/pruneTask.js +53 -13
  221. package/dist/native/retention/pruneTask.js.map +1 -1
  222. package/dist/native/rollupAcc.js +12 -1
  223. package/dist/native/rollupAcc.js.map +1 -1
  224. package/dist/native/rollups/bucketKey.js +15 -0
  225. package/dist/native/rollups/bucketKey.js.map +1 -0
  226. package/dist/native/rollups/bumpRollups.js +116 -0
  227. package/dist/native/rollups/bumpRollups.js.map +1 -0
  228. package/dist/native/rollups/deltas.js +86 -11
  229. package/dist/native/rollups/deltas.js.map +1 -1
  230. package/dist/native/rollups/insertManyIfNew.js +93 -0
  231. package/dist/native/rollups/insertManyIfNew.js.map +1 -0
  232. package/dist/next/captureRewrites.d.ts +42 -0
  233. package/dist/next/captureRewrites.js +57 -0
  234. package/dist/next/captureRewrites.js.map +1 -0
  235. package/dist/next/posthogProxyRewrites.d.ts +5 -0
  236. package/dist/next/posthogProxyRewrites.js +16 -21
  237. package/dist/next/posthogProxyRewrites.js.map +1 -1
  238. package/dist/plugin/documentEndpoint.js +71 -24
  239. package/dist/plugin/documentEndpoint.js.map +1 -1
  240. package/dist/plugin/errors.d.ts +31 -0
  241. package/dist/plugin/errors.js +79 -0
  242. package/dist/plugin/errors.js.map +1 -0
  243. package/dist/plugin/goalHint.d.ts +1 -0
  244. package/dist/plugin/goalHint.js +24 -0
  245. package/dist/plugin/goalHint.js.map +1 -0
  246. package/dist/plugin/paths.js +17 -1
  247. package/dist/plugin/paths.js.map +1 -1
  248. package/dist/plugin/queryEndpoint.js +124 -0
  249. package/dist/plugin/queryEndpoint.js.map +1 -0
  250. package/dist/plugin/readContextForRequest.js +73 -0
  251. package/dist/plugin/readContextForRequest.js.map +1 -0
  252. package/dist/plugin/realtimeEndpoint.js +37 -16
  253. package/dist/plugin/realtimeEndpoint.js.map +1 -1
  254. package/dist/plugin/refreshEndpoint.js +113 -0
  255. package/dist/plugin/refreshEndpoint.js.map +1 -0
  256. package/dist/plugin/runtime.d.ts +1 -0
  257. package/dist/plugin/runtime.js +33 -1
  258. package/dist/plugin/runtime.js.map +1 -1
  259. package/dist/plugin/scopeChange.js +22 -0
  260. package/dist/plugin/scopeChange.js.map +1 -0
  261. package/dist/plugin/scopeFieldBoot.js +22 -0
  262. package/dist/plugin/scopeFieldBoot.js.map +1 -0
  263. package/dist/plugin/sourcesEndpoint.js +16 -41
  264. package/dist/plugin/sourcesEndpoint.js.map +1 -1
  265. package/dist/plugin/warmTask.js +0 -0
  266. package/dist/plugin/warmTask.js.map +1 -1
  267. package/dist/providers/collection.js +89 -29
  268. package/dist/providers/collection.js.map +1 -1
  269. package/dist/providers/factory.js +11 -3
  270. package/dist/providers/factory.js.map +1 -1
  271. package/dist/providers/resolver.js.map +1 -1
  272. package/dist/query/dates.js +64 -0
  273. package/dist/query/dates.js.map +1 -0
  274. package/dist/query/errors.d.ts +14 -0
  275. package/dist/query/fetchQuery.d.ts +79 -0
  276. package/dist/query/fetchQuery.js +117 -0
  277. package/dist/query/fetchQuery.js.map +1 -0
  278. package/dist/query/parse.d.ts +22 -0
  279. package/dist/query/parse.js +168 -0
  280. package/dist/query/parse.js.map +1 -0
  281. package/dist/query/response.d.ts +39 -0
  282. package/dist/query/response.js +11 -0
  283. package/dist/query/response.js.map +1 -0
  284. package/dist/react/AnalyticsProvider.d.ts +50 -0
  285. package/dist/react/AnalyticsProvider.js +67 -0
  286. package/dist/react/AnalyticsProvider.js.map +1 -0
  287. package/dist/react/TrackerBoot.d.ts +22 -0
  288. package/dist/react/TrackerBoot.js +30 -0
  289. package/dist/react/TrackerBoot.js.map +1 -0
  290. package/dist/react/useAnalytics.d.ts +20 -0
  291. package/dist/react/useAnalytics.js +52 -0
  292. package/dist/react/useAnalytics.js.map +1 -0
  293. package/dist/rsc/AnalyticsScripts.d.ts +30 -0
  294. package/dist/rsc/AnalyticsScripts.js +41 -0
  295. package/dist/rsc/AnalyticsScripts.js.map +1 -0
  296. package/dist/surfacing/cacheStore.d.ts +1 -0
  297. package/dist/surfacing/cacheStore.js +18 -2
  298. package/dist/surfacing/cacheStore.js.map +1 -1
  299. package/dist/surfacing/engine.d.ts +1 -0
  300. package/dist/surfacing/engine.js +47 -6
  301. package/dist/surfacing/engine.js.map +1 -1
  302. package/dist/surfacing/epoch.d.ts +1 -0
  303. package/dist/surfacing/epoch.js +72 -0
  304. package/dist/surfacing/epoch.js.map +1 -0
  305. package/dist/surfacing/queue.js +22 -6
  306. package/dist/surfacing/queue.js.map +1 -1
  307. package/dist/surfacing/rateLimiter.js +130 -0
  308. package/dist/surfacing/rateLimiter.js.map +1 -0
  309. package/dist/surfacing/retryPolicy.js +15 -0
  310. package/dist/surfacing/retryPolicy.js.map +1 -0
  311. package/dist/sync/collection.js +23 -7
  312. package/dist/sync/collection.js.map +1 -1
  313. package/dist/sync/syncTask.js +68 -33
  314. package/dist/sync/syncTask.js.map +1 -1
  315. package/dist/testing/memoryAdapter.d.ts +2 -0
  316. package/dist/testing/memoryAdapter.js +17 -1
  317. package/dist/testing/memoryAdapter.js.map +1 -1
  318. package/dist/timeframe/tz.js +17 -6
  319. package/dist/timeframe/tz.js.map +1 -1
  320. package/dist/tracker/autoCapture.d.ts +48 -0
  321. package/dist/tracker/autoCapture.js +129 -0
  322. package/dist/tracker/autoCapture.js.map +1 -0
  323. package/dist/tracker/consent.d.ts +24 -0
  324. package/dist/tracker/consent.js +48 -0
  325. package/dist/tracker/consent.js.map +1 -0
  326. package/dist/tracker/createTracker.d.ts +18 -0
  327. package/dist/tracker/createTracker.js +215 -0
  328. package/dist/tracker/createTracker.js.map +1 -0
  329. package/dist/tracker/exclusion.d.ts +28 -0
  330. package/dist/tracker/exclusion.js +50 -0
  331. package/dist/tracker/exclusion.js.map +1 -0
  332. package/dist/tracker/initTracker.d.ts +22 -0
  333. package/dist/tracker/initTracker.js +33 -0
  334. package/dist/tracker/initTracker.js.map +1 -0
  335. package/dist/tracker/loadScript.d.ts +14 -0
  336. package/dist/tracker/loadScript.js +41 -0
  337. package/dist/tracker/loadScript.js.map +1 -0
  338. package/dist/tracker/pageTracking.d.ts +17 -0
  339. package/dist/tracker/pageTracking.js +38 -0
  340. package/dist/tracker/pageTracking.js.map +1 -0
  341. package/dist/tracker/registry.d.ts +27 -0
  342. package/dist/tracker/registry.js +73 -0
  343. package/dist/tracker/registry.js.map +1 -0
  344. package/dist/tracker/sinks/ga4.d.ts +29 -0
  345. package/dist/tracker/sinks/ga4.js +30 -0
  346. package/dist/tracker/sinks/ga4.js.map +1 -0
  347. package/dist/tracker/sinks/native.d.ts +22 -0
  348. package/dist/tracker/sinks/native.js +56 -0
  349. package/dist/tracker/sinks/native.js.map +1 -0
  350. package/dist/tracker/sinks/plausible.d.ts +20 -0
  351. package/dist/tracker/sinks/plausible.js +32 -0
  352. package/dist/tracker/sinks/plausible.js.map +1 -0
  353. package/dist/tracker/sinks/posthog.d.ts +18 -0
  354. package/dist/tracker/sinks/posthog.js +30 -0
  355. package/dist/tracker/sinks/posthog.js.map +1 -0
  356. package/dist/tracker/sinks/umami.d.ts +18 -0
  357. package/dist/tracker/sinks/umami.js +23 -0
  358. package/dist/tracker/sinks/umami.js.map +1 -0
  359. package/dist/tracker/sinks/vendor.d.ts +14 -0
  360. package/dist/tracker/sinks/vendor.js +81 -0
  361. package/dist/tracker/sinks/vendor.js.map +1 -0
  362. package/dist/tracker/transport.js +28 -0
  363. package/dist/tracker/transport.js.map +1 -0
  364. package/dist/tracker/types.d.ts +91 -0
  365. package/dist/translations/ar.js +221 -0
  366. package/dist/translations/ar.js.map +1 -0
  367. package/dist/translations/de.js +145 -1
  368. package/dist/translations/de.js.map +1 -1
  369. package/dist/translations/en.js +145 -1
  370. package/dist/translations/en.js.map +1 -1
  371. package/dist/translations/es.js +221 -0
  372. package/dist/translations/es.js.map +1 -0
  373. package/dist/translations/fr.js +221 -0
  374. package/dist/translations/fr.js.map +1 -0
  375. package/dist/translations/id.js +221 -0
  376. package/dist/translations/id.js.map +1 -0
  377. package/dist/translations/index.d.ts +10 -1
  378. package/dist/translations/index.js +19 -1
  379. package/dist/translations/index.js.map +1 -1
  380. package/dist/translations/keys.d.ts +144 -0
  381. package/dist/translations/keys.js +145 -1
  382. package/dist/translations/keys.js.map +1 -1
  383. package/dist/translations/ko.js +221 -0
  384. package/dist/translations/ko.js.map +1 -0
  385. package/dist/translations/metricKeys.js +8 -2
  386. package/dist/translations/metricKeys.js.map +1 -1
  387. package/dist/translations/pt.js +221 -0
  388. package/dist/translations/pt.js.map +1 -0
  389. package/dist/translations/ru.js +221 -0
  390. package/dist/translations/ru.js.map +1 -0
  391. package/dist/translations/server.js.map +1 -1
  392. package/dist/translations/uk.js +221 -0
  393. package/dist/translations/uk.js.map +1 -0
  394. package/dist/translations/zh.js +221 -0
  395. package/dist/translations/zh.js.map +1 -0
  396. package/dist/view/AnalyticsNavLink.d.ts +19 -0
  397. package/dist/view/AnalyticsNavLink.js +40 -0
  398. package/dist/view/AnalyticsNavLink.js.map +1 -0
  399. package/dist/view/AnalyticsView.d.ts +23 -0
  400. package/dist/view/AnalyticsView.js +45 -0
  401. package/dist/view/AnalyticsView.js.map +1 -0
  402. package/dist/view/AnalyticsViewClient.d.ts +15 -0
  403. package/dist/view/AnalyticsViewClient.js +244 -0
  404. package/dist/view/AnalyticsViewClient.js.map +1 -0
  405. package/dist/view/authRedirect.js +32 -0
  406. package/dist/view/authRedirect.js.map +1 -0
  407. package/dist/view/conversionRate.js +18 -0
  408. package/dist/view/conversionRate.js.map +1 -0
  409. package/dist/view/dayRange.js +68 -0
  410. package/dist/view/dayRange.js.map +1 -0
  411. package/dist/view/gating.d.ts +1 -0
  412. package/dist/view/gating.js +142 -0
  413. package/dist/view/gating.js.map +1 -0
  414. package/dist/view/labels.js +76 -0
  415. package/dist/view/labels.js.map +1 -0
  416. package/dist/view/registerView.js +48 -0
  417. package/dist/view/registerView.js.map +1 -0
  418. package/dist/view/sections/Breakdowns.js +198 -0
  419. package/dist/view/sections/Breakdowns.js.map +1 -0
  420. package/dist/view/sections/EmptyStates.js +92 -0
  421. package/dist/view/sections/EmptyStates.js.map +1 -0
  422. package/dist/view/sections/FilterChips.js +46 -0
  423. package/dist/view/sections/FilterChips.js.map +1 -0
  424. package/dist/view/sections/GoalsPanel.js +82 -0
  425. package/dist/view/sections/GoalsPanel.js.map +1 -0
  426. package/dist/view/sections/OverviewCards.js +65 -0
  427. package/dist/view/sections/OverviewCards.js.map +1 -0
  428. package/dist/view/sections/RealtimeStrip.js +80 -0
  429. package/dist/view/sections/RealtimeStrip.js.map +1 -0
  430. package/dist/view/sections/Toolbar.js +207 -0
  431. package/dist/view/sections/Toolbar.js.map +1 -0
  432. package/dist/view/sections/Trend.js +125 -0
  433. package/dist/view/sections/Trend.js.map +1 -0
  434. package/dist/view/sections/styles.js +76 -0
  435. package/dist/view/sections/styles.js.map +1 -0
  436. package/dist/view/state.js +211 -0
  437. package/dist/view/state.js.map +1 -0
  438. package/dist/view/useViewQueries.js +153 -0
  439. package/dist/view/useViewQueries.js.map +1 -0
  440. package/dist/view/viewProps.d.ts +40 -0
  441. package/dist/view/viewProps.js +90 -0
  442. package/dist/view/viewProps.js.map +1 -0
  443. package/dist/widgets/AnalyticsBreakdownWidget.d.ts +2 -1
  444. package/dist/widgets/AnalyticsBreakdownWidget.js +78 -11
  445. package/dist/widgets/AnalyticsBreakdownWidget.js.map +1 -1
  446. package/dist/widgets/AnalyticsGoalsWidget.d.ts +14 -0
  447. package/dist/widgets/AnalyticsGoalsWidget.js +196 -0
  448. package/dist/widgets/AnalyticsGoalsWidget.js.map +1 -0
  449. package/dist/widgets/AnalyticsMetricWidget.d.ts +2 -1
  450. package/dist/widgets/AnalyticsMetricWidget.js +63 -13
  451. package/dist/widgets/AnalyticsMetricWidget.js.map +1 -1
  452. package/dist/widgets/AnalyticsRealtimeWidget.d.ts +2 -1
  453. package/dist/widgets/AnalyticsRealtimeWidget.js +44 -22
  454. package/dist/widgets/AnalyticsRealtimeWidget.js.map +1 -1
  455. package/dist/widgets/AnalyticsTrendWidget.d.ts +2 -1
  456. package/dist/widgets/AnalyticsTrendWidget.js +79 -16
  457. package/dist/widgets/AnalyticsTrendWidget.js.map +1 -1
  458. package/dist/widgets/RealtimeCounter.d.ts +4 -0
  459. package/dist/widgets/RealtimeCounter.js +18 -4
  460. package/dist/widgets/RealtimeCounter.js.map +1 -1
  461. package/dist/widgets/WidgetViewLink.js +17 -0
  462. package/dist/widgets/WidgetViewLink.js.map +1 -0
  463. package/dist/widgets/breakdownTypes.js +38 -0
  464. package/dist/widgets/breakdownTypes.js.map +1 -1
  465. package/dist/widgets/cardChrome.js +31 -7
  466. package/dist/widgets/cardChrome.js.map +1 -1
  467. package/dist/widgets/comparison.js +15 -1
  468. package/dist/widgets/comparison.js.map +1 -1
  469. package/dist/widgets/defaults.d.ts +6 -2
  470. package/dist/widgets/defaults.js +78 -23
  471. package/dist/widgets/defaults.js.map +1 -1
  472. package/dist/widgets/filterCaption.js +27 -0
  473. package/dist/widgets/filterCaption.js.map +1 -0
  474. package/dist/widgets/filterField.d.ts +19 -0
  475. package/dist/widgets/filterField.js +75 -0
  476. package/dist/widgets/filterField.js.map +1 -0
  477. package/dist/widgets/prepareWidgetRead.d.ts +7 -0
  478. package/dist/widgets/prepareWidgetRead.js +74 -0
  479. package/dist/widgets/prepareWidgetRead.js.map +1 -0
  480. package/dist/widgets/range.js +43 -12
  481. package/dist/widgets/range.js.map +1 -1
  482. package/dist/widgets/readForWidget.d.ts +26 -3
  483. package/dist/widgets/readForWidget.js +48 -44
  484. package/dist/widgets/readForWidget.js.map +1 -1
  485. package/dist/widgets/readForWidgetBreakdown.d.ts +34 -2
  486. package/dist/widgets/readForWidgetBreakdown.js +53 -49
  487. package/dist/widgets/readForWidgetBreakdown.js.map +1 -1
  488. package/dist/widgets/readForWidgetGoals.d.ts +67 -0
  489. package/dist/widgets/readForWidgetGoals.js +140 -0
  490. package/dist/widgets/readForWidgetGoals.js.map +1 -0
  491. package/dist/widgets/readForWidgetRealtime.d.ts +8 -0
  492. package/dist/widgets/readForWidgetRealtime.js +23 -21
  493. package/dist/widgets/readForWidgetRealtime.js.map +1 -1
  494. package/dist/widgets/readForWidgetSeries.d.ts +26 -2
  495. package/dist/widgets/readForWidgetSeries.js +78 -53
  496. package/dist/widgets/readForWidgetSeries.js.map +1 -1
  497. package/dist/widgets/readMeta.js +18 -0
  498. package/dist/widgets/readMeta.js.map +1 -0
  499. package/dist/widgets/realtimePoll.js +13 -1
  500. package/dist/widgets/realtimePoll.js.map +1 -1
  501. package/dist/widgets/registerWidgets.js +62 -10
  502. package/dist/widgets/registerWidgets.js.map +1 -1
  503. package/dist/widgets/types.d.ts +15 -0
  504. package/dist/widgets/types.js +16 -2
  505. package/dist/widgets/types.js.map +1 -1
  506. package/dist/widgets/viewLink.d.ts +22 -0
  507. package/dist/widgets/viewLink.js +86 -0
  508. package/dist/widgets/viewLink.js.map +1 -0
  509. package/package.json +28 -8
  510. package/dist/native/rollups/applyDistinctDeltas.js +0 -9
  511. package/dist/native/rollups/applyDistinctDeltas.js.map +0 -1
  512. package/dist/native/rollups/bumpRollup.js +0 -68
  513. package/dist/native/rollups/bumpRollup.js.map +0 -1
  514. package/dist/native/rollups/insertIfNew.js +0 -19
  515. package/dist/native/rollups/insertIfNew.js.map +0 -1
@@ -1,28 +1,114 @@
1
+ import { readCappedBody } from "../../capture/requestBody.js";
2
+ import { analyticsError, errorResponse } from "../../plugin/errors.js";
1
3
  import { flushBatch } from "./flushBatch.js";
2
4
  import { normalizeEvent } from "./normalizeEvent.js";
3
5
  import { dailySalt } from "./salt.js";
6
+ import { rawEventError } from "./validate.js";
7
+ import { isBot } from "./bots.js";
8
+ import { resolveEventHostname } from "./resolveHostname.js";
4
9
  //#region src/native/ingest/endpoint.ts
5
- const makeIngestHandler = (geoResolver, getBuffer = () => null, resolvers = {}) => async (req) => {
6
- const { scope: resolveScope, timezone: resolveTimezone } = resolvers;
7
- const raw = (req.headers.get("content-type") ?? "").startsWith("application/json") ? await req.json?.() : JSON.parse(await req.text?.() ?? "{}");
8
- if (!raw?.path || !raw?.hostname || raw.type !== "pageview" && raw.type !== "event") return Response.json({ error: "invalid payload" }, { status: 400 });
9
- const now = /* @__PURE__ */ new Date();
10
- const salt = await dailySalt(req.payload, now);
11
- const scope = resolveScope ? await resolveScope(req) ?? "" : void 0;
12
- const timezone = resolveTimezone ? await resolveTimezone(req, scope ?? null) : void 0;
13
- const event = await normalizeEvent({
14
- raw,
15
- headers: req.headers,
16
- geoResolver,
17
- salt,
18
- now,
19
- scope,
20
- timezone
21
- });
22
- const buffer = getBuffer();
23
- if (buffer) buffer.add(event);
24
- else await flushBatch(req.payload, [event]);
25
- return Response.json({ ok: true }, { status: 202 });
10
+ /**
11
+ * What an accepted beacon answers, and what a dropped one answers too: same status, same body,
12
+ * no header of its own and nothing logged per event, so the endpoint never tells a prober which
13
+ * hosts or tenants exist.
14
+ */
15
+ const accepted = () => Response.json({ ok: true }, { status: 202 });
16
+ const REQUEST_HOSTNAME = { kind: "request" };
17
+ /**
18
+ * What an operator has to hear about once, and the line that tells them where to look. A
19
+ * misconfigured resolver, a broken filter or a header-stripping proxy otherwise costs an
20
+ * install its traffic in silence, since the response cannot say any of it.
21
+ */
22
+ const WARN_REASONS = {
23
+ "no scope resolved": "analytics: ingest is dropping events because scopeResolver answered no scope for them. Check that resolver, and list any platform domain in platformHostnames so it ingests under the null scope. Logged once per process.",
24
+ "hostname refused": "analytics: ingest is dropping events because the hostname option refused the request host. Check that option, and platformHostnames for a platform domain. Logged once per process.",
25
+ "bot filter threw": "analytics: the filterBots function threw, so the event was kept. Check that function; every beacon counts while it throws. Logged once per process.",
26
+ "no user agent": "analytics: a beacon arrived with no user agent and was not recorded: an intermediary may be stripping the header. Logged once per process."
27
+ };
28
+ /** One event, not a session replay: far above any legitimate payload, far below a DoS. */
29
+ const MAX_INGEST_BODY_BYTES = 64 * 1024;
30
+ /**
31
+ * Parses the buffered body, answering undefined for anything the validator could not judge:
32
+ * a garbage POST is a 400 like any other bad payload rather than a thrown parse error that
33
+ * escapes into Payload's routeError as a logged 500.
34
+ */
35
+ const parseBody = (bytes) => {
36
+ try {
37
+ const parsed = JSON.parse(new TextDecoder().decode(bytes));
38
+ return typeof parsed === "object" && parsed !== null ? parsed : void 0;
39
+ } catch {
40
+ return;
41
+ }
42
+ };
43
+ /**
44
+ * The refusal for the first field an event failed on: naming it costs nothing and saves a
45
+ * tracker author guessing which one of four the endpoint refused. A body that is not an
46
+ * object at all fails the very check a typeless event does, so it names `type`.
47
+ */
48
+ const invalidField = (param) => errorResponse(400, analyticsError("invalid_param", `analytics: ${param} is missing or invalid`, param));
49
+ const makeIngestHandler = ({ geoResolver, getBuffer = () => null, resolvers = {}, attribution = {}, filterBots = isBot }) => {
50
+ const warned = /* @__PURE__ */ new Set();
51
+ const warnOnce = (req, reason) => {
52
+ if (!warned.has(reason)) {
53
+ warned.add(reason);
54
+ req.payload.logger?.warn(WARN_REASONS[reason]);
55
+ }
56
+ };
57
+ const drop = (req, reason) => {
58
+ warnOnce(req, reason);
59
+ return accepted();
60
+ };
61
+ return async (req) => {
62
+ const userAgent = req.headers.get("user-agent") ?? "";
63
+ let automated = false;
64
+ try {
65
+ automated = filterBots(userAgent);
66
+ } catch {
67
+ warnOnce(req, "bot filter threw");
68
+ }
69
+ if (automated) {
70
+ if (userAgent.trim() === "") warnOnce(req, "no user agent");
71
+ return accepted();
72
+ }
73
+ const { scope: resolveScope, timezone: resolveTimezone, goals: resolveGoals } = resolvers;
74
+ const read = await readCappedBody(req, MAX_INGEST_BODY_BYTES);
75
+ if (!read.ok) return read.reason === "too-large" ? errorResponse(413, analyticsError("payload_too_large", "analytics: the event body is too large")) : errorResponse(400, analyticsError("invalid_param", "analytics: the event body could not be read"));
76
+ const raw = parseBody(read.body);
77
+ if (raw === void 0) return invalidField("type");
78
+ const param = rawEventError(raw);
79
+ if (param !== void 0) return invalidField(param);
80
+ const now = /* @__PURE__ */ new Date();
81
+ const resolvedScope = resolveScope ? await resolveScope(req) : null;
82
+ const scope = resolveScope ? resolvedScope ?? "" : void 0;
83
+ const hostname = await resolveEventHostname({
84
+ option: attribution.hostname ?? REQUEST_HOSTNAME,
85
+ claimed: raw.hostname,
86
+ req,
87
+ scope: resolvedScope,
88
+ trustedProxyHops: attribution.trustedProxyHops
89
+ });
90
+ if (hostname === null) return drop(req, "hostname refused");
91
+ if (resolveScope !== void 0 && resolvedScope === null && !(attribution.platformHostnames?.has(hostname) ?? false)) return drop(req, "no scope resolved");
92
+ const salt = await dailySalt(req.payload, now);
93
+ const timezone = resolveTimezone ? await resolveTimezone(req, scope ?? null) : void 0;
94
+ const goals = resolveGoals ? await resolveGoals(req, scope ?? null) : void 0;
95
+ const event = await normalizeEvent({
96
+ raw,
97
+ hostname,
98
+ headers: req.headers,
99
+ geoResolver,
100
+ salt,
101
+ now,
102
+ scope,
103
+ timezone,
104
+ goals,
105
+ trustedProxyHops: attribution.trustedProxyHops
106
+ });
107
+ const buffer = getBuffer();
108
+ if (buffer) buffer.add(event);
109
+ else await flushBatch(req.payload, [event]);
110
+ return accepted();
111
+ };
26
112
  };
27
113
  //#endregion
28
114
  export { makeIngestHandler };
@@ -1 +1 @@
1
- {"version":3,"file":"endpoint.js","names":[],"sources":["../../../src/native/ingest/endpoint.ts"],"sourcesContent":["import type { PayloadHandler, PayloadRequest } from 'payload'\nimport type { GeoResolver } from '../geo/geoResolver'\nimport { flushBatch } from './flushBatch'\nimport { normalizeEvent, type RawEventInput, type StoredEvent } from './normalizeEvent'\nimport { dailySalt } from './salt'\nimport type { WriteBuffer } from './writeBuffer'\n\nexport interface IngestResolvers {\n\tscope?: (req: PayloadRequest) => Promise<string | null>\n\ttimezone?: (req: PayloadRequest, scope?: string | null) => Promise<string>\n}\n\nexport const makeIngestHandler =\n\t(\n\t\tgeoResolver: GeoResolver,\n\t\tgetBuffer: () => WriteBuffer<StoredEvent> | null = () => null,\n\t\tresolvers: IngestResolvers = {}\n\t): PayloadHandler =>\n\tasync (req) => {\n\t\tconst { scope: resolveScope, timezone: resolveTimezone } = resolvers\n\t\tconst ct = req.headers.get('content-type') ?? ''\n\t\tconst raw = (\n\t\t\tct.startsWith('application/json')\n\t\t\t\t? await req.json?.()\n\t\t\t\t: JSON.parse((await req.text?.()) ?? '{}')\n\t\t) as RawEventInput\n\t\tif (!raw?.path || !raw?.hostname || (raw.type !== 'pageview' && raw.type !== 'event')) {\n\t\t\treturn Response.json({ error: 'invalid payload' }, { status: 400 })\n\t\t}\n\t\tconst now = new Date()\n\t\tconst salt = await dailySalt(req.payload, now)\n\t\tconst scope = resolveScope ? ((await resolveScope(req)) ?? '') : undefined\n\t\tconst timezone = resolveTimezone ? await resolveTimezone(req, scope ?? null) : undefined\n\t\tconst event = await normalizeEvent({\n\t\t\traw,\n\t\t\theaders: req.headers,\n\t\t\tgeoResolver,\n\t\t\tsalt,\n\t\t\tnow,\n\t\t\tscope,\n\t\t\ttimezone,\n\t\t})\n\t\tconst buffer = getBuffer()\n\t\tif (buffer) {\n\t\t\tbuffer.add(event)\n\t\t} else {\n\t\t\tawait flushBatch(req.payload, [event])\n\t\t}\n\t\treturn Response.json({ ok: true }, { status: 202 })\n\t}\n"],"mappings":";;;;AAYA,MAAa,qBAEX,aACA,kBAAyD,MACzD,YAA6B,CAAC,MAE/B,OAAO,QAAQ;CACd,MAAM,EAAE,OAAO,cAAc,UAAU,oBAAoB;CAE3D,MAAM,OADK,IAAI,QAAQ,IAAI,cAAc,KAAK,IAE1C,WAAW,kBAAkB,IAC7B,MAAM,IAAI,OAAO,IACjB,KAAK,MAAO,MAAM,IAAI,OAAO,KAAM,IAAI;CAE3C,IAAI,CAAC,KAAK,QAAQ,CAAC,KAAK,YAAa,IAAI,SAAS,cAAc,IAAI,SAAS,SAC5E,OAAO,SAAS,KAAK,EAAE,OAAO,kBAAkB,GAAG,EAAE,QAAQ,IAAI,CAAC;CAEnE,MAAM,sBAAM,IAAI,KAAK;CACrB,MAAM,OAAO,MAAM,UAAU,IAAI,SAAS,GAAG;CAC7C,MAAM,QAAQ,eAAiB,MAAM,aAAa,GAAG,KAAM,KAAM,KAAA;CACjE,MAAM,WAAW,kBAAkB,MAAM,gBAAgB,KAAK,SAAS,IAAI,IAAI,KAAA;CAC/E,MAAM,QAAQ,MAAM,eAAe;EAClC;EACA,SAAS,IAAI;EACb;EACA;EACA;EACA;EACA;CACD,CAAC;CACD,MAAM,SAAS,UAAU;CACzB,IAAI,QACH,OAAO,IAAI,KAAK;MAEhB,MAAM,WAAW,IAAI,SAAS,CAAC,KAAK,CAAC;CAEtC,OAAO,SAAS,KAAK,EAAE,IAAI,KAAK,GAAG,EAAE,QAAQ,IAAI,CAAC;AACnD"}
1
+ {"version":3,"file":"endpoint.js","names":[],"sources":["../../../src/native/ingest/endpoint.ts"],"sourcesContent":["import type { PayloadHandler, PayloadRequest } from 'payload'\nimport { readCappedBody } from '../../capture/requestBody'\nimport type { Goal } from '../../goals/types'\nimport { analyticsError, errorResponse } from '../../plugin/errors'\nimport type { GeoResolver } from '../geo/geoResolver'\nimport { type BotFilter, isBot } from './bots'\nimport { flushBatch } from './flushBatch'\nimport { normalizeEvent, type RawEventInput, type StoredEvent } from './normalizeEvent'\nimport { type ResolvedHostnameOption, resolveEventHostname } from './resolveHostname'\nimport { dailySalt } from './salt'\nimport { type RawEventField, rawEventError } from './validate'\nimport type { WriteBuffer } from './writeBuffer'\n\nexport interface IngestResolvers {\n\tscope?: (req: PayloadRequest) => Promise<string | null>\n\ttimezone?: (req: PayloadRequest, scope?: string | null) => Promise<string>\n\tgoals?: (req: PayloadRequest, scope?: string | null) => Promise<Goal[]>\n}\n\n/** How the handler reads the request behind an event, as opposed to what the body claims. */\nexport interface IngestAttribution {\n\ttrustedProxyHops?: number\n\t/** Resolved `hostname` option; absent attributes the event to the request's own host. */\n\thostname?: ResolvedHostnameOption\n\t/** Hostnames that ingest on a scoped install even when the request resolves no scope. */\n\tplatformHostnames?: ReadonlySet<string>\n}\n\n/**\n * What an accepted beacon answers, and what a dropped one answers too: same status, same body,\n * no header of its own and nothing logged per event, so the endpoint never tells a prober which\n * hosts or tenants exist.\n */\nconst accepted = (): Response => Response.json({ ok: true }, { status: 202 })\n\nconst REQUEST_HOSTNAME: ResolvedHostnameOption = { kind: 'request' }\n\n/**\n * What an operator has to hear about once, and the line that tells them where to look. A\n * misconfigured resolver, a broken filter or a header-stripping proxy otherwise costs an\n * install its traffic in silence, since the response cannot say any of it.\n */\nconst WARN_REASONS = {\n\t'no scope resolved':\n\t\t'analytics: ingest is dropping events because scopeResolver answered no scope for them. Check that resolver, and list any platform domain in platformHostnames so it ingests under the null scope. Logged once per process.',\n\t'hostname refused':\n\t\t'analytics: ingest is dropping events because the hostname option refused the request host. Check that option, and platformHostnames for a platform domain. Logged once per process.',\n\t'bot filter threw':\n\t\t'analytics: the filterBots function threw, so the event was kept. Check that function; every beacon counts while it throws. Logged once per process.',\n\t'no user agent':\n\t\t'analytics: a beacon arrived with no user agent and was not recorded: an intermediary may be stripping the header. Logged once per process.',\n} as const\n\ntype WarnReason = keyof typeof WARN_REASONS\n\n/** One event, not a session replay: far above any legitimate payload, far below a DoS. */\nexport const MAX_INGEST_BODY_BYTES = 64 * 1024\n\n/**\n * Parses the buffered body, answering undefined for anything the validator could not judge:\n * a garbage POST is a 400 like any other bad payload rather than a thrown parse error that\n * escapes into Payload's routeError as a logged 500.\n */\nconst parseBody = (bytes: ArrayBuffer): RawEventInput | undefined => {\n\ttry {\n\t\tconst parsed: unknown = JSON.parse(new TextDecoder().decode(bytes))\n\t\treturn typeof parsed === 'object' && parsed !== null ? (parsed as RawEventInput) : undefined\n\t} catch {\n\t\treturn undefined\n\t}\n}\n\n/**\n * The refusal for the first field an event failed on: naming it costs nothing and saves a\n * tracker author guessing which one of four the endpoint refused. A body that is not an\n * object at all fails the very check a typeless event does, so it names `type`.\n */\nconst invalidField = (param: RawEventField): Response =>\n\terrorResponse(\n\t\t400,\n\t\tanalyticsError('invalid_param', `analytics: ${param} is missing or invalid`, param)\n\t)\n\nexport interface IngestHandlerOptions {\n\tgeoResolver: GeoResolver\n\tgetBuffer?: () => WriteBuffer<StoredEvent> | null\n\tresolvers?: IngestResolvers\n\tattribution?: IngestAttribution\n\t/** Which agents never reach the pipeline. Defaults to the built-in `isBot`. */\n\tfilterBots?: BotFilter\n}\n\nexport const makeIngestHandler = ({\n\tgeoResolver,\n\tgetBuffer = () => null,\n\tresolvers = {},\n\tattribution = {},\n\tfilterBots = isBot,\n}: IngestHandlerOptions): PayloadHandler => {\n\t// One line per reason for the life of the handler, which is the life of the process: a\n\t// public endpoint must not be a log amplifier, and an attacker chooses how often it drops.\n\tconst warned = new Set<WarnReason>()\n\tconst warnOnce = (req: PayloadRequest, reason: WarnReason): void => {\n\t\tif (!warned.has(reason)) {\n\t\t\twarned.add(reason)\n\t\t\treq.payload.logger?.warn(WARN_REASONS[reason])\n\t\t}\n\t}\n\tconst drop = (req: PayloadRequest, reason: WarnReason): Response => {\n\t\twarnOnce(req, reason)\n\t\treturn accepted()\n\t}\n\n\treturn async (req) => {\n\t\t// First, before the body is even read, so a crawler costs one header lookup. Its beacon\n\t\t// is answered exactly like a kept one and nothing is logged: bots are expected traffic\n\t\t// rather than a misconfiguration an operator has to hear about once per process.\n\t\tconst userAgent = req.headers.get('user-agent') ?? ''\n\t\tlet automated = false\n\t\ttry {\n\t\t\tautomated = filterBots(userAgent)\n\t\t} catch {\n\t\t\twarnOnce(req, 'bot filter threw')\n\t\t}\n\t\tif (automated) {\n\t\t\t// The exception to the silence: an agent-less beacon is indistinguishable from an\n\t\t\t// intermediary stripping the header, which would zero an install's numbers.\n\t\t\tif (userAgent.trim() === '') {\n\t\t\t\twarnOnce(req, 'no user agent')\n\t\t\t}\n\t\t\treturn accepted()\n\t\t}\n\t\tconst { scope: resolveScope, timezone: resolveTimezone, goals: resolveGoals } = resolvers\n\t\t// Read like the capture proxy does, and for the same reasons: this is a public,\n\t\t// unauthenticated path, so the body is capped before it is buffered and a body that\n\t\t// dies in transit (a beacon from an unloading tab) answers 400 rather than throwing.\n\t\tconst read = await readCappedBody(req, MAX_INGEST_BODY_BYTES)\n\t\tif (!read.ok) {\n\t\t\treturn read.reason === 'too-large'\n\t\t\t\t? errorResponse(\n\t\t\t\t\t\t413,\n\t\t\t\t\t\tanalyticsError('payload_too_large', 'analytics: the event body is too large')\n\t\t\t\t\t)\n\t\t\t\t: errorResponse(\n\t\t\t\t\t\t400,\n\t\t\t\t\t\tanalyticsError('invalid_param', 'analytics: the event body could not be read')\n\t\t\t\t\t)\n\t\t}\n\t\tconst raw = parseBody(read.body)\n\t\tif (raw === undefined) {\n\t\t\treturn invalidField('type')\n\t\t}\n\t\tconst param = rawEventError(raw)\n\t\tif (param !== undefined) {\n\t\t\treturn invalidField(param)\n\t\t}\n\t\tconst now = new Date()\n\t\tconst resolvedScope = resolveScope ? await resolveScope(req) : null\n\t\tconst scope = resolveScope ? (resolvedScope ?? '') : undefined\n\t\tconst hostname = await resolveEventHostname({\n\t\t\toption: attribution.hostname ?? REQUEST_HOSTNAME,\n\t\t\tclaimed: raw.hostname,\n\t\t\treq,\n\t\t\tscope: resolvedScope,\n\t\t\ttrustedProxyHops: attribution.trustedProxyHops,\n\t\t})\n\t\tif (hostname === null) {\n\t\t\treturn drop(req, 'hostname refused')\n\t\t}\n\t\t// A scoped install keeps only what a scope answers for. Its platform hostnames are\n\t\t// infrastructure rather than tenants, so they ingest under the null scope.\n\t\tconst unscoped =\n\t\t\tresolveScope !== undefined &&\n\t\t\tresolvedScope === null &&\n\t\t\t!(attribution.platformHostnames?.has(hostname) ?? false)\n\t\tif (unscoped) {\n\t\t\treturn drop(req, 'no scope resolved')\n\t\t}\n\t\tconst salt = await dailySalt(req.payload, now)\n\t\tconst timezone = resolveTimezone ? await resolveTimezone(req, scope ?? null) : undefined\n\t\t// Goals resolve under the same scope the event is stamped with, so a tenant's event\n\t\t// can only ever complete that tenant's goals.\n\t\tconst goals = resolveGoals ? await resolveGoals(req, scope ?? null) : undefined\n\t\tconst event = await normalizeEvent({\n\t\t\traw,\n\t\t\thostname,\n\t\t\theaders: req.headers,\n\t\t\tgeoResolver,\n\t\t\tsalt,\n\t\t\tnow,\n\t\t\tscope,\n\t\t\ttimezone,\n\t\t\tgoals,\n\t\t\ttrustedProxyHops: attribution.trustedProxyHops,\n\t\t})\n\t\tconst buffer = getBuffer()\n\t\tif (buffer) {\n\t\t\tbuffer.add(event)\n\t\t} else {\n\t\t\tawait flushBatch(req.payload, [event])\n\t\t}\n\t\treturn accepted()\n\t}\n}\n"],"mappings":"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"}
@@ -1,10 +1,18 @@
1
- import "../collections/seen.js";
2
- import { bumpRollup } from "../rollups/bumpRollup.js";
3
- import { insertIfNew } from "../rollups/insertIfNew.js";
4
- import { bucketKey } from "../rollups/applyDistinctDeltas.js";
1
+ import { SEEN_SLUG } from "../collections/seen.js";
2
+ import { bucketKey } from "../rollups/bucketKey.js";
3
+ import { bumpRollups } from "../rollups/bumpRollups.js";
5
4
  import { computeRollupDeltas } from "../rollups/deltas.js";
5
+ import { insertManyIfNew } from "../rollups/insertManyIfNew.js";
6
6
  import { writeEvent } from "./writeEvent.js";
7
7
  //#region src/native/ingest/flushBatch.ts
8
+ /**
9
+ * Coalesces a batch of events into the fewest writes: events insert concurrently, base rollup
10
+ * increments are summed per bucket, and distinct visitor / session candidates are de-duplicated
11
+ * within the batch before one insert-if-new decides which of them the ledger had not seen.
12
+ * Whatever the batch size, that is three round trips for the rollups and the ledger rather
13
+ * than one per bucket. Correctness matches the per-event path because the seen ledger still
14
+ * de-dupes across batches, and its unique index still arbitrates between concurrent writers.
15
+ */
8
16
  async function flushBatch(payload, events) {
9
17
  if (events.length === 0) return;
10
18
  await Promise.all(events.map((event) => writeEvent(payload, event)));
@@ -13,14 +21,10 @@ async function flushBatch(payload, events) {
13
21
  for (const event of events) for (const delta of computeRollupDeltas(event)) {
14
22
  const bk = bucketKey(delta.key);
15
23
  const agg = base.get(bk);
16
- if (agg) {
17
- agg.pageviews += delta.inc.pageviews;
18
- agg.events += delta.inc.events;
19
- agg.durationMs += delta.inc.durationMs;
20
- agg.samples += delta.inc.samples;
21
- } else base.set(bk, {
24
+ if (agg) for (const metric of Object.keys(agg.inc)) agg.inc[metric] += delta.inc[metric];
25
+ else base.set(bk, {
22
26
  key: delta.key,
23
- ...delta.inc
27
+ inc: { ...delta.inc }
24
28
  });
25
29
  candidates.set(`${bk}|visitor|${event.visitorHash}`, {
26
30
  key: delta.key,
@@ -33,35 +37,22 @@ async function flushBatch(payload, events) {
33
37
  value: event.sessionId
34
38
  });
35
39
  }
36
- for (const agg of base.values()) await bumpRollup(payload, agg.key, {
37
- pageviews: agg.pageviews,
38
- events: agg.events,
39
- durationMs: agg.durationMs,
40
- samples: agg.samples
41
- });
42
- const distinct = /* @__PURE__ */ new Map();
43
- for (const candidate of candidates.values()) {
44
- if (!await insertIfNew(payload, "analytics-seen", {
45
- bucket: bucketKey(candidate.key),
46
- kind: candidate.kind,
47
- value: candidate.value,
48
- period: candidate.key.period
49
- })) continue;
50
- const metric = candidate.kind === "visitor" ? "visitors" : "sessions";
51
- const dk = `${bucketKey(candidate.key)}|${metric}`;
52
- const entry = distinct.get(dk);
53
- if (entry) entry.count += 1;
54
- else distinct.set(dk, {
40
+ await bumpRollups(payload, [...base.values()]);
41
+ const pending = [...candidates.values()];
42
+ const isNew = await insertManyIfNew(payload, SEEN_SLUG, pending.map((candidate) => ({
43
+ bucket: bucketKey(candidate.key),
44
+ kind: candidate.kind,
45
+ value: candidate.value,
46
+ period: candidate.key.period
47
+ })));
48
+ const distinct = [];
49
+ pending.forEach((candidate, index) => {
50
+ if (isNew[index]) distinct.push({
55
51
  key: candidate.key,
56
- metric,
57
- count: 1
52
+ inc: candidate.kind === "visitor" ? { visitors: 1 } : { sessions: 1 }
58
53
  });
59
- }
60
- for (const entry of distinct.values()) {
61
- const inc = {};
62
- inc[entry.metric] = entry.count;
63
- await bumpRollup(payload, entry.key, inc);
64
- }
54
+ });
55
+ await bumpRollups(payload, distinct);
65
56
  }
66
57
  //#endregion
67
58
  export { flushBatch };
@@ -1 +1 @@
1
- {"version":3,"file":"flushBatch.js","names":[],"sources":["../../../src/native/ingest/flushBatch.ts"],"sourcesContent":["import type { Payload } from 'payload'\nimport { SEEN_SLUG } from '../collections/seen'\nimport { bucketKey } from '../rollups/applyDistinctDeltas'\nimport { bumpRollup } from '../rollups/bumpRollup'\nimport { computeRollupDeltas, type RollupKey, type RollupMetric } from '../rollups/deltas'\nimport { insertIfNew } from '../rollups/insertIfNew'\nimport type { StoredEvent } from './normalizeEvent'\nimport { writeEvent } from './writeEvent'\n\ninterface BaseAgg {\n\tkey: RollupKey\n\tpageviews: number\n\tevents: number\n\tdurationMs: number\n\tsamples: number\n}\n\ninterface DistinctCandidate {\n\tkey: RollupKey\n\tkind: 'visitor' | 'session'\n\tvalue: string\n}\n\n// Coalesces a batch of events into the fewest writes: events insert concurrently, base\n// rollup increments are summed per bucket into one upsert each, and distinct visitor /\n// session candidates are de-duplicated within the batch before insert-if-new. Correctness\n// matches the per-event path because the seen ledger still de-dupes across batches.\nexport async function flushBatch(payload: Payload, events: StoredEvent[]): Promise<void> {\n\tif (events.length === 0) {\n\t\treturn\n\t}\n\tawait Promise.all(events.map((event) => writeEvent(payload, event)))\n\n\tconst base = new Map<string, BaseAgg>()\n\tconst candidates = new Map<string, DistinctCandidate>()\n\tfor (const event of events) {\n\t\tfor (const delta of computeRollupDeltas(event)) {\n\t\t\tconst bk = bucketKey(delta.key)\n\t\t\tconst agg = base.get(bk)\n\t\t\tif (agg) {\n\t\t\t\tagg.pageviews += delta.inc.pageviews\n\t\t\t\tagg.events += delta.inc.events\n\t\t\t\tagg.durationMs += delta.inc.durationMs\n\t\t\t\tagg.samples += delta.inc.samples\n\t\t\t} else {\n\t\t\t\tbase.set(bk, { key: delta.key, ...delta.inc })\n\t\t\t}\n\t\t\tcandidates.set(`${bk}|visitor|${event.visitorHash}`, {\n\t\t\t\tkey: delta.key,\n\t\t\t\tkind: 'visitor',\n\t\t\t\tvalue: event.visitorHash,\n\t\t\t})\n\t\t\tcandidates.set(`${bk}|session|${event.sessionId}`, {\n\t\t\t\tkey: delta.key,\n\t\t\t\tkind: 'session',\n\t\t\t\tvalue: event.sessionId,\n\t\t\t})\n\t\t}\n\t}\n\n\tfor (const agg of base.values()) {\n\t\tawait bumpRollup(payload, agg.key, {\n\t\t\tpageviews: agg.pageviews,\n\t\t\tevents: agg.events,\n\t\t\tdurationMs: agg.durationMs,\n\t\t\tsamples: agg.samples,\n\t\t})\n\t}\n\n\tconst distinct = new Map<string, { key: RollupKey; metric: RollupMetric; count: number }>()\n\tfor (const candidate of candidates.values()) {\n\t\tconst isNew = await insertIfNew(payload, SEEN_SLUG, {\n\t\t\tbucket: bucketKey(candidate.key),\n\t\t\tkind: candidate.kind,\n\t\t\tvalue: candidate.value,\n\t\t\tperiod: candidate.key.period,\n\t\t})\n\t\tif (!isNew) {\n\t\t\tcontinue\n\t\t}\n\t\tconst metric: RollupMetric = candidate.kind === 'visitor' ? 'visitors' : 'sessions'\n\t\tconst dk = `${bucketKey(candidate.key)}|${metric}`\n\t\tconst entry = distinct.get(dk)\n\t\tif (entry) {\n\t\t\tentry.count += 1\n\t\t} else {\n\t\t\tdistinct.set(dk, { key: candidate.key, metric, count: 1 })\n\t\t}\n\t}\n\n\tfor (const entry of distinct.values()) {\n\t\tconst inc: Partial<Record<RollupMetric, number>> = {}\n\t\tinc[entry.metric] = entry.count\n\t\tawait bumpRollup(payload, entry.key, inc)\n\t}\n}\n"],"mappings":";;;;;;;AA2BA,eAAsB,WAAW,SAAkB,QAAsC;CACxF,IAAI,OAAO,WAAW,GACrB;CAED,MAAM,QAAQ,IAAI,OAAO,KAAK,UAAU,WAAW,SAAS,KAAK,CAAC,CAAC;CAEnE,MAAM,uBAAO,IAAI,IAAqB;CACtC,MAAM,6BAAa,IAAI,IAA+B;CACtD,KAAK,MAAM,SAAS,QACnB,KAAK,MAAM,SAAS,oBAAoB,KAAK,GAAG;EAC/C,MAAM,KAAK,UAAU,MAAM,GAAG;EAC9B,MAAM,MAAM,KAAK,IAAI,EAAE;EACvB,IAAI,KAAK;GACR,IAAI,aAAa,MAAM,IAAI;GAC3B,IAAI,UAAU,MAAM,IAAI;GACxB,IAAI,cAAc,MAAM,IAAI;GAC5B,IAAI,WAAW,MAAM,IAAI;EAC1B,OACC,KAAK,IAAI,IAAI;GAAE,KAAK,MAAM;GAAK,GAAG,MAAM;EAAI,CAAC;EAE9C,WAAW,IAAI,GAAG,GAAG,WAAW,MAAM,eAAe;GACpD,KAAK,MAAM;GACX,MAAM;GACN,OAAO,MAAM;EACd,CAAC;EACD,WAAW,IAAI,GAAG,GAAG,WAAW,MAAM,aAAa;GAClD,KAAK,MAAM;GACX,MAAM;GACN,OAAO,MAAM;EACd,CAAC;CACF;CAGD,KAAK,MAAM,OAAO,KAAK,OAAO,GAC7B,MAAM,WAAW,SAAS,IAAI,KAAK;EAClC,WAAW,IAAI;EACf,QAAQ,IAAI;EACZ,YAAY,IAAI;EAChB,SAAS,IAAI;CACd,CAAC;CAGF,MAAM,2BAAW,IAAI,IAAqE;CAC1F,KAAK,MAAM,aAAa,WAAW,OAAO,GAAG;EAO5C,IAAI,CAAC,MANe,YAAY,SAAA,kBAAoB;GACnD,QAAQ,UAAU,UAAU,GAAG;GAC/B,MAAM,UAAU;GAChB,OAAO,UAAU;GACjB,QAAQ,UAAU,IAAI;EACvB,CAAC,GAEA;EAED,MAAM,SAAuB,UAAU,SAAS,YAAY,aAAa;EACzE,MAAM,KAAK,GAAG,UAAU,UAAU,GAAG,EAAE,GAAG;EAC1C,MAAM,QAAQ,SAAS,IAAI,EAAE;EAC7B,IAAI,OACH,MAAM,SAAS;OAEf,SAAS,IAAI,IAAI;GAAE,KAAK,UAAU;GAAK;GAAQ,OAAO;EAAE,CAAC;CAE3D;CAEA,KAAK,MAAM,SAAS,SAAS,OAAO,GAAG;EACtC,MAAM,MAA6C,CAAC;EACpD,IAAI,MAAM,UAAU,MAAM;EAC1B,MAAM,WAAW,SAAS,MAAM,KAAK,GAAG;CACzC;AACD"}
1
+ {"version":3,"file":"flushBatch.js","names":[],"sources":["../../../src/native/ingest/flushBatch.ts"],"sourcesContent":["import type { Payload } from 'payload'\nimport { SEEN_SLUG } from '../collections/seen'\nimport { bucketKey } from '../rollups/bucketKey'\nimport { bumpRollups, type RollupBump } from '../rollups/bumpRollups'\nimport { computeRollupDeltas, type RollupInc, type RollupKey } from '../rollups/deltas'\nimport { insertManyIfNew } from '../rollups/insertManyIfNew'\nimport type { StoredEvent } from './normalizeEvent'\nimport { writeEvent } from './writeEvent'\n\ninterface BaseAgg {\n\tkey: RollupKey\n\tinc: RollupInc\n}\n\ninterface DistinctCandidate {\n\tkey: RollupKey\n\tkind: 'visitor' | 'session'\n\tvalue: string\n}\n\n/**\n * Coalesces a batch of events into the fewest writes: events insert concurrently, base rollup\n * increments are summed per bucket, and distinct visitor / session candidates are de-duplicated\n * within the batch before one insert-if-new decides which of them the ledger had not seen.\n * Whatever the batch size, that is three round trips for the rollups and the ledger rather\n * than one per bucket. Correctness matches the per-event path because the seen ledger still\n * de-dupes across batches, and its unique index still arbitrates between concurrent writers.\n */\nexport async function flushBatch(payload: Payload, events: StoredEvent[]): Promise<void> {\n\tif (events.length === 0) {\n\t\treturn\n\t}\n\tawait Promise.all(events.map((event) => writeEvent(payload, event)))\n\n\tconst base = new Map<string, BaseAgg>()\n\tconst candidates = new Map<string, DistinctCandidate>()\n\tfor (const event of events) {\n\t\tfor (const delta of computeRollupDeltas(event)) {\n\t\t\tconst bk = bucketKey(delta.key)\n\t\t\tconst agg = base.get(bk)\n\t\t\tif (agg) {\n\t\t\t\tfor (const metric of Object.keys(agg.inc) as Array<keyof RollupInc>) {\n\t\t\t\t\tagg.inc[metric] += delta.inc[metric]\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tbase.set(bk, { key: delta.key, inc: { ...delta.inc } })\n\t\t\t}\n\t\t\tcandidates.set(`${bk}|visitor|${event.visitorHash}`, {\n\t\t\t\tkey: delta.key,\n\t\t\t\tkind: 'visitor',\n\t\t\t\tvalue: event.visitorHash,\n\t\t\t})\n\t\t\tcandidates.set(`${bk}|session|${event.sessionId}`, {\n\t\t\t\tkey: delta.key,\n\t\t\t\tkind: 'session',\n\t\t\t\tvalue: event.sessionId,\n\t\t\t})\n\t\t}\n\t}\n\n\tawait bumpRollups(payload, [...base.values()])\n\n\tconst pending = [...candidates.values()]\n\tconst isNew = await insertManyIfNew(\n\t\tpayload,\n\t\tSEEN_SLUG,\n\t\tpending.map((candidate) => ({\n\t\t\tbucket: bucketKey(candidate.key),\n\t\t\tkind: candidate.kind,\n\t\t\tvalue: candidate.value,\n\t\t\tperiod: candidate.key.period,\n\t\t}))\n\t)\n\t// One increment per ledger row this flush actually created; bumpRollups sums the ones that\n\t// land on the same bucket.\n\tconst distinct: RollupBump[] = []\n\tpending.forEach((candidate, index) => {\n\t\tif (isNew[index]) {\n\t\t\tdistinct.push({\n\t\t\t\tkey: candidate.key,\n\t\t\t\tinc: candidate.kind === 'visitor' ? { visitors: 1 } : { sessions: 1 },\n\t\t\t})\n\t\t}\n\t})\n\tawait bumpRollups(payload, distinct)\n}\n"],"mappings":";;;;;;;;;;;;;;;AA4BA,eAAsB,WAAW,SAAkB,QAAsC;CACxF,IAAI,OAAO,WAAW,GACrB;CAED,MAAM,QAAQ,IAAI,OAAO,KAAK,UAAU,WAAW,SAAS,KAAK,CAAC,CAAC;CAEnE,MAAM,uBAAO,IAAI,IAAqB;CACtC,MAAM,6BAAa,IAAI,IAA+B;CACtD,KAAK,MAAM,SAAS,QACnB,KAAK,MAAM,SAAS,oBAAoB,KAAK,GAAG;EAC/C,MAAM,KAAK,UAAU,MAAM,GAAG;EAC9B,MAAM,MAAM,KAAK,IAAI,EAAE;EACvB,IAAI,KACH,KAAK,MAAM,UAAU,OAAO,KAAK,IAAI,GAAG,GACvC,IAAI,IAAI,WAAW,MAAM,IAAI;OAG9B,KAAK,IAAI,IAAI;GAAE,KAAK,MAAM;GAAK,KAAK,EAAE,GAAG,MAAM,IAAI;EAAE,CAAC;EAEvD,WAAW,IAAI,GAAG,GAAG,WAAW,MAAM,eAAe;GACpD,KAAK,MAAM;GACX,MAAM;GACN,OAAO,MAAM;EACd,CAAC;EACD,WAAW,IAAI,GAAG,GAAG,WAAW,MAAM,aAAa;GAClD,KAAK,MAAM;GACX,MAAM;GACN,OAAO,MAAM;EACd,CAAC;CACF;CAGD,MAAM,YAAY,SAAS,CAAC,GAAG,KAAK,OAAO,CAAC,CAAC;CAE7C,MAAM,UAAU,CAAC,GAAG,WAAW,OAAO,CAAC;CACvC,MAAM,QAAQ,MAAM,gBACnB,SACA,WACA,QAAQ,KAAK,eAAe;EAC3B,QAAQ,UAAU,UAAU,GAAG;EAC/B,MAAM,UAAU;EAChB,OAAO,UAAU;EACjB,QAAQ,UAAU,IAAI;CACvB,EAAE,CACH;CAGA,MAAM,WAAyB,CAAC;CAChC,QAAQ,SAAS,WAAW,UAAU;EACrC,IAAI,MAAM,QACT,SAAS,KAAK;GACb,KAAK,UAAU;GACf,KAAK,UAAU,SAAS,YAAY,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU,EAAE;EACrE,CAAC;CAEH,CAAC;CACD,MAAM,YAAY,SAAS,QAAQ;AACpC"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * The visitor's preferred language from an `Accept-Language` header: the first tag, without
3
+ * its quality factor, lowercased (`de-DE,de;q=0.9` becomes `de-de`). A wildcard or an absent
4
+ * header states no preference, so nothing is reported.
5
+ */
6
+ const primaryLanguage = (acceptLanguage) => {
7
+ const tag = acceptLanguage?.split(",")[0]?.split(";")[0]?.trim().toLowerCase();
8
+ if (!tag || tag === "*") return;
9
+ return tag.slice(0, 16);
10
+ };
11
+ //#endregion
12
+ export { primaryLanguage };
13
+
14
+ //# sourceMappingURL=language.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"language.js","names":[],"sources":["../../../src/native/ingest/language.ts"],"sourcesContent":["/** Long enough for any real BCP 47 tag; a longer one is junk, not a language. */\nexport const MAX_LANGUAGE_LENGTH = 16\n\n/**\n * The visitor's preferred language from an `Accept-Language` header: the first tag, without\n * its quality factor, lowercased (`de-DE,de;q=0.9` becomes `de-de`). A wildcard or an absent\n * header states no preference, so nothing is reported.\n */\nexport const primaryLanguage = (acceptLanguage: string | null): string | undefined => {\n\tconst tag = acceptLanguage?.split(',')[0]?.split(';')[0]?.trim().toLowerCase()\n\tif (!tag || tag === '*') {\n\t\treturn undefined\n\t}\n\treturn tag.slice(0, MAX_LANGUAGE_LENGTH)\n}\n"],"mappings":";;;;;AAQA,MAAa,mBAAmB,mBAAsD;CACrF,MAAM,MAAM,gBAAgB,MAAM,GAAG,EAAE,IAAI,MAAM,GAAG,EAAE,IAAI,KAAK,EAAE,YAAY;CAC7E,IAAI,CAAC,OAAO,QAAQ,KACnB;CAED,OAAO,IAAI,MAAM,GAAA,EAAsB;AACxC"}
@@ -0,0 +1,30 @@
1
+ //#region src/native/ingest/normalizeEvent.d.ts
2
+ type EventType = 'pageview' | 'event' | 'goal';
3
+ interface RawEventInput {
4
+ type: EventType;
5
+ /** Event name, or the goal slug on a `goal`. Absent on pageviews. */
6
+ name?: string;
7
+ path: string;
8
+ /**
9
+ * The hostname the client claims. Ignored by the HTTP ingest, which attributes an event to
10
+ * the request that carried it; `trackServerEvent` supplies its own and it is used as sent.
11
+ */
12
+ hostname?: string;
13
+ referrer?: string;
14
+ /**
15
+ * The page's query string, without its leading `?`. Read for its utm keys and then
16
+ * discarded: it is never stored, so a session token or an email address that happens to
17
+ * be in the URL never lands in the events collection.
18
+ */
19
+ query?: string;
20
+ durationMs?: number;
21
+ props?: Record<string, unknown>;
22
+ /** Revenue for a goal completion, in `currency`. */
23
+ value?: number;
24
+ currency?: string;
25
+ /** Max scroll depth reached on a pageview, 0-100. */
26
+ scrollDepth?: number;
27
+ }
28
+ //#endregion
29
+ export { RawEventInput };
30
+ //# sourceMappingURL=normalizeEvent.d.ts.map
@@ -1,34 +1,133 @@
1
+ import { clientIpFromHeaders } from "./clientIp.js";
1
2
  import { classifyDevice } from "./device.js";
2
- import { deriveSource } from "./source.js";
3
+ import { matchGoals } from "../../goals/match.js";
4
+ import { classifyBrowser, classifyOs } from "./browser.js";
5
+ import { primaryLanguage } from "./language.js";
6
+ import { referrerHost, storedReferrer } from "./referrer.js";
7
+ import { extractUtm } from "./utm.js";
8
+ import { classifyChannel, deriveSource } from "./source.js";
3
9
  import { dailyVisitorHash, deriveSessionId } from "./visitorHash.js";
4
10
  //#region src/native/ingest/normalizeEvent.ts
5
- async function normalizeEvent({ raw, headers, geoResolver, salt, now, scope, timezone }) {
6
- const geo = await geoResolver(headers);
7
- const ip = (headers.get("x-forwarded-for")?.split(",")[0] ?? headers.get("x-real-ip") ?? "").trim();
11
+ const MAX_PROPS = 20;
12
+ const MAX_KEY_LENGTH = 64;
13
+ const MAX_VALUE_LENGTH = 256;
14
+ const MAX_NAME_LENGTH = 128;
15
+ const MAX_PATH_LENGTH = 512;
16
+ /** 24 hours. A longer duration is a broken clock, not a session. */
17
+ const MAX_DURATION_MS = 864e5;
18
+ const CURRENCY = /^[A-Z]{3}$/;
19
+ /**
20
+ * Optional wire fields are sanitized rather than rejected: a bad `value`, `currency`,
21
+ * `scrollDepth`, `durationMs`, or prop is dropped and the event is still ingested, so one malformed
22
+ * attribute never costs a pageview. Only the required fields (checked in the endpoint)
23
+ * can fail an event.
24
+ */
25
+ const nonNegative = (value) => typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : void 0;
26
+ const currencyCode = (value) => typeof value === "string" && CURRENCY.test(value) ? value : void 0;
27
+ /**
28
+ * Infinity would poison a rollup bucket's average for good, and a string would throw out of
29
+ * the write and take the whole buffered batch with it, so a duration outside the ceiling is
30
+ * dropped like any other malformed attribute.
31
+ */
32
+ const duration = (value) => {
33
+ const n = nonNegative(value);
34
+ return n === void 0 || n > MAX_DURATION_MS ? void 0 : n;
35
+ };
36
+ /** The event name is a rollup bucket key (the `event` dimension), so it is length-capped. */
37
+ const eventName = (value) => typeof value === "string" ? value.slice(0, MAX_NAME_LENGTH) : void 0;
38
+ /**
39
+ * A geo value as stored. The resolver reads request headers a client can set, and each value
40
+ * becomes a rollup dimvalue and part of a seen-ledger key, so it is capped here rather than
41
+ * trusted. An empty value stays absent: '' would be a bucket of its own.
42
+ */
43
+ const geoValue = (value) => value ? value.slice(0, 128) : void 0;
44
+ /** Capped before it is parsed, so a hostile query cannot make ingest do unbounded work. */
45
+ const queryString = (value) => typeof value === "string" ? value.slice(0, 512) : void 0;
46
+ const depth = (value) => {
47
+ const n = nonNegative(value);
48
+ return n === void 0 ? void 0 : Math.round(Math.min(n, 100));
49
+ };
50
+ /** Flat JSON scalars only: the first 20 short-keyed string/number/boolean entries. */
51
+ const sanitizeProps = (raw) => {
52
+ if (typeof raw !== "object" || raw === null || Array.isArray(raw)) return;
53
+ const out = {};
54
+ let kept = 0;
55
+ for (const [key, value] of Object.entries(raw)) {
56
+ if (kept >= MAX_PROPS || key.length > MAX_KEY_LENGTH) continue;
57
+ if (typeof value === "string") out[key] = value.slice(0, MAX_VALUE_LENGTH);
58
+ else if (typeof value === "boolean" || typeof value === "number" && Number.isFinite(value)) out[key] = value;
59
+ else continue;
60
+ kept++;
61
+ }
62
+ return kept > 0 ? out : void 0;
63
+ };
64
+ async function normalizeEvent({ raw, hostname: rawHostname, headers, geoResolver, salt, now, scope, timezone, goals, trustedProxyHops }) {
65
+ const geo = await geoResolver(headers, { trustedProxyHops });
66
+ const ip = clientIpFromHeaders(headers, { trustedProxyHops }) ?? "";
8
67
  const ua = headers.get("user-agent") ?? "";
68
+ const path = raw.path.slice(0, MAX_PATH_LENGTH);
69
+ const hostname = rawHostname.slice(0, 253);
9
70
  const visitorHash = dailyVisitorHash({
10
71
  ip,
11
72
  ua,
12
- site: raw.hostname,
73
+ site: hostname,
13
74
  salt
14
75
  });
15
76
  const hourBucket = now.toISOString().slice(0, 13);
77
+ const props = sanitizeProps(raw.props);
78
+ const value = nonNegative(raw.value);
79
+ const scrollDepth = depth(raw.scrollDepth);
80
+ const durationMs = duration(raw.durationMs);
81
+ const name = eventName(raw.name);
82
+ const device = classifyDevice(ua);
83
+ const browser = classifyBrowser(ua);
84
+ const os = classifyOs(ua);
85
+ const language = primaryLanguage(headers.get("accept-language"));
86
+ const refHost = referrerHost(raw.referrer, hostname);
87
+ const query = queryString(raw.query);
88
+ const utm = extractUtm(query);
89
+ const completions = goals?.length ? matchGoals({
90
+ type: raw.type,
91
+ name,
92
+ path,
93
+ props,
94
+ value
95
+ }, goals) : [];
16
96
  return {
17
97
  timestamp: now,
18
98
  type: raw.type,
19
- name: raw.name,
20
- path: raw.path,
21
- hostname: raw.hostname,
22
- referrer: raw.referrer,
23
- device: classifyDevice(ua),
24
- source: deriveSource(raw.referrer, raw.hostname),
25
- country: geo.country,
26
- region: geo.region,
27
- city: geo.city,
99
+ name,
100
+ path,
101
+ hostname,
102
+ referrer: storedReferrer(raw.referrer),
103
+ ...refHost ? { referrerHost: refHost } : {},
104
+ ...device ? { device } : {},
105
+ ...browser ? { browser } : {},
106
+ ...os ? { os } : {},
107
+ source: deriveSource({
108
+ referrerHost: refHost,
109
+ utmSource: utm.utmSource
110
+ }),
111
+ channel: classifyChannel({
112
+ referrerHost: refHost,
113
+ utmSource: utm.utmSource,
114
+ utmMedium: utm.utmMedium,
115
+ query
116
+ }),
117
+ channelVersion: 1,
118
+ ...utm,
119
+ country: geoValue(geo.country),
120
+ region: geoValue(geo.region),
121
+ city: geoValue(geo.city),
122
+ ...language ? { language } : {},
28
123
  visitorHash,
29
124
  sessionId: deriveSessionId(visitorHash, hourBucket),
30
- durationMs: raw.durationMs,
31
- props: raw.props,
125
+ props,
126
+ ...durationMs !== void 0 ? { durationMs } : {},
127
+ ...value !== void 0 ? { value } : {},
128
+ ...currencyCode(raw.currency) !== void 0 ? { currency: raw.currency } : {},
129
+ ...scrollDepth !== void 0 ? { scrollDepth } : {},
130
+ ...completions.length > 0 ? { goals: completions } : {},
32
131
  ...scope !== void 0 ? { scope } : {},
33
132
  ...timezone !== void 0 ? { timezone } : {}
34
133
  };
@@ -1 +1 @@
1
- {"version":3,"file":"normalizeEvent.js","names":[],"sources":["../../../src/native/ingest/normalizeEvent.ts"],"sourcesContent":["import type { GeoResolver } from '../geo/geoResolver'\nimport { classifyDevice, type DeviceType } from './device'\nimport { deriveSource } from './source'\nimport { dailyVisitorHash, deriveSessionId } from './visitorHash'\n\nexport interface RawEventInput {\n\ttype: 'pageview' | 'event'\n\tname?: string\n\tpath: string\n\thostname: string\n\treferrer?: string\n\tdurationMs?: number\n\tprops?: Record<string, unknown>\n}\n\nexport interface StoredEvent {\n\ttimestamp: Date\n\ttype: 'pageview' | 'event'\n\tname?: string\n\tpath: string\n\thostname: string\n\treferrer?: string\n\tdevice?: DeviceType\n\tsource?: string\n\tcountry?: string\n\tregion?: string\n\tcity?: string\n\tvisitorHash: string\n\tsessionId: string\n\tdurationMs?: number\n\tprops?: Record<string, unknown>\n\t/** Set only in scoped installs; '' is the null scope. */\n\tscope?: string\n\t/**\n\t * Reporting timezone the rollup day bucket is computed in. In-memory only: stripped\n\t * before the event row is persisted (day bucketing happens at rollup time). Absent\n\t * means UTC.\n\t */\n\ttimezone?: string\n}\n\nexport interface NormalizeArgs {\n\traw: RawEventInput\n\theaders: Headers\n\tgeoResolver: GeoResolver\n\tsalt: string\n\tnow: Date\n\tscope?: string\n\ttimezone?: string\n}\n\nexport async function normalizeEvent({\n\traw,\n\theaders,\n\tgeoResolver,\n\tsalt,\n\tnow,\n\tscope,\n\ttimezone,\n}: NormalizeArgs): Promise<StoredEvent> {\n\tconst geo = await geoResolver(headers)\n\tconst ip = (\n\t\theaders.get('x-forwarded-for')?.split(',')[0] ??\n\t\theaders.get('x-real-ip') ??\n\t\t''\n\t).trim()\n\tconst ua = headers.get('user-agent') ?? ''\n\tconst visitorHash = dailyVisitorHash({ ip, ua, site: raw.hostname, salt })\n\tconst hourBucket = now.toISOString().slice(0, 13)\n\treturn {\n\t\ttimestamp: now,\n\t\ttype: raw.type,\n\t\tname: raw.name,\n\t\tpath: raw.path,\n\t\thostname: raw.hostname,\n\t\treferrer: raw.referrer,\n\t\tdevice: classifyDevice(ua),\n\t\tsource: deriveSource(raw.referrer, raw.hostname),\n\t\tcountry: geo.country,\n\t\tregion: geo.region,\n\t\tcity: geo.city,\n\t\tvisitorHash,\n\t\tsessionId: deriveSessionId(visitorHash, hourBucket),\n\t\tdurationMs: raw.durationMs,\n\t\tprops: raw.props,\n\t\t...(scope !== undefined ? { scope } : {}),\n\t\t...(timezone !== undefined ? { timezone } : {}),\n\t}\n}\n"],"mappings":";;;;AAmDA,eAAsB,eAAe,EACpC,KACA,SACA,aACA,MACA,KACA,OACA,YACuC;CACvC,MAAM,MAAM,MAAM,YAAY,OAAO;CACrC,MAAM,MACL,QAAQ,IAAI,iBAAiB,GAAG,MAAM,GAAG,EAAE,MAC3C,QAAQ,IAAI,WAAW,KACvB,IACC,KAAK;CACP,MAAM,KAAK,QAAQ,IAAI,YAAY,KAAK;CACxC,MAAM,cAAc,iBAAiB;EAAE;EAAI;EAAI,MAAM,IAAI;EAAU;CAAK,CAAC;CACzE,MAAM,aAAa,IAAI,YAAY,EAAE,MAAM,GAAG,EAAE;CAChD,OAAO;EACN,WAAW;EACX,MAAM,IAAI;EACV,MAAM,IAAI;EACV,MAAM,IAAI;EACV,UAAU,IAAI;EACd,UAAU,IAAI;EACd,QAAQ,eAAe,EAAE;EACzB,QAAQ,aAAa,IAAI,UAAU,IAAI,QAAQ;EAC/C,SAAS,IAAI;EACb,QAAQ,IAAI;EACZ,MAAM,IAAI;EACV;EACA,WAAW,gBAAgB,aAAa,UAAU;EAClD,YAAY,IAAI;EAChB,OAAO,IAAI;EACX,GAAI,UAAU,KAAA,IAAY,EAAE,MAAM,IAAI,CAAC;EACvC,GAAI,aAAa,KAAA,IAAY,EAAE,SAAS,IAAI,CAAC;CAC9C;AACD"}
1
+ {"version":3,"file":"normalizeEvent.js","names":[],"sources":["../../../src/native/ingest/normalizeEvent.ts"],"sourcesContent":["import { type GoalCompletion, matchGoals } from '../../goals/match'\nimport type { Goal } from '../../goals/types'\nimport { 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 { MAX_HOSTNAME_LENGTH } from './requestHost'\nimport { CHANNEL_TAXONOMY_VERSION, classifyChannel, 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\t/**\n\t * The hostname the client claims. Ignored by the HTTP ingest, which attributes an event to\n\t * the request that carried it; `trackServerEvent` supplies its own and it is used as sent.\n\t */\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 visit's named origin: its `utm_source`, else the referrer host, else `direct`.\n\t * Typed as a string because a row written before this classification holds whatever the\n\t * rules of its day decided, and rows are never rewritten.\n\t */\n\tsource?: string\n\t/** The hit's acquisition channel (`TrafficChannel`), classified from the origin above. */\n\tchannel: string\n\t/**\n\t * The taxonomy the `channel` beside it was decided under, so a later reclassify can find\n\t * the rows an older rule set wrote.\n\t */\n\tchannelVersion: number\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\t/** Already resolved and capped: what the event is attributed to, not what it claimed. */\n\thostname: string\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\t/** Trusted proxies in front of the app, for the client address the hash and geo read. */\n\ttrustedProxyHops?: number\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/** 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\thostname: rawHostname,\n\theaders,\n\tgeoResolver,\n\tsalt,\n\tnow,\n\tscope,\n\ttimezone,\n\tgoals,\n\ttrustedProxyHops,\n}: NormalizeArgs): Promise<StoredEvent> {\n\tconst geo = await geoResolver(headers, { trustedProxyHops })\n\tconst ip = clientIpFromHeaders(headers, { trustedProxyHops }) ?? ''\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 = rawHostname.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, utmSource: utm.utmSource }),\n\t\tchannel: classifyChannel({\n\t\t\treferrerHost: refHost,\n\t\t\tutmSource: utm.utmSource,\n\t\t\tutmMedium: utm.utmMedium,\n\t\t\tquery,\n\t\t}),\n\t\tchannelVersion: CHANNEL_TAXONOMY_VERSION,\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":"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"}
@@ -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"}