@10x-media/analytics 0.1.0-beta.5 → 1.0.0-beta.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +40 -0
- package/LICENSE +21 -21
- package/README.md +5 -1
- package/dist/adapters/ga4/ga4.js +13 -0
- package/dist/adapters/ga4/ga4.js.map +1 -1
- package/dist/adapters/http/fetchJson.js +1 -1
- package/dist/adapters/plausible/plausible.d.ts +10 -0
- package/dist/adapters/plausible/plausible.js +51 -0
- package/dist/adapters/plausible/plausible.js.map +1 -1
- package/dist/adapters/posthog/posthog.d.ts +10 -1
- package/dist/adapters/posthog/posthog.js +84 -3
- package/dist/adapters/posthog/posthog.js.map +1 -1
- package/dist/adapters/series.js +11 -1
- package/dist/adapters/series.js.map +1 -1
- package/dist/adapters/umami/umami.d.ts +5 -1
- package/dist/adapters/umami/umami.js +28 -0
- package/dist/adapters/umami/umami.js.map +1 -1
- package/dist/capture/getTrackerConfig.d.ts +20 -0
- package/dist/capture/getTrackerConfig.js +31 -0
- package/dist/capture/getTrackerConfig.js.map +1 -0
- package/dist/capture/mountPath.js +14 -0
- package/dist/capture/mountPath.js.map +1 -0
- package/dist/capture/proxyEndpoint.js +123 -0
- package/dist/capture/proxyEndpoint.js.map +1 -0
- package/dist/capture/proxyRoutes.js +108 -0
- package/dist/capture/proxyRoutes.js.map +1 -0
- package/dist/capture/requestBody.js +68 -0
- package/dist/capture/requestBody.js.map +1 -0
- package/dist/capture/slots.d.ts +8 -0
- package/dist/capture/slots.js +43 -0
- package/dist/capture/slots.js.map +1 -0
- package/dist/capture/trackerConfig.d.ts +32 -0
- package/dist/capture/trackerConfig.js +70 -0
- package/dist/capture/trackerConfig.js.map +1 -0
- package/dist/capture/trackerEndpoint.js +35 -0
- package/dist/capture/trackerEndpoint.js.map +1 -0
- package/dist/core/capabilities.d.ts +5 -1
- package/dist/core/capabilities.js +4 -0
- package/dist/core/capabilities.js.map +1 -1
- package/dist/core/capture.d.ts +49 -0
- package/dist/core/contract.d.ts +33 -4
- package/dist/core/contract.js.map +1 -1
- package/dist/core/options.d.ts +97 -2
- package/dist/core/options.js +63 -2
- package/dist/core/options.js.map +1 -1
- package/dist/core/registry.d.ts +1 -0
- package/dist/core/scopeList.js +32 -0
- package/dist/core/scopeList.js.map +1 -0
- package/dist/core/scopedRead.js +13 -6
- package/dist/core/scopedRead.js.map +1 -1
- package/dist/exports/next.d.ts +2 -1
- package/dist/exports/next.js +2 -1
- package/dist/exports/react.d.ts +4 -0
- package/dist/exports/react.js +5 -0
- package/dist/exports/rsc.d.ts +5 -2
- package/dist/exports/rsc.js +3 -1
- package/dist/exports/tracker.d.ts +18 -0
- package/dist/exports/tracker.js +12 -0
- package/dist/exports/types.d.ts +6 -3
- package/dist/fields/AnalyticsPanelClient.js +1 -1
- package/dist/fields/config/narrow.js +2 -0
- package/dist/fields/config/narrow.js.map +1 -1
- package/dist/fields/readForDocument.js +1 -1
- package/dist/goals/match.js +64 -0
- package/dist/goals/match.js.map +1 -0
- package/dist/goals/types.d.ts +33 -0
- package/dist/goals/types.js +7 -0
- package/dist/goals/types.js.map +1 -0
- package/dist/index.d.ts +4 -3
- package/dist/index.js +39 -10
- package/dist/index.js.map +1 -1
- package/dist/native/collections/events.js +21 -1
- package/dist/native/collections/events.js.map +1 -1
- package/dist/native/collections/rollups.js +39 -3
- package/dist/native/collections/rollups.js.map +1 -1
- package/dist/native/geo/maxmindResolver.js +2 -4
- package/dist/native/geo/maxmindResolver.js.map +1 -1
- package/dist/native/ingest/clientIp.js +15 -0
- package/dist/native/ingest/clientIp.js.map +1 -0
- package/dist/native/ingest/endpoint.js +40 -4
- package/dist/native/ingest/endpoint.js.map +1 -1
- package/dist/native/ingest/flushBatch.js +4 -13
- package/dist/native/ingest/flushBatch.js.map +1 -1
- package/dist/native/ingest/normalizeEvent.js +76 -9
- package/dist/native/ingest/normalizeEvent.js.map +1 -1
- package/dist/native/nativeAdapter.js +100 -5
- package/dist/native/nativeAdapter.js.map +1 -1
- package/dist/native/query/eventAgg.js +161 -0
- package/dist/native/query/eventAgg.js.map +1 -0
- package/dist/native/rollupAcc.js +12 -1
- package/dist/native/rollupAcc.js.map +1 -1
- package/dist/native/rollups/applyDistinctDeltas.js +1 -1
- package/dist/native/rollups/applyDistinctDeltas.js.map +1 -1
- package/dist/native/rollups/bumpRollup.js +7 -2
- package/dist/native/rollups/bumpRollup.js.map +1 -1
- package/dist/native/rollups/deltas.js +70 -11
- package/dist/native/rollups/deltas.js.map +1 -1
- package/dist/next/captureRewrites.d.ts +41 -0
- package/dist/next/captureRewrites.js +56 -0
- package/dist/next/captureRewrites.js.map +1 -0
- package/dist/next/posthogProxyRewrites.d.ts +5 -0
- package/dist/next/posthogProxyRewrites.js +16 -21
- package/dist/next/posthogProxyRewrites.js.map +1 -1
- package/dist/plugin/documentEndpoint.js +1 -1
- package/dist/plugin/paths.js +11 -1
- package/dist/plugin/paths.js.map +1 -1
- package/dist/plugin/runtime.d.ts +1 -0
- package/dist/plugin/runtime.js.map +1 -1
- package/dist/plugin/sourcesEndpoint.js +16 -4
- package/dist/plugin/sourcesEndpoint.js.map +1 -1
- package/dist/plugin/warmTask.js +0 -0
- package/dist/plugin/warmTask.js.map +1 -1
- package/dist/providers/collection.js +56 -16
- package/dist/providers/collection.js.map +1 -1
- package/dist/providers/factory.js +8 -2
- package/dist/providers/factory.js.map +1 -1
- package/dist/react/AnalyticsProvider.d.ts +43 -0
- package/dist/react/AnalyticsProvider.js +66 -0
- package/dist/react/AnalyticsProvider.js.map +1 -0
- package/dist/react/TrackerBoot.d.ts +22 -0
- package/dist/react/TrackerBoot.js +30 -0
- package/dist/react/TrackerBoot.js.map +1 -0
- package/dist/react/useAnalytics.d.ts +17 -0
- package/dist/react/useAnalytics.js +36 -0
- package/dist/react/useAnalytics.js.map +1 -0
- package/dist/rsc/AnalyticsScripts.d.ts +30 -0
- package/dist/rsc/AnalyticsScripts.js +41 -0
- package/dist/rsc/AnalyticsScripts.js.map +1 -0
- package/dist/surfacing/cacheStore.d.ts +1 -0
- package/dist/surfacing/cacheStore.js +18 -2
- package/dist/surfacing/cacheStore.js.map +1 -1
- package/dist/surfacing/engine.d.ts +1 -0
- package/dist/surfacing/engine.js +44 -4
- package/dist/surfacing/engine.js.map +1 -1
- package/dist/surfacing/queue.js +22 -6
- package/dist/surfacing/queue.js.map +1 -1
- package/dist/surfacing/rateLimiter.js +130 -0
- package/dist/surfacing/rateLimiter.js.map +1 -0
- package/dist/surfacing/retryPolicy.js +15 -0
- package/dist/surfacing/retryPolicy.js.map +1 -0
- package/dist/sync/collection.js +23 -7
- package/dist/sync/collection.js.map +1 -1
- package/dist/sync/syncTask.js +68 -33
- package/dist/sync/syncTask.js.map +1 -1
- package/dist/testing/memoryAdapter.d.ts +2 -0
- package/dist/testing/memoryAdapter.js +17 -0
- package/dist/testing/memoryAdapter.js.map +1 -1
- package/dist/tracker/autoCapture.d.ts +48 -0
- package/dist/tracker/autoCapture.js +129 -0
- package/dist/tracker/autoCapture.js.map +1 -0
- package/dist/tracker/consent.d.ts +24 -0
- package/dist/tracker/consent.js +48 -0
- package/dist/tracker/consent.js.map +1 -0
- package/dist/tracker/createTracker.d.ts +18 -0
- package/dist/tracker/createTracker.js +163 -0
- package/dist/tracker/createTracker.js.map +1 -0
- package/dist/tracker/initTracker.d.ts +22 -0
- package/dist/tracker/initTracker.js +33 -0
- package/dist/tracker/initTracker.js.map +1 -0
- package/dist/tracker/loadScript.d.ts +14 -0
- package/dist/tracker/loadScript.js +41 -0
- package/dist/tracker/loadScript.js.map +1 -0
- package/dist/tracker/pageTracking.d.ts +17 -0
- package/dist/tracker/pageTracking.js +38 -0
- package/dist/tracker/pageTracking.js.map +1 -0
- package/dist/tracker/registry.d.ts +27 -0
- package/dist/tracker/registry.js +73 -0
- package/dist/tracker/registry.js.map +1 -0
- package/dist/tracker/sinks/native.d.ts +22 -0
- package/dist/tracker/sinks/native.js +56 -0
- package/dist/tracker/sinks/native.js.map +1 -0
- package/dist/tracker/sinks/plausible.d.ts +14 -0
- package/dist/tracker/sinks/plausible.js +25 -0
- package/dist/tracker/sinks/plausible.js.map +1 -0
- package/dist/tracker/sinks/posthog.d.ts +14 -0
- package/dist/tracker/sinks/posthog.js +18 -0
- package/dist/tracker/sinks/posthog.js.map +1 -0
- package/dist/tracker/sinks/umami.d.ts +13 -0
- package/dist/tracker/sinks/umami.js +17 -0
- package/dist/tracker/sinks/umami.js.map +1 -0
- package/dist/tracker/sinks/vendor.d.ts +14 -0
- package/dist/tracker/sinks/vendor.js +66 -0
- package/dist/tracker/sinks/vendor.js.map +1 -0
- package/dist/tracker/transport.js +28 -0
- package/dist/tracker/transport.js.map +1 -0
- package/dist/tracker/types.d.ts +66 -0
- package/dist/translations/ar.js +83 -0
- package/dist/translations/ar.js.map +1 -0
- package/dist/translations/de.js +6 -0
- package/dist/translations/de.js.map +1 -1
- package/dist/translations/en.js +6 -0
- package/dist/translations/en.js.map +1 -1
- package/dist/translations/es.js +83 -0
- package/dist/translations/es.js.map +1 -0
- package/dist/translations/fr.js +83 -0
- package/dist/translations/fr.js.map +1 -0
- package/dist/translations/id.js +83 -0
- package/dist/translations/id.js.map +1 -0
- package/dist/translations/index.d.ts +10 -1
- package/dist/translations/index.js +19 -1
- package/dist/translations/index.js.map +1 -1
- package/dist/translations/keys.d.ts +6 -0
- package/dist/translations/keys.js +6 -0
- package/dist/translations/keys.js.map +1 -1
- package/dist/translations/ko.js +83 -0
- package/dist/translations/ko.js.map +1 -0
- package/dist/translations/pt.js +83 -0
- package/dist/translations/pt.js.map +1 -0
- package/dist/translations/ru.js +83 -0
- package/dist/translations/ru.js.map +1 -0
- package/dist/translations/uk.js +83 -0
- package/dist/translations/uk.js.map +1 -0
- package/dist/translations/zh.js +83 -0
- package/dist/translations/zh.js.map +1 -0
- package/dist/widgets/breakdownTypes.js +5 -0
- package/dist/widgets/breakdownTypes.js.map +1 -1
- package/dist/widgets/readForWidget.d.ts +2 -1
- package/dist/widgets/readForWidget.js +32 -14
- package/dist/widgets/readForWidget.js.map +1 -1
- package/dist/widgets/readForWidgetBreakdown.d.ts +2 -1
- package/dist/widgets/readForWidgetBreakdown.js +29 -15
- package/dist/widgets/readForWidgetBreakdown.js.map +1 -1
- package/dist/widgets/readForWidgetRealtime.js +1 -1
- package/dist/widgets/readForWidgetSeries.d.ts +2 -1
- package/dist/widgets/readForWidgetSeries.js +34 -16
- package/dist/widgets/readForWidgetSeries.js.map +1 -1
- package/dist/widgets/registerWidgets.js +10 -4
- package/dist/widgets/registerWidgets.js.map +1 -1
- package/dist/widgets/types.js +4 -1
- package/dist/widgets/types.js.map +1 -1
- package/package.json +20 -5
package/dist/index.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import { DOCUMENT_PATH, REALTIME_PATH, SOURCES_PATH } from "./plugin/paths.js";
|
|
2
|
+
import { isValidTimeZone } from "./timeframe/tz.js";
|
|
3
|
+
import { setRuntime } from "./plugin/runtime.js";
|
|
4
|
+
import { proxyEndpoints } from "./capture/proxyEndpoint.js";
|
|
1
5
|
import { buildProvidersCollection } from "./providers/collection.js";
|
|
2
6
|
import { resolveOptions } from "./core/options.js";
|
|
7
|
+
import { trackerEndpoint } from "./capture/trackerEndpoint.js";
|
|
3
8
|
import { createRegistry, staticRegistryResolver } from "./core/registry.js";
|
|
4
|
-
import { isValidTimeZone } from "./timeframe/tz.js";
|
|
5
|
-
import { setRuntime } from "./plugin/runtime.js";
|
|
6
9
|
import { PLATFORM_SCOPE } from "./core/contract.js";
|
|
7
|
-
import { DOCUMENT_PATH, REALTIME_PATH, SOURCES_PATH } from "./plugin/paths.js";
|
|
8
10
|
import { makeDocumentHandler } from "./plugin/documentEndpoint.js";
|
|
9
11
|
import { isModuleNotFoundError } from "./plugin/peerImportError.js";
|
|
10
12
|
import { kvCacheStore } from "./surfacing/cacheStore.js";
|
|
@@ -26,6 +28,7 @@ const analytics = definePlugin({
|
|
|
26
28
|
plugin: async ({ config, plugins: _plugins, ...options }) => {
|
|
27
29
|
if (options.disabled === true) return config;
|
|
28
30
|
const resolved = resolveOptions(options);
|
|
31
|
+
const providersEnabled = resolved.providers.collection.enabled || Boolean(resolved.providers.resolve);
|
|
29
32
|
const defaultLayout = config.admin?.dashboard?.defaultLayout;
|
|
30
33
|
registerTranslations(config, options.translations);
|
|
31
34
|
const registry = createRegistry(resolved.adapters, resolved.defaultAdapter);
|
|
@@ -89,12 +92,14 @@ const analytics = definePlugin({
|
|
|
89
92
|
return "UTC";
|
|
90
93
|
}
|
|
91
94
|
};
|
|
95
|
+
const resolveGoals = async (_req, _scope) => resolved.goals;
|
|
92
96
|
for (const adapter of resolved.adapters) adapter.register?.(config, {
|
|
93
97
|
scoped: resolved.scoped,
|
|
94
98
|
resolveScope,
|
|
95
|
-
resolveTimezone
|
|
99
|
+
resolveTimezone,
|
|
100
|
+
resolveGoals
|
|
96
101
|
});
|
|
97
|
-
if (resolved.adapters.some((a) => a.capabilities.realtime && typeof a.realtime === "function")) config.endpoints = [...config.endpoints ?? [], {
|
|
102
|
+
if (resolved.adapters.some((a) => a.capabilities.realtime && typeof a.realtime === "function") || providersEnabled) config.endpoints = [...config.endpoints ?? [], {
|
|
98
103
|
method: "get",
|
|
99
104
|
path: REALTIME_PATH,
|
|
100
105
|
handler: makeRealtimeHandler()
|
|
@@ -109,11 +114,17 @@ const analytics = definePlugin({
|
|
|
109
114
|
path: SOURCES_PATH,
|
|
110
115
|
handler: makeSourcesHandler()
|
|
111
116
|
}];
|
|
117
|
+
if (resolved.adapters.some((a) => a.capture) || providersEnabled) config.endpoints = [
|
|
118
|
+
...config.endpoints,
|
|
119
|
+
...proxyEndpoints(),
|
|
120
|
+
trackerEndpoint()
|
|
121
|
+
];
|
|
112
122
|
if (resolved.widgets.enabled) {
|
|
113
|
-
const multiProvider = registry.isMultiProvider() ||
|
|
123
|
+
const multiProvider = registry.isMultiProvider() || providersEnabled;
|
|
114
124
|
registerWidgets(config, {
|
|
115
125
|
adapters: resolved.adapters,
|
|
116
126
|
multiProvider,
|
|
127
|
+
providersEnabled,
|
|
117
128
|
disabled: resolved.widgets.disabled,
|
|
118
129
|
register: resolved.widgets.register,
|
|
119
130
|
localizeText: resolved.widgets.localizeText,
|
|
@@ -122,17 +133,23 @@ const analytics = definePlugin({
|
|
|
122
133
|
}
|
|
123
134
|
if (resolved.cache.warm.enabled) config.jobs = {
|
|
124
135
|
...config.jobs,
|
|
125
|
-
tasks: [...config.jobs?.tasks ?? [], warmTask(resolved.cache.warm.cron, defaultLayout)]
|
|
136
|
+
tasks: [...config.jobs?.tasks ?? [], warmTask(resolved.cache.warm.cron, defaultLayout, resolved.scopes)]
|
|
126
137
|
};
|
|
127
138
|
if (resolved.sync.enabled) {
|
|
128
|
-
config.collections = [...config.collections ?? [], syncCollection(resolved.sync.collectionSlug, resolved.sync.hidden
|
|
139
|
+
config.collections = [...config.collections ?? [], syncCollection(resolved.sync.collectionSlug, resolved.sync.hidden, {
|
|
140
|
+
scoped: resolved.scoped,
|
|
141
|
+
scopeField: "scope",
|
|
142
|
+
resolveScope,
|
|
143
|
+
platformRead: resolved.access.platformRead
|
|
144
|
+
})];
|
|
129
145
|
config.jobs = {
|
|
130
146
|
...config.jobs,
|
|
131
147
|
tasks: [...config.jobs?.tasks ?? [], syncTask({
|
|
132
148
|
cron: resolved.sync.cron,
|
|
133
149
|
lookbackDays: resolved.sync.lookbackDays,
|
|
134
150
|
collectionSlug: resolved.sync.collectionSlug,
|
|
135
|
-
adapterIds: resolved.sync.adapters
|
|
151
|
+
adapterIds: resolved.sync.adapters,
|
|
152
|
+
scopes: resolved.scopes
|
|
136
153
|
})]
|
|
137
154
|
};
|
|
138
155
|
}
|
|
@@ -145,7 +162,11 @@ const analytics = definePlugin({
|
|
|
145
162
|
const engine = createEngine({
|
|
146
163
|
store: kvCacheStore(payload.kv),
|
|
147
164
|
queue: { concurrency: 4 },
|
|
148
|
-
ttl: resolved.cache.ttl
|
|
165
|
+
ttl: resolved.cache.ttl,
|
|
166
|
+
timeoutMs: resolved.cache.timeoutMs,
|
|
167
|
+
onError: (err, adapterId) => {
|
|
168
|
+
payload.logger?.warn(`analytics: read failed for adapter "${adapterId}": ${String(err)}`);
|
|
169
|
+
}
|
|
149
170
|
});
|
|
150
171
|
setRuntime(payload, {
|
|
151
172
|
registry,
|
|
@@ -153,6 +174,14 @@ const analytics = definePlugin({
|
|
|
153
174
|
resolveScope,
|
|
154
175
|
resolveTimezone,
|
|
155
176
|
platformAdapterId: resolved.platformAdapter,
|
|
177
|
+
captureSlots: resolved.capture.slots,
|
|
178
|
+
capturePaths: resolved.capture.paths,
|
|
179
|
+
captureProxy: resolved.capture.proxy,
|
|
180
|
+
consentFor: resolved.capture.consent,
|
|
181
|
+
autoCapture: resolved.capture.autoCapture,
|
|
182
|
+
goals: resolved.goals,
|
|
183
|
+
ingestPath: resolved.adapters.find((a) => a.ingest)?.ingest?.path,
|
|
184
|
+
scoped: resolved.scoped,
|
|
156
185
|
configAdapterIds: new Set(resolved.adapters.map((a) => a.id)),
|
|
157
186
|
platformRead: resolved.access.platformRead,
|
|
158
187
|
bindings: resolved.bindings,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import { type Config, definePlugin, type PayloadRequest } from 'payload'\n\nimport { type AnalyticsPluginOptions, resolveOptions } from './core/options'\nimport { createRegistry, staticRegistryResolver } from './core/registry'\nimport { DOCUMENT_PATH, makeDocumentHandler } from './plugin/documentEndpoint'\nimport { isModuleNotFoundError } from './plugin/peerImportError'\nimport { makeRealtimeHandler, REALTIME_PATH } from './plugin/realtimeEndpoint'\nimport { registerTranslations } from './plugin/registerTranslations'\nimport { setRuntime } from './plugin/runtime'\nimport { makeSourcesHandler, SOURCES_PATH } from './plugin/sourcesEndpoint'\nimport { warmTask } from './plugin/warmTask'\nimport type { BuildSecretField } from './providers/collection'\nimport { buildProvidersCollection } from './providers/collection'\nimport {\n\tcollectionProvidersSource,\n\tcombineRegistries,\n\tcreateScopedRegistryResolver,\n} from './providers/resolver'\nimport { kvCacheStore } from './surfacing/cacheStore'\nimport { createEngine } from './surfacing/engine'\nimport { syncCollection } from './sync/collection'\nimport { syncTask } from './sync/syncTask'\nimport { DEFAULT_TIMEZONE, isValidTimeZone } from './timeframe/tz'\nimport { registerWidgets } from './widgets/registerWidgets'\n\ndeclare module 'payload' {\n\tinterface RegisteredPlugins {\n\t\t'@10x-media/analytics': AnalyticsPluginOptions\n\t}\n}\n\nexport const analytics = definePlugin<AnalyticsPluginOptions>({\n\tslug: '@10x-media/analytics',\n\tplugin: async ({ config, plugins: _plugins, ...options }): Promise<Config> => {\n\t\tif (options.disabled === true) {\n\t\t\treturn config\n\t\t}\n\t\tconst resolved = resolveOptions(options)\n\t\tconst defaultLayout = config.admin?.dashboard?.defaultLayout\n\t\tregisterTranslations(config, options.translations)\n\t\tconst registry = createRegistry(resolved.adapters, resolved.defaultAdapter)\n\t\tconst registryBase = { adapters: resolved.adapters, defaultId: resolved.defaultAdapter }\n\t\tlet resolveRegistry = staticRegistryResolver(registry)\n\t\tlet invalidateProviders = () => {}\n\t\tconst providersResolve = resolved.providers.resolve\n\t\tif (providersResolve) {\n\t\t\tresolveRegistry = async (args) =>\n\t\t\t\tcombineRegistries(\n\t\t\t\t\tregistryBase,\n\t\t\t\t\tawait providersResolve({ payload: args.payload, req: args.req, scope: args.scope })\n\t\t\t\t)\n\t\t} else if (resolved.providers.collection.enabled) {\n\t\t\tconst scoped = createScopedRegistryResolver({\n\t\t\t\tbase: registryBase,\n\t\t\t\tsource: collectionProvidersSource(\n\t\t\t\t\tresolved.providers.collection.slug,\n\t\t\t\t\tresolved.providers.collection.scopeField\n\t\t\t\t),\n\t\t\t})\n\t\t\tresolveRegistry = scoped.resolver\n\t\t\tinvalidateProviders = scoped.invalidate\n\t\t}\n\t\tconst resolveScope = async (req: PayloadRequest) => resolved.scopeResolver({ req })\n\t\tif (resolved.providers.collection.enabled) {\n\t\t\tconst { encryptedField, withEncryptedQueryRewrite } = await import(\n\t\t\t\t'@10x-media/fields/encrypted'\n\t\t\t).catch((err: unknown) => {\n\t\t\t\tif (isModuleNotFoundError(err)) {\n\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t'analytics: providers.collection requires @10x-media/fields (peer). Install it: pnpm add @10x-media/fields'\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tthrow err\n\t\t\t})\n\t\t\tconst encryption = resolved.providers.collection.encryption\n\t\t\tconst buildSecret: BuildSecretField = (source) =>\n\t\t\t\tencryptedField(source, {\n\t\t\t\t\tprotection: 'writeOnly',\n\t\t\t\t\taadScope: '10x-analytics:providers',\n\t\t\t\t\t...(source.type === 'text' ? { hint: { suffix: 4 } } : {}),\n\t\t\t\t\t...(encryption?.keys ? { keys: encryption.keys } : {}),\n\t\t\t\t})\n\t\t\tconst providersCollection = buildProvidersCollection({\n\t\t\t\tslug: resolved.providers.collection.slug,\n\t\t\t\taccess: resolved.providers.collection.access,\n\t\t\t\toverrides: resolved.providers.collection.overrides,\n\t\t\t\tonChange: () => invalidateProviders(),\n\t\t\t\tscoped: resolved.scoped,\n\t\t\t\tscopeField: resolved.providers.collection.scopeField,\n\t\t\t\tresolveScope,\n\t\t\t\tplatformRead: resolved.access.platformRead,\n\t\t\t\tbuildSecret,\n\t\t\t})\n\t\t\t// Always wrap, even though a consumer's own @10x-media/fields plugin (if\n\t\t\t// registered) may wrap this collection again later: double application is\n\t\t\t// inert here (writeOnly forbids queryable, so there are no blind-index\n\t\t\t// markers to rewrite and the response strip is an idempotent delete),\n\t\t\t// while skipping would leak ciphertext whenever fields() runs before this\n\t\t\t// collection exists (e.g. an earlier plugin `order`).\n\t\t\tconfig.collections = [\n\t\t\t\t...(config.collections ?? []),\n\t\t\t\twithEncryptedQueryRewrite(providersCollection),\n\t\t\t]\n\t\t}\n\t\tconst resolveTimezone = async (req: PayloadRequest, scope?: string | null): Promise<string> => {\n\t\t\tconst opt = resolved.reportingTimezone\n\t\t\tif (opt === undefined) {\n\t\t\t\treturn DEFAULT_TIMEZONE\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tconst tz =\n\t\t\t\t\ttypeof opt === 'string'\n\t\t\t\t\t\t? opt\n\t\t\t\t\t\t: await opt({ req, scope: scope !== undefined ? scope : await resolveScope(req) })\n\t\t\t\treturn tz && isValidTimeZone(tz) ? tz : DEFAULT_TIMEZONE\n\t\t\t} catch (err) {\n\t\t\t\treq.payload?.logger?.warn?.(\n\t\t\t\t\t`analytics: reportingTimezone resolution failed, falling back to UTC: ${String(err)}`\n\t\t\t\t)\n\t\t\t\treturn DEFAULT_TIMEZONE\n\t\t\t}\n\t\t}\n\t\tfor (const adapter of resolved.adapters) {\n\t\t\tadapter.register?.(config, { scoped: resolved.scoped, resolveScope, resolveTimezone })\n\t\t}\n\t\tif (\n\t\t\tresolved.adapters.some((a) => a.capabilities.realtime && typeof a.realtime === 'function')\n\t\t) {\n\t\t\tconfig.endpoints = [\n\t\t\t\t...(config.endpoints ?? []),\n\t\t\t\t{ method: 'get', path: REALTIME_PATH, handler: makeRealtimeHandler() },\n\t\t\t]\n\t\t}\n\t\tif (Object.keys(resolved.bindings).length > 0) {\n\t\t\tconfig.endpoints = [\n\t\t\t\t...(config.endpoints ?? []),\n\t\t\t\t{ method: 'get', path: DOCUMENT_PATH, handler: makeDocumentHandler() },\n\t\t\t]\n\t\t}\n\t\tconfig.endpoints = [\n\t\t\t...(config.endpoints ?? []),\n\t\t\t{ method: 'get', path: SOURCES_PATH, handler: makeSourcesHandler() },\n\t\t]\n\t\tif (resolved.widgets.enabled) {\n\t\t\tconst multiProvider =\n\t\t\t\tregistry.isMultiProvider() ||\n\t\t\t\tresolved.providers.collection.enabled ||\n\t\t\t\tBoolean(resolved.providers.resolve)\n\t\t\tregisterWidgets(config, {\n\t\t\t\tadapters: resolved.adapters,\n\t\t\t\tmultiProvider,\n\t\t\t\tdisabled: resolved.widgets.disabled,\n\t\t\t\tregister: resolved.widgets.register,\n\t\t\t\tlocalizeText: resolved.widgets.localizeText,\n\t\t\t\tdefaultId: resolved.defaultAdapter,\n\t\t\t})\n\t\t}\n\t\tif (resolved.cache.warm.enabled) {\n\t\t\tconfig.jobs = {\n\t\t\t\t...config.jobs,\n\t\t\t\ttasks: [...(config.jobs?.tasks ?? []), warmTask(resolved.cache.warm.cron, defaultLayout)],\n\t\t\t}\n\t\t}\n\t\tif (resolved.sync.enabled) {\n\t\t\tconfig.collections = [\n\t\t\t\t...(config.collections ?? []),\n\t\t\t\tsyncCollection(resolved.sync.collectionSlug, resolved.sync.hidden),\n\t\t\t]\n\t\t\tconfig.jobs = {\n\t\t\t\t...config.jobs,\n\t\t\t\ttasks: [\n\t\t\t\t\t...(config.jobs?.tasks ?? []),\n\t\t\t\t\tsyncTask({\n\t\t\t\t\t\tcron: resolved.sync.cron,\n\t\t\t\t\t\tlookbackDays: resolved.sync.lookbackDays,\n\t\t\t\t\t\tcollectionSlug: resolved.sync.collectionSlug,\n\t\t\t\t\t\tadapterIds: resolved.sync.adapters,\n\t\t\t\t\t}),\n\t\t\t\t],\n\t\t\t}\n\t\t}\n\t\tconst prevOnInit = config.onInit\n\t\t// The runtime is installed before the app's own onInit runs so consumer init code\n\t\t// (seeding, cache warming, sync passes) can already read through the plugin.\n\t\tconfig.onInit = async (payload) => {\n\t\t\tif (resolved.providers.collection.enabled) {\n\t\t\t\tconst { validateEncryptedBoot } = await import('@10x-media/fields/encrypted')\n\t\t\t\tawait validateEncryptedBoot(payload, resolved.providers.collection.encryption?.keys)\n\t\t\t}\n\t\t\tconst engine = createEngine({\n\t\t\t\tstore: kvCacheStore(payload.kv),\n\t\t\t\tqueue: { concurrency: 4 },\n\t\t\t\tttl: resolved.cache.ttl,\n\t\t\t})\n\t\t\tsetRuntime(payload, {\n\t\t\t\tregistry,\n\t\t\t\tresolveRegistry,\n\t\t\t\tresolveScope,\n\t\t\t\tresolveTimezone,\n\t\t\t\tplatformAdapterId: resolved.platformAdapter,\n\t\t\t\tconfigAdapterIds: new Set(resolved.adapters.map((a) => a.id)),\n\t\t\t\tplatformRead: resolved.access.platformRead,\n\t\t\t\tbindings: resolved.bindings,\n\t\t\t\tengine,\n\t\t\t\tttl: resolved.cache.ttl,\n\t\t\t\tcomparison: resolved.widgets.comparison,\n\t\t\t})\n\t\t\tawait prevOnInit?.(payload)\n\t\t}\n\t\treturn config\n\t},\n})\n\nexport type {\n\tAnalyticsBinding,\n\tBindingContext,\n\tHostnameResolver,\n\tPathResolver,\n} from './binding/types'\nexport { PLATFORM_SCOPE } from './core/contract'\nexport type {\n\tAnalyticsAccessOptions,\n\tAnalyticsPluginOptions,\n\tAnalyticsPluginOptions as PluginOptions,\n\tPlatformReadAccess,\n\tProvidersCollectionOptions,\n\tProvidersOptions,\n\tProvidersResolve,\n\tScopeResolver,\n\tTimezoneResolver,\n} from './core/options'\nexport type {\n\tAnalyticsFieldsOptions,\n\tAnalyticsMetricLabel,\n\tAnalyticsMetricLabels,\n\tAnalyticsStatOptions,\n\tAnalyticsStatRowOptions,\n\tAnalyticsTabOptions,\n} from './fields/factories'\nexport {\n\tanalyticsFields,\n\tanalyticsStat,\n\tanalyticsStatRow,\n\tanalyticsTab,\n\tanalyticsTabsField,\n} from './fields/factories'\nexport type { TimeframePreset } from './timeframe/presets'\nexport type { CustomWidgetDef } from './widgets/customWidget'\nexport { analyticsDefaultWidgets } from './widgets/defaults'\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AA+BA,MAAa,YAAY,aAAqC;CAC7D,MAAM;CACN,QAAQ,OAAO,EAAE,QAAQ,SAAS,UAAU,GAAG,cAA+B;EAC7E,IAAI,QAAQ,aAAa,MACxB,OAAO;EAER,MAAM,WAAW,eAAe,OAAO;EACvC,MAAM,gBAAgB,OAAO,OAAO,WAAW;EAC/C,qBAAqB,QAAQ,QAAQ,YAAY;EACjD,MAAM,WAAW,eAAe,SAAS,UAAU,SAAS,cAA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names":[],"sources":["../src/index.ts"],"sourcesContent":["import { type Config, definePlugin, type PayloadRequest } from 'payload'\n\nimport { proxyEndpoints } from './capture/proxyEndpoint'\nimport { trackerEndpoint } from './capture/trackerEndpoint'\nimport { type AnalyticsPluginOptions, resolveOptions } from './core/options'\nimport { createRegistry, staticRegistryResolver } from './core/registry'\nimport type { Goal } from './goals/types'\nimport { DOCUMENT_PATH, makeDocumentHandler } from './plugin/documentEndpoint'\nimport { isModuleNotFoundError } from './plugin/peerImportError'\nimport { makeRealtimeHandler, REALTIME_PATH } from './plugin/realtimeEndpoint'\nimport { registerTranslations } from './plugin/registerTranslations'\nimport { setRuntime } from './plugin/runtime'\nimport { makeSourcesHandler, SOURCES_PATH } from './plugin/sourcesEndpoint'\nimport { warmTask } from './plugin/warmTask'\nimport type { BuildSecretField } from './providers/collection'\nimport { buildProvidersCollection } from './providers/collection'\nimport {\n\tcollectionProvidersSource,\n\tcombineRegistries,\n\tcreateScopedRegistryResolver,\n} from './providers/resolver'\nimport { kvCacheStore } from './surfacing/cacheStore'\nimport { createEngine } from './surfacing/engine'\nimport { syncCollection } from './sync/collection'\nimport { syncTask } from './sync/syncTask'\nimport { DEFAULT_TIMEZONE, isValidTimeZone } from './timeframe/tz'\nimport { registerWidgets } from './widgets/registerWidgets'\n\ndeclare module 'payload' {\n\tinterface RegisteredPlugins {\n\t\t'@10x-media/analytics': AnalyticsPluginOptions\n\t}\n}\n\nexport const analytics = definePlugin<AnalyticsPluginOptions>({\n\tslug: '@10x-media/analytics',\n\tplugin: async ({ config, plugins: _plugins, ...options }): Promise<Config> => {\n\t\tif (options.disabled === true) {\n\t\t\treturn config\n\t\t}\n\t\tconst resolved = resolveOptions(options)\n\t\t// A tenant's own runtime adapter (providers.collection or providers.resolve) has\n\t\t// capabilities unknown at config time, so it opens the widget/endpoint gates that\n\t\t// otherwise key off the config-adapter capability union.\n\t\tconst providersEnabled =\n\t\t\tresolved.providers.collection.enabled || Boolean(resolved.providers.resolve)\n\t\tconst defaultLayout = config.admin?.dashboard?.defaultLayout\n\t\tregisterTranslations(config, options.translations)\n\t\tconst registry = createRegistry(resolved.adapters, resolved.defaultAdapter)\n\t\tconst registryBase = { adapters: resolved.adapters, defaultId: resolved.defaultAdapter }\n\t\tlet resolveRegistry = staticRegistryResolver(registry)\n\t\tlet invalidateProviders = () => {}\n\t\tconst providersResolve = resolved.providers.resolve\n\t\tif (providersResolve) {\n\t\t\tresolveRegistry = async (args) =>\n\t\t\t\tcombineRegistries(\n\t\t\t\t\tregistryBase,\n\t\t\t\t\tawait providersResolve({ payload: args.payload, req: args.req, scope: args.scope })\n\t\t\t\t)\n\t\t} else if (resolved.providers.collection.enabled) {\n\t\t\tconst scoped = createScopedRegistryResolver({\n\t\t\t\tbase: registryBase,\n\t\t\t\tsource: collectionProvidersSource(\n\t\t\t\t\tresolved.providers.collection.slug,\n\t\t\t\t\tresolved.providers.collection.scopeField\n\t\t\t\t),\n\t\t\t})\n\t\t\tresolveRegistry = scoped.resolver\n\t\t\tinvalidateProviders = scoped.invalidate\n\t\t}\n\t\tconst resolveScope = async (req: PayloadRequest) => resolved.scopeResolver({ req })\n\t\tif (resolved.providers.collection.enabled) {\n\t\t\tconst { encryptedField, withEncryptedQueryRewrite } = await import(\n\t\t\t\t'@10x-media/fields/encrypted'\n\t\t\t).catch((err: unknown) => {\n\t\t\t\tif (isModuleNotFoundError(err)) {\n\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t'analytics: providers.collection requires @10x-media/fields (peer). Install it: pnpm add @10x-media/fields'\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tthrow err\n\t\t\t})\n\t\t\tconst encryption = resolved.providers.collection.encryption\n\t\t\tconst buildSecret: BuildSecretField = (source) =>\n\t\t\t\tencryptedField(source, {\n\t\t\t\t\tprotection: 'writeOnly',\n\t\t\t\t\taadScope: '10x-analytics:providers',\n\t\t\t\t\t...(source.type === 'text' ? { hint: { suffix: 4 } } : {}),\n\t\t\t\t\t...(encryption?.keys ? { keys: encryption.keys } : {}),\n\t\t\t\t})\n\t\t\tconst providersCollection = buildProvidersCollection({\n\t\t\t\tslug: resolved.providers.collection.slug,\n\t\t\t\taccess: resolved.providers.collection.access,\n\t\t\t\toverrides: resolved.providers.collection.overrides,\n\t\t\t\tonChange: () => invalidateProviders(),\n\t\t\t\tscoped: resolved.scoped,\n\t\t\t\tscopeField: resolved.providers.collection.scopeField,\n\t\t\t\tresolveScope,\n\t\t\t\tplatformRead: resolved.access.platformRead,\n\t\t\t\tbuildSecret,\n\t\t\t})\n\t\t\t// Always wrap, even though a consumer's own @10x-media/fields plugin (if\n\t\t\t// registered) may wrap this collection again later: double application is\n\t\t\t// inert here (writeOnly forbids queryable, so there are no blind-index\n\t\t\t// markers to rewrite and the response strip is an idempotent delete),\n\t\t\t// while skipping would leak ciphertext whenever fields() runs before this\n\t\t\t// collection exists (e.g. an earlier plugin `order`).\n\t\t\tconfig.collections = [\n\t\t\t\t...(config.collections ?? []),\n\t\t\t\twithEncryptedQueryRewrite(providersCollection),\n\t\t\t]\n\t\t}\n\t\tconst resolveTimezone = async (req: PayloadRequest, scope?: string | null): Promise<string> => {\n\t\t\tconst opt = resolved.reportingTimezone\n\t\t\tif (opt === undefined) {\n\t\t\t\treturn DEFAULT_TIMEZONE\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tconst tz =\n\t\t\t\t\ttypeof opt === 'string'\n\t\t\t\t\t\t? opt\n\t\t\t\t\t\t: await opt({ req, scope: scope !== undefined ? scope : await resolveScope(req) })\n\t\t\t\treturn tz && isValidTimeZone(tz) ? tz : DEFAULT_TIMEZONE\n\t\t\t} catch (err) {\n\t\t\t\treq.payload?.logger?.warn?.(\n\t\t\t\t\t`analytics: reportingTimezone resolution failed, falling back to UTC: ${String(err)}`\n\t\t\t\t)\n\t\t\t\treturn DEFAULT_TIMEZONE\n\t\t\t}\n\t\t}\n\t\t// Config goals are install-wide, so scope is accepted and ignored here; a collection\n\t\t// source resolves per scope behind the same signature.\n\t\tconst resolveGoals = async (_req: PayloadRequest, _scope?: string | null): Promise<Goal[]> =>\n\t\t\tresolved.goals\n\t\tfor (const adapter of resolved.adapters) {\n\t\t\tadapter.register?.(config, {\n\t\t\t\tscoped: resolved.scoped,\n\t\t\t\tresolveScope,\n\t\t\t\tresolveTimezone,\n\t\t\t\tresolveGoals,\n\t\t\t})\n\t\t}\n\t\tif (\n\t\t\tresolved.adapters.some((a) => a.capabilities.realtime && typeof a.realtime === 'function') ||\n\t\t\tprovidersEnabled\n\t\t) {\n\t\t\tconfig.endpoints = [\n\t\t\t\t...(config.endpoints ?? []),\n\t\t\t\t{ method: 'get', path: REALTIME_PATH, handler: makeRealtimeHandler() },\n\t\t\t]\n\t\t}\n\t\tif (Object.keys(resolved.bindings).length > 0) {\n\t\t\tconfig.endpoints = [\n\t\t\t\t...(config.endpoints ?? []),\n\t\t\t\t{ method: 'get', path: DOCUMENT_PATH, handler: makeDocumentHandler() },\n\t\t\t]\n\t\t}\n\t\tconfig.endpoints = [\n\t\t\t...(config.endpoints ?? []),\n\t\t\t{ method: 'get', path: SOURCES_PATH, handler: makeSourcesHandler() },\n\t\t]\n\t\t// A runtime provider's capture support is unknown at config time, so providers\n\t\t// alone are enough to mount the proxy; every slot is still resolved per request.\n\t\tif (resolved.adapters.some((a) => a.capture) || providersEnabled) {\n\t\t\tconfig.endpoints = [...config.endpoints, ...proxyEndpoints(), trackerEndpoint()]\n\t\t}\n\t\tif (resolved.widgets.enabled) {\n\t\t\tconst multiProvider = registry.isMultiProvider() || providersEnabled\n\t\t\tregisterWidgets(config, {\n\t\t\t\tadapters: resolved.adapters,\n\t\t\t\tmultiProvider,\n\t\t\t\tprovidersEnabled,\n\t\t\t\tdisabled: resolved.widgets.disabled,\n\t\t\t\tregister: resolved.widgets.register,\n\t\t\t\tlocalizeText: resolved.widgets.localizeText,\n\t\t\t\tdefaultId: resolved.defaultAdapter,\n\t\t\t})\n\t\t}\n\t\tif (resolved.cache.warm.enabled) {\n\t\t\tconfig.jobs = {\n\t\t\t\t...config.jobs,\n\t\t\t\ttasks: [\n\t\t\t\t\t...(config.jobs?.tasks ?? []),\n\t\t\t\t\twarmTask(resolved.cache.warm.cron, defaultLayout, resolved.scopes),\n\t\t\t\t],\n\t\t\t}\n\t\t}\n\t\tif (resolved.sync.enabled) {\n\t\t\tconfig.collections = [\n\t\t\t\t...(config.collections ?? []),\n\t\t\t\tsyncCollection(resolved.sync.collectionSlug, resolved.sync.hidden, {\n\t\t\t\t\tscoped: resolved.scoped,\n\t\t\t\t\tscopeField: 'scope',\n\t\t\t\t\tresolveScope,\n\t\t\t\t\tplatformRead: resolved.access.platformRead,\n\t\t\t\t}),\n\t\t\t]\n\t\t\tconfig.jobs = {\n\t\t\t\t...config.jobs,\n\t\t\t\ttasks: [\n\t\t\t\t\t...(config.jobs?.tasks ?? []),\n\t\t\t\t\tsyncTask({\n\t\t\t\t\t\tcron: resolved.sync.cron,\n\t\t\t\t\t\tlookbackDays: resolved.sync.lookbackDays,\n\t\t\t\t\t\tcollectionSlug: resolved.sync.collectionSlug,\n\t\t\t\t\t\tadapterIds: resolved.sync.adapters,\n\t\t\t\t\t\tscopes: resolved.scopes,\n\t\t\t\t\t}),\n\t\t\t\t],\n\t\t\t}\n\t\t}\n\t\tconst prevOnInit = config.onInit\n\t\t// The runtime is installed before the app's own onInit runs so consumer init code\n\t\t// (seeding, cache warming, sync passes) can already read through the plugin.\n\t\tconfig.onInit = async (payload) => {\n\t\t\tif (resolved.providers.collection.enabled) {\n\t\t\t\tconst { validateEncryptedBoot } = await import('@10x-media/fields/encrypted')\n\t\t\t\tawait validateEncryptedBoot(payload, resolved.providers.collection.encryption?.keys)\n\t\t\t}\n\t\t\tconst engine = createEngine({\n\t\t\t\tstore: kvCacheStore(payload.kv),\n\t\t\t\tqueue: { concurrency: 4 },\n\t\t\t\tttl: resolved.cache.ttl,\n\t\t\t\ttimeoutMs: resolved.cache.timeoutMs,\n\t\t\t\tonError: (err, adapterId) => {\n\t\t\t\t\tpayload.logger?.warn(`analytics: read failed for adapter \"${adapterId}\": ${String(err)}`)\n\t\t\t\t},\n\t\t\t})\n\t\t\tsetRuntime(payload, {\n\t\t\t\tregistry,\n\t\t\t\tresolveRegistry,\n\t\t\t\tresolveScope,\n\t\t\t\tresolveTimezone,\n\t\t\t\tplatformAdapterId: resolved.platformAdapter,\n\t\t\t\tcaptureSlots: resolved.capture.slots,\n\t\t\t\tcapturePaths: resolved.capture.paths,\n\t\t\t\tcaptureProxy: resolved.capture.proxy,\n\t\t\t\tconsentFor: resolved.capture.consent,\n\t\t\t\tautoCapture: resolved.capture.autoCapture,\n\t\t\t\tgoals: resolved.goals,\n\t\t\t\tingestPath: resolved.adapters.find((a) => a.ingest)?.ingest?.path,\n\t\t\t\tscoped: resolved.scoped,\n\t\t\t\tconfigAdapterIds: new Set(resolved.adapters.map((a) => a.id)),\n\t\t\t\tplatformRead: resolved.access.platformRead,\n\t\t\t\tbindings: resolved.bindings,\n\t\t\t\tengine,\n\t\t\t\tttl: resolved.cache.ttl,\n\t\t\t\tcomparison: resolved.widgets.comparison,\n\t\t\t})\n\t\t\tawait prevOnInit?.(payload)\n\t\t}\n\t\treturn config\n\t},\n})\n\nexport type {\n\tAnalyticsBinding,\n\tBindingContext,\n\tHostnameResolver,\n\tPathResolver,\n} from './binding/types'\nexport { PLATFORM_SCOPE } from './core/contract'\nexport type {\n\tAnalyticsAccessOptions,\n\tAnalyticsCaptureOptions,\n\tAnalyticsGoalsOptions,\n\tAnalyticsPluginOptions,\n\tAnalyticsPluginOptions as PluginOptions,\n\tAutoCaptureOptions,\n\tCaptureConsentOption,\n\tConsentMode,\n\tConsentResolver,\n\tPlatformReadAccess,\n\tProvidersCollectionOptions,\n\tProvidersOptions,\n\tProvidersResolve,\n\tScopeResolver,\n\tScopesResolver,\n\tTimezoneResolver,\n} from './core/options'\nexport type {\n\tAnalyticsFieldsOptions,\n\tAnalyticsMetricLabel,\n\tAnalyticsMetricLabels,\n\tAnalyticsStatOptions,\n\tAnalyticsStatRowOptions,\n\tAnalyticsTabOptions,\n} from './fields/factories'\nexport {\n\tanalyticsFields,\n\tanalyticsStat,\n\tanalyticsStatRow,\n\tanalyticsTab,\n\tanalyticsTabsField,\n} from './fields/factories'\nexport type { Goal, GoalMatch, TrackerGoal } from './goals/types'\nexport type { TimeframePreset } from './timeframe/presets'\nexport type { CustomWidgetDef } from './widgets/customWidget'\nexport { analyticsDefaultWidgets } from './widgets/defaults'\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,MAAa,YAAY,aAAqC;CAC7D,MAAM;CACN,QAAQ,OAAO,EAAE,QAAQ,SAAS,UAAU,GAAG,cAA+B;EAC7E,IAAI,QAAQ,aAAa,MACxB,OAAO;EAER,MAAM,WAAW,eAAe,OAAO;EAIvC,MAAM,mBACL,SAAS,UAAU,WAAW,WAAW,QAAQ,SAAS,UAAU,OAAO;EAC5E,MAAM,gBAAgB,OAAO,OAAO,WAAW;EAC/C,qBAAqB,QAAQ,QAAQ,YAAY;EACjD,MAAM,WAAW,eAAe,SAAS,UAAU,SAAS,cAAc;EAC1E,MAAM,eAAe;GAAE,UAAU,SAAS;GAAU,WAAW,SAAS;EAAe;EACvF,IAAI,kBAAkB,uBAAuB,QAAQ;EACrD,IAAI,4BAA4B,CAAC;EACjC,MAAM,mBAAmB,SAAS,UAAU;EAC5C,IAAI,kBACH,kBAAkB,OAAO,SACxB,kBACC,cACA,MAAM,iBAAiB;GAAE,SAAS,KAAK;GAAS,KAAK,KAAK;GAAK,OAAO,KAAK;EAAM,CAAC,CACnF;OACK,IAAI,SAAS,UAAU,WAAW,SAAS;GACjD,MAAM,SAAS,6BAA6B;IAC3C,MAAM;IACN,QAAQ,0BACP,SAAS,UAAU,WAAW,MAC9B,SAAS,UAAU,WAAW,UAC/B;GACD,CAAC;GACD,kBAAkB,OAAO;GACzB,sBAAsB,OAAO;EAC9B;EACA,MAAM,eAAe,OAAO,QAAwB,SAAS,cAAc,EAAE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AAO;CACR;AACD,CAAC"}
|
|
@@ -25,7 +25,11 @@ const eventsCollection = (scoped = false) => ({
|
|
|
25
25
|
name: "type",
|
|
26
26
|
type: "select",
|
|
27
27
|
required: true,
|
|
28
|
-
options: [
|
|
28
|
+
options: [
|
|
29
|
+
"pageview",
|
|
30
|
+
"event",
|
|
31
|
+
"goal"
|
|
32
|
+
],
|
|
29
33
|
index: true
|
|
30
34
|
},
|
|
31
35
|
{
|
|
@@ -87,6 +91,22 @@ const eventsCollection = (scoped = false) => ({
|
|
|
87
91
|
name: "props",
|
|
88
92
|
type: "json"
|
|
89
93
|
},
|
|
94
|
+
{
|
|
95
|
+
name: "value",
|
|
96
|
+
type: "number"
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
name: "currency",
|
|
100
|
+
type: "text"
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
name: "scrollDepth",
|
|
104
|
+
type: "number"
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
name: "goals",
|
|
108
|
+
type: "json"
|
|
109
|
+
},
|
|
90
110
|
...scoped ? [{
|
|
91
111
|
name: "scope",
|
|
92
112
|
type: "text",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"events.js","names":[],"sources":["../../../src/native/collections/events.ts"],"sourcesContent":["import type { CollectionConfig, Field } from 'payload'\n\nexport const EVENTS_SLUG = 'analytics-events'\n\n/**\n * The scope column exists only for scoped installs (a configured scopeResolver);\n * enabling scope on an existing native install therefore needs a migration adding\n * the column to events and rollups.\n */\nexport const eventsCollection = (scoped = false): CollectionConfig => ({\n\tslug: EVENTS_SLUG,\n\tadmin: { hidden: true },\n\taccess: { read: () => false, create: () => true, update: () => false, delete: () => false },\n\tfields: [\n\t\t{ name: 'timestamp', type: 'date', required: true, index: true },\n\t\t{
|
|
1
|
+
{"version":3,"file":"events.js","names":[],"sources":["../../../src/native/collections/events.ts"],"sourcesContent":["import type { CollectionConfig, Field } from 'payload'\n\nexport const EVENTS_SLUG = 'analytics-events'\n\n/**\n * The scope column exists only for scoped installs (a configured scopeResolver);\n * enabling scope on an existing native install therefore needs a migration adding\n * the column to events and rollups.\n */\nexport const eventsCollection = (scoped = false): CollectionConfig => ({\n\tslug: EVENTS_SLUG,\n\tadmin: { hidden: true },\n\taccess: { read: () => false, create: () => true, update: () => false, delete: () => false },\n\tfields: [\n\t\t{ name: 'timestamp', type: 'date', required: true, index: true },\n\t\t{\n\t\t\tname: 'type',\n\t\t\ttype: 'select',\n\t\t\trequired: true,\n\t\t\toptions: ['pageview', 'event', 'goal'],\n\t\t\tindex: true,\n\t\t},\n\t\t{ name: 'name', type: 'text' },\n\t\t{ name: 'path', type: 'text', required: true, index: true },\n\t\t{ name: 'hostname', type: 'text', required: true },\n\t\t{ name: 'referrer', type: 'text' },\n\t\t{ name: 'device', type: 'text' },\n\t\t{ name: 'source', type: 'text' },\n\t\t{ name: 'country', type: 'text' },\n\t\t{ name: 'region', type: 'text' },\n\t\t{ name: 'city', type: 'text' },\n\t\t{ name: 'visitorHash', type: 'text', required: true, index: true },\n\t\t{ name: 'sessionId', type: 'text', required: true, index: true },\n\t\t{ name: 'durationMs', type: 'number' },\n\t\t{ name: 'props', type: 'json' },\n\t\t{ name: 'value', type: 'number' },\n\t\t{ name: 'currency', type: 'text' },\n\t\t{ name: 'scrollDepth', type: 'number' },\n\t\t// The completions matched at ingest, so a raw-event read (filtered or hourly) reports\n\t\t// the same conversions the rollups hold instead of re-matching against goals that may\n\t\t// since have changed.\n\t\t{ name: 'goals', type: 'json' },\n\t\t...(scoped ? [{ name: 'scope', type: 'text', index: true } satisfies Field] : []),\n\t],\n\tindexes: [{ fields: ['path', 'timestamp'] }, { fields: ['type', 'timestamp'] }],\n})\n"],"mappings":";AAEA,MAAa,cAAc;;;;;;AAO3B,MAAa,oBAAoB,SAAS,WAA6B;CACtE,MAAM;CACN,OAAO,EAAE,QAAQ,KAAK;CACtB,QAAQ;EAAE,YAAY;EAAO,cAAc;EAAM,cA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4
|
-
*
|
|
4
|
+
* `hostname` ('' = the hostname-less family) is part of the unique bucket on every
|
|
5
|
+
* native install: unfiltered reads hit the '' family and hostname-scoped reads hit the
|
|
6
|
+
* exact-hostname family, both kept per-bucket exact by dual emission in computeRollupDeltas.
|
|
7
|
+
* Scoped installs add a further required scope key ('' = the null scope) to every rollup
|
|
5
8
|
* row and to the unique bucket index, so each scope accumulates its own buckets.
|
|
6
|
-
* Existing native installs
|
|
9
|
+
* Existing native installs need a migration for the new hostname column (and, if
|
|
10
|
+
* enabling scope, the scope column too).
|
|
7
11
|
*/
|
|
8
12
|
const rollupsCollection = (scoped = false) => ({
|
|
9
13
|
slug: ROLLUPS_SLUG,
|
|
@@ -44,6 +48,12 @@ const rollupsCollection = (scoped = false) => ({
|
|
|
44
48
|
required: true,
|
|
45
49
|
defaultValue: ""
|
|
46
50
|
},
|
|
51
|
+
{
|
|
52
|
+
name: "hostname",
|
|
53
|
+
type: "text",
|
|
54
|
+
required: true,
|
|
55
|
+
defaultValue: ""
|
|
56
|
+
},
|
|
47
57
|
...scoped ? [{
|
|
48
58
|
name: "scope",
|
|
49
59
|
type: "text",
|
|
@@ -86,6 +96,30 @@ const rollupsCollection = (scoped = false) => ({
|
|
|
86
96
|
type: "number",
|
|
87
97
|
required: true,
|
|
88
98
|
defaultValue: 0
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
name: "conversions",
|
|
102
|
+
type: "number",
|
|
103
|
+
required: true,
|
|
104
|
+
defaultValue: 0
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
name: "revenue",
|
|
108
|
+
type: "number",
|
|
109
|
+
required: true,
|
|
110
|
+
defaultValue: 0
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
name: "scrollDepthSum",
|
|
114
|
+
type: "number",
|
|
115
|
+
required: true,
|
|
116
|
+
defaultValue: 0
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
name: "scrollSamples",
|
|
120
|
+
type: "number",
|
|
121
|
+
required: true,
|
|
122
|
+
defaultValue: 0
|
|
89
123
|
}
|
|
90
124
|
],
|
|
91
125
|
indexes: [scoped ? {
|
|
@@ -95,6 +129,7 @@ const rollupsCollection = (scoped = false) => ({
|
|
|
95
129
|
"path",
|
|
96
130
|
"dimension",
|
|
97
131
|
"dimvalue",
|
|
132
|
+
"hostname",
|
|
98
133
|
"scope"
|
|
99
134
|
],
|
|
100
135
|
unique: true
|
|
@@ -104,7 +139,8 @@ const rollupsCollection = (scoped = false) => ({
|
|
|
104
139
|
"period",
|
|
105
140
|
"path",
|
|
106
141
|
"dimension",
|
|
107
|
-
"dimvalue"
|
|
142
|
+
"dimvalue",
|
|
143
|
+
"hostname"
|
|
108
144
|
],
|
|
109
145
|
unique: true
|
|
110
146
|
}]
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rollups.js","names":[],"sources":["../../../src/native/collections/rollups.ts"],"sourcesContent":["import type { CollectionConfig, Field } from 'payload'\n\nexport const ROLLUPS_SLUG = 'analytics-rollups'\n\n/**\n * Scoped installs add a required scope key ('' = the null scope) to every rollup\n * row and to the unique bucket index, so each scope accumulates its own buckets.\n * Existing native installs
|
|
1
|
+
{"version":3,"file":"rollups.js","names":[],"sources":["../../../src/native/collections/rollups.ts"],"sourcesContent":["import type { CollectionConfig, Field } from 'payload'\n\nexport const ROLLUPS_SLUG = 'analytics-rollups'\n\n/**\n * `hostname` ('' = the hostname-less family) is part of the unique bucket on every\n * native install: unfiltered reads hit the '' family and hostname-scoped reads hit the\n * exact-hostname family, both kept per-bucket exact by dual emission in computeRollupDeltas.\n * Scoped installs add a further required scope key ('' = the null scope) to every rollup\n * row and to the unique bucket index, so each scope accumulates its own buckets.\n * Existing native installs need a migration for the new hostname column (and, if\n * enabling scope, the scope column too).\n */\nexport const rollupsCollection = (scoped = false): CollectionConfig => ({\n\tslug: ROLLUPS_SLUG,\n\tadmin: { hidden: true },\n\taccess: { read: () => false, create: () => true, update: () => true, delete: () => true },\n\tfields: [\n\t\t{ name: 'granularity', type: 'text', required: true },\n\t\t{ name: 'period', type: 'date', required: true, index: true },\n\t\t{ name: 'path', type: 'text', required: true, index: true },\n\t\t{ name: 'dimension', type: 'text', required: true, defaultValue: '' },\n\t\t{ name: 'dimvalue', type: 'text', required: true, defaultValue: '' },\n\t\t{ name: 'hostname', type: 'text', required: true, defaultValue: '' },\n\t\t...(scoped\n\t\t\t? [\n\t\t\t\t\t{\n\t\t\t\t\t\tname: 'scope',\n\t\t\t\t\t\ttype: 'text',\n\t\t\t\t\t\trequired: true,\n\t\t\t\t\t\tdefaultValue: '',\n\t\t\t\t\t\tindex: true,\n\t\t\t\t\t} satisfies Field,\n\t\t\t\t]\n\t\t\t: []),\n\t\t{ name: 'pageviews', type: 'number', required: true, defaultValue: 0 },\n\t\t{ name: 'events', type: 'number', required: true, defaultValue: 0 },\n\t\t{ name: 'durationMs', type: 'number', required: true, defaultValue: 0 },\n\t\t{ name: 'visitors', type: 'number', required: true, defaultValue: 0 },\n\t\t{ name: 'sessions', type: 'number', required: true, defaultValue: 0 },\n\t\t{ name: 'samples', type: 'number', required: true, defaultValue: 0 },\n\t\t{ name: 'conversions', type: 'number', required: true, defaultValue: 0 },\n\t\t// Revenue is stored in the event's own currency and never converted, so a bucket\n\t\t// spanning several currencies sums figures that are not comparable.\n\t\t{ name: 'revenue', type: 'number', required: true, defaultValue: 0 },\n\t\t{ name: 'scrollDepthSum', type: 'number', required: true, defaultValue: 0 },\n\t\t{ name: 'scrollSamples', type: 'number', required: true, defaultValue: 0 },\n\t],\n\tindexes: [\n\t\tscoped\n\t\t\t? {\n\t\t\t\t\tfields: ['granularity', 'period', 'path', 'dimension', 'dimvalue', 'hostname', 'scope'],\n\t\t\t\t\tunique: true,\n\t\t\t\t}\n\t\t\t: {\n\t\t\t\t\tfields: ['granularity', 'period', 'path', 'dimension', 'dimvalue', 'hostname'],\n\t\t\t\t\tunique: true,\n\t\t\t\t},\n\t],\n})\n"],"mappings":";AAEA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AAc;EAAE;CAC1E;CACA,SAAS,CACR,SACG;EACA,QAAQ;GAAC;GAAe;GAAU;GAAQ;GAAa;GAAY;GAAY;EAAO;EACtF,QAAQ;CACT,IACC;EACA,QAAQ;GAAC;GAAe;GAAU;GAAQ;GAAa;GAAY;EAAU;EAC7E,QAAQ;CACT,CACH;AACD"}
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
+
import { clientIpFromHeaders } from "../ingest/clientIp.js";
|
|
1
2
|
//#region src/native/geo/maxmindResolver.ts
|
|
2
|
-
const clientIp = (headers) => {
|
|
3
|
-
return headers.get("x-forwarded-for")?.split(",")[0]?.trim() || headers.get("x-real-ip") || null;
|
|
4
|
-
};
|
|
5
3
|
/**
|
|
6
4
|
* Optional MaxMind GeoLite2 resolver. The mmdb and the maxmind lib are loaded
|
|
7
5
|
* lazily on first use. Any failure (lib not installed, file missing/unreadable,
|
|
@@ -26,7 +24,7 @@ function maxmindResolver(options) {
|
|
|
26
24
|
return readerPromise;
|
|
27
25
|
};
|
|
28
26
|
return async (headers) => {
|
|
29
|
-
const ip =
|
|
27
|
+
const ip = clientIpFromHeaders(headers);
|
|
30
28
|
if (!ip) return {};
|
|
31
29
|
let reader;
|
|
32
30
|
try {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"maxmindResolver.js","names":[],"sources":["../../../src/native/geo/maxmindResolver.ts"],"sourcesContent":["import type { CityResponse, Reader } from 'maxmind'\nimport type { Geo, GeoResolver } from './geoResolver'\n\nexport interface MaxmindOptions {\n\tdbPath: string\n}\n\
|
|
1
|
+
{"version":3,"file":"maxmindResolver.js","names":[],"sources":["../../../src/native/geo/maxmindResolver.ts"],"sourcesContent":["import type { CityResponse, Reader } from 'maxmind'\nimport { clientIpFromHeaders } from '../ingest/clientIp'\nimport type { Geo, GeoResolver } from './geoResolver'\n\nexport interface MaxmindOptions {\n\tdbPath: string\n}\n\n/**\n * Optional MaxMind GeoLite2 resolver. The mmdb and the maxmind lib are loaded\n * lazily on first use. Any failure (lib not installed, file missing/unreadable,\n * lookup error) degrades to empty geo after a single warning, so a missing or\n * unconfigured database never breaks ingestion.\n */\nexport function maxmindResolver(options: MaxmindOptions): GeoResolver {\n\tlet readerPromise: Promise<Reader<CityResponse>> | undefined\n\tlet warned = false\n\n\tconst warn = (detail: unknown): null => {\n\t\tif (!warned) {\n\t\t\twarned = true\n\t\t\tconsole.warn(\n\t\t\t\t`[analytics] MaxMind geo unavailable for \"${options.dbPath}\"; geo will be empty.`,\n\t\t\t\tdetail instanceof Error ? detail.message : detail\n\t\t\t)\n\t\t}\n\t\treturn null\n\t}\n\n\tconst getReader = (): Promise<Reader<CityResponse>> => {\n\t\tif (!readerPromise) {\n\t\t\treaderPromise = (async () => {\n\t\t\t\tconst { open } = await import('maxmind')\n\t\t\t\treturn open<CityResponse>(options.dbPath)\n\t\t\t})()\n\t\t}\n\t\treturn readerPromise\n\t}\n\n\treturn async (headers: Headers): Promise<Geo> => {\n\t\tconst ip = clientIpFromHeaders(headers)\n\t\tif (!ip) {\n\t\t\treturn {}\n\t\t}\n\t\tlet reader: Reader<CityResponse> | null\n\t\ttry {\n\t\t\treader = await getReader()\n\t\t} catch (err) {\n\t\t\twarn(err)\n\t\t\treturn {}\n\t\t}\n\t\ttry {\n\t\t\tconst r = reader.get(ip)\n\t\t\tif (!r) {\n\t\t\t\treturn {}\n\t\t\t}\n\t\t\treturn {\n\t\t\t\tcountry: r.country?.iso_code ?? undefined,\n\t\t\t\tregion: r.subdivisions?.[0]?.iso_code ?? undefined,\n\t\t\t\tcity: r.city?.names.en ?? undefined,\n\t\t\t}\n\t\t} catch (err) {\n\t\t\twarn(err)\n\t\t\treturn {}\n\t\t}\n\t}\n}\n"],"mappings":";;;;;;;;AAcA,SAAgB,gBAAgB,SAAsC;CACrE,IAAI;CACJ,IAAI,SAAS;CAEb,MAAM,QAAQ,WAA0B;EACvC,IAAI,CAAC,QAAQ;GACZ,SAAS;GACT,QAAQ,KACP,4CAA4C,QAAQ,OAAO,wBAC3D,kBAAkB,QAAQ,OAAO,UAAU,MAC5C;EACD;EACA,OAAO;CACR;CAEA,MAAM,kBAAiD;EACtD,IAAI,CAAC,eACJ,iBAAiB,YAAY;GAC5B,MAAM,EAAE,SAAS,MAAM,OAAO;GAC9B,OAAO,KAAmB,QAAQ,MAAM;EACzC,GAAG;EAEJ,OAAO;CACR;CAEA,OAAO,OAAO,YAAmC;EAChD,MAAM,KAAK,oBAAoB,OAAO;EACtC,IAAI,CAAC,IACJ,OAAO,CAAC;EAET,IAAI;EACJ,IAAI;GACH,SAAS,MAAM,UAAU;EAC1B,SAAS,KAAK;GACb,KAAK,GAAG;GACR,OAAO,CAAC;EACT;EACA,IAAI;GACH,MAAM,IAAI,OAAO,IAAI,EAAE;GACvB,IAAI,CAAC,GACJ,OAAO,CAAC;GAET,OAAO;IACN,SAAS,EAAE,SAAS,YAAY,KAAA;IAChC,QAAQ,EAAE,eAAe,IAAI,YAAY,KAAA;IACzC,MAAM,EAAE,MAAM,MAAM,MAAM,KAAA;GAC3B;EACD,SAAS,KAAK;GACb,KAAK,GAAG;GACR,OAAO,CAAC;EACT;CACD;AACD"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//#region src/native/ingest/clientIp.ts
|
|
2
|
+
/**
|
|
3
|
+
* The originating client IP a proxy chain reported: the first hop of
|
|
4
|
+
* `x-forwarded-for`, else `x-real-ip`. Null when the request carries neither, so
|
|
5
|
+
* callers can omit the value instead of forwarding a fabricated one.
|
|
6
|
+
*/
|
|
7
|
+
const clientIpFromHeaders = (headers) => {
|
|
8
|
+
const forwarded = headers.get("x-forwarded-for")?.split(",")[0]?.trim();
|
|
9
|
+
if (forwarded) return forwarded;
|
|
10
|
+
return headers.get("x-real-ip")?.trim() || null;
|
|
11
|
+
};
|
|
12
|
+
//#endregion
|
|
13
|
+
export { clientIpFromHeaders };
|
|
14
|
+
|
|
15
|
+
//# sourceMappingURL=clientIp.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clientIp.js","names":[],"sources":["../../../src/native/ingest/clientIp.ts"],"sourcesContent":["/**\n * The originating client IP a proxy chain reported: the first hop of\n * `x-forwarded-for`, else `x-real-ip`. Null when the request carries neither, so\n * callers can omit the value instead of forwarding a fabricated one.\n */\nexport const clientIpFromHeaders = (headers: Headers): string | null => {\n\tconst forwarded = headers.get('x-forwarded-for')?.split(',')[0]?.trim()\n\tif (forwarded) {\n\t\treturn forwarded\n\t}\n\treturn headers.get('x-real-ip')?.trim() || null\n}\n"],"mappings":";;;;;;AAKA,MAAa,uBAAuB,YAAoC;CACvE,MAAM,YAAY,QAAQ,IAAI,iBAAiB,GAAG,MAAM,GAAG,EAAE,IAAI,KAAK;CACtE,IAAI,WACH,OAAO;CAER,OAAO,QAAQ,IAAI,WAAW,GAAG,KAAK,KAAK;AAC5C"}
|
|
@@ -1,15 +1,50 @@
|
|
|
1
|
+
import { readCappedBody } from "../../capture/requestBody.js";
|
|
1
2
|
import { flushBatch } from "./flushBatch.js";
|
|
2
3
|
import { normalizeEvent } from "./normalizeEvent.js";
|
|
3
4
|
import { dailySalt } from "./salt.js";
|
|
4
5
|
//#region src/native/ingest/endpoint.ts
|
|
6
|
+
const TYPES = new Set([
|
|
7
|
+
"pageview",
|
|
8
|
+
"event",
|
|
9
|
+
"goal"
|
|
10
|
+
]);
|
|
11
|
+
/** One event, not a session replay: far above any legitimate payload, far below a DoS. */
|
|
12
|
+
const MAX_INGEST_BODY_BYTES = 64 * 1024;
|
|
13
|
+
/**
|
|
14
|
+
* Parses the buffered body, answering undefined for anything the validator could not judge:
|
|
15
|
+
* a garbage POST is a 400 like any other bad payload rather than a thrown parse error that
|
|
16
|
+
* escapes into Payload's routeError as a logged 500.
|
|
17
|
+
*/
|
|
18
|
+
const parseBody = (bytes) => {
|
|
19
|
+
try {
|
|
20
|
+
const parsed = JSON.parse(new TextDecoder().decode(bytes));
|
|
21
|
+
return typeof parsed === "object" && parsed !== null ? parsed : void 0;
|
|
22
|
+
} catch {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* An event is rejected only on its required fields: a known `type`, a `path`, a `hostname`,
|
|
28
|
+
* and a `name` (the event name, or the goal slug on a `goal`) for everything but a pageview.
|
|
29
|
+
* Optional fields are sanitized in `normalizeEvent` and dropped when malformed, so one bad
|
|
30
|
+
* attribute costs an attribute rather than the whole event.
|
|
31
|
+
*/
|
|
32
|
+
const nonEmptyString = (value) => typeof value === "string" && value.length > 0;
|
|
33
|
+
const isValid = (raw) => {
|
|
34
|
+
if (!raw || !TYPES.has(raw.type) || !nonEmptyString(raw.path) || !nonEmptyString(raw.hostname)) return false;
|
|
35
|
+
return raw.type === "pageview" || nonEmptyString(raw.name);
|
|
36
|
+
};
|
|
5
37
|
const makeIngestHandler = (geoResolver, getBuffer = () => null, resolvers = {}) => async (req) => {
|
|
6
|
-
const { scope: resolveScope, timezone: resolveTimezone } = resolvers;
|
|
7
|
-
const
|
|
8
|
-
if (!
|
|
38
|
+
const { scope: resolveScope, timezone: resolveTimezone, goals: resolveGoals } = resolvers;
|
|
39
|
+
const read = await readCappedBody(req, MAX_INGEST_BODY_BYTES);
|
|
40
|
+
if (!read.ok) return Response.json({ error: read.reason === "too-large" ? "payload too large" : "invalid payload" }, { status: read.reason === "too-large" ? 413 : 400 });
|
|
41
|
+
const raw = parseBody(read.body);
|
|
42
|
+
if (!isValid(raw)) return Response.json({ error: "invalid payload" }, { status: 400 });
|
|
9
43
|
const now = /* @__PURE__ */ new Date();
|
|
10
44
|
const salt = await dailySalt(req.payload, now);
|
|
11
45
|
const scope = resolveScope ? await resolveScope(req) ?? "" : void 0;
|
|
12
46
|
const timezone = resolveTimezone ? await resolveTimezone(req, scope ?? null) : void 0;
|
|
47
|
+
const goals = resolveGoals ? await resolveGoals(req, scope ?? null) : void 0;
|
|
13
48
|
const event = await normalizeEvent({
|
|
14
49
|
raw,
|
|
15
50
|
headers: req.headers,
|
|
@@ -17,7 +52,8 @@ const makeIngestHandler = (geoResolver, getBuffer = () => null, resolvers = {})
|
|
|
17
52
|
salt,
|
|
18
53
|
now,
|
|
19
54
|
scope,
|
|
20
|
-
timezone
|
|
55
|
+
timezone,
|
|
56
|
+
goals
|
|
21
57
|
});
|
|
22
58
|
const buffer = getBuffer();
|
|
23
59
|
if (buffer) buffer.add(event);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"endpoint.js","names":[],"sources":["../../../src/native/ingest/endpoint.ts"],"sourcesContent":["import type { PayloadHandler, PayloadRequest } from 'payload'\nimport type { GeoResolver } from '../geo/geoResolver'\nimport { flushBatch } from './flushBatch'\nimport {
|
|
1
|
+
{"version":3,"file":"endpoint.js","names":[],"sources":["../../../src/native/ingest/endpoint.ts"],"sourcesContent":["import type { PayloadHandler, PayloadRequest } from 'payload'\nimport { readCappedBody } from '../../capture/requestBody'\nimport type { Goal } from '../../goals/types'\nimport type { GeoResolver } from '../geo/geoResolver'\nimport { flushBatch } from './flushBatch'\nimport {\n\ttype EventType,\n\tnormalizeEvent,\n\ttype RawEventInput,\n\ttype StoredEvent,\n} from './normalizeEvent'\nimport { dailySalt } from './salt'\nimport type { WriteBuffer } from './writeBuffer'\n\nexport interface IngestResolvers {\n\tscope?: (req: PayloadRequest) => Promise<string | null>\n\ttimezone?: (req: PayloadRequest, scope?: string | null) => Promise<string>\n\tgoals?: (req: PayloadRequest, scope?: string | null) => Promise<Goal[]>\n}\n\nconst TYPES: ReadonlySet<string> = new Set<EventType>(['pageview', 'event', 'goal'])\n\n/** One event, not a session replay: far above any legitimate payload, far below a DoS. */\nexport const MAX_INGEST_BODY_BYTES = 64 * 1024\n\n/**\n * Parses the buffered body, answering undefined for anything the validator could not judge:\n * a garbage POST is a 400 like any other bad payload rather than a thrown parse error that\n * escapes into Payload's routeError as a logged 500.\n */\nconst parseBody = (bytes: ArrayBuffer): RawEventInput | undefined => {\n\ttry {\n\t\tconst parsed: unknown = JSON.parse(new TextDecoder().decode(bytes))\n\t\treturn typeof parsed === 'object' && parsed !== null ? (parsed as RawEventInput) : undefined\n\t} catch {\n\t\treturn undefined\n\t}\n}\n\n/**\n * An event is rejected only on its required fields: a known `type`, a `path`, a `hostname`,\n * and a `name` (the event name, or the goal slug on a `goal`) for everything but a pageview.\n * Optional fields are sanitized in `normalizeEvent` and dropped when malformed, so one bad\n * attribute costs an attribute rather than the whole event.\n */\nconst nonEmptyString = (value: unknown): boolean => typeof value === 'string' && value.length > 0\n\nconst isValid = (raw: RawEventInput | undefined): raw is RawEventInput => {\n\t// Types are checked, not just truthiness: a non-string path would otherwise reach goal\n\t// matching and throw there rather than answering 400 here.\n\tif (!raw || !TYPES.has(raw.type) || !nonEmptyString(raw.path) || !nonEmptyString(raw.hostname)) {\n\t\treturn false\n\t}\n\treturn raw.type === 'pageview' || nonEmptyString(raw.name)\n}\n\nexport const makeIngestHandler =\n\t(\n\t\tgeoResolver: GeoResolver,\n\t\tgetBuffer: () => WriteBuffer<StoredEvent> | null = () => null,\n\t\tresolvers: IngestResolvers = {}\n\t): PayloadHandler =>\n\tasync (req) => {\n\t\tconst { scope: resolveScope, timezone: resolveTimezone, goals: resolveGoals } = resolvers\n\t\t// Read like the capture proxy does, and for the same reasons: this is a public,\n\t\t// unauthenticated path, so the body is capped before it is buffered and a body that\n\t\t// dies in transit (a beacon from an unloading tab) answers 400 rather than throwing.\n\t\tconst read = await readCappedBody(req, MAX_INGEST_BODY_BYTES)\n\t\tif (!read.ok) {\n\t\t\treturn Response.json(\n\t\t\t\t{ error: read.reason === 'too-large' ? 'payload too large' : 'invalid payload' },\n\t\t\t\t{ status: read.reason === 'too-large' ? 413 : 400 }\n\t\t\t)\n\t\t}\n\t\tconst raw = parseBody(read.body)\n\t\tif (!isValid(raw)) {\n\t\t\treturn Response.json({ error: 'invalid payload' }, { status: 400 })\n\t\t}\n\t\tconst now = new Date()\n\t\tconst salt = await dailySalt(req.payload, now)\n\t\tconst scope = resolveScope ? ((await resolveScope(req)) ?? '') : undefined\n\t\tconst timezone = resolveTimezone ? await resolveTimezone(req, scope ?? null) : undefined\n\t\t// Goals resolve under the same scope the event is stamped with, so a tenant's event\n\t\t// can only ever complete that tenant's goals.\n\t\tconst goals = resolveGoals ? await resolveGoals(req, scope ?? null) : undefined\n\t\tconst event = await normalizeEvent({\n\t\t\traw,\n\t\t\theaders: req.headers,\n\t\t\tgeoResolver,\n\t\t\tsalt,\n\t\t\tnow,\n\t\t\tscope,\n\t\t\ttimezone,\n\t\t\tgoals,\n\t\t})\n\t\tconst buffer = getBuffer()\n\t\tif (buffer) {\n\t\t\tbuffer.add(event)\n\t\t} else {\n\t\t\tawait flushBatch(req.payload, [event])\n\t\t}\n\t\treturn Response.json({ ok: true }, { status: 202 })\n\t}\n"],"mappings":";;;;;AAoBA,MAAM,QAA6B,IAAI,IAAe;CAAC;CAAY;CAAS;AAAM,CAAC;;AAGnF,MAAa,wBAAwB,KAAK;;;;;;AAO1C,MAAM,aAAa,UAAkD;CACpE,IAAI;EACH,MAAM,SAAkB,KAAK,MAAM,IAAI,YAAY,EAAE,OAAO,KAAK,CAAC;EAClE,OAAO,OAAO,WAAW,YAAY,WAAW,OAAQ,SAA2B,KAAA;CACpF,QAAQ;EACP;CACD;AACD;;;;;;;AAQA,MAAM,kBAAkB,UAA4B,OAAO,UAAU,YAAY,MAAM,SAAS;AAEhG,MAAM,WAAW,QAAyD;CAGzE,IAAI,CAAC,OAAO,CAAC,MAAM,IAAI,IAAI,IAAI,KAAK,CAAC,eAAe,IAAI,IAAI,KAAK,CAAC,eAAe,IAAI,QAAQ,GAC5F,OAAO;CAER,OAAO,IAAI,SAAS,cAAc,eAAe,IAAI,IAAI;AAC1D;AAEA,MAAa,qBAEX,aACA,kBAAyD,MACzD,YAA6B,CAAC,MAE/B,OAAO,QAAQ;CACd,MAAM,EAAE,OAAO,cAAc,UAAU,iBAAiB,OAAO,iBAAiB;CAIhF,MAAM,OAAO,MAAM,eAAe,KAAK,qBAAqB;CAC5D,IAAI,CAAC,KAAK,IACT,OAAO,SAAS,KACf,EAAE,OAAO,KAAK,WAAW,cAAc,sBAAsB,kBAAkB,GAC/E,EAAE,QAAQ,KAAK,WAAW,cAAc,MAAM,IAAI,CACnD;CAED,MAAM,MAAM,UAAU,KAAK,IAAI;CAC/B,IAAI,CAAC,QAAQ,GAAG,GACf,OAAO,SAAS,KAAK,EAAE,OAAO,kBAAkB,GAAG,EAAE,QAAQ,IAAI,CAAC;CAEnE,MAAM,sBAAM,IAAI,KAAK;CACrB,MAAM,OAAO,MAAM,UAAU,IAAI,SAAS,GAAG;CAC7C,MAAM,QAAQ,eAAiB,MAAM,aAAa,GAAG,KAAM,KAAM,KAAA;CACjE,MAAM,WAAW,kBAAkB,MAAM,gBAAgB,KAAK,SAAS,IAAI,IAAI,KAAA;CAG/E,MAAM,QAAQ,eAAe,MAAM,aAAa,KAAK,SAAS,IAAI,IAAI,KAAA;CACtE,MAAM,QAAQ,MAAM,eAAe;EAClC;EACA,SAAS,IAAI;EACb;EACA;EACA;EACA;EACA;EACA;CACD,CAAC;CACD,MAAM,SAAS,UAAU;CACzB,IAAI,QACH,OAAO,IAAI,KAAK;MAEhB,MAAM,WAAW,IAAI,SAAS,CAAC,KAAK,CAAC;CAEtC,OAAO,SAAS,KAAK,EAAE,IAAI,KAAK,GAAG,EAAE,QAAQ,IAAI,CAAC;AACnD"}
|
|
@@ -13,14 +13,10 @@ async function flushBatch(payload, events) {
|
|
|
13
13
|
for (const event of events) for (const delta of computeRollupDeltas(event)) {
|
|
14
14
|
const bk = bucketKey(delta.key);
|
|
15
15
|
const agg = base.get(bk);
|
|
16
|
-
if (agg)
|
|
17
|
-
|
|
18
|
-
agg.events += delta.inc.events;
|
|
19
|
-
agg.durationMs += delta.inc.durationMs;
|
|
20
|
-
agg.samples += delta.inc.samples;
|
|
21
|
-
} else base.set(bk, {
|
|
16
|
+
if (agg) for (const metric of Object.keys(agg.inc)) agg.inc[metric] += delta.inc[metric];
|
|
17
|
+
else base.set(bk, {
|
|
22
18
|
key: delta.key,
|
|
23
|
-
...delta.inc
|
|
19
|
+
inc: { ...delta.inc }
|
|
24
20
|
});
|
|
25
21
|
candidates.set(`${bk}|visitor|${event.visitorHash}`, {
|
|
26
22
|
key: delta.key,
|
|
@@ -33,12 +29,7 @@ async function flushBatch(payload, events) {
|
|
|
33
29
|
value: event.sessionId
|
|
34
30
|
});
|
|
35
31
|
}
|
|
36
|
-
for (const agg of base.values()) await bumpRollup(payload, agg.key,
|
|
37
|
-
pageviews: agg.pageviews,
|
|
38
|
-
events: agg.events,
|
|
39
|
-
durationMs: agg.durationMs,
|
|
40
|
-
samples: agg.samples
|
|
41
|
-
});
|
|
32
|
+
for (const agg of base.values()) await bumpRollup(payload, agg.key, agg.inc);
|
|
42
33
|
const distinct = /* @__PURE__ */ new Map();
|
|
43
34
|
for (const candidate of candidates.values()) {
|
|
44
35
|
if (!await insertIfNew(payload, "analytics-seen", {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"flushBatch.js","names":[],"sources":["../../../src/native/ingest/flushBatch.ts"],"sourcesContent":["import type { Payload } from 'payload'\nimport { SEEN_SLUG } from '../collections/seen'\nimport { bucketKey } from '../rollups/applyDistinctDeltas'\nimport { bumpRollup } from '../rollups/bumpRollup'\nimport {
|
|
1
|
+
{"version":3,"file":"flushBatch.js","names":[],"sources":["../../../src/native/ingest/flushBatch.ts"],"sourcesContent":["import type { Payload } from 'payload'\nimport { SEEN_SLUG } from '../collections/seen'\nimport { bucketKey } from '../rollups/applyDistinctDeltas'\nimport { bumpRollup } from '../rollups/bumpRollup'\nimport {\n\tcomputeRollupDeltas,\n\ttype RollupInc,\n\ttype RollupKey,\n\ttype RollupMetric,\n} from '../rollups/deltas'\nimport { insertIfNew } from '../rollups/insertIfNew'\nimport type { StoredEvent } from './normalizeEvent'\nimport { writeEvent } from './writeEvent'\n\ninterface BaseAgg {\n\tkey: RollupKey\n\tinc: RollupInc\n}\n\ninterface DistinctCandidate {\n\tkey: RollupKey\n\tkind: 'visitor' | 'session'\n\tvalue: string\n}\n\n// Coalesces a batch of events into the fewest writes: events insert concurrently, base\n// rollup increments are summed per bucket into one upsert each, and distinct visitor /\n// session candidates are de-duplicated within the batch before insert-if-new. Correctness\n// matches the per-event path because the seen ledger still de-dupes across batches.\nexport async function flushBatch(payload: Payload, events: StoredEvent[]): Promise<void> {\n\tif (events.length === 0) {\n\t\treturn\n\t}\n\tawait Promise.all(events.map((event) => writeEvent(payload, event)))\n\n\tconst base = new Map<string, BaseAgg>()\n\tconst candidates = new Map<string, DistinctCandidate>()\n\tfor (const event of events) {\n\t\tfor (const delta of computeRollupDeltas(event)) {\n\t\t\tconst bk = bucketKey(delta.key)\n\t\t\tconst agg = base.get(bk)\n\t\t\tif (agg) {\n\t\t\t\tfor (const metric of Object.keys(agg.inc) as Array<keyof RollupInc>) {\n\t\t\t\t\tagg.inc[metric] += delta.inc[metric]\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tbase.set(bk, { key: delta.key, inc: { ...delta.inc } })\n\t\t\t}\n\t\t\tcandidates.set(`${bk}|visitor|${event.visitorHash}`, {\n\t\t\t\tkey: delta.key,\n\t\t\t\tkind: 'visitor',\n\t\t\t\tvalue: event.visitorHash,\n\t\t\t})\n\t\t\tcandidates.set(`${bk}|session|${event.sessionId}`, {\n\t\t\t\tkey: delta.key,\n\t\t\t\tkind: 'session',\n\t\t\t\tvalue: event.sessionId,\n\t\t\t})\n\t\t}\n\t}\n\n\tfor (const agg of base.values()) {\n\t\tawait bumpRollup(payload, agg.key, agg.inc)\n\t}\n\n\tconst distinct = new Map<string, { key: RollupKey; metric: RollupMetric; count: number }>()\n\tfor (const candidate of candidates.values()) {\n\t\tconst isNew = await insertIfNew(payload, SEEN_SLUG, {\n\t\t\tbucket: bucketKey(candidate.key),\n\t\t\tkind: candidate.kind,\n\t\t\tvalue: candidate.value,\n\t\t\tperiod: candidate.key.period,\n\t\t})\n\t\tif (!isNew) {\n\t\t\tcontinue\n\t\t}\n\t\tconst metric: RollupMetric = candidate.kind === 'visitor' ? 'visitors' : 'sessions'\n\t\tconst dk = `${bucketKey(candidate.key)}|${metric}`\n\t\tconst entry = distinct.get(dk)\n\t\tif (entry) {\n\t\t\tentry.count += 1\n\t\t} else {\n\t\t\tdistinct.set(dk, { key: candidate.key, metric, count: 1 })\n\t\t}\n\t}\n\n\tfor (const entry of distinct.values()) {\n\t\tconst inc: Partial<Record<RollupMetric, number>> = {}\n\t\tinc[entry.metric] = entry.count\n\t\tawait bumpRollup(payload, entry.key, inc)\n\t}\n}\n"],"mappings":";;;;;;;AA6BA,eAAsB,WAAW,SAAkB,QAAsC;CACxF,IAAI,OAAO,WAAW,GACrB;CAED,MAAM,QAAQ,IAAI,OAAO,KAAK,UAAU,WAAW,SAAS,KAAK,CAAC,CAAC;CAEnE,MAAM,uBAAO,IAAI,IAAqB;CACtC,MAAM,6BAAa,IAAI,IAA+B;CACtD,KAAK,MAAM,SAAS,QACnB,KAAK,MAAM,SAAS,oBAAoB,KAAK,GAAG;EAC/C,MAAM,KAAK,UAAU,MAAM,GAAG;EAC9B,MAAM,MAAM,KAAK,IAAI,EAAE;EACvB,IAAI,KACH,KAAK,MAAM,UAAU,OAAO,KAAK,IAAI,GAAG,GACvC,IAAI,IAAI,WAAW,MAAM,IAAI;OAG9B,KAAK,IAAI,IAAI;GAAE,KAAK,MAAM;GAAK,KAAK,EAAE,GAAG,MAAM,IAAI;EAAE,CAAC;EAEvD,WAAW,IAAI,GAAG,GAAG,WAAW,MAAM,eAAe;GACpD,KAAK,MAAM;GACX,MAAM;GACN,OAAO,MAAM;EACd,CAAC;EACD,WAAW,IAAI,GAAG,GAAG,WAAW,MAAM,aAAa;GAClD,KAAK,MAAM;GACX,MAAM;GACN,OAAO,MAAM;EACd,CAAC;CACF;CAGD,KAAK,MAAM,OAAO,KAAK,OAAO,GAC7B,MAAM,WAAW,SAAS,IAAI,KAAK,IAAI,GAAG;CAG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