@10x-media/analytics 1.0.0-beta.7 → 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 +118 -0
- package/LICENSE +21 -21
- package/README.md +10 -4
- 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 +179 -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 +112 -43
- 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/capture/slots.js +16 -5
- package/dist/capture/slots.js.map +1 -1
- package/dist/capture/trackerConfig.d.ts +1 -0
- package/dist/capture/trackerConfig.js +28 -3
- package/dist/capture/trackerConfig.js.map +1 -1
- package/dist/charts/BarList.d.ts +15 -1
- package/dist/charts/BarList.js +41 -19
- package/dist/charts/BarList.js.map +1 -1
- package/dist/charts/TrendChart.d.ts +16 -3
- package/dist/charts/TrendChart.js +172 -106
- package/dist/charts/TrendChart.js.map +1 -1
- package/dist/charts/bars.d.ts +2 -0
- package/dist/charts/bars.js +2 -1
- package/dist/charts/bars.js.map +1 -1
- package/dist/charts/bucket.js +10 -1
- package/dist/charts/bucket.js.map +1 -1
- package/dist/charts/spline.js +2 -2
- package/dist/charts/spline.js.map +1 -1
- package/dist/charts/styles.js +18 -1
- package/dist/charts/styles.js.map +1 -1
- package/dist/core/cacheKey.js +24 -2
- 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 +46 -9
- package/dist/core/contract.js +29 -1
- package/dist/core/contract.js.map +1 -1
- package/dist/core/granularity.js +1 -1
- package/dist/core/options.d.ts +62 -7
- package/dist/core/options.js +63 -4
- package/dist/core/options.js.map +1 -1
- package/dist/core/scopedRead.js +33 -23
- package/dist/core/scopedRead.js.map +1 -1
- package/dist/core/serverEvent.d.ts +44 -0
- package/dist/core/serverEvent.js +12 -0
- package/dist/core/serverEvent.js.map +1 -0
- package/dist/exports/adapters/ga4.d.ts +2 -1
- package/dist/exports/adapters/ga4.js +2 -1
- package/dist/exports/client.d.ts +12 -3
- package/dist/exports/client.js +9 -1
- package/dist/exports/rsc.d.ts +7 -2
- package/dist/exports/rsc.js +6 -3
- package/dist/exports/tracker.d.ts +3 -2
- package/dist/exports/tracker.js +2 -1
- package/dist/exports/types.d.ts +13 -4
- package/dist/exports/types.js +3 -2
- package/dist/fields/AnalyticsPanelClient.js +1 -1
- package/dist/fields/AnalyticsPanelField.js +1 -1
- package/dist/fields/AnalyticsStatField.js +2 -2
- 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 +3 -3
- package/dist/fields/config/MetricSelectField.js.map +1 -1
- package/dist/fields/config/SourceSelectField.js +3 -3
- package/dist/fields/config/SourceSelectField.js.map +1 -1
- package/dist/fields/config/fetchSources.d.ts +5 -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.d.ts +5 -0
- package/dist/fields/readForDocument.js +21 -9
- package/dist/fields/readForDocument.js.map +1 -1
- package/dist/goals/GoalSelectField.d.ts +7 -0
- package/dist/goals/GoalSelectField.js +77 -0
- package/dist/goals/GoalSelectField.js.map +1 -0
- package/dist/goals/collection.js +250 -0
- package/dist/goals/collection.js.map +1 -0
- package/dist/goals/currency.js +14 -0
- package/dist/goals/currency.js.map +1 -0
- package/dist/goals/fetchGoals.d.ts +18 -0
- package/dist/goals/fetchGoals.js +29 -0
- package/dist/goals/fetchGoals.js.map +1 -0
- package/dist/goals/goalField.d.ts +31 -0
- package/dist/goals/goalField.js +56 -0
- package/dist/goals/goalField.js.map +1 -0
- package/dist/goals/goalsEndpoint.d.ts +1 -0
- package/dist/goals/goalsEndpoint.js +55 -0
- package/dist/goals/goalsEndpoint.js.map +1 -0
- package/dist/goals/resolver.d.ts +8 -0
- package/dist/goals/resolver.js +147 -0
- package/dist/goals/resolver.js.map +1 -0
- package/dist/goals/trackGoalAction.d.ts +74 -0
- package/dist/goals/trackGoalAction.js +139 -0
- package/dist/goals/trackGoalAction.js.map +1 -0
- package/dist/goals/useAnalyticsGoals.d.ts +20 -0
- package/dist/goals/useAnalyticsGoals.js +67 -0
- package/dist/goals/useAnalyticsGoals.js.map +1 -0
- package/dist/index.d.ts +12 -3
- package/dist/index.js +75 -11
- 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/device.js +11 -3
- package/dist/native/ingest/device.js.map +1 -1
- package/dist/native/ingest/endpoint.d.ts +1 -0
- package/dist/native/ingest/endpoint.js +2 -17
- package/dist/native/ingest/endpoint.js.map +1 -1
- 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 +26 -0
- package/dist/native/ingest/normalizeEvent.js +35 -7
- 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.d.ts +20 -0
- package/dist/native/ingest/serverTrack.js +100 -0
- package/dist/native/ingest/serverTrack.js.map +1 -0
- 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/ingest/validate.js +27 -0
- package/dist/native/ingest/validate.js.map +1 -0
- package/dist/native/nativeAdapter.d.ts +1 -1
- package/dist/native/nativeAdapter.js +53 -36
- 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/retention/pruneTask.js +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 +52 -15
- 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/paths.js +5 -1
- package/dist/plugin/paths.js.map +1 -1
- package/dist/plugin/queryEndpoint.js +120 -0
- package/dist/plugin/queryEndpoint.js.map +1 -0
- package/dist/plugin/readContextForRequest.js +71 -0
- package/dist/plugin/readContextForRequest.js.map +1 -0
- package/dist/plugin/realtimeEndpoint.js +43 -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/runtime.js +31 -1
- package/dist/plugin/runtime.js.map +1 -1
- package/dist/plugin/scopeFieldBoot.js +22 -0
- package/dist/plugin/scopeFieldBoot.js.map +1 -0
- package/dist/plugin/sourcesEndpoint.js +21 -52
- 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 +29 -13
- 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/providers/resolver.js.map +1 -1
- package/dist/query/dates.js +64 -0
- package/dist/query/dates.js.map +1 -0
- package/dist/query/errors.d.ts +17 -0
- package/dist/query/errors.js +10 -0
- package/dist/query/errors.js.map +1 -0
- package/dist/query/fetchQuery.d.ts +59 -0
- package/dist/query/fetchQuery.js +110 -0
- package/dist/query/fetchQuery.js.map +1 -0
- package/dist/query/parse.d.ts +22 -0
- package/dist/query/parse.js +168 -0
- package/dist/query/parse.js.map +1 -0
- package/dist/query/response.d.ts +39 -0
- 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/timeframe/tz.js +17 -6
- package/dist/timeframe/tz.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 +127 -1
- package/dist/translations/ar.js.map +1 -1
- package/dist/translations/de.js +127 -1
- package/dist/translations/de.js.map +1 -1
- package/dist/translations/en.js +127 -1
- package/dist/translations/en.js.map +1 -1
- package/dist/translations/es.js +127 -1
- package/dist/translations/es.js.map +1 -1
- package/dist/translations/fr.js +127 -1
- package/dist/translations/fr.js.map +1 -1
- package/dist/translations/id.js +127 -1
- package/dist/translations/id.js.map +1 -1
- package/dist/translations/keys.d.ts +126 -0
- package/dist/translations/keys.js +127 -1
- package/dist/translations/keys.js.map +1 -1
- package/dist/translations/ko.js +127 -1
- package/dist/translations/ko.js.map +1 -1
- package/dist/translations/metricKeys.js +8 -2
- package/dist/translations/metricKeys.js.map +1 -1
- package/dist/translations/pt.js +127 -1
- package/dist/translations/pt.js.map +1 -1
- package/dist/translations/ru.js +127 -1
- package/dist/translations/ru.js.map +1 -1
- package/dist/translations/server.js.map +1 -1
- package/dist/translations/uk.js +127 -1
- package/dist/translations/uk.js.map +1 -1
- package/dist/translations/zh.js +127 -1
- package/dist/translations/zh.js.map +1 -1
- package/dist/view/AnalyticsNavLink.d.ts +19 -0
- package/dist/view/AnalyticsNavLink.js +40 -0
- package/dist/view/AnalyticsNavLink.js.map +1 -0
- package/dist/view/AnalyticsView.d.ts +23 -0
- package/dist/view/AnalyticsView.js +45 -0
- package/dist/view/AnalyticsView.js.map +1 -0
- package/dist/view/AnalyticsViewClient.d.ts +15 -0
- package/dist/view/AnalyticsViewClient.js +200 -0
- package/dist/view/AnalyticsViewClient.js.map +1 -0
- package/dist/view/authRedirect.js +32 -0
- package/dist/view/authRedirect.js.map +1 -0
- package/dist/view/conversionRate.js +18 -0
- package/dist/view/conversionRate.js.map +1 -0
- package/dist/view/dayRange.js +68 -0
- package/dist/view/dayRange.js.map +1 -0
- package/dist/view/gating.d.ts +1 -0
- package/dist/view/gating.js +142 -0
- package/dist/view/gating.js.map +1 -0
- package/dist/view/labels.js +64 -0
- package/dist/view/labels.js.map +1 -0
- package/dist/view/registerView.js +48 -0
- package/dist/view/registerView.js.map +1 -0
- package/dist/view/sections/Breakdowns.js +197 -0
- package/dist/view/sections/Breakdowns.js.map +1 -0
- package/dist/view/sections/EmptyStates.js +92 -0
- package/dist/view/sections/EmptyStates.js.map +1 -0
- package/dist/view/sections/FilterChips.js +46 -0
- package/dist/view/sections/FilterChips.js.map +1 -0
- package/dist/view/sections/GoalsPanel.js +82 -0
- package/dist/view/sections/GoalsPanel.js.map +1 -0
- package/dist/view/sections/OverviewCards.js +65 -0
- package/dist/view/sections/OverviewCards.js.map +1 -0
- package/dist/view/sections/RealtimeStrip.js +80 -0
- package/dist/view/sections/RealtimeStrip.js.map +1 -0
- package/dist/view/sections/Toolbar.js +186 -0
- package/dist/view/sections/Toolbar.js.map +1 -0
- package/dist/view/sections/Trend.js +125 -0
- package/dist/view/sections/Trend.js.map +1 -0
- package/dist/view/sections/styles.js +74 -0
- package/dist/view/sections/styles.js.map +1 -0
- package/dist/view/state.js +211 -0
- package/dist/view/state.js.map +1 -0
- package/dist/view/useViewQueries.js +153 -0
- package/dist/view/useViewQueries.js.map +1 -0
- package/dist/view/viewProps.d.ts +40 -0
- package/dist/view/viewProps.js +90 -0
- package/dist/view/viewProps.js.map +1 -0
- package/dist/widgets/AnalyticsBreakdownWidget.d.ts +2 -1
- package/dist/widgets/AnalyticsBreakdownWidget.js +78 -11
- package/dist/widgets/AnalyticsBreakdownWidget.js.map +1 -1
- package/dist/widgets/AnalyticsGoalsWidget.d.ts +14 -0
- package/dist/widgets/AnalyticsGoalsWidget.js +196 -0
- package/dist/widgets/AnalyticsGoalsWidget.js.map +1 -0
- package/dist/widgets/AnalyticsMetricWidget.d.ts +2 -1
- package/dist/widgets/AnalyticsMetricWidget.js +63 -13
- package/dist/widgets/AnalyticsMetricWidget.js.map +1 -1
- package/dist/widgets/AnalyticsRealtimeWidget.d.ts +2 -1
- package/dist/widgets/AnalyticsRealtimeWidget.js +44 -22
- package/dist/widgets/AnalyticsRealtimeWidget.js.map +1 -1
- package/dist/widgets/AnalyticsTrendWidget.d.ts +2 -1
- package/dist/widgets/AnalyticsTrendWidget.js +79 -16
- 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/WidgetViewLink.js +17 -0
- package/dist/widgets/WidgetViewLink.js.map +1 -0
- package/dist/widgets/breakdownTypes.js +28 -1
- package/dist/widgets/breakdownTypes.js.map +1 -1
- package/dist/widgets/cardChrome.js +31 -7
- package/dist/widgets/cardChrome.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/range.js +43 -12
- package/dist/widgets/range.js.map +1 -1
- package/dist/widgets/readForWidget.d.ts +24 -2
- package/dist/widgets/readForWidget.js +34 -48
- package/dist/widgets/readForWidget.js.map +1 -1
- package/dist/widgets/readForWidgetBreakdown.d.ts +32 -1
- package/dist/widgets/readForWidgetBreakdown.js +32 -42
- package/dist/widgets/readForWidgetBreakdown.js.map +1 -1
- package/dist/widgets/readForWidgetGoals.d.ts +67 -0
- package/dist/widgets/readForWidgetGoals.js +140 -0
- package/dist/widgets/readForWidgetGoals.js.map +1 -0
- 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 +24 -1
- package/dist/widgets/readForWidgetSeries.js +63 -56
- 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 +53 -7
- package/dist/widgets/registerWidgets.js.map +1 -1
- package/dist/widgets/types.d.ts +15 -0
- package/dist/widgets/types.js +12 -1
- package/dist/widgets/types.js.map +1 -1
- package/dist/widgets/viewLink.d.ts +22 -0
- package/dist/widgets/viewLink.js +86 -0
- package/dist/widgets/viewLink.js.map +1 -0
- package/package.json +9 -4
- 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 -19
- package/dist/native/rollups/insertIfNew.js.map +0 -1
|
@@ -1,15 +1,27 @@
|
|
|
1
1
|
import { AnalyticsFilter, DateRange, MetricKey } from "../core/contract.js";
|
|
2
2
|
import { TimeframePreset } from "../timeframe/presets.js";
|
|
3
|
+
import { WidgetReadStatus } from "./prepareWidgetRead.js";
|
|
3
4
|
import { PayloadRequest } from "payload";
|
|
4
5
|
|
|
5
6
|
//#region src/widgets/readForWidget.d.ts
|
|
6
|
-
type WidgetReadStatus = 'ok' | 'not-configured' | 'unavailable';
|
|
7
7
|
interface WidgetReadResult {
|
|
8
8
|
status: WidgetReadStatus;
|
|
9
9
|
adapterId: string;
|
|
10
10
|
dateRange: DateRange;
|
|
11
11
|
metrics: Partial<Record<MetricKey, number>>;
|
|
12
|
+
/** The source that answered, absent on a read that never reached one. */
|
|
13
|
+
provider?: string;
|
|
12
14
|
clamped?: boolean;
|
|
15
|
+
/** True when the engine served a stale cache entry after a failed provider read. */
|
|
16
|
+
stale?: boolean;
|
|
17
|
+
/** True when the source answered without one of the filters the read carried. */
|
|
18
|
+
filtersUnapplied?: boolean;
|
|
19
|
+
/** True when the read hit the source's event scan cap, so the numbers are a floor. */
|
|
20
|
+
sampled?: boolean;
|
|
21
|
+
/** True when the source could not read the scope's goals, so a conversions total means nothing. */
|
|
22
|
+
goalsUnresolved?: boolean;
|
|
23
|
+
/** The read asked about goals and the scope has none configured. */
|
|
24
|
+
noGoals?: boolean;
|
|
13
25
|
/** Previous-window totals, present only when the adapter supports comparison. */
|
|
14
26
|
previousMetrics?: Partial<Record<MetricKey, number>>;
|
|
15
27
|
/** The previous comparable window, present only when comparison ran. */
|
|
@@ -24,9 +36,19 @@ interface ReadForWidgetArgs {
|
|
|
24
36
|
range?: DateRange;
|
|
25
37
|
/** Explicit scope override; omitted resolves via the plugin's scopeResolver. */
|
|
26
38
|
scope?: string | null;
|
|
39
|
+
/**
|
|
40
|
+
* Reporting timezone the caller already resolved, reused rather than resolved again so
|
|
41
|
+
* a caller-supplied `range` is read in the very timezone it was interpreted in.
|
|
42
|
+
*/
|
|
43
|
+
timezone?: string;
|
|
27
44
|
filters?: AnalyticsFilter[];
|
|
45
|
+
/**
|
|
46
|
+
* Read the previous window too, where the install and the source both allow it. Defaults
|
|
47
|
+
* to on; a caller that never renders a delta passes false to save the second read.
|
|
48
|
+
*/
|
|
49
|
+
comparison?: boolean;
|
|
28
50
|
}
|
|
29
51
|
declare const readForWidget: (args: ReadForWidgetArgs) => Promise<WidgetReadResult>;
|
|
30
52
|
//#endregion
|
|
31
|
-
export { ReadForWidgetArgs, WidgetReadResult,
|
|
53
|
+
export { ReadForWidgetArgs, WidgetReadResult, readForWidget };
|
|
32
54
|
//# sourceMappingURL=readForWidget.d.ts.map
|
|
@@ -1,70 +1,55 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { previousWindow } from "./comparison.js";
|
|
1
|
+
import { comparisonOf } from "../core/capabilities.js";
|
|
2
|
+
import { previousWindow, withinLookback } from "./comparison.js";
|
|
3
|
+
import { prepareWidgetRead } from "./prepareWidgetRead.js";
|
|
4
|
+
import { readMeta } from "./readMeta.js";
|
|
6
5
|
//#region src/widgets/readForWidget.ts
|
|
7
6
|
const readForWidget = async (args) => {
|
|
8
7
|
const { req, metrics, timeframe, adapterId, now, range, filters } = args;
|
|
9
8
|
const emptyMetrics = {};
|
|
10
|
-
const
|
|
11
|
-
if (!runtime) return {
|
|
12
|
-
status: "unavailable",
|
|
13
|
-
adapterId: adapterId ?? "",
|
|
14
|
-
dateRange: range ?? resolveTimeframe(timeframe, now),
|
|
15
|
-
metrics: emptyMetrics
|
|
16
|
-
};
|
|
17
|
-
const ctx = await resolveReadContext({
|
|
18
|
-
runtime,
|
|
9
|
+
const prepared = await prepareWidgetRead({
|
|
19
10
|
req,
|
|
11
|
+
now,
|
|
12
|
+
timeframe,
|
|
20
13
|
adapterId,
|
|
21
|
-
scope: args.scope
|
|
14
|
+
scope: args.scope,
|
|
15
|
+
timezone: args.timezone,
|
|
16
|
+
range,
|
|
17
|
+
filters,
|
|
18
|
+
requires: { metrics },
|
|
19
|
+
goalRead: () => ({ metrics })
|
|
22
20
|
});
|
|
23
|
-
if (!
|
|
24
|
-
status:
|
|
25
|
-
adapterId: adapterId
|
|
26
|
-
dateRange:
|
|
21
|
+
if (!prepared.ok) return {
|
|
22
|
+
status: prepared.status,
|
|
23
|
+
adapterId: prepared.adapterId,
|
|
24
|
+
dateRange: prepared.dateRange,
|
|
27
25
|
metrics: emptyMetrics
|
|
28
26
|
};
|
|
29
|
-
const
|
|
30
|
-
const dateRange = range ?? resolveTimeframe(timeframe, now, tz);
|
|
27
|
+
const { runtime, adapter, tz, dateRange } = prepared;
|
|
31
28
|
const base = {
|
|
32
29
|
dateRange,
|
|
33
30
|
metrics: emptyMetrics
|
|
34
31
|
};
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
if (!satisfiesCapabilities(adapter.capabilities, {
|
|
32
|
+
const previousRange = args.comparison !== false && runtime.comparison && comparisonOf(adapter.capabilities) ? previousWindow(dateRange, tz) : null;
|
|
33
|
+
const comparisonRange = previousRange && withinLookback(previousRange, adapter.capabilities.maxLookbackDays, {
|
|
34
|
+
tz,
|
|
35
|
+
now
|
|
36
|
+
}) ? previousRange : void 0;
|
|
37
|
+
const readBase = {
|
|
42
38
|
metrics,
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
} : {}
|
|
47
|
-
})) return {
|
|
48
|
-
status: "unavailable",
|
|
49
|
-
adapterId: adapter.id,
|
|
50
|
-
...base
|
|
39
|
+
filters,
|
|
40
|
+
timezone: tz,
|
|
41
|
+
scope: prepared.queryScope,
|
|
42
|
+
...prepared.goalSlugs === void 0 ? {} : { goalSlugs: prepared.goalSlugs }
|
|
51
43
|
};
|
|
52
|
-
const comparisonRange = runtime.comparison && adapter.capabilities.comparison ? previousWindow(dateRange, tz) ?? void 0 : void 0;
|
|
53
44
|
let result;
|
|
54
45
|
let previous;
|
|
55
46
|
try {
|
|
56
47
|
[result, previous] = await Promise.all([runtime.engine.read(adapter, {
|
|
57
|
-
|
|
58
|
-
dateRange
|
|
59
|
-
filters,
|
|
60
|
-
timezone: tz,
|
|
61
|
-
scope: ctx.queryScope
|
|
48
|
+
...readBase,
|
|
49
|
+
dateRange
|
|
62
50
|
}), comparisonRange ? runtime.engine.read(adapter, {
|
|
63
|
-
|
|
64
|
-
dateRange: comparisonRange
|
|
65
|
-
filters,
|
|
66
|
-
timezone: tz,
|
|
67
|
-
scope: ctx.queryScope
|
|
51
|
+
...readBase,
|
|
52
|
+
dateRange: comparisonRange
|
|
68
53
|
}) : void 0]);
|
|
69
54
|
} catch {
|
|
70
55
|
return {
|
|
@@ -79,7 +64,8 @@ const readForWidget = async (args) => {
|
|
|
79
64
|
adapterId: adapter.id,
|
|
80
65
|
dateRange,
|
|
81
66
|
metrics: result.totals ?? {},
|
|
82
|
-
|
|
67
|
+
...readMeta(result),
|
|
68
|
+
noGoals: Array.isArray(prepared.goalSlugs) && prepared.goalSlugs.length === 0,
|
|
83
69
|
previousMetrics,
|
|
84
70
|
comparisonRange
|
|
85
71
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"readForWidget.js","names":[],"sources":["../../src/widgets/readForWidget.ts"],"sourcesContent":["import type { PayloadRequest } from 'payload'\nimport {
|
|
1
|
+
{"version":3,"file":"readForWidget.js","names":[],"sources":["../../src/widgets/readForWidget.ts"],"sourcesContent":["import type { PayloadRequest } from 'payload'\nimport { comparisonOf } from '../core/capabilities'\nimport type { AnalyticsFilter, AnalyticsResult, DateRange, MetricKey } from '../core/contract'\nimport type { TimeframePreset } from '../timeframe/presets'\nimport { previousWindow, withinLookback } from './comparison'\nimport { prepareWidgetRead, type WidgetReadStatus } from './prepareWidgetRead'\nimport { readMeta } from './readMeta'\n\nexport type { WidgetReadStatus } from './prepareWidgetRead'\n\nexport interface WidgetReadResult {\n\tstatus: WidgetReadStatus\n\tadapterId: string\n\tdateRange: DateRange\n\tmetrics: Partial<Record<MetricKey, number>>\n\t/** The source that answered, absent on a read that never reached one. */\n\tprovider?: string\n\tclamped?: boolean\n\t/** True when the engine served a stale cache entry after a failed provider read. */\n\tstale?: boolean\n\t/** True when the source answered without one of the filters the read carried. */\n\tfiltersUnapplied?: boolean\n\t/** True when the read hit the source's event scan cap, so the numbers are a floor. */\n\tsampled?: boolean\n\t/** True when the source could not read the scope's goals, so a conversions total means nothing. */\n\tgoalsUnresolved?: boolean\n\t/** The read asked about goals and the scope has none configured. */\n\tnoGoals?: boolean\n\t/** Previous-window totals, present only when the adapter supports comparison. */\n\tpreviousMetrics?: Partial<Record<MetricKey, number>>\n\t/** The previous comparable window, present only when comparison ran. */\n\tcomparisonRange?: DateRange\n}\n\nexport interface ReadForWidgetArgs {\n\treq: PayloadRequest\n\tmetrics: MetricKey[]\n\ttimeframe: TimeframePreset\n\tadapterId?: string\n\tnow: Date\n\trange?: DateRange\n\t/** Explicit scope override; omitted resolves via the plugin's scopeResolver. */\n\tscope?: string | null\n\t/**\n\t * Reporting timezone the caller already resolved, reused rather than resolved again so\n\t * a caller-supplied `range` is read in the very timezone it was interpreted in.\n\t */\n\ttimezone?: string\n\tfilters?: AnalyticsFilter[]\n\t/**\n\t * Read the previous window too, where the install and the source both allow it. Defaults\n\t * to on; a caller that never renders a delta passes false to save the second read.\n\t */\n\tcomparison?: boolean\n}\n\nexport const readForWidget = async (args: ReadForWidgetArgs): Promise<WidgetReadResult> => {\n\tconst { req, metrics, timeframe, adapterId, now, range, filters } = args\n\tconst emptyMetrics = {} as Partial<Record<MetricKey, number>>\n\n\tconst prepared = await prepareWidgetRead({\n\t\treq,\n\t\tnow,\n\t\ttimeframe,\n\t\tadapterId,\n\t\tscope: args.scope,\n\t\ttimezone: args.timezone,\n\t\trange,\n\t\tfilters,\n\t\trequires: { metrics },\n\t\tgoalRead: () => ({ metrics }),\n\t})\n\tif (!prepared.ok) {\n\t\treturn {\n\t\t\tstatus: prepared.status,\n\t\t\tadapterId: prepared.adapterId,\n\t\t\tdateRange: prepared.dateRange,\n\t\t\tmetrics: emptyMetrics,\n\t\t}\n\t}\n\tconst { runtime, adapter, tz, dateRange } = prepared\n\tconst base = { dateRange, metrics: emptyMetrics }\n\n\tconst previousRange =\n\t\targs.comparison !== false && runtime.comparison && comparisonOf(adapter.capabilities)\n\t\t\t? previousWindow(dateRange, tz)\n\t\t\t: null\n\tconst comparisonRange =\n\t\tpreviousRange &&\n\t\twithinLookback(previousRange, adapter.capabilities.maxLookbackDays, { tz, now })\n\t\t\t? previousRange\n\t\t\t: undefined\n\tconst readBase = {\n\t\tmetrics,\n\t\tfilters,\n\t\ttimezone: tz,\n\t\tscope: prepared.queryScope,\n\t\t...(prepared.goalSlugs === undefined ? {} : { goalSlugs: prepared.goalSlugs }),\n\t}\n\tlet result: AnalyticsResult\n\tlet previous: AnalyticsResult | undefined\n\ttry {\n\t\t;[result, previous] = await Promise.all([\n\t\t\truntime.engine.read(adapter, { ...readBase, dateRange }),\n\t\t\tcomparisonRange\n\t\t\t\t? runtime.engine.read(adapter, { ...readBase, dateRange: comparisonRange })\n\t\t\t\t: undefined,\n\t\t])\n\t} catch {\n\t\t// No cache entry (fresh or stale) survived the failed read; degrade like an\n\t\t// unsupported capability instead of throwing through the widget render tree.\n\t\treturn { status: 'unavailable', adapterId: adapter.id, ...base }\n\t}\n\tconst previousMetrics = previous ? (previous.totals ?? {}) : undefined\n\treturn {\n\t\tstatus: 'ok',\n\t\tadapterId: adapter.id,\n\t\tdateRange,\n\t\tmetrics: result.totals ?? {},\n\t\t...readMeta(result),\n\t\tnoGoals: Array.isArray(prepared.goalSlugs) && prepared.goalSlugs.length === 0,\n\t\tpreviousMetrics,\n\t\tcomparisonRange,\n\t}\n}\n"],"mappings":";;;;;AAwDA,MAAa,gBAAgB,OAAO,SAAuD;CAC1F,MAAM,EAAE,KAAK,SAAS,WAAW,WAAW,KAAK,OAAO,YAAY;CACpE,MAAM,eAAe,CAAC;CAEtB,MAAM,WAAW,MAAM,kBAAkB;EACxC;EACA;EACA;EACA;EACA,OAAO,KAAK;EACZ,UAAU,KAAK;EACf;EACA;EACA,UAAU,EAAE,QAAQ;EACpB,iBAAiB,EAAE,QAAQ;CAC5B,CAAC;CACD,IAAI,CAAC,SAAS,IACb,OAAO;EACN,QAAQ,SAAS;EACjB,WAAW,SAAS;EACpB,WAAW,SAAS;EACpB,SAAS;CACV;CAED,MAAM,EAAE,SAAS,SAAS,IAAI,cAAc;CAC5C,MAAM,OAAO;EAAE;EAAW,SAAS;CAAa;CAEhD,MAAM,gBACL,KAAK,eAAe,SAAS,QAAQ,cAAc,aAAa,QAAQ,YAAY,IACjF,eAAe,WAAW,EAAE,IAC5B;CACJ,MAAM,kBACL,iBACA,eAAe,eAAe,QAAQ,aAAa,iBAAiB;EAAE;EAAI;CAAI,CAAC,IAC5E,gBACA,KAAA;CACJ,MAAM,WAAW;EAChB;EACA;EACA,UAAU;EACV,OAAO,SAAS;EAChB,GAAI,SAAS,cAAc,KAAA,IAAY,CAAC,IAAI,EAAE,WAAW,SAAS,UAAU;CAC7E;CACA,IAAI;CACJ,IAAI;CACJ,IAAI;EACF,CAAC,QAAQ,YAAY,MAAM,QAAQ,IAAI,CACvC,QAAQ,OAAO,KAAK,SAAS;GAAE,GAAG;GAAU;EAAU,CAAC,GACvD,kBACG,QAAQ,OAAO,KAAK,SAAS;GAAE,GAAG;GAAU,WAAW;EAAgB,CAAC,IACxE,KAAA,CACJ,CAAC;CACF,QAAQ;EAGP,OAAO;GAAE,QAAQ;GAAe,WAAW,QAAQ;GAAI,GAAG;EAAK;CAChE;CACA,MAAM,kBAAkB,WAAY,SAAS,UAAU,CAAC,IAAK,KAAA;CAC7D,OAAO;EACN,QAAQ;EACR,WAAW,QAAQ;EACnB;EACA,SAAS,OAAO,UAAU,CAAC;EAC3B,GAAG,SAAS,MAAM;EAClB,SAAS,MAAM,QAAQ,SAAS,SAAS,KAAK,SAAS,UAAU,WAAW;EAC5E;EACA;CACD;AACD"}
|
|
@@ -1,19 +1,33 @@
|
|
|
1
1
|
import { AnalyticsFilter, DateRange, DimensionKey, MetricKey } from "../core/contract.js";
|
|
2
2
|
import { TimeframePreset } from "../timeframe/presets.js";
|
|
3
|
-
import { WidgetReadStatus } from "./
|
|
3
|
+
import { WidgetReadStatus } from "./prepareWidgetRead.js";
|
|
4
4
|
import { PayloadRequest } from "payload";
|
|
5
5
|
|
|
6
6
|
//#region src/widgets/readForWidgetBreakdown.d.ts
|
|
7
7
|
interface BreakdownRow {
|
|
8
8
|
label: string;
|
|
9
9
|
value: number;
|
|
10
|
+
/** Every metric the read asked for, present only when `extraMetrics` asked for more. */
|
|
11
|
+
metrics?: Partial<Record<MetricKey, number>>;
|
|
10
12
|
}
|
|
11
13
|
interface WidgetBreakdownResult {
|
|
12
14
|
status: WidgetReadStatus;
|
|
13
15
|
adapterId: string;
|
|
14
16
|
dateRange: DateRange;
|
|
15
17
|
rows: BreakdownRow[];
|
|
18
|
+
/** The source that answered, absent on a read that never reached one. */
|
|
19
|
+
provider?: string;
|
|
16
20
|
clamped?: boolean;
|
|
21
|
+
/** True when the engine served a stale cache entry after a failed provider read. */
|
|
22
|
+
stale?: boolean;
|
|
23
|
+
/** True when the source answered without one of the filters the read carried. */
|
|
24
|
+
filtersUnapplied?: boolean;
|
|
25
|
+
/** True when the read hit the source's event scan cap, so the numbers are a floor. */
|
|
26
|
+
sampled?: boolean;
|
|
27
|
+
/** True when the source could not read the scope's goals; the rows say nothing about them. */
|
|
28
|
+
goalsUnresolved?: boolean;
|
|
29
|
+
/** True when the read asked about goals and the scope configures none: an empty table. */
|
|
30
|
+
noGoals?: boolean;
|
|
17
31
|
}
|
|
18
32
|
interface ReadForWidgetBreakdownArgs {
|
|
19
33
|
req: PayloadRequest;
|
|
@@ -26,7 +40,24 @@ interface ReadForWidgetBreakdownArgs {
|
|
|
26
40
|
range?: DateRange;
|
|
27
41
|
/** Explicit scope override; omitted resolves via the plugin's scopeResolver. */
|
|
28
42
|
scope?: string | null;
|
|
43
|
+
/**
|
|
44
|
+
* Reporting timezone the caller already resolved, reused rather than resolved again so
|
|
45
|
+
* a caller-supplied `range` is read in the very timezone it was interpreted in.
|
|
46
|
+
*/
|
|
47
|
+
timezone?: string;
|
|
29
48
|
filters?: AnalyticsFilter[];
|
|
49
|
+
/**
|
|
50
|
+
* Further metrics to read alongside the ranked one, narrowed to what the source serves
|
|
51
|
+
* so an unsupported one never turns the whole read unavailable. They reach each row's
|
|
52
|
+
* `metrics`; ranking and status still follow `metric` alone.
|
|
53
|
+
*/
|
|
54
|
+
extraMetrics?: MetricKey[];
|
|
55
|
+
/**
|
|
56
|
+
* The scope's goal slugs, for a `goal` or `conversions` read, or `'unresolved'` when the
|
|
57
|
+
* caller's own resolver failed. Resolved here when omitted, so a caller that already
|
|
58
|
+
* resolved them (the goals table) does not resolve them twice.
|
|
59
|
+
*/
|
|
60
|
+
goalSlugs?: string[] | 'unresolved';
|
|
30
61
|
}
|
|
31
62
|
/**
|
|
32
63
|
* Site-wide ranked breakdown of one metric by one dimension, read through the engine.
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { resolveReadContext } from "../core/scopedRead.js";
|
|
4
|
-
import { resolveTimeframe } from "../timeframe/presets.js";
|
|
1
|
+
import { prepareWidgetRead } from "./prepareWidgetRead.js";
|
|
2
|
+
import { readMeta } from "./readMeta.js";
|
|
5
3
|
//#region src/widgets/readForWidgetBreakdown.ts
|
|
6
4
|
/**
|
|
7
5
|
* Site-wide ranked breakdown of one metric by one dimension, read through the engine.
|
|
@@ -11,53 +9,42 @@ import { resolveTimeframe } from "../timeframe/presets.js";
|
|
|
11
9
|
const readForWidgetBreakdown = async (args) => {
|
|
12
10
|
const { req, metric, dimension, timeframe, limit, adapterId, now, range, filters } = args;
|
|
13
11
|
const emptyRows = [];
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
status: "unavailable",
|
|
17
|
-
adapterId: adapterId ?? "",
|
|
18
|
-
dateRange: range ?? resolveTimeframe(timeframe, now),
|
|
19
|
-
rows: emptyRows
|
|
20
|
-
};
|
|
21
|
-
const ctx = await resolveReadContext({
|
|
22
|
-
runtime,
|
|
12
|
+
const metricsFor = (adapter) => [metric, ...(args.extraMetrics ?? []).filter((m) => m !== metric && adapter.capabilities.metrics.has(m))];
|
|
13
|
+
const prepared = await prepareWidgetRead({
|
|
23
14
|
req,
|
|
15
|
+
now,
|
|
16
|
+
timeframe,
|
|
24
17
|
adapterId,
|
|
25
|
-
scope: args.scope
|
|
18
|
+
scope: args.scope,
|
|
19
|
+
timezone: args.timezone,
|
|
20
|
+
range,
|
|
21
|
+
filters,
|
|
22
|
+
requires: {
|
|
23
|
+
metrics: [metric],
|
|
24
|
+
dimensions: [dimension]
|
|
25
|
+
},
|
|
26
|
+
goalRead: (adapter) => ({
|
|
27
|
+
metrics: metricsFor(adapter),
|
|
28
|
+
dimensions: [dimension]
|
|
29
|
+
}),
|
|
30
|
+
goalSlugs: args.goalSlugs
|
|
26
31
|
});
|
|
27
|
-
if (!
|
|
28
|
-
status:
|
|
29
|
-
adapterId: adapterId
|
|
30
|
-
dateRange:
|
|
32
|
+
if (!prepared.ok) return {
|
|
33
|
+
status: prepared.status,
|
|
34
|
+
adapterId: prepared.adapterId,
|
|
35
|
+
dateRange: prepared.dateRange,
|
|
31
36
|
rows: emptyRows
|
|
32
37
|
};
|
|
33
|
-
const
|
|
34
|
-
const dateRange = range ?? resolveTimeframe(timeframe, now, tz);
|
|
38
|
+
const { runtime, adapter, tz, dateRange, goalSlugs } = prepared;
|
|
35
39
|
const base = {
|
|
36
40
|
dateRange,
|
|
37
41
|
rows: emptyRows
|
|
38
42
|
};
|
|
39
|
-
const
|
|
40
|
-
if (!adapter.isConfigured()) return {
|
|
41
|
-
status: "not-configured",
|
|
42
|
-
adapterId: adapter.id,
|
|
43
|
-
...base
|
|
44
|
-
};
|
|
45
|
-
if (!satisfiesCapabilities(adapter.capabilities, {
|
|
46
|
-
metrics: [metric],
|
|
47
|
-
dimensions: [dimension],
|
|
48
|
-
...filters && filters.length > 0 ? {
|
|
49
|
-
filters: filters.map((f) => f.dimension),
|
|
50
|
-
filterOperators: filters.map((f) => f.operator)
|
|
51
|
-
} : {}
|
|
52
|
-
})) return {
|
|
53
|
-
status: "unavailable",
|
|
54
|
-
adapterId: adapter.id,
|
|
55
|
-
...base
|
|
56
|
-
};
|
|
43
|
+
const metrics = metricsFor(adapter);
|
|
57
44
|
let result;
|
|
58
45
|
try {
|
|
59
46
|
result = await runtime.engine.read(adapter, {
|
|
60
|
-
metrics
|
|
47
|
+
metrics,
|
|
61
48
|
dimensions: [dimension],
|
|
62
49
|
dateRange,
|
|
63
50
|
limit,
|
|
@@ -67,7 +54,8 @@ const readForWidgetBreakdown = async (args) => {
|
|
|
67
54
|
},
|
|
68
55
|
filters,
|
|
69
56
|
timezone: tz,
|
|
70
|
-
scope:
|
|
57
|
+
scope: prepared.queryScope,
|
|
58
|
+
...goalSlugs === void 0 ? {} : { goalSlugs }
|
|
71
59
|
});
|
|
72
60
|
} catch {
|
|
73
61
|
return {
|
|
@@ -78,14 +66,16 @@ const readForWidgetBreakdown = async (args) => {
|
|
|
78
66
|
}
|
|
79
67
|
const rows = result.rows.map((row) => ({
|
|
80
68
|
label: row.dimensions?.[dimension] ?? "(none)",
|
|
81
|
-
value: row.metrics[metric] ?? 0
|
|
69
|
+
value: row.metrics[metric] ?? 0,
|
|
70
|
+
...metrics.length > 1 ? { metrics: row.metrics } : {}
|
|
82
71
|
}));
|
|
83
72
|
return {
|
|
84
73
|
status: "ok",
|
|
85
74
|
adapterId: adapter.id,
|
|
86
75
|
dateRange,
|
|
87
76
|
rows,
|
|
88
|
-
|
|
77
|
+
...readMeta(result),
|
|
78
|
+
noGoals: Array.isArray(goalSlugs) && goalSlugs.length === 0
|
|
89
79
|
};
|
|
90
80
|
};
|
|
91
81
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"readForWidgetBreakdown.js","names":[],"sources":["../../src/widgets/readForWidgetBreakdown.ts"],"sourcesContent":["import type { PayloadRequest } from 'payload'\nimport
|
|
1
|
+
{"version":3,"file":"readForWidgetBreakdown.js","names":[],"sources":["../../src/widgets/readForWidgetBreakdown.ts"],"sourcesContent":["import type { PayloadRequest } from 'payload'\nimport type {\n\tAnalyticsAdapter,\n\tAnalyticsFilter,\n\tAnalyticsResult,\n\tDateRange,\n\tDimensionKey,\n\tMetricKey,\n} from '../core/contract'\nimport type { TimeframePreset } from '../timeframe/presets'\nimport { prepareWidgetRead, type WidgetReadStatus } from './prepareWidgetRead'\nimport { readMeta } from './readMeta'\n\nexport interface BreakdownRow {\n\tlabel: string\n\tvalue: number\n\t/** Every metric the read asked for, present only when `extraMetrics` asked for more. */\n\tmetrics?: Partial<Record<MetricKey, number>>\n}\n\nexport interface WidgetBreakdownResult {\n\tstatus: WidgetReadStatus\n\tadapterId: string\n\tdateRange: DateRange\n\trows: BreakdownRow[]\n\t/** The source that answered, absent on a read that never reached one. */\n\tprovider?: string\n\tclamped?: boolean\n\t/** True when the engine served a stale cache entry after a failed provider read. */\n\tstale?: boolean\n\t/** True when the source answered without one of the filters the read carried. */\n\tfiltersUnapplied?: boolean\n\t/** True when the read hit the source's event scan cap, so the numbers are a floor. */\n\tsampled?: boolean\n\t/** True when the source could not read the scope's goals; the rows say nothing about them. */\n\tgoalsUnresolved?: boolean\n\t/** True when the read asked about goals and the scope configures none: an empty table. */\n\tnoGoals?: boolean\n}\n\nexport interface ReadForWidgetBreakdownArgs {\n\treq: PayloadRequest\n\tmetric: MetricKey\n\tdimension: DimensionKey\n\ttimeframe: TimeframePreset\n\tlimit: number\n\tadapterId?: string\n\tnow: Date\n\trange?: DateRange\n\t/** Explicit scope override; omitted resolves via the plugin's scopeResolver. */\n\tscope?: string | null\n\t/**\n\t * Reporting timezone the caller already resolved, reused rather than resolved again so\n\t * a caller-supplied `range` is read in the very timezone it was interpreted in.\n\t */\n\ttimezone?: string\n\tfilters?: AnalyticsFilter[]\n\t/**\n\t * Further metrics to read alongside the ranked one, narrowed to what the source serves\n\t * so an unsupported one never turns the whole read unavailable. They reach each row's\n\t * `metrics`; ranking and status still follow `metric` alone.\n\t */\n\textraMetrics?: MetricKey[]\n\t/**\n\t * The scope's goal slugs, for a `goal` or `conversions` read, or `'unresolved'` when the\n\t * caller's own resolver failed. Resolved here when omitted, so a caller that already\n\t * resolved them (the goals table) does not resolve them twice.\n\t */\n\tgoalSlugs?: string[] | 'unresolved'\n}\n\n/**\n * Site-wide ranked breakdown of one metric by one dimension, read through the engine.\n * Mirrors `readForWidget` but gates on the dimension too and maps each result row to a\n * `{ label, value }` pair the bar list renders. The adapter owns sorting and limiting.\n */\nexport const readForWidgetBreakdown = async (\n\targs: ReadForWidgetBreakdownArgs\n): Promise<WidgetBreakdownResult> => {\n\tconst { req, metric, dimension, timeframe, limit, adapterId, now, range, filters } = args\n\tconst emptyRows = [] as BreakdownRow[]\n\n\tconst metricsFor = (adapter: AnalyticsAdapter): MetricKey[] => [\n\t\tmetric,\n\t\t...(args.extraMetrics ?? []).filter((m) => m !== metric && adapter.capabilities.metrics.has(m)),\n\t]\n\tconst prepared = await prepareWidgetRead({\n\t\treq,\n\t\tnow,\n\t\ttimeframe,\n\t\tadapterId,\n\t\tscope: args.scope,\n\t\ttimezone: args.timezone,\n\t\trange,\n\t\tfilters,\n\t\trequires: { metrics: [metric], dimensions: [dimension] },\n\t\tgoalRead: (adapter) => ({ metrics: metricsFor(adapter), dimensions: [dimension] }),\n\t\tgoalSlugs: args.goalSlugs,\n\t})\n\tif (!prepared.ok) {\n\t\treturn {\n\t\t\tstatus: prepared.status,\n\t\t\tadapterId: prepared.adapterId,\n\t\t\tdateRange: prepared.dateRange,\n\t\t\trows: emptyRows,\n\t\t}\n\t}\n\tconst { runtime, adapter, tz, dateRange, goalSlugs } = prepared\n\tconst base = { dateRange, rows: emptyRows }\n\tconst metrics = metricsFor(adapter)\n\tlet result: AnalyticsResult\n\ttry {\n\t\tresult = await runtime.engine.read(adapter, {\n\t\t\tmetrics,\n\t\t\tdimensions: [dimension],\n\t\t\tdateRange,\n\t\t\tlimit,\n\t\t\torder: { metric, direction: 'desc' },\n\t\t\tfilters,\n\t\t\ttimezone: tz,\n\t\t\tscope: prepared.queryScope,\n\t\t\t...(goalSlugs === undefined ? {} : { goalSlugs }),\n\t\t})\n\t} catch {\n\t\t// No cache entry (fresh or stale) survived the failed read; degrade like an\n\t\t// unsupported capability instead of throwing through the widget render tree.\n\t\treturn { status: 'unavailable', adapterId: adapter.id, ...base }\n\t}\n\tconst rows = result.rows.map((row) => ({\n\t\tlabel: row.dimensions?.[dimension] ?? '(none)',\n\t\tvalue: row.metrics[metric] ?? 0,\n\t\t...(metrics.length > 1 ? { metrics: row.metrics } : {}),\n\t}))\n\treturn {\n\t\tstatus: 'ok',\n\t\tadapterId: adapter.id,\n\t\tdateRange,\n\t\trows,\n\t\t...readMeta(result),\n\t\t// The hint is set only for a read about goals, so an empty one is a scope that\n\t\t// configured none rather than a read that never asked.\n\t\tnoGoals: Array.isArray(goalSlugs) && goalSlugs.length === 0,\n\t}\n}\n"],"mappings":";;;;;;;;AA4EA,MAAa,yBAAyB,OACrC,SACoC;CACpC,MAAM,EAAE,KAAK,QAAQ,WAAW,WAAW,OAAO,WAAW,KAAK,OAAO,YAAY;CACrF,MAAM,YAAY,CAAC;CAEnB,MAAM,cAAc,YAA2C,CAC9D,QACA,IAAI,KAAK,gBAAgB,CAAC,GAAG,QAAQ,MAAM,MAAM,UAAU,QAAQ,aAAa,QAAQ,IAAI,CAAC,CAAC,CAC/F;CACA,MAAM,WAAW,MAAM,kBAAkB;EACxC;EACA;EACA;EACA;EACA,OAAO,KAAK;EACZ,UAAU,KAAK;EACf;EACA;EACA,UAAU;GAAE,SAAS,CAAC,MAAM;GAAG,YAAY,CAAC,SAAS;EAAE;EACvD,WAAW,aAAa;GAAE,SAAS,WAAW,OAAO;GAAG,YAAY,CAAC,SAAS;EAAE;EAChF,WAAW,KAAK;CACjB,CAAC;CACD,IAAI,CAAC,SAAS,IACb,OAAO;EACN,QAAQ,SAAS;EACjB,WAAW,SAAS;EACpB,WAAW,SAAS;EACpB,MAAM;CACP;CAED,MAAM,EAAE,SAAS,SAAS,IAAI,WAAW,cAAc;CACvD,MAAM,OAAO;EAAE;EAAW,MAAM;CAAU;CAC1C,MAAM,UAAU,WAAW,OAAO;CAClC,IAAI;CACJ,IAAI;EACH,SAAS,MAAM,QAAQ,OAAO,KAAK,SAAS;GAC3C;GACA,YAAY,CAAC,SAAS;GACtB;GACA;GACA,OAAO;IAAE;IAAQ,WAAW;GAAO;GACnC;GACA,UAAU;GACV,OAAO,SAAS;GAChB,GAAI,cAAc,KAAA,IAAY,CAAC,IAAI,EAAE,UAAU;EAChD,CAAC;CACF,QAAQ;EAGP,OAAO;GAAE,QAAQ;GAAe,WAAW,QAAQ;GAAI,GAAG;EAAK;CAChE;CACA,MAAM,OAAO,OAAO,KAAK,KAAK,SAAS;EACtC,OAAO,IAAI,aAAa,cAAc;EACtC,OAAO,IAAI,QAAQ,WAAW;EAC9B,GAAI,QAAQ,SAAS,IAAI,EAAE,SAAS,IAAI,QAAQ,IAAI,CAAC;CACtD,EAAE;CACF,OAAO;EACN,QAAQ;EACR,WAAW,QAAQ;EACnB;EACA;EACA,GAAG,SAAS,MAAM;EAGlB,SAAS,MAAM,QAAQ,SAAS,KAAK,UAAU,WAAW;CAC3D;AACD"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { DateRange } from "../core/contract.js";
|
|
2
|
+
import { TimeframePreset } from "../timeframe/presets.js";
|
|
3
|
+
import { WidgetReadStatus } from "./prepareWidgetRead.js";
|
|
4
|
+
import { PayloadRequest } from "payload";
|
|
5
|
+
|
|
6
|
+
//#region src/widgets/readForWidgetGoals.d.ts
|
|
7
|
+
interface GoalRow {
|
|
8
|
+
slug: string;
|
|
9
|
+
/** The goal's admin-facing name, or its slug when no configured goal claims it. */
|
|
10
|
+
name: string;
|
|
11
|
+
conversions: number;
|
|
12
|
+
revenue?: number;
|
|
13
|
+
/** 0..1 share of the site's visitors, absent when either side of the ratio is missing. */
|
|
14
|
+
rate?: number;
|
|
15
|
+
previousConversions?: number;
|
|
16
|
+
}
|
|
17
|
+
interface WidgetGoalsResult {
|
|
18
|
+
status: WidgetReadStatus;
|
|
19
|
+
adapterId: string;
|
|
20
|
+
dateRange: DateRange;
|
|
21
|
+
timezone: string;
|
|
22
|
+
rows: GoalRow[];
|
|
23
|
+
/** Range total the rate divides by; absent when the source does not serve visitors. */
|
|
24
|
+
siteVisitors?: number;
|
|
25
|
+
/** The source that answered, absent on a read that never reached one. */
|
|
26
|
+
provider?: string;
|
|
27
|
+
clamped?: boolean;
|
|
28
|
+
stale?: boolean;
|
|
29
|
+
/** True when the source answered without one of the filters a read carried. */
|
|
30
|
+
filtersUnapplied?: boolean;
|
|
31
|
+
/** True when a read hit the source's event scan cap, so the numbers are a floor. */
|
|
32
|
+
sampled?: boolean;
|
|
33
|
+
/** True when the source could not read the scope's goals, so the empty table means nothing. */
|
|
34
|
+
goalsUnresolved?: boolean;
|
|
35
|
+
/** True when the scope configures no goals at all, which is an empty table, not a failure. */
|
|
36
|
+
noGoals?: boolean;
|
|
37
|
+
}
|
|
38
|
+
interface ReadForWidgetGoalsArgs {
|
|
39
|
+
req: PayloadRequest;
|
|
40
|
+
timeframe: TimeframePreset;
|
|
41
|
+
limit: number;
|
|
42
|
+
/** Read the previous window too, when the source can compare. */
|
|
43
|
+
compare: boolean;
|
|
44
|
+
adapterId?: string;
|
|
45
|
+
now: Date;
|
|
46
|
+
range?: DateRange;
|
|
47
|
+
/** Explicit scope override; omitted resolves via the plugin's scopeResolver. */
|
|
48
|
+
scope?: string | null;
|
|
49
|
+
/**
|
|
50
|
+
* Reporting timezone the caller already resolved, reused rather than resolved again so
|
|
51
|
+
* a caller-supplied `range` is read in the very timezone it was interpreted in.
|
|
52
|
+
*/
|
|
53
|
+
timezone?: string;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The goals table behind the goals widget: conversions per goal over the window, each with
|
|
57
|
+
* its revenue, its share of the site's visitors, and (when comparing) the same goal's
|
|
58
|
+
* conversions in the previous window. Rows come from one `goal` breakdown read and the site
|
|
59
|
+
* total from one totals read, both through the existing widget helpers so capability
|
|
60
|
+
* gating, scoping and caching stay in one place. Revenue and visitors ride along only where
|
|
61
|
+
* the source serves them; the status is the breakdown's, since a table without its rows has
|
|
62
|
+
* nothing to show.
|
|
63
|
+
*/
|
|
64
|
+
declare const readForWidgetGoals: (args: ReadForWidgetGoalsArgs) => Promise<WidgetGoalsResult>;
|
|
65
|
+
//#endregion
|
|
66
|
+
export { GoalRow, ReadForWidgetGoalsArgs, WidgetGoalsResult, readForWidgetGoals };
|
|
67
|
+
//# sourceMappingURL=readForWidgetGoals.d.ts.map
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { resolveGoalsDetailedFor } from "../plugin/runtime.js";
|
|
2
|
+
import { comparisonOf } from "../core/capabilities.js";
|
|
3
|
+
import { previousWindow, withinLookback } from "./comparison.js";
|
|
4
|
+
import { prepareWidgetRead } from "./prepareWidgetRead.js";
|
|
5
|
+
import { readForWidget } from "./readForWidget.js";
|
|
6
|
+
import { readForWidgetBreakdown } from "./readForWidgetBreakdown.js";
|
|
7
|
+
import { conversionRate } from "../view/conversionRate.js";
|
|
8
|
+
//#region src/widgets/readForWidgetGoals.ts
|
|
9
|
+
/**
|
|
10
|
+
* How much wider than the table the previous window is read. Both windows rank by their own
|
|
11
|
+
* conversions, so a goal in today's top rows may sit well below them in the previous window;
|
|
12
|
+
* reading only as many rows as the table shows would drop exactly those deltas. Capped at
|
|
13
|
+
* the bound every analytics query is held to.
|
|
14
|
+
*/
|
|
15
|
+
const PREVIOUS_LIMIT_FACTOR = 4;
|
|
16
|
+
const previousLimit = (limit) => Math.min(limit * PREVIOUS_LIMIT_FACTOR, 500);
|
|
17
|
+
/**
|
|
18
|
+
* Goal names for the read's own scope. A resolver that throws must not cost the widget its
|
|
19
|
+
* table, so the rows fall back to their slugs; the failure travels on as the hint, which is
|
|
20
|
+
* what tells the read apart from a scope that configured no goals at all.
|
|
21
|
+
*/
|
|
22
|
+
const goalNames = async (runtime, req, scope) => {
|
|
23
|
+
try {
|
|
24
|
+
const resolved = await resolveGoalsDetailedFor(runtime, req, scope);
|
|
25
|
+
return new Map(resolved.map(({ goal }) => [goal.slug, goal.name]));
|
|
26
|
+
} catch (err) {
|
|
27
|
+
req.payload.logger?.warn(`analytics: widget goal names failed to resolve: ${String(err)}`);
|
|
28
|
+
return "unresolved";
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* The goals table behind the goals widget: conversions per goal over the window, each with
|
|
33
|
+
* its revenue, its share of the site's visitors, and (when comparing) the same goal's
|
|
34
|
+
* conversions in the previous window. Rows come from one `goal` breakdown read and the site
|
|
35
|
+
* total from one totals read, both through the existing widget helpers so capability
|
|
36
|
+
* gating, scoping and caching stay in one place. Revenue and visitors ride along only where
|
|
37
|
+
* the source serves them; the status is the breakdown's, since a table without its rows has
|
|
38
|
+
* nothing to show.
|
|
39
|
+
*/
|
|
40
|
+
const readForWidgetGoals = async (args) => {
|
|
41
|
+
const { req, timeframe, limit, compare, adapterId, now } = args;
|
|
42
|
+
const fallback = (status, id, window) => ({
|
|
43
|
+
status,
|
|
44
|
+
adapterId: id,
|
|
45
|
+
rows: [],
|
|
46
|
+
...window
|
|
47
|
+
});
|
|
48
|
+
const prepared = await prepareWidgetRead({
|
|
49
|
+
req,
|
|
50
|
+
now,
|
|
51
|
+
timeframe,
|
|
52
|
+
adapterId,
|
|
53
|
+
scope: args.scope,
|
|
54
|
+
timezone: args.timezone,
|
|
55
|
+
range: args.range
|
|
56
|
+
});
|
|
57
|
+
if (!prepared.ok) return fallback(prepared.status, prepared.adapterId, {
|
|
58
|
+
dateRange: prepared.dateRange,
|
|
59
|
+
timezone: prepared.tz
|
|
60
|
+
});
|
|
61
|
+
const { runtime, adapter, tz, dateRange } = prepared;
|
|
62
|
+
const shared = {
|
|
63
|
+
req,
|
|
64
|
+
timeframe,
|
|
65
|
+
adapterId: adapter.id,
|
|
66
|
+
scope: prepared.scope,
|
|
67
|
+
timezone: tz,
|
|
68
|
+
now
|
|
69
|
+
};
|
|
70
|
+
const previousRange = compare && runtime.comparison && comparisonOf(adapter.capabilities) ? previousWindow(dateRange, tz) : null;
|
|
71
|
+
const comparisonRange = previousRange && withinLookback(previousRange, adapter.capabilities.maxLookbackDays, {
|
|
72
|
+
tz,
|
|
73
|
+
now
|
|
74
|
+
}) ? previousRange : void 0;
|
|
75
|
+
const resolved = await goalNames(runtime, req, prepared.scope);
|
|
76
|
+
const names = resolved === "unresolved" ? /* @__PURE__ */ new Map() : resolved;
|
|
77
|
+
const goalSlugs = resolved === "unresolved" ? "unresolved" : [...names.keys()];
|
|
78
|
+
const [breakdown, totals, previous] = await Promise.all([
|
|
79
|
+
readForWidgetBreakdown({
|
|
80
|
+
...shared,
|
|
81
|
+
range: dateRange,
|
|
82
|
+
metric: "conversions",
|
|
83
|
+
dimension: "goal",
|
|
84
|
+
limit,
|
|
85
|
+
extraMetrics: ["revenue", "visitors"],
|
|
86
|
+
goalSlugs
|
|
87
|
+
}),
|
|
88
|
+
adapter.capabilities.metrics.has("visitors") ? readForWidget({
|
|
89
|
+
...shared,
|
|
90
|
+
range: dateRange,
|
|
91
|
+
metrics: ["visitors"],
|
|
92
|
+
comparison: false
|
|
93
|
+
}) : void 0,
|
|
94
|
+
comparisonRange ? readForWidgetBreakdown({
|
|
95
|
+
...shared,
|
|
96
|
+
range: comparisonRange,
|
|
97
|
+
metric: "conversions",
|
|
98
|
+
dimension: "goal",
|
|
99
|
+
limit: previousLimit(limit),
|
|
100
|
+
goalSlugs
|
|
101
|
+
}) : void 0
|
|
102
|
+
]);
|
|
103
|
+
if (breakdown.status !== "ok") return fallback(breakdown.status, breakdown.adapterId, {
|
|
104
|
+
dateRange,
|
|
105
|
+
timezone: tz
|
|
106
|
+
});
|
|
107
|
+
const previousBySlug = new Map((previous?.status === "ok" ? previous.rows : []).map((row) => [row.label, row.value]));
|
|
108
|
+
const siteVisitors = totals?.status === "ok" ? totals.metrics.visitors : void 0;
|
|
109
|
+
const rows = breakdown.rows.map((row) => {
|
|
110
|
+
const rate = conversionRate(row.metrics?.visitors, siteVisitors);
|
|
111
|
+
const previousConversions = previousBySlug.get(row.label);
|
|
112
|
+
return {
|
|
113
|
+
slug: row.label,
|
|
114
|
+
name: names.get(row.label) ?? row.label,
|
|
115
|
+
conversions: row.value,
|
|
116
|
+
...row.metrics?.revenue !== void 0 ? { revenue: row.metrics.revenue } : {},
|
|
117
|
+
...rate !== null ? { rate } : {},
|
|
118
|
+
...previousConversions !== void 0 ? { previousConversions } : {}
|
|
119
|
+
};
|
|
120
|
+
}).sort((a, b) => b.conversions - a.conversions).slice(0, limit);
|
|
121
|
+
return {
|
|
122
|
+
status: "ok",
|
|
123
|
+
adapterId: adapter.id,
|
|
124
|
+
dateRange,
|
|
125
|
+
timezone: tz,
|
|
126
|
+
rows,
|
|
127
|
+
...siteVisitors !== void 0 ? { siteVisitors } : {},
|
|
128
|
+
...breakdown.provider === void 0 ? {} : { provider: breakdown.provider },
|
|
129
|
+
clamped: Boolean(breakdown.clamped || totals?.clamped || previous?.clamped),
|
|
130
|
+
stale: Boolean(breakdown.stale || totals?.stale || previous?.stale),
|
|
131
|
+
filtersUnapplied: Boolean(breakdown.filtersUnapplied || totals?.filtersUnapplied || previous?.filtersUnapplied),
|
|
132
|
+
sampled: Boolean(breakdown.sampled || totals?.sampled || previous?.sampled),
|
|
133
|
+
goalsUnresolved: breakdown.goalsUnresolved === true,
|
|
134
|
+
noGoals: breakdown.noGoals === true
|
|
135
|
+
};
|
|
136
|
+
};
|
|
137
|
+
//#endregion
|
|
138
|
+
export { readForWidgetGoals };
|
|
139
|
+
|
|
140
|
+
//# sourceMappingURL=readForWidgetGoals.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"readForWidgetGoals.js","names":[],"sources":["../../src/widgets/readForWidgetGoals.ts"],"sourcesContent":["import type { PayloadRequest } from 'payload'\nimport { comparisonOf } from '../core/capabilities'\nimport type { DateRange } from '../core/contract'\nimport { type AnalyticsRuntime, resolveGoalsDetailedFor } from '../plugin/runtime'\nimport { MAX_QUERY_LIMIT } from '../query/limits'\nimport type { TimeframePreset } from '../timeframe/presets'\nimport { conversionRate } from '../view/conversionRate'\nimport { previousWindow, withinLookback } from './comparison'\nimport { prepareWidgetRead, type WidgetReadStatus } from './prepareWidgetRead'\nimport { readForWidget } from './readForWidget'\nimport { readForWidgetBreakdown } from './readForWidgetBreakdown'\n\nexport interface GoalRow {\n\tslug: string\n\t/** The goal's admin-facing name, or its slug when no configured goal claims it. */\n\tname: string\n\tconversions: number\n\trevenue?: number\n\t/** 0..1 share of the site's visitors, absent when either side of the ratio is missing. */\n\trate?: number\n\tpreviousConversions?: number\n}\n\nexport interface WidgetGoalsResult {\n\tstatus: WidgetReadStatus\n\tadapterId: string\n\tdateRange: DateRange\n\ttimezone: string\n\trows: GoalRow[]\n\t/** Range total the rate divides by; absent when the source does not serve visitors. */\n\tsiteVisitors?: number\n\t/** The source that answered, absent on a read that never reached one. */\n\tprovider?: string\n\tclamped?: boolean\n\tstale?: boolean\n\t/** True when the source answered without one of the filters a read carried. */\n\tfiltersUnapplied?: boolean\n\t/** True when a read hit the source's event scan cap, so the numbers are a floor. */\n\tsampled?: boolean\n\t/** True when the source could not read the scope's goals, so the empty table means nothing. */\n\tgoalsUnresolved?: boolean\n\t/** True when the scope configures no goals at all, which is an empty table, not a failure. */\n\tnoGoals?: boolean\n}\n\nexport interface ReadForWidgetGoalsArgs {\n\treq: PayloadRequest\n\ttimeframe: TimeframePreset\n\tlimit: number\n\t/** Read the previous window too, when the source can compare. */\n\tcompare: boolean\n\tadapterId?: string\n\tnow: Date\n\trange?: DateRange\n\t/** Explicit scope override; omitted resolves via the plugin's scopeResolver. */\n\tscope?: string | null\n\t/**\n\t * Reporting timezone the caller already resolved, reused rather than resolved again so\n\t * a caller-supplied `range` is read in the very timezone it was interpreted in.\n\t */\n\ttimezone?: string\n}\n\n/**\n * How much wider than the table the previous window is read. Both windows rank by their own\n * conversions, so a goal in today's top rows may sit well below them in the previous window;\n * reading only as many rows as the table shows would drop exactly those deltas. Capped at\n * the bound every analytics query is held to.\n */\nconst PREVIOUS_LIMIT_FACTOR = 4\n\nconst previousLimit = (limit: number): number =>\n\tMath.min(limit * PREVIOUS_LIMIT_FACTOR, MAX_QUERY_LIMIT)\n\n/**\n * Goal names for the read's own scope. A resolver that throws must not cost the widget its\n * table, so the rows fall back to their slugs; the failure travels on as the hint, which is\n * what tells the read apart from a scope that configured no goals at all.\n */\nconst goalNames = async (\n\truntime: AnalyticsRuntime,\n\treq: PayloadRequest,\n\tscope: string | null\n): Promise<Map<string, string> | 'unresolved'> => {\n\ttry {\n\t\tconst resolved = await resolveGoalsDetailedFor(runtime, req, scope)\n\t\treturn new Map(resolved.map(({ goal }) => [goal.slug, goal.name]))\n\t} catch (err) {\n\t\treq.payload.logger?.warn(`analytics: widget goal names failed to resolve: ${String(err)}`)\n\t\treturn 'unresolved'\n\t}\n}\n\n/**\n * The goals table behind the goals widget: conversions per goal over the window, each with\n * its revenue, its share of the site's visitors, and (when comparing) the same goal's\n * conversions in the previous window. Rows come from one `goal` breakdown read and the site\n * total from one totals read, both through the existing widget helpers so capability\n * gating, scoping and caching stay in one place. Revenue and visitors ride along only where\n * the source serves them; the status is the breakdown's, since a table without its rows has\n * nothing to show.\n */\nexport const readForWidgetGoals = async (\n\targs: ReadForWidgetGoalsArgs\n): Promise<WidgetGoalsResult> => {\n\tconst { req, timeframe, limit, compare, adapterId, now } = args\n\tconst fallback = (\n\t\tstatus: WidgetReadStatus,\n\t\tid: string,\n\t\twindow: { dateRange: DateRange; timezone: string }\n\t): WidgetGoalsResult => ({ status, adapterId: id, rows: [], ...window })\n\n\tconst prepared = await prepareWidgetRead({\n\t\treq,\n\t\tnow,\n\t\ttimeframe,\n\t\tadapterId,\n\t\tscope: args.scope,\n\t\ttimezone: args.timezone,\n\t\trange: args.range,\n\t})\n\tif (!prepared.ok) {\n\t\treturn fallback(prepared.status, prepared.adapterId, {\n\t\t\tdateRange: prepared.dateRange,\n\t\t\ttimezone: prepared.tz,\n\t\t})\n\t}\n\tconst { runtime, adapter, tz, dateRange } = prepared\n\t// The sub-reads resolve their own context; pinning the adapter, scope, timezone and\n\t// window keeps all three answering about exactly the same read.\n\tconst shared = { req, timeframe, adapterId: adapter.id, scope: prepared.scope, timezone: tz, now }\n\tconst previousRange =\n\t\tcompare && runtime.comparison && comparisonOf(adapter.capabilities)\n\t\t\t? previousWindow(dateRange, tz)\n\t\t\t: null\n\tconst comparisonRange =\n\t\tpreviousRange &&\n\t\twithinLookback(previousRange, adapter.capabilities.maxLookbackDays, { tz, now })\n\t\t\t? previousRange\n\t\t\t: undefined\n\n\t// The names are resolved first: their slugs are the hint a provider source restricts its\n\t// goal rows to, so both reads below need them before they run.\n\tconst resolved = await goalNames(runtime, req, prepared.scope)\n\tconst names = resolved === 'unresolved' ? new Map<string, string>() : resolved\n\tconst goalSlugs = resolved === 'unresolved' ? 'unresolved' : [...names.keys()]\n\tconst [breakdown, totals, previous] = await Promise.all([\n\t\treadForWidgetBreakdown({\n\t\t\t...shared,\n\t\t\trange: dateRange,\n\t\t\tmetric: 'conversions',\n\t\t\tdimension: 'goal',\n\t\t\tlimit,\n\t\t\textraMetrics: ['revenue', 'visitors'],\n\t\t\tgoalSlugs,\n\t\t}),\n\t\tadapter.capabilities.metrics.has('visitors')\n\t\t\t? // The site total is a denominator, never a delta: its own previous window would\n\t\t\t\t// be a second read nothing renders.\n\t\t\t\treadForWidget({ ...shared, range: dateRange, metrics: ['visitors'], comparison: false })\n\t\t\t: undefined,\n\t\tcomparisonRange\n\t\t\t? readForWidgetBreakdown({\n\t\t\t\t\t...shared,\n\t\t\t\t\trange: comparisonRange,\n\t\t\t\t\tmetric: 'conversions',\n\t\t\t\t\tdimension: 'goal',\n\t\t\t\t\tlimit: previousLimit(limit),\n\t\t\t\t\tgoalSlugs,\n\t\t\t\t})\n\t\t\t: undefined,\n\t])\n\tif (breakdown.status !== 'ok') {\n\t\treturn fallback(breakdown.status, breakdown.adapterId, { dateRange, timezone: tz })\n\t}\n\n\tconst previousBySlug = new Map(\n\t\t(previous?.status === 'ok' ? previous.rows : []).map((row) => [row.label, row.value])\n\t)\n\tconst siteVisitors = totals?.status === 'ok' ? totals.metrics.visitors : undefined\n\tconst rows: GoalRow[] = breakdown.rows\n\t\t.map((row) => {\n\t\t\tconst rate = conversionRate(row.metrics?.visitors, siteVisitors)\n\t\t\tconst previousConversions = previousBySlug.get(row.label)\n\t\t\treturn {\n\t\t\t\tslug: row.label,\n\t\t\t\tname: names.get(row.label) ?? row.label,\n\t\t\t\tconversions: row.value,\n\t\t\t\t...(row.metrics?.revenue !== undefined ? { revenue: row.metrics.revenue } : {}),\n\t\t\t\t...(rate !== null ? { rate } : {}),\n\t\t\t\t...(previousConversions !== undefined ? { previousConversions } : {}),\n\t\t\t}\n\t\t})\n\t\t// The adapter ranks and caps already; re-applying both keeps one that does not from\n\t\t// spilling a long, unranked table into a dashboard card.\n\t\t.sort((a, b) => b.conversions - a.conversions)\n\t\t.slice(0, limit)\n\n\treturn {\n\t\tstatus: 'ok',\n\t\tadapterId: adapter.id,\n\t\tdateRange,\n\t\ttimezone: tz,\n\t\trows,\n\t\t...(siteVisitors !== undefined ? { siteVisitors } : {}),\n\t\t...(breakdown.provider === undefined ? {} : { provider: breakdown.provider }),\n\t\tclamped: Boolean(breakdown.clamped || totals?.clamped || previous?.clamped),\n\t\tstale: Boolean(breakdown.stale || totals?.stale || previous?.stale),\n\t\tfiltersUnapplied: Boolean(\n\t\t\tbreakdown.filtersUnapplied || totals?.filtersUnapplied || previous?.filtersUnapplied\n\t\t),\n\t\tsampled: Boolean(breakdown.sampled || totals?.sampled || previous?.sampled),\n\t\tgoalsUnresolved: breakdown.goalsUnresolved === true,\n\t\tnoGoals: breakdown.noGoals === true,\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;AAqEA,MAAM,wBAAwB;AAE9B,MAAM,iBAAiB,UACtB,KAAK,IAAI,QAAQ,uBAAA,GAAsC;;;;;;AAOxD,MAAM,YAAY,OACjB,SACA,KACA,UACiD;CACjD,IAAI;EACH,MAAM,WAAW,MAAM,wBAAwB,SAAS,KAAK,KAAK;EAClE,OAAO,IAAI,IAAI,SAAS,KAAK,EAAE,WAAW,CAAC,KAAK,MAAM,KAAK,IAAI,CAAC,CAAC;CAClE,SAAS,KAAK;EACb,IAAI,QAAQ,QAAQ,KAAK,mDAAmD,OAAO,GAAG,GAAG;EACzF,OAAO;CACR;AACD;;;;;;;;;;AAWA,MAAa,qBAAqB,OACjC,SACgC;CAChC,MAAM,EAAE,KAAK,WAAW,OAAO,SAAS,WAAW,QAAQ;CAC3D,MAAM,YACL,QACA,IACA,YACwB;EAAE;EAAQ,WAAW;EAAI,MAAM,CAAC;EAAG,GAAG;CAAO;CAEtE,MAAM,WAAW,MAAM,kBAAkB;EACxC;EACA;EACA;EACA;EACA,OAAO,KAAK;EACZ,UAAU,KAAK;EACf,OAAO,KAAK;CACb,CAAC;CACD,IAAI,CAAC,SAAS,IACb,OAAO,SAAS,SAAS,QAAQ,SAAS,WAAW;EACpD,WAAW,SAAS;EACpB,UAAU,SAAS;CACpB,CAAC;CAEF,MAAM,EAAE,SAAS,SAAS,IAAI,cAAc;CAG5C,MAAM,SAAS;EAAE;EAAK;EAAW,WAAW,QAAQ;EAAI,OAAO,SAAS;EAAO,UAAU;EAAI;CAAI;CACjG,MAAM,gBACL,WAAW,QAAQ,cAAc,aAAa,QAAQ,YAAY,IAC/D,eAAe,WAAW,EAAE,IAC5B;CACJ,MAAM,kBACL,iBACA,eAAe,eAAe,QAAQ,aAAa,iBAAiB;EAAE;EAAI;CAAI,CAAC,IAC5E,gBACA,KAAA;CAIJ,MAAM,WAAW,MAAM,UAAU,SAAS,KAAK,SAAS,KAAK;CAC7D,MAAM,QAAQ,aAAa,+BAAe,IAAI,IAAoB,IAAI;CACtE,MAAM,YAAY,aAAa,eAAe,eAAe,CAAC,GAAG,MAAM,KAAK,CAAC;CAC7E,MAAM,CAAC,WAAW,QAAQ,YAAY,MAAM,QAAQ,IAAI;EACvD,uBAAuB;GACtB,GAAG;GACH,OAAO;GACP,QAAQ;GACR,WAAW;GACX;GACA,cAAc,CAAC,WAAW,UAAU;GACpC;EACD,CAAC;EACD,QAAQ,aAAa,QAAQ,IAAI,UAAU,IAGzC,cAAc;GAAE,GAAG;GAAQ,OAAO;GAAW,SAAS,CAAC,UAAU;GAAG,YAAY;EAAM,CAAC,IACtF,KAAA;EACH,kBACG,uBAAuB;GACvB,GAAG;GACH,OAAO;GACP,QAAQ;GACR,WAAW;GACX,OAAO,cAAc,KAAK;GAC1B;EACD,CAAC,IACA,KAAA;CACJ,CAAC;CACD,IAAI,UAAU,WAAW,MACxB,OAAO,SAAS,UAAU,QAAQ,UAAU,WAAW;EAAE;EAAW,UAAU;CAAG,CAAC;CAGnF,MAAM,iBAAiB,IAAI,KACzB,UAAU,WAAW,OAAO,SAAS,OAAO,CAAC,GAAG,KAAK,QAAQ,CAAC,IAAI,OAAO,IAAI,KAAK,CAAC,CACrF;CACA,MAAM,eAAe,QAAQ,WAAW,OAAO,OAAO,QAAQ,WAAW,KAAA;CACzE,MAAM,OAAkB,UAAU,KAChC,KAAK,QAAQ;EACb,MAAM,OAAO,eAAe,IAAI,SAAS,UAAU,YAAY;EAC/D,MAAM,sBAAsB,eAAe,IAAI,IAAI,KAAK;EACxD,OAAO;GACN,MAAM,IAAI;GACV,MAAM,MAAM,IAAI,IAAI,KAAK,KAAK,IAAI;GAClC,aAAa,IAAI;GACjB,GAAI,IAAI,SAAS,YAAY,KAAA,IAAY,EAAE,SAAS,IAAI,QAAQ,QAAQ,IAAI,CAAC;GAC7E,GAAI,SAAS,OAAO,EAAE,KAAK,IAAI,CAAC;GAChC,GAAI,wBAAwB,KAAA,IAAY,EAAE,oBAAoB,IAAI,CAAC;EACpE;CACD,CAAC,EAGA,MAAM,GAAG,MAAM,EAAE,cAAc,EAAE,WAAW,EAC5C,MAAM,GAAG,KAAK;CAEhB,OAAO;EACN,QAAQ;EACR,WAAW,QAAQ;EACnB;EACA,UAAU;EACV;EACA,GAAI,iBAAiB,KAAA,IAAY,EAAE,aAAa,IAAI,CAAC;EACrD,GAAI,UAAU,aAAa,KAAA,IAAY,CAAC,IAAI,EAAE,UAAU,UAAU,SAAS;EAC3E,SAAS,QAAQ,UAAU,WAAW,QAAQ,WAAW,UAAU,OAAO;EAC1E,OAAO,QAAQ,UAAU,SAAS,QAAQ,SAAS,UAAU,KAAK;EAClE,kBAAkB,QACjB,UAAU,oBAAoB,QAAQ,oBAAoB,UAAU,gBACrE;EACA,SAAS,QAAQ,UAAU,WAAW,QAAQ,WAAW,UAAU,OAAO;EAC1E,iBAAiB,UAAU,oBAAoB;EAC/C,SAAS,UAAU,YAAY;CAChC;AACD"}
|