@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.
- package/CHANGELOG.md +70 -0
- package/LICENSE +21 -21
- package/README.md +3 -3
- package/dist/adapters/ga4/eventName.d.ts +15 -0
- package/dist/adapters/ga4/eventName.js +19 -0
- package/dist/adapters/ga4/eventName.js.map +1 -0
- package/dist/adapters/ga4/ga4.d.ts +5 -0
- package/dist/adapters/ga4/ga4.js +178 -70
- package/dist/adapters/ga4/ga4.js.map +1 -1
- package/dist/adapters/ga4/measurementId.js +15 -0
- package/dist/adapters/ga4/measurementId.js.map +1 -0
- package/dist/adapters/goalRead.js +126 -0
- package/dist/adapters/goalRead.js.map +1 -0
- package/dist/adapters/plausible/plausible.d.ts +15 -0
- package/dist/adapters/plausible/plausible.js +110 -42
- package/dist/adapters/plausible/plausible.js.map +1 -1
- package/dist/adapters/posthog/posthog.js +47 -25
- package/dist/adapters/posthog/posthog.js.map +1 -1
- package/dist/adapters/umami/umami.d.ts +8 -0
- package/dist/adapters/umami/umami.js +237 -28
- package/dist/adapters/umami/umami.js.map +1 -1
- package/dist/charts/BarList.js +8 -2
- package/dist/charts/BarList.js.map +1 -1
- package/dist/core/cacheKey.js +11 -1
- package/dist/core/cacheKey.js.map +1 -1
- package/dist/core/capabilities.js +7 -2
- package/dist/core/capabilities.js.map +1 -1
- package/dist/core/capture.d.ts +1 -1
- package/dist/core/contract.d.ts +32 -2
- package/dist/core/contract.js.map +1 -1
- package/dist/core/options.d.ts +8 -2
- package/dist/core/options.js +4 -2
- package/dist/core/options.js.map +1 -1
- package/dist/core/serverEvent.d.ts +4 -2
- package/dist/core/serverEvent.js.map +1 -1
- package/dist/exports/adapters/ga4.d.ts +2 -1
- package/dist/exports/adapters/ga4.js +2 -1
- package/dist/exports/client.d.ts +5 -2
- package/dist/exports/client.js +4 -1
- package/dist/exports/rsc.d.ts +2 -1
- package/dist/exports/tracker.d.ts +3 -2
- package/dist/exports/tracker.js +2 -1
- package/dist/exports/types.d.ts +2 -1
- package/dist/exports/types.js +2 -1
- package/dist/fields/config/FilterDimensionSelectField.d.ts +14 -0
- package/dist/fields/config/FilterDimensionSelectField.js +77 -0
- package/dist/fields/config/FilterDimensionSelectField.js.map +1 -0
- package/dist/fields/config/FilterOperatorSelectField.d.ts +14 -0
- package/dist/fields/config/FilterOperatorSelectField.js +75 -0
- package/dist/fields/config/FilterOperatorSelectField.js.map +1 -0
- package/dist/fields/config/MetricSelectField.js +1 -1
- package/dist/fields/config/SourceSelectField.js +1 -1
- package/dist/fields/config/sourcesState.js +19 -3
- package/dist/fields/config/sourcesState.js.map +1 -1
- package/dist/fields/config/useAnalyticsSources.d.ts +8 -3
- package/dist/fields/config/useAnalyticsSources.js +9 -6
- package/dist/fields/config/useAnalyticsSources.js.map +1 -1
- package/dist/fields/config/useFilterCapabilities.d.ts +28 -0
- package/dist/fields/config/useFilterCapabilities.js +52 -0
- package/dist/fields/config/useFilterCapabilities.js.map +1 -0
- package/dist/fields/readForDocument.js +16 -4
- package/dist/fields/readForDocument.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/native/collections/events.js +36 -0
- package/dist/native/collections/events.js.map +1 -1
- package/dist/native/eventScan.js +25 -0
- package/dist/native/eventScan.js.map +1 -0
- package/dist/native/ingest/browser.js +47 -0
- package/dist/native/ingest/browser.js.map +1 -0
- package/dist/native/ingest/flushBatch.js +26 -26
- package/dist/native/ingest/flushBatch.js.map +1 -1
- package/dist/native/ingest/language.js +14 -0
- package/dist/native/ingest/language.js.map +1 -0
- package/dist/native/ingest/normalizeEvent.d.ts +6 -0
- package/dist/native/ingest/normalizeEvent.js +32 -5
- package/dist/native/ingest/normalizeEvent.js.map +1 -1
- package/dist/native/ingest/referrer.js +48 -0
- package/dist/native/ingest/referrer.js.map +1 -0
- package/dist/native/ingest/serverTrack.js +1 -0
- package/dist/native/ingest/serverTrack.js.map +1 -1
- package/dist/native/ingest/source.d.ts +7 -0
- package/dist/native/ingest/source.js +139 -14
- package/dist/native/ingest/source.js.map +1 -1
- package/dist/native/ingest/utm.js +33 -0
- package/dist/native/ingest/utm.js.map +1 -0
- package/dist/native/nativeAdapter.js +32 -26
- package/dist/native/nativeAdapter.js.map +1 -1
- package/dist/native/query/eventAgg.js +14 -1
- package/dist/native/query/eventAgg.js.map +1 -1
- package/dist/native/rollups/bucketKey.js +15 -0
- package/dist/native/rollups/bucketKey.js.map +1 -0
- package/dist/native/rollups/bumpRollups.js +116 -0
- package/dist/native/rollups/bumpRollups.js.map +1 -0
- package/dist/native/rollups/deltas.js +30 -15
- package/dist/native/rollups/deltas.js.map +1 -1
- package/dist/native/rollups/insertManyIfNew.js +93 -0
- package/dist/native/rollups/insertManyIfNew.js.map +1 -0
- package/dist/next/captureRewrites.d.ts +3 -2
- package/dist/next/captureRewrites.js +3 -2
- package/dist/next/captureRewrites.js.map +1 -1
- package/dist/plugin/documentEndpoint.js +26 -7
- package/dist/plugin/documentEndpoint.js.map +1 -1
- package/dist/plugin/goalHint.d.ts +1 -0
- package/dist/plugin/goalHint.js +24 -0
- package/dist/plugin/goalHint.js.map +1 -0
- package/dist/plugin/queryEndpoint.js +19 -12
- package/dist/plugin/queryEndpoint.js.map +1 -1
- package/dist/plugin/realtimeEndpoint.js +40 -15
- package/dist/plugin/realtimeEndpoint.js.map +1 -1
- package/dist/plugin/responses.js +17 -0
- package/dist/plugin/responses.js.map +1 -0
- package/dist/plugin/sourcesEndpoint.js +10 -3
- package/dist/plugin/sourcesEndpoint.js.map +1 -1
- package/dist/providers/collection.js +14 -4
- package/dist/providers/collection.js.map +1 -1
- package/dist/providers/factory.js +4 -2
- package/dist/providers/factory.js.map +1 -1
- package/dist/query/response.d.ts +4 -1
- package/dist/query/response.js +11 -0
- package/dist/query/response.js.map +1 -0
- package/dist/surfacing/engine.js +1 -1
- package/dist/surfacing/engine.js.map +1 -1
- package/dist/testing/memoryAdapter.js +0 -1
- package/dist/testing/memoryAdapter.js.map +1 -1
- package/dist/tracker/createTracker.js +13 -1
- package/dist/tracker/createTracker.js.map +1 -1
- package/dist/tracker/sinks/ga4.d.ts +21 -0
- package/dist/tracker/sinks/ga4.js +26 -0
- package/dist/tracker/sinks/ga4.js.map +1 -0
- package/dist/tracker/types.d.ts +5 -0
- package/dist/translations/ar.js +30 -1
- package/dist/translations/ar.js.map +1 -1
- package/dist/translations/de.js +30 -1
- package/dist/translations/de.js.map +1 -1
- package/dist/translations/en.js +30 -1
- package/dist/translations/en.js.map +1 -1
- package/dist/translations/es.js +30 -1
- package/dist/translations/es.js.map +1 -1
- package/dist/translations/fr.js +30 -1
- package/dist/translations/fr.js.map +1 -1
- package/dist/translations/id.js +30 -1
- package/dist/translations/id.js.map +1 -1
- package/dist/translations/keys.d.ts +29 -0
- package/dist/translations/keys.js +30 -1
- package/dist/translations/keys.js.map +1 -1
- package/dist/translations/ko.js +30 -1
- package/dist/translations/ko.js.map +1 -1
- package/dist/translations/metricKeys.js +7 -1
- package/dist/translations/metricKeys.js.map +1 -1
- package/dist/translations/pt.js +30 -1
- package/dist/translations/pt.js.map +1 -1
- package/dist/translations/ru.js +30 -1
- package/dist/translations/ru.js.map +1 -1
- package/dist/translations/server.js.map +1 -1
- package/dist/translations/uk.js +30 -1
- package/dist/translations/uk.js.map +1 -1
- package/dist/translations/zh.js +30 -1
- package/dist/translations/zh.js.map +1 -1
- package/dist/view/AnalyticsViewClient.js +13 -2
- package/dist/view/AnalyticsViewClient.js.map +1 -1
- package/dist/view/gating.js +40 -3
- package/dist/view/gating.js.map +1 -1
- package/dist/view/labels.js +21 -1
- package/dist/view/labels.js.map +1 -1
- package/dist/view/sections/Breakdowns.js +78 -38
- package/dist/view/sections/Breakdowns.js.map +1 -1
- package/dist/view/sections/FilterChips.js +8 -3
- package/dist/view/sections/FilterChips.js.map +1 -1
- package/dist/view/sections/GoalsPanel.js +2 -1
- package/dist/view/sections/GoalsPanel.js.map +1 -1
- package/dist/view/sections/RealtimeStrip.js +4 -1
- package/dist/view/sections/RealtimeStrip.js.map +1 -1
- package/dist/view/sections/Toolbar.js +10 -3
- package/dist/view/sections/Toolbar.js.map +1 -1
- package/dist/view/sections/Trend.js +8 -2
- package/dist/view/sections/Trend.js.map +1 -1
- package/dist/view/state.js +7 -2
- package/dist/view/state.js.map +1 -1
- package/dist/view/useViewQueries.js +6 -3
- package/dist/view/useViewQueries.js.map +1 -1
- package/dist/widgets/AnalyticsBreakdownWidget.js +59 -10
- package/dist/widgets/AnalyticsBreakdownWidget.js.map +1 -1
- package/dist/widgets/AnalyticsGoalsWidget.js +10 -2
- package/dist/widgets/AnalyticsGoalsWidget.js.map +1 -1
- package/dist/widgets/AnalyticsMetricWidget.js +30 -3
- package/dist/widgets/AnalyticsMetricWidget.js.map +1 -1
- package/dist/widgets/AnalyticsRealtimeWidget.js +3 -1
- package/dist/widgets/AnalyticsRealtimeWidget.js.map +1 -1
- package/dist/widgets/AnalyticsTrendWidget.js +30 -3
- package/dist/widgets/AnalyticsTrendWidget.js.map +1 -1
- package/dist/widgets/RealtimeCounter.d.ts +4 -0
- package/dist/widgets/RealtimeCounter.js +10 -1
- package/dist/widgets/RealtimeCounter.js.map +1 -1
- package/dist/widgets/breakdownTypes.js.map +1 -1
- package/dist/widgets/comparison.js +15 -1
- package/dist/widgets/comparison.js.map +1 -1
- package/dist/widgets/defaults.d.ts +6 -2
- package/dist/widgets/defaults.js +72 -25
- package/dist/widgets/defaults.js.map +1 -1
- package/dist/widgets/filterCaption.js +27 -0
- package/dist/widgets/filterCaption.js.map +1 -0
- package/dist/widgets/filterField.d.ts +19 -0
- package/dist/widgets/filterField.js +75 -0
- package/dist/widgets/filterField.js.map +1 -0
- package/dist/widgets/prepareWidgetRead.d.ts +7 -0
- package/dist/widgets/prepareWidgetRead.js +74 -0
- package/dist/widgets/prepareWidgetRead.js.map +1 -0
- package/dist/widgets/readForWidget.d.ts +12 -2
- package/dist/widgets/readForWidget.js +34 -49
- package/dist/widgets/readForWidget.js.map +1 -1
- package/dist/widgets/readForWidgetBreakdown.d.ts +17 -1
- package/dist/widgets/readForWidgetBreakdown.js +29 -42
- package/dist/widgets/readForWidgetBreakdown.js.map +1 -1
- package/dist/widgets/readForWidgetGoals.d.ts +11 -1
- package/dist/widgets/readForWidgetGoals.js +44 -30
- package/dist/widgets/readForWidgetGoals.js.map +1 -1
- package/dist/widgets/readForWidgetRealtime.d.ts +8 -0
- package/dist/widgets/readForWidgetRealtime.js +21 -20
- package/dist/widgets/readForWidgetRealtime.js.map +1 -1
- package/dist/widgets/readForWidgetSeries.d.ts +11 -1
- package/dist/widgets/readForWidgetSeries.js +38 -49
- package/dist/widgets/readForWidgetSeries.js.map +1 -1
- package/dist/widgets/readMeta.js +18 -0
- package/dist/widgets/readMeta.js.map +1 -0
- package/dist/widgets/registerWidgets.js +5 -2
- package/dist/widgets/registerWidgets.js.map +1 -1
- package/dist/widgets/types.d.ts +15 -0
- package/dist/widgets/types.js.map +1 -1
- package/dist/widgets/viewLink.js +5 -2
- package/dist/widgets/viewLink.js.map +1 -1
- package/package.json +5 -5
- package/dist/native/rollups/applyDistinctDeltas.js +0 -9
- package/dist/native/rollups/applyDistinctDeltas.js.map +0 -1
- package/dist/native/rollups/bumpRollup.js +0 -73
- package/dist/native/rollups/bumpRollup.js.map +0 -1
- package/dist/native/rollups/insertIfNew.js +0 -53
- package/dist/native/rollups/insertIfNew.js.map +0 -1
|
@@ -1,14 +1,27 @@
|
|
|
1
1
|
import { startOfDayInTz } from "../../timeframe/tz.js";
|
|
2
2
|
//#region src/native/query/eventAgg.ts
|
|
3
3
|
const HOUR_MS = 36e5;
|
|
4
|
-
/**
|
|
4
|
+
/**
|
|
5
|
+
* Dimensions native can filter/group on directly against the events collection. `referrer`
|
|
6
|
+
* reads the host derived at ingest rather than the raw referrer, since a `where` cannot
|
|
7
|
+
* reduce a URL to its host; it is the same field the rollup bucket is keyed by.
|
|
8
|
+
*/
|
|
5
9
|
const EVENT_FIELD = {
|
|
6
10
|
page: "path",
|
|
7
11
|
country: "country",
|
|
8
12
|
region: "region",
|
|
9
13
|
city: "city",
|
|
10
14
|
device: "device",
|
|
15
|
+
browser: "browser",
|
|
16
|
+
os: "os",
|
|
17
|
+
language: "language",
|
|
11
18
|
source: "source",
|
|
19
|
+
referrer: "referrerHost",
|
|
20
|
+
utmSource: "utmSource",
|
|
21
|
+
utmMedium: "utmMedium",
|
|
22
|
+
utmCampaign: "utmCampaign",
|
|
23
|
+
utmContent: "utmContent",
|
|
24
|
+
utmTerm: "utmTerm",
|
|
12
25
|
event: "name"
|
|
13
26
|
};
|
|
14
27
|
const FILTER_OPERATOR = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"eventAgg.js","names":[],"sources":["../../../src/native/query/eventAgg.ts"],"sourcesContent":["import type { AnalyticsFilter, AnalyticsRow, DimensionKey, MetricKey } from '../../core/contract'\nimport { DEFAULT_TIMEZONE, startOfDayInTz } from '../../timeframe/tz'\n\n/** Raw event shape aggregateEvents/filtersToWhere operate on; matches StoredEvent as read back from the events collection (timestamp comes back as an ISO string). */\nexport interface EventLike {\n\ttimestamp: string | Date\n\ttype: 'pageview' | 'event' | 'goal'\n\tname?: string\n\tpath: string\n\tdevice?: string\n\tsource?: string\n\tcountry?: string\n\tregion?: string\n\tcity?: string\n\tvisitorHash: string\n\tsessionId: string\n\tdurationMs?: number\n\tscrollDepth?: number\n\t/** Goal completions stamped at ingest; the source of conversions/revenue on this path. */\n\tgoals?: Array<{ slug: string; value: number }>\n}\n\nconst HOUR_MS = 3_600_000\n\ntype EventStringField = 'path' | 'country' | 'region' | 'city' | 'device' | 'source' | 'name'\n\n/** Dimensions native can filter/group on directly against the events collection. */\nconst EVENT_FIELD: Partial<Record<DimensionKey, EventStringField>> = {\n\tpage: 'path',\n\tcountry: 'country',\n\tregion: 'region',\n\tcity: 'city',\n\tdevice: 'device',\n\tsource: 'source',\n\tevent: 'name',\n}\n\nconst FILTER_OPERATOR: Record<'eq' | 'contains', string> = { eq: 'equals', contains: 'contains' }\n\n/**\n * Payload where fragment for supported filters; unsupported dimensions/operators are\n * dropped. Capability gating (`filters`/`filterOperators`) is the real contract upstream,\n * this is just the safety net so a stray unsupported filter never throws.\n * `contains` is case-insensitive on both DBs; on Postgres its value is a raw ILIKE\n * pattern (`%`/`_` act as wildcards), while Mongo regex-escapes the value first.\n * One fragment per filter under `and`, not merged by key: two filters on the same field\n * (e.g. two `eq` values) must both constrain the query rather than the later one silently\n * overwriting the earlier one.\n */\nexport const filtersToWhere = (filters: AnalyticsFilter[]): Record<string, unknown> => {\n\tconst fragments: Record<string, unknown>[] = []\n\tfor (const filter of filters) {\n\t\tconst field = EVENT_FIELD[filter.dimension]\n\t\tif (!field || filter.operator === 'matches') {\n\t\t\tcontinue\n\t\t}\n\t\tconst op = FILTER_OPERATOR[filter.operator]\n\t\tif (!op) {\n\t\t\tcontinue\n\t\t}\n\t\tfragments.push({ [field]: { [op]: filter.value } })\n\t}\n\treturn fragments.length > 0 ? { and: fragments } : {}\n}\n\ninterface Bucket {\n\tpageviews: number\n\tevents: number\n\tdurationMs: number\n\tvisitors: Set<string>\n\tsessions: Set<string>\n\tconversions: number\n\trevenue: number\n\tscrollDepthSum: number\n\tscrollSamples: number\n}\n\nconst emptyBucket = (): Bucket => ({\n\tpageviews: 0,\n\tevents: 0,\n\tdurationMs: 0,\n\tvisitors: new Set(),\n\tsessions: new Set(),\n\tconversions: 0,\n\trevenue: 0,\n\tscrollDepthSum: 0,\n\tscrollSamples: 0,\n})\n\n/**\n * `completions` narrows what the event contributes to conversions/revenue: a goal breakdown\n * row counts only its own goal, every other bucket counts all of them. Mirrors how\n * computeRollupDeltas fills a `goal` bucket versus a site or page bucket.\n */\nconst addEvent = (bucket: Bucket, event: EventLike, completions = event.goals ?? []): void => {\n\tif (event.type === 'pageview') {\n\t\tbucket.pageviews++\n\t\tbucket.durationMs += event.durationMs ?? 0\n\t} else {\n\t\tbucket.events++\n\t}\n\tif (event.scrollDepth !== undefined) {\n\t\tbucket.scrollDepthSum += event.scrollDepth\n\t\tbucket.scrollSamples++\n\t}\n\tbucket.conversions += completions.length\n\tfor (const completion of completions) {\n\t\tbucket.revenue += completion.value\n\t}\n\tbucket.visitors.add(event.visitorHash)\n\tbucket.sessions.add(event.sessionId)\n}\n\nconst selectMetrics = (bucket: Bucket, wanted: MetricKey[]): Partial<Record<MetricKey, number>> => {\n\tconst out: Partial<Record<MetricKey, number>> = {}\n\tif (wanted.includes('pageviews')) out.pageviews = bucket.pageviews\n\tif (wanted.includes('events')) out.events = bucket.events\n\tif (wanted.includes('visitors')) out.visitors = bucket.visitors.size\n\tif (wanted.includes('sessions')) out.sessions = bucket.sessions.size\n\tif (wanted.includes('conversions')) out.conversions = bucket.conversions\n\tif (wanted.includes('revenue')) out.revenue = bucket.revenue\n\tif (wanted.includes('avgDuration')) {\n\t\tout.avgDuration = bucket.pageviews > 0 ? Math.round(bucket.durationMs / bucket.pageviews) : 0\n\t}\n\tif (wanted.includes('scrollDepth')) {\n\t\tout.scrollDepth =\n\t\t\tbucket.scrollSamples > 0 ? Math.round(bucket.scrollDepthSum / bucket.scrollSamples) : 0\n\t}\n\treturn out\n}\n\nexport interface AggregateEventsArgs {\n\tmetrics: MetricKey[]\n\tdimension?: DimensionKey\n\tgranularity?: 'hour' | 'day'\n\ttimezone?: string\n\t/** Sort for dimension breakdown rows; defaults to pageviews desc, same as the rollup path. */\n\torder?: { metric: MetricKey; direction: 'asc' | 'desc' }\n\t/** Row cap for dimension breakdown rows, applied after sorting. */\n\tlimit?: number\n}\n\nexport interface AggregateEventsResult {\n\trows: AnalyticsRow[]\n\ttotals: Partial<Record<MetricKey, number>>\n}\n\n/**\n * Hour buckets are UTC-hour floors regardless of `timezone`; sub-day bucketing ignoring\n * the reporting timezone is a documented v1 limitation. Day buckets floor in `timezone`\n * to stay aligned with native rollup day boundaries.\n */\nconst bucketKeyFor = (event: EventLike, granularity: 'hour' | 'day', timezone: string): string => {\n\tconst ts = new Date(event.timestamp)\n\tif (granularity === 'hour') {\n\t\treturn new Date(Math.floor(ts.getTime() / HOUR_MS) * HOUR_MS).toISOString()\n\t}\n\treturn startOfDayInTz(ts, timezone).toISOString()\n}\n\n/**\n * Aggregate raw events into totals, plus one of: dimension breakdown rows or hour/day\n * series rows (dimension wins if both are requested). Totals always cover every event\n * passed in, independent of which rows a breakdown or series produces.\n */\nexport const aggregateEvents = (\n\tevents: EventLike[],\n\t{\n\t\tmetrics,\n\t\tdimension,\n\t\tgranularity,\n\t\ttimezone = DEFAULT_TIMEZONE,\n\t\torder,\n\t\tlimit,\n\t}: AggregateEventsArgs\n): AggregateEventsResult => {\n\tconst totalsBucket = emptyBucket()\n\tfor (const event of events) {\n\t\taddEvent(totalsBucket, event)\n\t}\n\tconst totals = selectMetrics(totalsBucket, metrics)\n\n\tconst field = dimension ? EVENT_FIELD[dimension] : undefined\n\tif (dimension && (field || dimension === 'goal')) {\n\t\tconst groups = new Map<string, Bucket>()\n\t\tconst bucketFor = (value: string): Bucket => {\n\t\t\tlet bucket = groups.get(value)\n\t\t\tif (!bucket) {\n\t\t\t\tbucket = emptyBucket()\n\t\t\t\tgroups.set(value, bucket)\n\t\t\t}\n\t\t\treturn bucket\n\t\t}\n\t\tfor (const event of events) {\n\t\t\tif (dimension === 'goal') {\n\t\t\t\tfor (const completion of event.goals ?? []) {\n\t\t\t\t\taddEvent(bucketFor(completion.slug), event, [completion])\n\t\t\t\t}\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\t// The event dimension reports named custom events only: a goal event's name is a\n\t\t\t// goal slug and belongs to the goal dimension instead.\n\t\t\tif (!field || (field === 'name' && event.type !== 'event')) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tconst value = event[field]\n\t\t\tif (!value) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\taddEvent(bucketFor(value), event)\n\t\t}\n\t\tlet rows: AnalyticsRow[] = [...groups].map(([value, bucket]) => ({\n\t\t\tdimensions: { [dimension]: value } as Partial<Record<DimensionKey, string>>,\n\t\t\tmetrics: selectMetrics(bucket, metrics),\n\t\t}))\n\t\tconst sortMetric = order?.metric ?? 'pageviews'\n\t\tconst direction = (order?.direction ?? 'desc') === 'asc' ? 1 : -1\n\t\trows.sort((a, b) => ((a.metrics[sortMetric] ?? 0) - (b.metrics[sortMetric] ?? 0)) * direction)\n\t\tif (limit) {\n\t\t\trows = rows.slice(0, limit)\n\t\t}\n\t\treturn { rows, totals }\n\t}\n\n\tif (granularity) {\n\t\tconst buckets = new Map<string, Bucket>()\n\t\tfor (const event of events) {\n\t\t\tconst key = bucketKeyFor(event, granularity, timezone)\n\t\t\tlet bucket = buckets.get(key)\n\t\t\tif (!bucket) {\n\t\t\t\tbucket = emptyBucket()\n\t\t\t\tbuckets.set(key, bucket)\n\t\t\t}\n\t\t\taddEvent(bucket, event)\n\t\t}\n\t\tconst rows: AnalyticsRow[] = [...buckets.entries()]\n\t\t\t.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))\n\t\t\t.map(([timestamp, bucket]) => ({ timestamp, metrics: selectMetrics(bucket, metrics) }))\n\t\treturn { rows, totals }\n\t}\n\n\treturn { rows: [{ metrics: totals }], totals }\n}\n"],"mappings":";;AAsBA,MAAM,UAAU;;AAKhB,MAAM,cAA+D;CACpE,MAAM;CACN,SAAS;CACT,QAAQ;CACR,MAAM;CACN,QAAQ;CACR,QAAQ;CACR,OAAO;AACR;AAEA,MAAM,kBAAqD;CAAE,IAAI;CAAU,UAAU;AAAW;;;;;;;;;;;AAYhG,MAAa,kBAAkB,YAAwD;CACtF,MAAM,YAAuC,CAAC;CAC9C,KAAK,MAAM,UAAU,SAAS;EAC7B,MAAM,QAAQ,YAAY,OAAO;EACjC,IAAI,CAAC,SAAS,OAAO,aAAa,WACjC;EAED,MAAM,KAAK,gBAAgB,OAAO;EAClC,IAAI,CAAC,IACJ;EAED,UAAU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vC,EAAE;EACF,MAAM,aAAa,OAAO,UAAU;EACpC,MAAM,aAAa,OAAO,aAAa,YAAY,QAAQ,IAAI;EAC/D,KAAK,MAAM,GAAG,QAAQ,EAAE,QAAQ,eAAe,MAAM,EAAE,QAAQ,eAAe,MAAM,SAAS;EAC7F,IAAI,OACH,OAAO,KAAK,MAAM,GAAG,KAAK;EAE3B,OAAO;GAAE;GAAM;EAAO;CACvB;CAEA,IAAI,aAAa;EAChB,MAAM,0BAAU,IAAI,IAAoB;EACxC,KAAK,MAAM,SAAS,QAAQ;GAC3B,MAAM,MAAM,aAAa,OAAO,aAAa,QAAQ;GACrD,IAAI,SAAS,QAAQ,IAAI,GAAG;GAC5B,IAAI,CAAC,QAAQ;IACZ,SAAS,YAAY;IACrB,QAAQ,IAAI,KAAK,MAAM;GACxB;GACA,SAAS,QAAQ,KAAK;EACvB;EAIA,OAAO;GAAE,MAHoB,CAAC,GAAG,QAAQ,QAAQ,CAAC,EAChD,MAAM,CAAC,IAAI,CAAC,OAAQ,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI,CAAE,EAC/C,KAAK,CAAC,WAAW,aAAa;IAAE;IAAW,SAAS,cAAc,QAAQ,OAAO;GAAE,EACzE;GAAG;EAAO;CACvB;CAEA,OAAO;EAAE,MAAM,CAAC,EAAE,SAAS,OAAO,CAAC;EAAG;CAAO;AAC9C"}
|
|
1
|
+
{"version":3,"file":"eventAgg.js","names":[],"sources":["../../../src/native/query/eventAgg.ts"],"sourcesContent":["import type { AnalyticsFilter, AnalyticsRow, DimensionKey, MetricKey } from '../../core/contract'\nimport { DEFAULT_TIMEZONE, startOfDayInTz } from '../../timeframe/tz'\n\n/** Raw event shape aggregateEvents/filtersToWhere operate on; matches StoredEvent as read back from the events collection (timestamp comes back as an ISO string). */\nexport interface EventLike {\n\ttimestamp: string | Date\n\ttype: 'pageview' | 'event' | 'goal'\n\tname?: string\n\tpath: string\n\tdevice?: string\n\tbrowser?: string\n\tos?: string\n\tsource?: string\n\t/** The referrer host derived at ingest; the raw referrer is never grouped or filtered on. */\n\treferrerHost?: string\n\tcountry?: string\n\tregion?: string\n\tcity?: string\n\tlanguage?: string\n\tutmSource?: string\n\tutmMedium?: string\n\tutmCampaign?: string\n\tutmContent?: string\n\tutmTerm?: string\n\tvisitorHash: string\n\tsessionId: string\n\tdurationMs?: number\n\tscrollDepth?: number\n\t/** Goal completions stamped at ingest; the source of conversions/revenue on this path. */\n\tgoals?: Array<{ slug: string; value: number }>\n}\n\nconst HOUR_MS = 3_600_000\n\ntype EventStringField =\n\t| 'path'\n\t| 'country'\n\t| 'region'\n\t| 'city'\n\t| 'device'\n\t| 'browser'\n\t| 'os'\n\t| 'language'\n\t| 'source'\n\t| 'referrerHost'\n\t| 'utmSource'\n\t| 'utmMedium'\n\t| 'utmCampaign'\n\t| 'utmContent'\n\t| 'utmTerm'\n\t| 'name'\n\n/**\n * Dimensions native can filter/group on directly against the events collection. `referrer`\n * reads the host derived at ingest rather than the raw referrer, since a `where` cannot\n * reduce a URL to its host; it is the same field the rollup bucket is keyed by.\n */\nconst EVENT_FIELD: Partial<Record<DimensionKey, EventStringField>> = {\n\tpage: 'path',\n\tcountry: 'country',\n\tregion: 'region',\n\tcity: 'city',\n\tdevice: 'device',\n\tbrowser: 'browser',\n\tos: 'os',\n\tlanguage: 'language',\n\tsource: 'source',\n\treferrer: 'referrerHost',\n\tutmSource: 'utmSource',\n\tutmMedium: 'utmMedium',\n\tutmCampaign: 'utmCampaign',\n\tutmContent: 'utmContent',\n\tutmTerm: 'utmTerm',\n\tevent: 'name',\n}\n\nconst FILTER_OPERATOR: Record<'eq' | 'contains', string> = { eq: 'equals', contains: 'contains' }\n\n/**\n * Payload where fragment for supported filters; unsupported dimensions/operators are\n * dropped. Capability gating (`filters`/`filterOperators`) is the real contract upstream,\n * this is just the safety net so a stray unsupported filter never throws.\n * `contains` is case-insensitive on both DBs; on Postgres its value is a raw ILIKE\n * pattern (`%`/`_` act as wildcards), while Mongo regex-escapes the value first.\n * One fragment per filter under `and`, not merged by key: two filters on the same field\n * (e.g. two `eq` values) must both constrain the query rather than the later one silently\n * overwriting the earlier one.\n */\nexport const filtersToWhere = (filters: AnalyticsFilter[]): Record<string, unknown> => {\n\tconst fragments: Record<string, unknown>[] = []\n\tfor (const filter of filters) {\n\t\tconst field = EVENT_FIELD[filter.dimension]\n\t\tif (!field || filter.operator === 'matches') {\n\t\t\tcontinue\n\t\t}\n\t\tconst op = FILTER_OPERATOR[filter.operator]\n\t\tif (!op) {\n\t\t\tcontinue\n\t\t}\n\t\tfragments.push({ [field]: { [op]: filter.value } })\n\t}\n\treturn fragments.length > 0 ? { and: fragments } : {}\n}\n\ninterface Bucket {\n\tpageviews: number\n\tevents: number\n\tdurationMs: number\n\tvisitors: Set<string>\n\tsessions: Set<string>\n\tconversions: number\n\trevenue: number\n\tscrollDepthSum: number\n\tscrollSamples: number\n}\n\nconst emptyBucket = (): Bucket => ({\n\tpageviews: 0,\n\tevents: 0,\n\tdurationMs: 0,\n\tvisitors: new Set(),\n\tsessions: new Set(),\n\tconversions: 0,\n\trevenue: 0,\n\tscrollDepthSum: 0,\n\tscrollSamples: 0,\n})\n\n/**\n * `completions` narrows what the event contributes to conversions/revenue: a goal breakdown\n * row counts only its own goal, every other bucket counts all of them. Mirrors how\n * computeRollupDeltas fills a `goal` bucket versus a site or page bucket.\n */\nconst addEvent = (bucket: Bucket, event: EventLike, completions = event.goals ?? []): void => {\n\tif (event.type === 'pageview') {\n\t\tbucket.pageviews++\n\t\tbucket.durationMs += event.durationMs ?? 0\n\t} else {\n\t\tbucket.events++\n\t}\n\tif (event.scrollDepth !== undefined) {\n\t\tbucket.scrollDepthSum += event.scrollDepth\n\t\tbucket.scrollSamples++\n\t}\n\tbucket.conversions += completions.length\n\tfor (const completion of completions) {\n\t\tbucket.revenue += completion.value\n\t}\n\tbucket.visitors.add(event.visitorHash)\n\tbucket.sessions.add(event.sessionId)\n}\n\nconst selectMetrics = (bucket: Bucket, wanted: MetricKey[]): Partial<Record<MetricKey, number>> => {\n\tconst out: Partial<Record<MetricKey, number>> = {}\n\tif (wanted.includes('pageviews')) out.pageviews = bucket.pageviews\n\tif (wanted.includes('events')) out.events = bucket.events\n\tif (wanted.includes('visitors')) out.visitors = bucket.visitors.size\n\tif (wanted.includes('sessions')) out.sessions = bucket.sessions.size\n\tif (wanted.includes('conversions')) out.conversions = bucket.conversions\n\tif (wanted.includes('revenue')) out.revenue = bucket.revenue\n\tif (wanted.includes('avgDuration')) {\n\t\tout.avgDuration = bucket.pageviews > 0 ? Math.round(bucket.durationMs / bucket.pageviews) : 0\n\t}\n\tif (wanted.includes('scrollDepth')) {\n\t\tout.scrollDepth =\n\t\t\tbucket.scrollSamples > 0 ? Math.round(bucket.scrollDepthSum / bucket.scrollSamples) : 0\n\t}\n\treturn out\n}\n\nexport interface AggregateEventsArgs {\n\tmetrics: MetricKey[]\n\tdimension?: DimensionKey\n\tgranularity?: 'hour' | 'day'\n\ttimezone?: string\n\t/** Sort for dimension breakdown rows; defaults to pageviews desc, same as the rollup path. */\n\torder?: { metric: MetricKey; direction: 'asc' | 'desc' }\n\t/** Row cap for dimension breakdown rows, applied after sorting. */\n\tlimit?: number\n}\n\nexport interface AggregateEventsResult {\n\trows: AnalyticsRow[]\n\ttotals: Partial<Record<MetricKey, number>>\n}\n\n/**\n * Hour buckets are UTC-hour floors regardless of `timezone`; sub-day bucketing ignoring\n * the reporting timezone is a documented v1 limitation. Day buckets floor in `timezone`\n * to stay aligned with native rollup day boundaries.\n */\nconst bucketKeyFor = (event: EventLike, granularity: 'hour' | 'day', timezone: string): string => {\n\tconst ts = new Date(event.timestamp)\n\tif (granularity === 'hour') {\n\t\treturn new Date(Math.floor(ts.getTime() / HOUR_MS) * HOUR_MS).toISOString()\n\t}\n\treturn startOfDayInTz(ts, timezone).toISOString()\n}\n\n/**\n * Aggregate raw events into totals, plus one of: dimension breakdown rows or hour/day\n * series rows (dimension wins if both are requested). Totals always cover every event\n * passed in, independent of which rows a breakdown or series produces.\n */\nexport const aggregateEvents = (\n\tevents: EventLike[],\n\t{\n\t\tmetrics,\n\t\tdimension,\n\t\tgranularity,\n\t\ttimezone = DEFAULT_TIMEZONE,\n\t\torder,\n\t\tlimit,\n\t}: AggregateEventsArgs\n): AggregateEventsResult => {\n\tconst totalsBucket = emptyBucket()\n\tfor (const event of events) {\n\t\taddEvent(totalsBucket, event)\n\t}\n\tconst totals = selectMetrics(totalsBucket, metrics)\n\n\tconst field = dimension ? EVENT_FIELD[dimension] : undefined\n\tif (dimension && (field || dimension === 'goal')) {\n\t\tconst groups = new Map<string, Bucket>()\n\t\tconst bucketFor = (value: string): Bucket => {\n\t\t\tlet bucket = groups.get(value)\n\t\t\tif (!bucket) {\n\t\t\t\tbucket = emptyBucket()\n\t\t\t\tgroups.set(value, bucket)\n\t\t\t}\n\t\t\treturn bucket\n\t\t}\n\t\tfor (const event of events) {\n\t\t\tif (dimension === 'goal') {\n\t\t\t\tfor (const completion of event.goals ?? []) {\n\t\t\t\t\taddEvent(bucketFor(completion.slug), event, [completion])\n\t\t\t\t}\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\t// The event dimension reports named custom events only: a goal event's name is a\n\t\t\t// goal slug and belongs to the goal dimension instead.\n\t\t\tif (!field || (field === 'name' && event.type !== 'event')) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tconst value = event[field]\n\t\t\tif (!value) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\taddEvent(bucketFor(value), event)\n\t\t}\n\t\tlet rows: AnalyticsRow[] = [...groups].map(([value, bucket]) => ({\n\t\t\tdimensions: { [dimension]: value } as Partial<Record<DimensionKey, string>>,\n\t\t\tmetrics: selectMetrics(bucket, metrics),\n\t\t}))\n\t\tconst sortMetric = order?.metric ?? 'pageviews'\n\t\tconst direction = (order?.direction ?? 'desc') === 'asc' ? 1 : -1\n\t\trows.sort((a, b) => ((a.metrics[sortMetric] ?? 0) - (b.metrics[sortMetric] ?? 0)) * direction)\n\t\tif (limit) {\n\t\t\trows = rows.slice(0, limit)\n\t\t}\n\t\treturn { rows, totals }\n\t}\n\n\tif (granularity) {\n\t\tconst buckets = new Map<string, Bucket>()\n\t\tfor (const event of events) {\n\t\t\tconst key = bucketKeyFor(event, granularity, timezone)\n\t\t\tlet bucket = buckets.get(key)\n\t\t\tif (!bucket) {\n\t\t\t\tbucket = emptyBucket()\n\t\t\t\tbuckets.set(key, bucket)\n\t\t\t}\n\t\t\taddEvent(bucket, event)\n\t\t}\n\t\tconst rows: AnalyticsRow[] = [...buckets.entries()]\n\t\t\t.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))\n\t\t\t.map(([timestamp, bucket]) => ({ timestamp, metrics: selectMetrics(bucket, metrics) }))\n\t\treturn { rows, totals }\n\t}\n\n\treturn { rows: [{ metrics: totals }], totals }\n}\n"],"mappings":";;AAgCA,MAAM,UAAU;;;;;;AAyBhB,MAAM,cAA+D;CACpE,MAAM;CACN,SAAS;CACT,QAAQ;CACR,MAAM;CACN,QAAQ;CACR,SAAS;CACT,IAAI;CACJ,UAAU;CACV,QAAQ;CACR,UAAU;CACV,WAAW;CACX,WAAW;CACX,aAAa;CACb,YAAY;CACZ,SAAS;CACT,OAAO;AACR;AAEA,MAAM,kBAAqD;CAAE,IAAI;CAAU,UAAU;AAAW;;;;;;;;;;;AAYhG,MAAa,kBAAkB,YAAwD;CACtF,MAAM,YAAuC,CAAC;CAC9C,KAAK,MAAM,UAAU,SAAS;EAC7B,MAAM,QAAQ,YAAY,OAAO;EACjC,IAAI,CAAC,SAAS,OAAO,aAAa,WACjC;EAED,MAAM,KAAK,gBAAgB,OAAO;EAClC,IAAI,CAAC,IACJ;EAED,UAAU,KAAK,GAAG,QAAQ,GAAG,KAAK,OAAO,MAAM,EAAE,CAAC;CACnD;CACA,OAAO,UAAU,SAAS,IAAI,EAAE,KAAK,UAAU,IAAI,CAAC;AACrD;AAcA,MAAM,qBAA6B;CAClC,WAAW;CACX,QAAQ;C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AS;EAC7F,IAAI,OACH,OAAO,KAAK,MAAM,GAAG,KAAK;EAE3B,OAAO;GAAE;GAAM;EAAO;CACvB;CAEA,IAAI,aAAa;EAChB,MAAM,0BAAU,IAAI,IAAoB;EACxC,KAAK,MAAM,SAAS,QAAQ;GAC3B,MAAM,MAAM,aAAa,OAAO,aAAa,QAAQ;GACrD,IAAI,SAAS,QAAQ,IAAI,GAAG;GAC5B,IAAI,CAAC,QAAQ;IACZ,SAAS,YAAY;IACrB,QAAQ,IAAI,KAAK,MAAM;GACxB;GACA,SAAS,QAAQ,KAAK;EACvB;EAIA,OAAO;GAAE,MAHoB,CAAC,GAAG,QAAQ,QAAQ,CAAC,EAChD,MAAM,CAAC,IAAI,CAAC,OAAQ,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI,CAAE,EAC/C,KAAK,CAAC,WAAW,aAAa;IAAE;IAAW,SAAS,cAAc,QAAQ,OAAO;GAAE,EACzE;GAAG;EAAO;CACvB;CAEA,OAAO;EAAE,MAAM,CAAC,EAAE,SAAS,OAAO,CAAC;EAAG;CAAO;AAC9C"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//#region src/native/rollups/bucketKey.ts
|
|
2
|
+
/**
|
|
3
|
+
* A rollup bucket as one string: the coalescing key a flush groups increments by, and the
|
|
4
|
+
* `bucket` column of the seen ledger, so a visitor is counted once per bucket per period.
|
|
5
|
+
* Scope is part of the key only where it is part of the bucket (a scoped install), which
|
|
6
|
+
* keeps an unscoped install's ledger rows byte-identical to what it already wrote.
|
|
7
|
+
*/
|
|
8
|
+
function bucketKey(key) {
|
|
9
|
+
const base = `${key.granularity}|${key.period.toISOString()}|${key.path}|${key.dimension}|${key.dimvalue}|${key.hostname}`;
|
|
10
|
+
return key.scope !== void 0 ? `${base}|${key.scope}` : base;
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
13
|
+
export { bucketKey };
|
|
14
|
+
|
|
15
|
+
//# sourceMappingURL=bucketKey.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bucketKey.js","names":[],"sources":["../../../src/native/rollups/bucketKey.ts"],"sourcesContent":["import type { RollupKey } from './deltas'\n\n/**\n * A rollup bucket as one string: the coalescing key a flush groups increments by, and the\n * `bucket` column of the seen ledger, so a visitor is counted once per bucket per period.\n * Scope is part of the key only where it is part of the bucket (a scoped install), which\n * keeps an unscoped install's ledger rows byte-identical to what it already wrote.\n */\nexport function bucketKey(key: RollupKey): string {\n\tconst base = `${key.granularity}|${key.period.toISOString()}|${key.path}|${key.dimension}|${key.dimvalue}|${key.hostname}`\n\treturn key.scope !== undefined ? `${base}|${key.scope}` : base\n}\n"],"mappings":";;;;;;;AAQA,SAAgB,UAAU,KAAwB;CACjD,MAAM,OAAO,GAAG,IAAI,YAAY,GAAG,IAAI,OAAO,YAAY,EAAE,GAAG,IAAI,KAAK,GAAG,IAAI,UAAU,GAAG,IAAI,SAAS,GAAG,IAAI;CAChH,OAAO,IAAI,UAAU,KAAA,IAAY,GAAG,KAAK,GAAG,IAAI,UAAU;AAC3D"}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { bucketKey } from "./bucketKey.js";
|
|
2
|
+
import { ROLLUPS_SLUG } from "../collections/rollups.js";
|
|
3
|
+
//#region src/native/rollups/bumpRollups.ts
|
|
4
|
+
const PG_TABLE_KEY = "analytics_rollups";
|
|
5
|
+
/**
|
|
6
|
+
* Loads the optional Postgres peer without a literal specifier: bundlers (Turbopack, webpack,
|
|
7
|
+
* Vite) resolve literal dynamic imports at build time, which fails a Mongo host's build even
|
|
8
|
+
* though this branch is unreachable there. The function-built specifier is opaque to their
|
|
9
|
+
* analyzers, the ignore comments cover bundlers that would still warn, and the type-only
|
|
10
|
+
* reference above erases at compile time, so the package is touched only when the Postgres
|
|
11
|
+
* branch actually runs.
|
|
12
|
+
*/
|
|
13
|
+
const importPostgresSql = () => {
|
|
14
|
+
return import(
|
|
15
|
+
/* webpackIgnore: true */
|
|
16
|
+
/* turbopackIgnore: true */
|
|
17
|
+
/* @vite-ignore */
|
|
18
|
+
["@payloadcms", "db-postgres"].join("/")
|
|
19
|
+
);
|
|
20
|
+
};
|
|
21
|
+
const ROLLUP_ZERO = {
|
|
22
|
+
pageviews: 0,
|
|
23
|
+
events: 0,
|
|
24
|
+
durationMs: 0,
|
|
25
|
+
samples: 0,
|
|
26
|
+
visitors: 0,
|
|
27
|
+
sessions: 0,
|
|
28
|
+
conversions: 0,
|
|
29
|
+
revenue: 0,
|
|
30
|
+
scrollDepthSum: 0,
|
|
31
|
+
scrollSamples: 0
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Sums bumps that land on the same bucket. Required, not an optimization: Postgres rejects a
|
|
35
|
+
* statement whose ON CONFLICT DO UPDATE would touch one row twice, and a flush routinely
|
|
36
|
+
* bumps a bucket's visitors and sessions in the same call.
|
|
37
|
+
*/
|
|
38
|
+
const coalesce = (bumps) => {
|
|
39
|
+
const merged = /* @__PURE__ */ new Map();
|
|
40
|
+
for (const bump of bumps) {
|
|
41
|
+
const existing = merged.get(bucketKey(bump.key));
|
|
42
|
+
if (!existing) {
|
|
43
|
+
merged.set(bucketKey(bump.key), {
|
|
44
|
+
key: bump.key,
|
|
45
|
+
inc: { ...bump.inc }
|
|
46
|
+
});
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
for (const [metric, amount] of Object.entries(bump.inc)) existing.inc[metric] = (existing.inc[metric] ?? 0) + amount;
|
|
50
|
+
}
|
|
51
|
+
return [...merged.values()];
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Upserts every bucket in one statement per database, incrementing where the row exists and
|
|
55
|
+
* seeding the full metric baseline where it does not (Payload's defaultValue is applied
|
|
56
|
+
* app-side, not as a SQL default, so a raw insert that omits a NOT NULL metric would fail on
|
|
57
|
+
* Postgres). This is the write path a flush uses: one round trip instead of one per bucket,
|
|
58
|
+
* which is the difference between a handful and a few hundred per ingested hit.
|
|
59
|
+
*/
|
|
60
|
+
async function bumpRollups(payload, bumps) {
|
|
61
|
+
const merged = coalesce(bumps);
|
|
62
|
+
if (merged.length === 0) return;
|
|
63
|
+
const scoped = merged[0]?.key.scope !== void 0;
|
|
64
|
+
if (merged.some(({ key }) => key.scope !== void 0 !== scoped)) throw new Error("analytics: rollup bumps mix scoped and unscoped buckets in one batch");
|
|
65
|
+
if (payload.db.name === "mongoose") {
|
|
66
|
+
const model = payload.db.collections[ROLLUPS_SLUG];
|
|
67
|
+
if (!model) throw new Error(`analytics: mongoose collection "${ROLLUPS_SLUG}" not found`);
|
|
68
|
+
const ops = merged.map(({ key, inc }) => {
|
|
69
|
+
const setOnInsert = { ...key };
|
|
70
|
+
for (const [metric, zero] of Object.entries(ROLLUP_ZERO)) if (!(metric in inc)) setOnInsert[metric] = zero;
|
|
71
|
+
return { updateOne: {
|
|
72
|
+
filter: key,
|
|
73
|
+
update: Object.keys(inc).length > 0 ? {
|
|
74
|
+
$inc: inc,
|
|
75
|
+
$setOnInsert: setOnInsert
|
|
76
|
+
} : { $setOnInsert: setOnInsert },
|
|
77
|
+
upsert: true
|
|
78
|
+
} };
|
|
79
|
+
});
|
|
80
|
+
await model.collection.bulkWrite(ops, { ordered: false });
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const { sql } = await importPostgresSql();
|
|
84
|
+
const db = payload.db;
|
|
85
|
+
const tableName = db.tableNameMap.get(PG_TABLE_KEY);
|
|
86
|
+
if (!tableName) throw new Error(`analytics: drizzle table "${PG_TABLE_KEY}" not found`);
|
|
87
|
+
const table = db.tables[tableName];
|
|
88
|
+
if (!table) throw new Error(`analytics: drizzle table object for "${tableName}" not found`);
|
|
89
|
+
const set = {};
|
|
90
|
+
for (const metric of Object.keys(ROLLUP_ZERO)) {
|
|
91
|
+
const column = table[metric];
|
|
92
|
+
if (!column?.name) throw new Error(`analytics: rollup column "${metric}" not found`);
|
|
93
|
+
set[metric] = sql`${table[metric]} + excluded.${sql.identifier(column.name)}`;
|
|
94
|
+
}
|
|
95
|
+
const target = [
|
|
96
|
+
table.granularity,
|
|
97
|
+
table.period,
|
|
98
|
+
table.path,
|
|
99
|
+
table.dimension,
|
|
100
|
+
table.dimvalue,
|
|
101
|
+
table.hostname
|
|
102
|
+
];
|
|
103
|
+
if (scoped) target.push(table.scope);
|
|
104
|
+
await db.drizzle.insert(table).values(merged.map(({ key, inc }) => ({
|
|
105
|
+
...key,
|
|
106
|
+
...ROLLUP_ZERO,
|
|
107
|
+
...inc
|
|
108
|
+
}))).onConflictDoUpdate({
|
|
109
|
+
target,
|
|
110
|
+
set
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
//#endregion
|
|
114
|
+
export { bumpRollups };
|
|
115
|
+
|
|
116
|
+
//# sourceMappingURL=bumpRollups.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bumpRollups.js","names":[],"sources":["../../../src/native/rollups/bumpRollups.ts"],"sourcesContent":["import type { Payload } from 'payload'\nimport { ROLLUPS_SLUG } from '../collections/rollups'\nimport { bucketKey } from './bucketKey'\nimport type { RollupKey, RollupMetric } from './deltas'\n\nconst PG_TABLE_KEY = 'analytics_rollups'\n\ntype PostgresSqlModule = { sql: typeof import('@payloadcms/db-postgres')['sql'] }\n\n/**\n * Loads the optional Postgres peer without a literal specifier: bundlers (Turbopack, webpack,\n * Vite) resolve literal dynamic imports at build time, which fails a Mongo host's build even\n * though this branch is unreachable there. The function-built specifier is opaque to their\n * analyzers, the ignore comments cover bundlers that would still warn, and the type-only\n * reference above erases at compile time, so the package is touched only when the Postgres\n * branch actually runs.\n */\nconst importPostgresSql = (): Promise<PostgresSqlModule> => {\n\tconst specifier = ['@payloadcms', 'db-postgres'].join('/')\n\treturn import(\n\t\t/* webpackIgnore: true */ /* turbopackIgnore: true */ /* @vite-ignore */ specifier\n\t) as Promise<PostgresSqlModule>\n}\n\n// payload.db raw-access shapes are intentionally loose; these narrow casts reach the\n// Mongoose driver collection / Drizzle instance for atomic upserts (no public typed API).\ntype MongoDb = {\n\tname: 'mongoose'\n\tcollections: Record<string, { collection: { bulkWrite: (ops: object[], o: object) => unknown } }>\n}\ntype PgDb = {\n\tname: 'postgres'\n\tdrizzle: {\n\t\tinsert: (t: unknown) => {\n\t\t\tvalues: (v: unknown[]) => { onConflictDoUpdate: (c: unknown) => Promise<unknown> }\n\t\t}\n\t}\n\ttables: Record<string, Record<string, unknown>>\n\ttableNameMap: Map<string, string>\n}\n\nexport const ROLLUP_ZERO: Record<RollupMetric, number> = {\n\tpageviews: 0,\n\tevents: 0,\n\tdurationMs: 0,\n\tsamples: 0,\n\tvisitors: 0,\n\tsessions: 0,\n\tconversions: 0,\n\trevenue: 0,\n\tscrollDepthSum: 0,\n\tscrollSamples: 0,\n}\n\nexport interface RollupBump {\n\tkey: RollupKey\n\tinc: Partial<Record<RollupMetric, number>>\n}\n\n/**\n * Sums bumps that land on the same bucket. Required, not an optimization: Postgres rejects a\n * statement whose ON CONFLICT DO UPDATE would touch one row twice, and a flush routinely\n * bumps a bucket's visitors and sessions in the same call.\n */\nconst coalesce = (bumps: RollupBump[]): RollupBump[] => {\n\tconst merged = new Map<string, RollupBump>()\n\tfor (const bump of bumps) {\n\t\tconst existing = merged.get(bucketKey(bump.key))\n\t\tif (!existing) {\n\t\t\tmerged.set(bucketKey(bump.key), { key: bump.key, inc: { ...bump.inc } })\n\t\t\tcontinue\n\t\t}\n\t\tfor (const [metric, amount] of Object.entries(bump.inc) as Array<[RollupMetric, number]>) {\n\t\t\texisting.inc[metric] = (existing.inc[metric] ?? 0) + amount\n\t\t}\n\t}\n\treturn [...merged.values()]\n}\n\n/**\n * Upserts every bucket in one statement per database, incrementing where the row exists and\n * seeding the full metric baseline where it does not (Payload's defaultValue is applied\n * app-side, not as a SQL default, so a raw insert that omits a NOT NULL metric would fail on\n * Postgres). This is the write path a flush uses: one round trip instead of one per bucket,\n * which is the difference between a handful and a few hundred per ingested hit.\n */\nexport async function bumpRollups(payload: Payload, bumps: RollupBump[]): Promise<void> {\n\tconst merged = coalesce(bumps)\n\tif (merged.length === 0) {\n\t\treturn\n\t}\n\t// A batch belongs to one install shape: scope is part of the unique bucket where it is\n\t// present, and Postgres needs one conflict target for the whole statement. A mixed batch has\n\t// no single right answer, so it is a caller bug on both adapters rather than rows quietly\n\t// filed under the first bucket's shape.\n\tconst scoped = merged[0]?.key.scope !== undefined\n\tif (merged.some(({ key }) => (key.scope !== undefined) !== scoped)) {\n\t\tthrow new Error('analytics: rollup bumps mix scoped and unscoped buckets in one batch')\n\t}\n\tif (payload.db.name === 'mongoose') {\n\t\tconst db = payload.db as unknown as MongoDb\n\t\tconst model = db.collections[ROLLUPS_SLUG]\n\t\tif (!model) throw new Error(`analytics: mongoose collection \"${ROLLUPS_SLUG}\" not found`)\n\t\tconst ops = merged.map(({ key, inc }) => {\n\t\t\tconst setOnInsert: Record<string, unknown> = { ...key }\n\t\t\tfor (const [metric, zero] of Object.entries(ROLLUP_ZERO)) {\n\t\t\t\tif (!(metric in inc)) setOnInsert[metric] = zero\n\t\t\t}\n\t\t\t// $inc rejects an empty operand, so a bump with no metrics only seeds the row.\n\t\t\tconst update =\n\t\t\t\tObject.keys(inc).length > 0\n\t\t\t\t\t? { $inc: inc, $setOnInsert: setOnInsert }\n\t\t\t\t\t: { $setOnInsert: setOnInsert }\n\t\t\treturn { updateOne: { filter: key, update, upsert: true } }\n\t\t})\n\t\tawait model.collection.bulkWrite(ops, { ordered: false })\n\t\treturn\n\t}\n\tconst { sql } = await importPostgresSql()\n\tconst db = payload.db as unknown as PgDb\n\tconst tableName = db.tableNameMap.get(PG_TABLE_KEY)\n\tif (!tableName) throw new Error(`analytics: drizzle table \"${PG_TABLE_KEY}\" not found`)\n\tconst table = db.tables[tableName]\n\tif (!table) throw new Error(`analytics: drizzle table object for \"${tableName}\" not found`)\n\t// Every metric is updated from `excluded`, the row this statement tried to insert, so one\n\t// statement can carry a different set of increments per bucket. The metrics a bump does\n\t// not name are zero in its row, and adding zero is the no-op it should be.\n\tconst set: Record<string, unknown> = {}\n\tfor (const metric of Object.keys(ROLLUP_ZERO)) {\n\t\tconst column = table[metric] as { name?: string } | undefined\n\t\tif (!column?.name) throw new Error(`analytics: rollup column \"${metric}\" not found`)\n\t\tset[metric] = sql`${table[metric]} + excluded.${sql.identifier(column.name)}`\n\t}\n\t// The conflict target must match the unique index exactly, which carries the scope column\n\t// only in scoped installs.\n\tconst target = [\n\t\ttable.granularity,\n\t\ttable.period,\n\t\ttable.path,\n\t\ttable.dimension,\n\t\ttable.dimvalue,\n\t\ttable.hostname,\n\t]\n\tif (scoped) {\n\t\ttarget.push(table.scope)\n\t}\n\tawait db.drizzle\n\t\t.insert(table)\n\t\t.values(merged.map(({ key, inc }) => ({ ...key, ...ROLLUP_ZERO, ...inc })))\n\t\t.onConflictDoUpdate({ target, set })\n}\n"],"mappings":";;;AAKA,MAAM,eAAe;;;;;;;;;AAYrB,MAAM,0BAAsD;CAE3D,OAAO;;;;EADW,CAAC,eAAe,aAAa,EAAE,KAAK,GAE4B;;AAEnF;AAmBA,MAAa,cAA4C;CACxD,WAAW;CACX,QAAQ;CACR,YAAY;CACZ,SAAS;CACT,UAAU;CACV,UAAU;CACV,aAAa;CACb,SAAS;CACT,gBAAgB;CAChB,eAAe;AAChB;;;;;;AAYA,MAAM,YAAY,UAAsC;CACvD,MAAM,yBAAS,IAAI,IAAwB;CAC3C,KAAK,MAAM,QAAQ,OAAO;EACzB,MAAM,WAAW,OAAO,IAAI,UAAU,KAAK,GAAG,CAAC;EAC/C,IAAI,CAAC,UAAU;GACd,OAAO,IAAI,UAAU,KAAK,GAAG,GAAG;IAAE,KAAK,KAAK;IAAK,KAAK,EAAE,GAAG,KAAK,IAAI;GAAE,CAAC;GACvE;EACD;EACA,KAAK,MAAM,CAAC,QAAQ,WAAW,OAAO,QAAQ,KAAK,GAAG,GACrD,SAAS,IAAI,WAAW,SAAS,IAAI,WAAW,KAAK;CAEvD;CACA,OAAO,CAAC,GAA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Q,MAAM,MAAM,IAAI,MAAM,6BAA6B,OAAO,YAAY;EACnF,IAAI,UAAU,GAAG,GAAG,MAAM,QAAQ,cAAc,IAAI,WAAW,OAAO,IAAI;CAC3E;CAGA,MAAM,SAAS;EACd,MAAM;EACN,MAAM;EACN,MAAM;EACN,MAAM;EACN,MAAM;EACN,MAAM;CACP;CACA,IAAI,QACH,OAAO,KAAK,MAAM,KAAK;CAExB,MAAM,GAAG,QACP,OAAO,KAAK,EACZ,OAAO,OAAO,KAAK,EAAE,KAAK,WAAW;EAAE,GAAG;EAAK,GAAG;EAAa,GAAG;CAAI,EAAE,CAAC,EACzE,mBAAmB;EAAE;EAAQ;CAAI,CAAC;AACrC"}
|
|
@@ -1,6 +1,28 @@
|
|
|
1
1
|
import { startOfDayInTz } from "../../timeframe/tz.js";
|
|
2
2
|
//#region src/native/rollups/deltas.ts
|
|
3
3
|
/**
|
|
4
|
+
* Every dimension whose bucket value is a stored string field, and the field it reads. A
|
|
5
|
+
* dimension the event carries no value for emits no bucket at all, so an unattributed hit
|
|
6
|
+
* never lands in an "unknown" row. `referrer` reads the host derived at ingest, the same
|
|
7
|
+
* field the raw-event read path groups and filters on.
|
|
8
|
+
*/
|
|
9
|
+
const DIMENSION_FIELDS = [
|
|
10
|
+
["country", "country"],
|
|
11
|
+
["region", "region"],
|
|
12
|
+
["city", "city"],
|
|
13
|
+
["device", "device"],
|
|
14
|
+
["browser", "browser"],
|
|
15
|
+
["os", "os"],
|
|
16
|
+
["language", "language"],
|
|
17
|
+
["source", "source"],
|
|
18
|
+
["referrer", "referrerHost"],
|
|
19
|
+
["utmSource", "utmSource"],
|
|
20
|
+
["utmMedium", "utmMedium"],
|
|
21
|
+
["utmCampaign", "utmCampaign"],
|
|
22
|
+
["utmContent", "utmContent"],
|
|
23
|
+
["utmTerm", "utmTerm"]
|
|
24
|
+
];
|
|
25
|
+
/**
|
|
4
26
|
* Distinct metrics (visitors, sessions) are exact per bucket and must never be summed
|
|
5
27
|
* across buckets, so an unfiltered read has to hit its own exact family rather than
|
|
6
28
|
* derive from a hostname-scoped one (or vice versa). Every bucket is therefore emitted
|
|
@@ -56,21 +78,14 @@ function computeRollupDeltas(event) {
|
|
|
56
78
|
"event",
|
|
57
79
|
event.name
|
|
58
80
|
]);
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
"
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
event.device
|
|
68
|
-
]);
|
|
69
|
-
if (event.source) buckets.push([
|
|
70
|
-
"",
|
|
71
|
-
"source",
|
|
72
|
-
event.source
|
|
73
|
-
]);
|
|
81
|
+
for (const [dimension, field] of DIMENSION_FIELDS) {
|
|
82
|
+
const value = event[field];
|
|
83
|
+
if (typeof value === "string" && value) buckets.push([
|
|
84
|
+
"",
|
|
85
|
+
dimension,
|
|
86
|
+
value
|
|
87
|
+
]);
|
|
88
|
+
}
|
|
74
89
|
const families = event.hostname ? ["", event.hostname] : [""];
|
|
75
90
|
const deltas = [];
|
|
76
91
|
for (const hostname of families) deltas.push(...buckets.map((bucket) => make(bucket, hostname)));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deltas.js","names":[],"sources":["../../../src/native/rollups/deltas.ts"],"sourcesContent":["import { startOfDayInTz } from '../../timeframe/tz'\nimport type { StoredEvent } from '../ingest/normalizeEvent'\n\nexport interface RollupKey {\n\tgranularity: 'day'\n\tperiod: Date\n\tpath: string\n\tdimension: string\n\tdimvalue: string\n\t/** '' is the hostname-less bucket family; a real hostname is its own family. */\n\thostname: string\n\t/** Present only in scoped installs, where it is part of the unique bucket. */\n\tscope?: string\n}\n\nexport type RollupMetric =\n\t| 'pageviews'\n\t| 'events'\n\t| 'durationMs'\n\t| 'samples'\n\t| 'visitors'\n\t| 'sessions'\n\t| 'conversions'\n\t| 'revenue'\n\t| 'scrollDepthSum'\n\t| 'scrollSamples'\n\n/**\n * `samples` counts every event in the bucket, so it cannot be the scroll-depth denominator:\n * `scrollSamples` counts only the pageviews that actually reported a depth, which is what\n * keeps the `scrollDepth` average honest when the tracker never got to send one.\n */\nexport type RollupInc = {\n\tpageviews: number\n\tevents: number\n\tdurationMs: number\n\tsamples: number\n\tconversions: number\n\trevenue: number\n\tscrollDepthSum: number\n\tscrollSamples: number\n}\n\nexport interface RollupDelta {\n\tkey: RollupKey\n\tinc: RollupInc\n}\n\n/**\n * Distinct metrics (visitors, sessions) are exact per bucket and must never be summed\n * across buckets, so an unfiltered read has to hit its own exact family rather than\n * derive from a hostname-scoped one (or vice versa). Every bucket is therefore emitted\n * twice when a hostname is present: once in the hostname-less ('') family that unfiltered\n * reads use, and once more in the exact-hostname family that a hostname-scoped read uses.\n * Both families stay per-bucket exact at the cost of one extra bucket set per distinct\n * hostname a site sees.\n *\n * Goal completions add one `('', 'goal', slug)` bucket each, carrying that goal's own single\n * conversion, while every other bucket carries the event's whole conversion count, so a\n * site-wide or per-page total never has to scan the goal buckets.\n */\nexport function computeRollupDeltas(event: StoredEvent): RollupDelta[] {\n\t// Bucket into the event's reporting-timezone day (UTC when unset), fixing the day\n\t// boundary at ingest. Existing rollups are not re-bucketed if the timezone changes.\n\tconst period = startOfDayInTz(event.timestamp, event.timezone)\n\tconst completions = event.goals ?? []\n\tconst reportedDepth = event.scrollDepth !== undefined\n\tconst inc: RollupInc = {\n\t\tpageviews: event.type === 'pageview' ? 1 : 0,\n\t\t// A `goal` event is a non-pageview hit like a custom event; it stays out of the\n\t\t// `event` dimension breakdown, which reports named custom events only.\n\t\tevents: event.type === 'pageview' ? 0 : 1,\n\t\tdurationMs: event.durationMs ?? 0,\n\t\tsamples: 1,\n\t\tconversions: completions.length,\n\t\trevenue: completions.reduce((sum, goal) => sum + goal.value, 0),\n\t\tscrollDepthSum: reportedDepth ? (event.scrollDepth ?? 0) : 0,\n\t\tscrollSamples: reportedDepth ? 1 : 0,\n\t}\n\tconst make = (\n\t\tbucket: [path: string, dimension: string, dimvalue: string],\n\t\thostname: string,\n\t\tover?: Partial<RollupInc>\n\t): RollupDelta => ({\n\t\tkey: {\n\t\t\tgranularity: 'day',\n\t\t\tperiod,\n\t\t\tpath: bucket[0],\n\t\t\tdimension: bucket[1],\n\t\t\tdimvalue: bucket[2],\n\t\t\thostname,\n\t\t\t...(event.scope !== undefined ? { scope: event.scope } : {}),\n\t\t},\n\t\tinc: { ...inc, ...over },\n\t})\n\tconst buckets: Array<[path: string, dimension: string, dimvalue: string]> = [\n\t\t[event.path, '', ''],\n\t\t['', '', ''],\n\t]\n\tif (event.type === 'event' && event.name) {\n\t\tbuckets.push(['', 'event', event.name])\n\t}\n\
|
|
1
|
+
{"version":3,"file":"deltas.js","names":[],"sources":["../../../src/native/rollups/deltas.ts"],"sourcesContent":["import type { DimensionKey } from '../../core/contract'\nimport { startOfDayInTz } from '../../timeframe/tz'\nimport type { StoredEvent } from '../ingest/normalizeEvent'\n\nexport interface RollupKey {\n\tgranularity: 'day'\n\tperiod: Date\n\tpath: string\n\tdimension: string\n\tdimvalue: string\n\t/** '' is the hostname-less bucket family; a real hostname is its own family. */\n\thostname: string\n\t/** Present only in scoped installs, where it is part of the unique bucket. */\n\tscope?: string\n}\n\nexport type RollupMetric =\n\t| 'pageviews'\n\t| 'events'\n\t| 'durationMs'\n\t| 'samples'\n\t| 'visitors'\n\t| 'sessions'\n\t| 'conversions'\n\t| 'revenue'\n\t| 'scrollDepthSum'\n\t| 'scrollSamples'\n\n/**\n * `samples` counts every event in the bucket, so it cannot be the scroll-depth denominator:\n * `scrollSamples` counts only the pageviews that actually reported a depth, which is what\n * keeps the `scrollDepth` average honest when the tracker never got to send one.\n */\nexport type RollupInc = {\n\tpageviews: number\n\tevents: number\n\tdurationMs: number\n\tsamples: number\n\tconversions: number\n\trevenue: number\n\tscrollDepthSum: number\n\tscrollSamples: number\n}\n\nexport interface RollupDelta {\n\tkey: RollupKey\n\tinc: RollupInc\n}\n\n/**\n * Every dimension whose bucket value is a stored string field, and the field it reads. A\n * dimension the event carries no value for emits no bucket at all, so an unattributed hit\n * never lands in an \"unknown\" row. `referrer` reads the host derived at ingest, the same\n * field the raw-event read path groups and filters on.\n */\nconst DIMENSION_FIELDS: ReadonlyArray<\n\treadonly [dimension: DimensionKey, field: keyof StoredEvent]\n> = [\n\t['country', 'country'],\n\t['region', 'region'],\n\t['city', 'city'],\n\t['device', 'device'],\n\t['browser', 'browser'],\n\t['os', 'os'],\n\t['language', 'language'],\n\t['source', 'source'],\n\t['referrer', 'referrerHost'],\n\t['utmSource', 'utmSource'],\n\t['utmMedium', 'utmMedium'],\n\t['utmCampaign', 'utmCampaign'],\n\t['utmContent', 'utmContent'],\n\t['utmTerm', 'utmTerm'],\n]\n\n/**\n * Distinct metrics (visitors, sessions) are exact per bucket and must never be summed\n * across buckets, so an unfiltered read has to hit its own exact family rather than\n * derive from a hostname-scoped one (or vice versa). Every bucket is therefore emitted\n * twice when a hostname is present: once in the hostname-less ('') family that unfiltered\n * reads use, and once more in the exact-hostname family that a hostname-scoped read uses.\n * Both families stay per-bucket exact at the cost of one extra bucket set per distinct\n * hostname a site sees.\n *\n * Goal completions add one `('', 'goal', slug)` bucket each, carrying that goal's own single\n * conversion, while every other bucket carries the event's whole conversion count, so a\n * site-wide or per-page total never has to scan the goal buckets.\n */\nexport function computeRollupDeltas(event: StoredEvent): RollupDelta[] {\n\t// Bucket into the event's reporting-timezone day (UTC when unset), fixing the day\n\t// boundary at ingest. Existing rollups are not re-bucketed if the timezone changes.\n\tconst period = startOfDayInTz(event.timestamp, event.timezone)\n\tconst completions = event.goals ?? []\n\tconst reportedDepth = event.scrollDepth !== undefined\n\tconst inc: RollupInc = {\n\t\tpageviews: event.type === 'pageview' ? 1 : 0,\n\t\t// A `goal` event is a non-pageview hit like a custom event; it stays out of the\n\t\t// `event` dimension breakdown, which reports named custom events only.\n\t\tevents: event.type === 'pageview' ? 0 : 1,\n\t\tdurationMs: event.durationMs ?? 0,\n\t\tsamples: 1,\n\t\tconversions: completions.length,\n\t\trevenue: completions.reduce((sum, goal) => sum + goal.value, 0),\n\t\tscrollDepthSum: reportedDepth ? (event.scrollDepth ?? 0) : 0,\n\t\tscrollSamples: reportedDepth ? 1 : 0,\n\t}\n\tconst make = (\n\t\tbucket: [path: string, dimension: string, dimvalue: string],\n\t\thostname: string,\n\t\tover?: Partial<RollupInc>\n\t): RollupDelta => ({\n\t\tkey: {\n\t\t\tgranularity: 'day',\n\t\t\tperiod,\n\t\t\tpath: bucket[0],\n\t\t\tdimension: bucket[1],\n\t\t\tdimvalue: bucket[2],\n\t\t\thostname,\n\t\t\t...(event.scope !== undefined ? { scope: event.scope } : {}),\n\t\t},\n\t\tinc: { ...inc, ...over },\n\t})\n\tconst buckets: Array<[path: string, dimension: string, dimvalue: string]> = [\n\t\t[event.path, '', ''],\n\t\t['', '', ''],\n\t]\n\tif (event.type === 'event' && event.name) {\n\t\tbuckets.push(['', 'event', event.name])\n\t}\n\tfor (const [dimension, field] of DIMENSION_FIELDS) {\n\t\tconst value = event[field]\n\t\tif (typeof value === 'string' && value) {\n\t\t\tbuckets.push(['', dimension, value])\n\t\t}\n\t}\n\tconst families = event.hostname ? ['', event.hostname] : ['']\n\tconst deltas: RollupDelta[] = []\n\tfor (const hostname of families) {\n\t\tdeltas.push(...buckets.map((bucket) => make(bucket, hostname)))\n\t}\n\tfor (const hostname of families) {\n\t\tfor (const completion of completions) {\n\t\t\tdeltas.push(\n\t\t\t\tmake(['', 'goal', completion.slug], hostname, {\n\t\t\t\t\tconversions: 1,\n\t\t\t\t\trevenue: completion.value,\n\t\t\t\t})\n\t\t\t)\n\t\t}\n\t}\n\treturn deltas\n}\n"],"mappings":";;;;;;;;AAuDA,MAAM,mBAEF;CACH,CAAC,WAAW,SAAS;CACrB,CAAC,UAAU,QAAQ;CACnB,CAAC,QAAQ,MAAM;CACf,CAAC,UAAU,QAAQ;CACnB,CAAC,WAAW,SAAS;CACrB,CAAC,MAAM,IAAI;CACX,CAAC,YAAY,UAAU;CACvB,CAAC,UAAU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AAC,IAAI,MAAM,QAAQ,IAAI,CAAC,EAAE;CAC5D,MAAM,SAAwB,CAAC;CAC/B,KAAK,MAAM,YAAY,UACtB,OAAO,KAAK,GAAG,QAAQ,KAAK,WAAW,KAAK,QAAQ,QAAQ,CAAC,CAAC;CAE/D,KAAK,MAAM,YAAY,UACtB,KAAK,MAAM,cAAc,aACxB,OAAO,KACN,KAAK;EAAC;EAAI;EAAQ,WAAW;CAAI,GAAG,UAAU;EAC7C,aAAa;EACb,SAAS,WAAW;CACrB,CAAC,CACF;CAGF,OAAO;AACR"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
//#region src/native/rollups/insertManyIfNew.ts
|
|
2
|
+
/** Mongo duplicate key. */
|
|
3
|
+
const MONGO_DUPLICATE = 11e3;
|
|
4
|
+
/**
|
|
5
|
+
* Which rows of an unordered `insertMany` the index rejected, or undefined when the failure
|
|
6
|
+
* was something else entirely (a dead connection, a validation error) and must be rethrown
|
|
7
|
+
* rather than read as "already seen".
|
|
8
|
+
*/
|
|
9
|
+
const duplicateIndexes = (err) => {
|
|
10
|
+
if (typeof err !== "object" || err === null) return;
|
|
11
|
+
const raw = err.writeErrors;
|
|
12
|
+
const errors = Array.isArray(raw) ? raw : raw ? [raw] : [];
|
|
13
|
+
if (errors.length === 0) return err.code === MONGO_DUPLICATE ? new Set([0]) : void 0;
|
|
14
|
+
const indexes = /* @__PURE__ */ new Set();
|
|
15
|
+
for (const error of errors) {
|
|
16
|
+
if (error.code !== MONGO_DUPLICATE || typeof error.index !== "number") return;
|
|
17
|
+
indexes.add(error.index);
|
|
18
|
+
}
|
|
19
|
+
return indexes;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Row identity for matching a returned row back to the row that asked for it. A date field is
|
|
23
|
+
* normalized through Date because Postgres hands a timestamp back as its own text
|
|
24
|
+
* (`2026-08-01 00:00:00+00`) rather than as the Date that was sent.
|
|
25
|
+
*/
|
|
26
|
+
const rowKey = (row, fields, dateFields) => JSON.stringify(fields.map((field) => {
|
|
27
|
+
const value = row[field];
|
|
28
|
+
if (!dateFields.has(field)) return String(value);
|
|
29
|
+
const time = new Date(value).getTime();
|
|
30
|
+
return Number.isNaN(time) ? String(value) : time;
|
|
31
|
+
}));
|
|
32
|
+
/**
|
|
33
|
+
* `insertIfNew` for a whole batch: one statement, one answer per row in the order they were
|
|
34
|
+
* given, true where that row was the one that created the ledger entry. The unique index is
|
|
35
|
+
* still the entire correctness mechanism, so a row another writer inserted first comes back
|
|
36
|
+
* false whether the driver reports it quietly or as a duplicate-key error.
|
|
37
|
+
*
|
|
38
|
+
* A row repeated inside one batch counts once, the first occurrence: Mongo would answer the
|
|
39
|
+
* repeat with a duplicate-key error while Postgres skips it silently, so the batch is
|
|
40
|
+
* de-duplicated here and both databases give the same answer.
|
|
41
|
+
*/
|
|
42
|
+
async function insertManyIfNew(payload, slug, rows) {
|
|
43
|
+
if (rows.length === 0) return [];
|
|
44
|
+
const fields = Object.keys(rows[0]);
|
|
45
|
+
const dateFields = new Set(fields.filter((f) => rows.some((row) => row[f] instanceof Date)));
|
|
46
|
+
const seenInBatch = /* @__PURE__ */ new Set();
|
|
47
|
+
const first = rows.map((row) => {
|
|
48
|
+
const key = rowKey(row, fields, dateFields);
|
|
49
|
+
if (seenInBatch.has(key)) return false;
|
|
50
|
+
seenInBatch.add(key);
|
|
51
|
+
return true;
|
|
52
|
+
});
|
|
53
|
+
const attempts = rows.filter((_, index) => first[index]);
|
|
54
|
+
const answer = (attempted) => {
|
|
55
|
+
let next = 0;
|
|
56
|
+
return first.map((isFirst) => isFirst ? attempted[next++] ?? false : false);
|
|
57
|
+
};
|
|
58
|
+
if (payload.db.name === "mongoose") {
|
|
59
|
+
const model = payload.db.collections[slug];
|
|
60
|
+
if (!model) throw new Error(`analytics: mongoose collection "${slug}" not found`);
|
|
61
|
+
try {
|
|
62
|
+
await model.collection.insertMany(attempts.map((row) => ({ ...row })), { ordered: false });
|
|
63
|
+
return answer(attempts.map(() => true));
|
|
64
|
+
} catch (err) {
|
|
65
|
+
const duplicates = duplicateIndexes(err);
|
|
66
|
+
if (!duplicates) throw err;
|
|
67
|
+
return answer(attempts.map((_, index) => !duplicates.has(index)));
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const db = payload.db;
|
|
71
|
+
const tableKey = slug.replace(/-/g, "_");
|
|
72
|
+
const tableName = db.tableNameMap.get(tableKey);
|
|
73
|
+
if (!tableName) throw new Error(`analytics: drizzle table "${tableKey}" not found`);
|
|
74
|
+
const table = db.tables[tableName];
|
|
75
|
+
if (!table) throw new Error(`analytics: drizzle table object for "${tableName}" not found`);
|
|
76
|
+
const inserted = await db.drizzle.insert(table).values(attempts).onConflictDoNothing().returning();
|
|
77
|
+
const created = /* @__PURE__ */ new Map();
|
|
78
|
+
for (const row of inserted) {
|
|
79
|
+
const key = rowKey(row, fields, dateFields);
|
|
80
|
+
created.set(key, (created.get(key) ?? 0) + 1);
|
|
81
|
+
}
|
|
82
|
+
return answer(attempts.map((row) => {
|
|
83
|
+
const key = rowKey(row, fields, dateFields);
|
|
84
|
+
const remaining = created.get(key) ?? 0;
|
|
85
|
+
if (remaining === 0) return false;
|
|
86
|
+
created.set(key, remaining - 1);
|
|
87
|
+
return true;
|
|
88
|
+
}));
|
|
89
|
+
}
|
|
90
|
+
//#endregion
|
|
91
|
+
export { insertManyIfNew };
|
|
92
|
+
|
|
93
|
+
//# sourceMappingURL=insertManyIfNew.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"insertManyIfNew.js","names":[],"sources":["../../../src/native/rollups/insertManyIfNew.ts"],"sourcesContent":["import type { Payload } from 'payload'\n\n// payload.db raw-access shapes are intentionally loose; these narrow casts reach the\n// Mongoose driver collection / Drizzle instance directly (no public typed API).\ntype MongoDb = {\n\tname: 'mongoose'\n\tcollections: Record<\n\t\tstring,\n\t\t{ collection: { insertMany: (docs: object[], o: object) => Promise<unknown> } }\n\t>\n}\ntype PgDb = {\n\tname: 'postgres'\n\tdrizzle: {\n\t\tinsert: (t: unknown) => {\n\t\t\tvalues: (v: unknown[]) => {\n\t\t\t\tonConflictDoNothing: () => { returning: () => Promise<Array<Record<string, unknown>>> }\n\t\t\t}\n\t\t}\n\t}\n\ttables: Record<string, Record<string, unknown>>\n\ttableNameMap: Map<string, string>\n}\n\n/** Mongo duplicate key. */\nconst MONGO_DUPLICATE = 11000\n\ntype WriteError = { index?: unknown; code?: unknown }\n\n/**\n * Which rows of an unordered `insertMany` the index rejected, or undefined when the failure\n * was something else entirely (a dead connection, a validation error) and must be rethrown\n * rather than read as \"already seen\".\n */\nconst duplicateIndexes = (err: unknown): Set<number> | undefined => {\n\tif (typeof err !== 'object' || err === null) {\n\t\treturn undefined\n\t}\n\tconst raw = (err as { writeErrors?: unknown }).writeErrors\n\tconst errors: WriteError[] = Array.isArray(raw) ? raw : raw ? [raw as WriteError] : []\n\tif (errors.length === 0) {\n\t\t// A single-document rejection can arrive as a plain duplicate-key error with no\n\t\t// writeErrors array at all.\n\t\treturn (err as { code?: unknown }).code === MONGO_DUPLICATE ? new Set([0]) : undefined\n\t}\n\tconst indexes = new Set<number>()\n\tfor (const error of errors) {\n\t\tif (error.code !== MONGO_DUPLICATE || typeof error.index !== 'number') {\n\t\t\treturn undefined\n\t\t}\n\t\tindexes.add(error.index)\n\t}\n\treturn indexes\n}\n\n/**\n * Row identity for matching a returned row back to the row that asked for it. A date field is\n * normalized through Date because Postgres hands a timestamp back as its own text\n * (`2026-08-01 00:00:00+00`) rather than as the Date that was sent.\n */\nconst rowKey = (\n\trow: Record<string, unknown>,\n\tfields: string[],\n\tdateFields: ReadonlySet<string>\n): string =>\n\tJSON.stringify(\n\t\tfields.map((field) => {\n\t\t\tconst value = row[field]\n\t\t\tif (!dateFields.has(field)) {\n\t\t\t\treturn String(value)\n\t\t\t}\n\t\t\tconst time = new Date(value as string | Date).getTime()\n\t\t\treturn Number.isNaN(time) ? String(value) : time\n\t\t})\n\t)\n\n/**\n * `insertIfNew` for a whole batch: one statement, one answer per row in the order they were\n * given, true where that row was the one that created the ledger entry. The unique index is\n * still the entire correctness mechanism, so a row another writer inserted first comes back\n * false whether the driver reports it quietly or as a duplicate-key error.\n *\n * A row repeated inside one batch counts once, the first occurrence: Mongo would answer the\n * repeat with a duplicate-key error while Postgres skips it silently, so the batch is\n * de-duplicated here and both databases give the same answer.\n */\nexport async function insertManyIfNew(\n\tpayload: Payload,\n\tslug: string,\n\trows: Array<Record<string, unknown>>\n): Promise<boolean[]> {\n\tif (rows.length === 0) {\n\t\treturn []\n\t}\n\tconst fields = Object.keys(rows[0] as Record<string, unknown>)\n\tconst dateFields = new Set(fields.filter((f) => rows.some((row) => row[f] instanceof Date)))\n\tconst seenInBatch = new Set<string>()\n\tconst first: boolean[] = rows.map((row) => {\n\t\tconst key = rowKey(row, fields, dateFields)\n\t\tif (seenInBatch.has(key)) {\n\t\t\treturn false\n\t\t}\n\t\tseenInBatch.add(key)\n\t\treturn true\n\t})\n\tconst attempts = rows.filter((_, index) => first[index])\n\n\tconst answer = (attempted: boolean[]): boolean[] => {\n\t\tlet next = 0\n\t\treturn first.map((isFirst) => (isFirst ? (attempted[next++] ?? false) : false))\n\t}\n\n\tif (payload.db.name === 'mongoose') {\n\t\tconst db = payload.db as unknown as MongoDb\n\t\tconst model = db.collections[slug]\n\t\tif (!model) throw new Error(`analytics: mongoose collection \"${slug}\" not found`)\n\t\ttry {\n\t\t\t// Copies, because insertMany stamps an _id onto every document it is handed.\n\t\t\tawait model.collection.insertMany(\n\t\t\t\tattempts.map((row) => ({ ...row })),\n\t\t\t\t{ ordered: false }\n\t\t\t)\n\t\t\treturn answer(attempts.map(() => true))\n\t\t} catch (err) {\n\t\t\tconst duplicates = duplicateIndexes(err)\n\t\t\tif (!duplicates) throw err\n\t\t\treturn answer(attempts.map((_, index) => !duplicates.has(index)))\n\t\t}\n\t}\n\tconst db = payload.db as unknown as PgDb\n\tconst tableKey = slug.replace(/-/g, '_')\n\tconst tableName = db.tableNameMap.get(tableKey)\n\tif (!tableName) throw new Error(`analytics: drizzle table \"${tableKey}\" not found`)\n\tconst table = db.tables[tableName]\n\tif (!table) throw new Error(`analytics: drizzle table object for \"${tableName}\" not found`)\n\tconst inserted = await db.drizzle.insert(table).values(attempts).onConflictDoNothing().returning()\n\t// Returned rows say which inserts happened, not which row asked for them, so they are\n\t// matched back by value. A count rather than a flag, so identical rows stay one to one.\n\tconst created = new Map<string, number>()\n\tfor (const row of inserted) {\n\t\tconst key = rowKey(row, fields, dateFields)\n\t\tcreated.set(key, (created.get(key) ?? 0) + 1)\n\t}\n\treturn answer(\n\t\tattempts.map((row) => {\n\t\t\tconst key = rowKey(row, fields, dateFields)\n\t\t\tconst remaining = created.get(key) ?? 0\n\t\t\tif (remaining === 0) {\n\t\t\t\treturn false\n\t\t\t}\n\t\t\tcreated.set(key, remaining - 1)\n\t\t\treturn true\n\t\t})\n\t)\n}\n"],"mappings":";;AAyBA,MAAM,kBAAkB;;;;;;AASxB,MAAM,oBAAoB,QAA0C;CACnE,IAAI,OAAO,QAAQ,YAAY,QAAQ,MACtC;CAED,MAAM,MAAO,IAAkC;CAC/C,MAAM,SAAuB,MAAM,QAAQ,GAAG,IAAI,MAAM,MAAM,CAAC,GAAiB,IAAI,CAAC;CACrF,IAAI,OAAO,WAAW,GAGrB,OAAQ,IAA2B,SAAS,kBAAkB,IAAI,IAAI,CAAC,CAAC,CAAC,IAAI,KAAA;CAE9E,MAAM,0BAAU,IAAI,IAAY;CAChC,KAAK,MAAM,SAAS,QAAQ;EAC3B,IAAI,MAAM,SAAS,mBAAmB,OAAO,MAAM,UAAU,UAC5D;EAED,QAAQ,IAAI,MAAM,KAAK;CACxB;CACA,OAAO;AACR;;;;;;AAOA,MAAM,UACL,KACA,QACA,eAEA,KAAK,UACJ,OAAO,KAAK,UAAU;CACrB,MAAM,QAAQ,IAAI;CAClB,IAAI,CAAC,WAAW,IAAI,KAAK,GACxB,OAAO,OAAO,KAAK;CAEpB,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,MAAM,GAAG;CACvC,MAAM,YAAY,GAAG,aAAa,IAAI,QAAQ;CAC9C,IAAI,CAAC,WAAW,MAAM,IAAI,MAAM,6BAA6B,SAAS,YAAY;CAClF,MAAM,QAAQ,GAAG,OAAO;CACxB,IAAI,CAAC,OAAO,MAAM,IAAI,MAAM,wCAAwC,UAAU,YAAY;CAC1F,MAAM,WAAW,MAAM,GAAG,QAAQ,OAAO,KAAK,EAAE,OAAO,QAAQ,EAAE,oBAAoB,EAAE,UAAU;CAGjG,MAAM,0BAAU,IAAI,IAAoB;CACxC,KAAK,MAAM,OAAO,UAAU;EAC3B,MAAM,MAAM,OAAO,KAAK,QAAQ,UAAU;EAC1C,QAAQ,IAAI,MAAM,QAAQ,IAAI,GAAG,KAAK,KAAK,CAAC;CAC7C;CACA,OAAO,OACN,SAAS,KAAK,QAAQ;EACrB,MAAM,MAAM,OAAO,KAAK,QAAQ,UAAU;EAC1C,MAAM,YAAY,QAAQ,IAAI,GAAG,KAAK;EACtC,IAAI,cAAc,GACjB,OAAO;EAER,QAAQ,IAAI,KAAK,YAAY,CAAC;EAC9B,OAAO;CACR,CAAC,CACF;AACD"}
|
|
@@ -21,8 +21,9 @@ interface CaptureRewriteMount {
|
|
|
21
21
|
* the runtime proxy, which can be told to keep one via `trailingSlashes`); harmless for
|
|
22
22
|
* every vendor this helper supports today.
|
|
23
23
|
*
|
|
24
|
-
* An adapter without `capture
|
|
25
|
-
* contributes nothing rather than throwing, so one
|
|
24
|
+
* An adapter without `capture`, or with no proxy routes (the native adapter, and GA4 whose
|
|
25
|
+
* tag is served from Google's own CDN), contributes nothing rather than throwing, so one
|
|
26
|
+
* broken slot never blanks the rest.
|
|
26
27
|
* Output preserves mount order, then each mount's declared route order.
|
|
27
28
|
*
|
|
28
29
|
* Throws for a mount path that normalizes to the site root (an unprefixed catch-all
|
|
@@ -20,8 +20,9 @@ const assertNoCollisions = (paths) => {
|
|
|
20
20
|
* the runtime proxy, which can be told to keep one via `trailingSlashes`); harmless for
|
|
21
21
|
* every vendor this helper supports today.
|
|
22
22
|
*
|
|
23
|
-
* An adapter without `capture
|
|
24
|
-
* contributes nothing rather than throwing, so one
|
|
23
|
+
* An adapter without `capture`, or with no proxy routes (the native adapter, and GA4 whose
|
|
24
|
+
* tag is served from Google's own CDN), contributes nothing rather than throwing, so one
|
|
25
|
+
* broken slot never blanks the rest.
|
|
25
26
|
* Output preserves mount order, then each mount's declared route order.
|
|
26
27
|
*
|
|
27
28
|
* Throws for a mount path that normalizes to the site root (an unprefixed catch-all
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"captureRewrites.js","names":[],"sources":["../../src/next/captureRewrites.ts"],"sourcesContent":["import { normalizeMountPath } from '../capture/mountPath'\nimport type { AnalyticsAdapter } from '../core/contract'\n\n/** One Next.js rewrite entry (the shape `next.config` `rewrites()` returns). */\nexport interface CaptureRewrite {\n\tsource: string\n\tdestination: string\n}\n\n/** An adapter's capture proxy mounted at a base path in the consumer's Next app. */\nexport interface CaptureRewriteMount {\n\t/** Base path this adapter's proxy is mounted at, e.g. '/ph' or '/api/analytics/p/global'. */\n\tpath: string\n\tadapter: AnalyticsAdapter\n}\n\nconst assertNotRoot = (path: string): void => {\n\tif (path === '/') {\n\t\tthrow new Error('captureRewrites: mount path must not be the site root')\n\t}\n}\n\n// One base is a prefix of another when they're equal or the longer one continues past a\n// '/' boundary, so '/ph' collides with '/ph' and '/ph/x' but not the mere string-prefix\n// sibling '/phx'.\nconst isPrefixOrEqual = (a: string, b: string): boolean => a === b || b.startsWith(`${a}/`)\n\nconst assertNoCollisions = (paths: string[]): void => {\n\tfor (let i = 0; i < paths.length; i++) {\n\t\tfor (let j = i + 1; j < paths.length; j++) {\n\t\t\tconst a = paths[i] as string\n\t\t\tconst b = paths[j] as string\n\t\t\tif (isPrefixOrEqual(a, b) || isPrefixOrEqual(b, a)) {\n\t\t\t\tthrow new Error(`captureRewrites: mount paths collide: '${a}' and '${b}'`)\n\t\t\t}\n\t\t}\n\t}\n}\n\n/**\n * Next.js `rewrites()` entries for every adapter's capture proxy, one mount at a time.\n * Each route's `source`/`upstream` already share `path-to-regexp` param names (e.g.\n * `:p*`), so no translation happens beyond prefixing `source` with the mount's\n * (normalized) path; `upstream` is used verbatim as `destination`. Next always strips a\n * trailing slash from the matched path before substituting it into `destination` (unlike\n * the runtime proxy, which can be told to keep one via `trailingSlashes`); harmless for\n * every vendor this helper supports today.\n *\n * An adapter without `capture
|
|
1
|
+
{"version":3,"file":"captureRewrites.js","names":[],"sources":["../../src/next/captureRewrites.ts"],"sourcesContent":["import { normalizeMountPath } from '../capture/mountPath'\nimport type { AnalyticsAdapter } from '../core/contract'\n\n/** One Next.js rewrite entry (the shape `next.config` `rewrites()` returns). */\nexport interface CaptureRewrite {\n\tsource: string\n\tdestination: string\n}\n\n/** An adapter's capture proxy mounted at a base path in the consumer's Next app. */\nexport interface CaptureRewriteMount {\n\t/** Base path this adapter's proxy is mounted at, e.g. '/ph' or '/api/analytics/p/global'. */\n\tpath: string\n\tadapter: AnalyticsAdapter\n}\n\nconst assertNotRoot = (path: string): void => {\n\tif (path === '/') {\n\t\tthrow new Error('captureRewrites: mount path must not be the site root')\n\t}\n}\n\n// One base is a prefix of another when they're equal or the longer one continues past a\n// '/' boundary, so '/ph' collides with '/ph' and '/ph/x' but not the mere string-prefix\n// sibling '/phx'.\nconst isPrefixOrEqual = (a: string, b: string): boolean => a === b || b.startsWith(`${a}/`)\n\nconst assertNoCollisions = (paths: string[]): void => {\n\tfor (let i = 0; i < paths.length; i++) {\n\t\tfor (let j = i + 1; j < paths.length; j++) {\n\t\t\tconst a = paths[i] as string\n\t\t\tconst b = paths[j] as string\n\t\t\tif (isPrefixOrEqual(a, b) || isPrefixOrEqual(b, a)) {\n\t\t\t\tthrow new Error(`captureRewrites: mount paths collide: '${a}' and '${b}'`)\n\t\t\t}\n\t\t}\n\t}\n}\n\n/**\n * Next.js `rewrites()` entries for every adapter's capture proxy, one mount at a time.\n * Each route's `source`/`upstream` already share `path-to-regexp` param names (e.g.\n * `:p*`), so no translation happens beyond prefixing `source` with the mount's\n * (normalized) path; `upstream` is used verbatim as `destination`. Next always strips a\n * trailing slash from the matched path before substituting it into `destination` (unlike\n * the runtime proxy, which can be told to keep one via `trailingSlashes`); harmless for\n * every vendor this helper supports today.\n *\n * An adapter without `capture`, or with no proxy routes (the native adapter, and GA4 whose\n * tag is served from Google's own CDN), contributes nothing rather than throwing, so one\n * broken slot never blanks the rest.\n * Output preserves mount order, then each mount's declared route order.\n *\n * Throws for a mount path that normalizes to the site root (an unprefixed catch-all\n * route would swallow the whole app) or for two mounts whose normalized paths are equal\n * or nested (Next matches rewrites first-match-wins, so an earlier catch-all would\n * silently shadow a later mount's routes). Distinct sibling paths that merely share a\n * string prefix, e.g. `/ph` and `/phx`, are unaffected.\n *\n * When any mounted adapter's descriptor sets `capture.proxy.trailingSlashes`, add\n * `skipTrailingSlashRedirect: true` to `next.config` (see `posthogProxyRewrites`'s\n * JSDoc for why PostHog needs it).\n */\nexport const captureRewrites = (mounts: CaptureRewriteMount[]): CaptureRewrite[] => {\n\tconst normalized = mounts.map((mount) => normalizeMountPath(mount.path))\n\tfor (const path of normalized) {\n\t\tassertNotRoot(path)\n\t}\n\tassertNoCollisions(normalized)\n\n\tconst rewrites: CaptureRewrite[] = []\n\tfor (let i = 0; i < mounts.length; i++) {\n\t\tconst mount = mounts[i] as CaptureRewriteMount\n\t\tconst routes = mount.adapter.capture?.proxy.routes\n\t\tif (!routes?.length) {\n\t\t\tcontinue\n\t\t}\n\t\tconst base = normalized[i] as string\n\t\tfor (const route of routes) {\n\t\t\trewrites.push({ source: `${base}${route.source}`, destination: route.upstream })\n\t\t}\n\t}\n\treturn rewrites\n}\n"],"mappings":";;AAgBA,MAAM,iBAAiB,SAAuB;CAC7C,IAAI,SAAS,KACZ,MAAM,IAAI,MAAM,uDAAuD;AAEzE;AAKA,MAAM,mBAAmB,GAAW,MAAuB,MAAM,KAAK,EAAE,WAAW,GAAG,EAAE,EAAE;AAE1F,MAAM,sBAAsB,UAA0B;CACrD,KAAK,IAAI,IAAI,GAAG,IAAI,MAAM,QAAQ,KACjC,KAAK,IAAI,IAAI,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;EAC1C,MAAM,IAAI,MAAM;EAChB,MAAM,IAAI,MAAM;EAChB,IAAI,gBAAgB,GAAG,CAAC,KAAK,gBAAgB,GAAG,CAAC,GAChD,MAAM,IAAI,MAAM,0CAA0C,EAAE,SAAS,EAAE,EAAE;CAE3E;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;AA0BA,MAAa,mBAAmB,WAAoD;CACnF,MAAM,aAAa,OAAO,KAAK,UAAU,mBAAmB,MAAM,IAAI,CAAC;CACvE,KAAK,MAAM,QAAQ,YAClB,cAAc,IAAI;CAEnB,mBAAmB,UAAU;CAE7B,MAAM,WAA6B,CAAC;CACpC,KAAK,IAAI,IAAI,GAAG,IAAI,OAAO,QAAQ,KAAK;EAEvC,MAAM,SADQ,OAAO,GACA,QAAQ,SAAS,MAAM;EAC5C,IAAI,CAAC,QAAQ,QACZ;EAED,MAAM,OAAO,WAAW;EACxB,KAAK,MAAM,SAAS,QACnB,SAAS,KAAK;GAAE,QAAQ,GAAG,OAAO,MAAM;GAAU,aAAa,MAAM;EAAS,CAAC;CAEjF;CACA,OAAO;AACR"}
|
|
@@ -4,6 +4,7 @@ import { TIMEFRAME_PRESETS } from "../timeframe/presets.js";
|
|
|
4
4
|
import { METRIC_KEYS } from "../translations/metricKeys.js";
|
|
5
5
|
import { readForField } from "../fields/readForDocument.js";
|
|
6
6
|
import { parseDayOrInstant } from "../query/dates.js";
|
|
7
|
+
import { NO_STORE } from "./responses.js";
|
|
7
8
|
//#region src/plugin/documentEndpoint.ts
|
|
8
9
|
const KNOWN_METRICS = new Set(Object.keys(METRIC_KEYS));
|
|
9
10
|
const MAX_METRICS = 8;
|
|
@@ -45,19 +46,34 @@ const parseRange = (from, to, timezone) => {
|
|
|
45
46
|
* `from`/`to` pair.
|
|
46
47
|
*/
|
|
47
48
|
const makeDocumentHandler = () => async (req) => {
|
|
48
|
-
if (!req.user) return Response.json({ error: "unauthorized" }, {
|
|
49
|
+
if (!req.user) return Response.json({ error: "unauthorized" }, {
|
|
50
|
+
status: 401,
|
|
51
|
+
headers: NO_STORE
|
|
52
|
+
});
|
|
49
53
|
const runtime = getRuntime(req.payload);
|
|
50
|
-
if (!runtime) return Response.json({ error: "unavailable" }, {
|
|
51
|
-
|
|
54
|
+
if (!runtime) return Response.json({ error: "unavailable" }, {
|
|
55
|
+
status: 503,
|
|
56
|
+
headers: NO_STORE
|
|
57
|
+
});
|
|
58
|
+
if (!await readAccessFor(runtime, req)) return Response.json({ error: "forbidden" }, {
|
|
59
|
+
status: 403,
|
|
60
|
+
headers: NO_STORE
|
|
61
|
+
});
|
|
52
62
|
const params = new URL(req.url ?? "", "http://localhost").searchParams;
|
|
53
63
|
const collection = params.get("collection") ?? "";
|
|
54
64
|
const id = params.get("id") ?? "";
|
|
55
|
-
if (!runtime.bindings[collection] || !id) return Response.json({ error: "not found" }, {
|
|
65
|
+
if (!runtime.bindings[collection] || !id) return Response.json({ error: "not found" }, {
|
|
66
|
+
status: 404,
|
|
67
|
+
headers: NO_STORE
|
|
68
|
+
});
|
|
56
69
|
const rawTimeframe = params.get("timeframe") ?? "last30days";
|
|
57
70
|
const timezone = rawTimeframe === "custom" ? await requestTimezone(req) : void 0;
|
|
58
71
|
const range = timezone !== void 0 ? parseRange(params.get("from"), params.get("to"), timezone) : void 0;
|
|
59
72
|
const timeframe = TIMEFRAME_PRESETS.includes(rawTimeframe) ? rawTimeframe : "last30days";
|
|
60
|
-
if (rawTimeframe === "custom" && !range) return Response.json({ error: "invalid range" }, {
|
|
73
|
+
if (rawTimeframe === "custom" && !range) return Response.json({ error: "invalid range" }, {
|
|
74
|
+
status: 400,
|
|
75
|
+
headers: NO_STORE
|
|
76
|
+
});
|
|
61
77
|
const metrics = parseMetrics(params.get("metrics")) ?? [
|
|
62
78
|
"pageviews",
|
|
63
79
|
"visitors",
|
|
@@ -75,7 +91,10 @@ const makeDocumentHandler = () => async (req) => {
|
|
|
75
91
|
req
|
|
76
92
|
});
|
|
77
93
|
} catch {
|
|
78
|
-
return Response.json({ error: "not found" }, {
|
|
94
|
+
return Response.json({ error: "not found" }, {
|
|
95
|
+
status: 404,
|
|
96
|
+
headers: NO_STORE
|
|
97
|
+
});
|
|
79
98
|
}
|
|
80
99
|
const result = await readForField({
|
|
81
100
|
req,
|
|
@@ -90,7 +109,7 @@ const makeDocumentHandler = () => async (req) => {
|
|
|
90
109
|
compare: params.get("compare") === "1",
|
|
91
110
|
series: params.get("series") === "1"
|
|
92
111
|
});
|
|
93
|
-
return Response.json(result);
|
|
112
|
+
return Response.json(result, { headers: NO_STORE });
|
|
94
113
|
};
|
|
95
114
|
//#endregion
|
|
96
115
|
export { makeDocumentHandler };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"documentEndpoint.js","names":[],"sources":["../../src/plugin/documentEndpoint.ts"],"sourcesContent":["import type { CollectionSlug, PayloadHandler } from 'payload'\nimport type { BindingDoc } from '../binding/types'\nimport type { DateRange, MetricKey } from '../core/contract'\nimport { readForField } from '../fields/readForDocument'\nimport { parseDayOrInstant } from '../query/dates'\nimport { TIMEFRAME_PRESETS, type TimeframePreset } from '../timeframe/presets'\nimport { METRIC_KEYS } from '../translations/metricKeys'\nimport { DOCUMENT_PATH } from './paths'\nimport { getRuntime, readAccessFor, requestTimezone } from './runtime'\n\nexport { DOCUMENT_PATH }\n\nconst KNOWN_METRICS = new Set(Object.keys(METRIC_KEYS) as MetricKey[])\nconst MAX_METRICS = 8\n\nconst parseMetrics = (raw: string | null): MetricKey[] | null => {\n\tif (!raw) {\n\t\treturn null\n\t}\n\tconst metrics = raw\n\t\t.split(',')\n\t\t.map((m) => m.trim())\n\t\t.filter((m): m is MetricKey => KNOWN_METRICS.has(m as MetricKey))\n\treturn metrics.length > 0 ? metrics.slice(0, MAX_METRICS) : null\n}\n\n/**\n * A custom window from the query string, read exactly like the query endpoint's: a\n * `YYYY-MM-DD` day is the whole calendar day in the reporting timezone (`to` inclusive of\n * its final instant), a datetime must carry `Z` or a `±HH:MM` offset, and anything else,\n * including an offset-less datetime, is rejected. `from` equal to `to` is one whole day for\n * day strings and a zero-width window for two instants, so only the latter is rejected.\n */\nconst parseRange = (from: string | null, to: string | null, timezone: string): DateRange | null => {\n\tif (!from || !to) {\n\t\treturn null\n\t}\n\tconst start = parseDayOrInstant(from, { timezone, edge: 'start' })\n\tconst end = parseDayOrInstant(to, { timezone, edge: 'end' })\n\tif (!start || !end || end.getTime() <= start.getTime()) {\n\t\treturn null\n\t}\n\treturn { start, end }\n}\n\n/**\n * Authenticated GET handler behind the interactive document analytics panel, gated\n * by `access.read` like every other read endpoint. The caller must also be able to\n * read the target document (enforced through `findByID`\n * without `overrideAccess`), so analytics never leak for content the user cannot\n * see; an unreadable or missing document is a uniform 404. Timeframe, metrics, and\n * data source are whitelist-validated; `timeframe=custom` requires a parseable\n * `from`/`to` pair.\n */\nexport const makeDocumentHandler = (): PayloadHandler => async (req) => {\n\tif (!req.user) {\n\t\treturn Response.json({ error: 'unauthorized' }, { status: 401 })\n\t}\n\tconst runtime = getRuntime(req.payload)\n\tif (!runtime) {\n\t\treturn Response.json({ error: 'unavailable' }, { status: 503 })\n\t}\n\tif (!(await readAccessFor(runtime, req))) {\n\t\treturn Response.json({ error: 'forbidden' }, { status: 403 })\n\t}\n\tconst params = new URL(req.url ?? '', 'http://localhost').searchParams\n\tconst collection = params.get('collection') ?? ''\n\tconst id = params.get('id') ?? ''\n\tif (!runtime.bindings[collection] || !id) {\n\t\treturn Response.json({ error: 'not found' }, { status: 404 })\n\t}\n\tconst rawTimeframe = params.get('timeframe') ?? 'last30days'\n\t// Custom bounds name calendar days, so the reporting timezone has to be resolved before\n\t// they can be read; a preset resolves its window inside `readForField` as before.\n\tconst timezone = rawTimeframe === 'custom' ? await requestTimezone(req) : undefined\n\tconst range =\n\t\ttimezone !== undefined ? parseRange(params.get('from'), params.get('to'), timezone) : undefined\n\tconst timeframe: TimeframePreset = TIMEFRAME_PRESETS.includes(rawTimeframe as TimeframePreset)\n\t\t? (rawTimeframe as TimeframePreset)\n\t\t: 'last30days'\n\tif (rawTimeframe === 'custom' && !range) {\n\t\treturn Response.json({ error: 'invalid range' }, { status: 400 })\n\t}\n\tconst metrics = parseMetrics(params.get('metrics')) ?? [\n\t\t'pageviews',\n\t\t'visitors',\n\t\t'sessions',\n\t\t'avgDuration',\n\t]\n\tlet data: BindingDoc\n\ttry {\n\t\tdata = (await req.payload.findByID({\n\t\t\tcollection: collection as CollectionSlug,\n\t\t\tid,\n\t\t\tdepth: 0,\n\t\t\toverrideAccess: false,\n\t\t\tuser: req.user,\n\t\t\treq,\n\t\t})) as BindingDoc\n\t} catch {\n\t\treturn Response.json({ error: 'not found' }, { status: 404 })\n\t}\n\tconst result = await readForField({\n\t\treq,\n\t\tcollectionSlug: collection,\n\t\tdata,\n\t\tmetrics,\n\t\ttimeframe,\n\t\trange: range ?? undefined,\n\t\t...(timezone !== undefined ? { timezone } : {}),\n\t\tadapterId: params.get('dataSource') ?? undefined,\n\t\tnow: new Date(),\n\t\tcompare: params.get('compare') === '1',\n\t\tseries: params.get('series') === '1',\n\t})\n\treturn Response.json(result)\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"documentEndpoint.js","names":[],"sources":["../../src/plugin/documentEndpoint.ts"],"sourcesContent":["import type { CollectionSlug, PayloadHandler } from 'payload'\nimport type { BindingDoc } from '../binding/types'\nimport type { DateRange, MetricKey } from '../core/contract'\nimport { readForField } from '../fields/readForDocument'\nimport { parseDayOrInstant } from '../query/dates'\nimport { TIMEFRAME_PRESETS, type TimeframePreset } from '../timeframe/presets'\nimport { METRIC_KEYS } from '../translations/metricKeys'\nimport { DOCUMENT_PATH } from './paths'\nimport { NO_STORE } from './responses'\nimport { getRuntime, readAccessFor, requestTimezone } from './runtime'\n\nexport { DOCUMENT_PATH }\n\nconst KNOWN_METRICS = new Set(Object.keys(METRIC_KEYS) as MetricKey[])\nconst MAX_METRICS = 8\n\nconst parseMetrics = (raw: string | null): MetricKey[] | null => {\n\tif (!raw) {\n\t\treturn null\n\t}\n\tconst metrics = raw\n\t\t.split(',')\n\t\t.map((m) => m.trim())\n\t\t.filter((m): m is MetricKey => KNOWN_METRICS.has(m as MetricKey))\n\treturn metrics.length > 0 ? metrics.slice(0, MAX_METRICS) : null\n}\n\n/**\n * A custom window from the query string, read exactly like the query endpoint's: a\n * `YYYY-MM-DD` day is the whole calendar day in the reporting timezone (`to` inclusive of\n * its final instant), a datetime must carry `Z` or a `±HH:MM` offset, and anything else,\n * including an offset-less datetime, is rejected. `from` equal to `to` is one whole day for\n * day strings and a zero-width window for two instants, so only the latter is rejected.\n */\nconst parseRange = (from: string | null, to: string | null, timezone: string): DateRange | null => {\n\tif (!from || !to) {\n\t\treturn null\n\t}\n\tconst start = parseDayOrInstant(from, { timezone, edge: 'start' })\n\tconst end = parseDayOrInstant(to, { timezone, edge: 'end' })\n\tif (!start || !end || end.getTime() <= start.getTime()) {\n\t\treturn null\n\t}\n\treturn { start, end }\n}\n\n/**\n * Authenticated GET handler behind the interactive document analytics panel, gated\n * by `access.read` like every other read endpoint. The caller must also be able to\n * read the target document (enforced through `findByID`\n * without `overrideAccess`), so analytics never leak for content the user cannot\n * see; an unreadable or missing document is a uniform 404. Timeframe, metrics, and\n * data source are whitelist-validated; `timeframe=custom` requires a parseable\n * `from`/`to` pair.\n */\nexport const makeDocumentHandler = (): PayloadHandler => async (req) => {\n\tif (!req.user) {\n\t\treturn Response.json({ error: 'unauthorized' }, { status: 401, headers: NO_STORE })\n\t}\n\tconst runtime = getRuntime(req.payload)\n\tif (!runtime) {\n\t\treturn Response.json({ error: 'unavailable' }, { status: 503, headers: NO_STORE })\n\t}\n\tif (!(await readAccessFor(runtime, req))) {\n\t\treturn Response.json({ error: 'forbidden' }, { status: 403, headers: NO_STORE })\n\t}\n\tconst params = new URL(req.url ?? '', 'http://localhost').searchParams\n\tconst collection = params.get('collection') ?? ''\n\tconst id = params.get('id') ?? ''\n\tif (!runtime.bindings[collection] || !id) {\n\t\treturn Response.json({ error: 'not found' }, { status: 404, headers: NO_STORE })\n\t}\n\tconst rawTimeframe = params.get('timeframe') ?? 'last30days'\n\t// Custom bounds name calendar days, so the reporting timezone has to be resolved before\n\t// they can be read; a preset resolves its window inside `readForField` as before.\n\tconst timezone = rawTimeframe === 'custom' ? await requestTimezone(req) : undefined\n\tconst range =\n\t\ttimezone !== undefined ? parseRange(params.get('from'), params.get('to'), timezone) : undefined\n\tconst timeframe: TimeframePreset = TIMEFRAME_PRESETS.includes(rawTimeframe as TimeframePreset)\n\t\t? (rawTimeframe as TimeframePreset)\n\t\t: 'last30days'\n\tif (rawTimeframe === 'custom' && !range) {\n\t\treturn Response.json({ error: 'invalid range' }, { status: 400, headers: NO_STORE })\n\t}\n\tconst metrics = parseMetrics(params.get('metrics')) ?? [\n\t\t'pageviews',\n\t\t'visitors',\n\t\t'sessions',\n\t\t'avgDuration',\n\t]\n\tlet data: BindingDoc\n\ttry {\n\t\tdata = (await req.payload.findByID({\n\t\t\tcollection: collection as CollectionSlug,\n\t\t\tid,\n\t\t\tdepth: 0,\n\t\t\toverrideAccess: false,\n\t\t\tuser: req.user,\n\t\t\treq,\n\t\t})) as BindingDoc\n\t} catch {\n\t\treturn Response.json({ error: 'not found' }, { status: 404, headers: NO_STORE })\n\t}\n\tconst result = await readForField({\n\t\treq,\n\t\tcollectionSlug: collection,\n\t\tdata,\n\t\tmetrics,\n\t\ttimeframe,\n\t\trange: range ?? undefined,\n\t\t...(timezone !== undefined ? { timezone } : {}),\n\t\tadapterId: params.get('dataSource') ?? undefined,\n\t\tnow: new Date(),\n\t\tcompare: params.get('compare') === '1',\n\t\tseries: params.get('series') === '1',\n\t})\n\treturn Response.json(result, { headers: NO_STORE })\n}\n"],"mappings":";;;;;;;;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