@10x-media/analytics 0.1.0-beta.1 → 0.1.0-beta.2
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 +56 -0
- package/README.md +1 -0
- package/dist/adapters/ga4/ga4.js +17 -7
- package/dist/adapters/ga4/ga4.js.map +1 -1
- package/dist/adapters/plausible/plausible.js +5 -0
- package/dist/adapters/plausible/plausible.js.map +1 -1
- package/dist/adapters/posthog/posthog.js +28 -17
- package/dist/adapters/posthog/posthog.js.map +1 -1
- package/dist/adapters/umami/umami.js +1 -1
- package/dist/adapters/umami/umami.js.map +1 -1
- package/dist/binding/types.d.ts +9 -7
- package/dist/charts/TrendChart.js +17 -8
- package/dist/charts/TrendChart.js.map +1 -1
- package/dist/charts/bucket.js +31 -24
- package/dist/charts/bucket.js.map +1 -1
- package/dist/core/cacheKey.js +4 -7
- package/dist/core/cacheKey.js.map +1 -1
- package/dist/core/contract.d.ts +12 -1
- package/dist/core/contract.js.map +1 -1
- package/dist/core/options.d.ts +27 -2
- package/dist/core/options.js +15 -8
- package/dist/core/options.js.map +1 -1
- package/dist/exports/rsc.d.ts +6 -3
- package/dist/exports/rsc.js +5 -2
- package/dist/fields/AnalyticsPanelClient.js +193 -0
- package/dist/fields/AnalyticsPanelClient.js.map +1 -0
- package/dist/fields/AnalyticsPanelField.d.ts +28 -0
- package/dist/fields/AnalyticsPanelField.js +54 -0
- package/dist/fields/AnalyticsPanelField.js.map +1 -0
- package/dist/fields/AnalyticsStatField.d.ts +1 -1
- package/dist/fields/AnalyticsStatField.js +14 -3
- package/dist/fields/AnalyticsStatField.js.map +1 -1
- package/dist/fields/emptyState.js +46 -0
- package/dist/fields/emptyState.js.map +1 -0
- package/dist/fields/factories.d.ts +10 -2
- package/dist/fields/factories.js +33 -4
- package/dist/fields/factories.js.map +1 -1
- package/dist/fields/readForDocument.d.ts +57 -0
- package/dist/fields/readForDocument.js +50 -10
- package/dist/fields/readForDocument.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +29 -4
- package/dist/index.js.map +1 -1
- package/dist/native/ingest/endpoint.js +5 -2
- package/dist/native/ingest/endpoint.js.map +1 -1
- package/dist/native/ingest/normalizeEvent.js +3 -2
- package/dist/native/ingest/normalizeEvent.js.map +1 -1
- package/dist/native/ingest/writeEvent.js +2 -1
- package/dist/native/ingest/writeEvent.js.map +1 -1
- package/dist/native/nativeAdapter.js +7 -4
- package/dist/native/nativeAdapter.js.map +1 -1
- package/dist/native/rollupAcc.js +9 -6
- package/dist/native/rollupAcc.js.map +1 -1
- package/dist/native/rollups/deltas.js +2 -2
- package/dist/native/rollups/deltas.js.map +1 -1
- package/dist/plugin/documentEndpoint.js +80 -0
- package/dist/plugin/documentEndpoint.js.map +1 -0
- package/dist/plugin/paths.js +11 -0
- package/dist/plugin/paths.js.map +1 -0
- package/dist/plugin/runtime.js +2 -1
- package/dist/plugin/runtime.js.map +1 -1
- package/dist/plugin/warmTask.js +1 -1
- package/dist/surfacing/engine.js +5 -5
- package/dist/surfacing/engine.js.map +1 -1
- package/dist/sync/collection.js +11 -6
- package/dist/sync/collection.js.map +1 -1
- package/dist/sync/syncTask.js +1 -1
- package/dist/timeframe/presets.js +12 -16
- package/dist/timeframe/presets.js.map +1 -1
- package/dist/timeframe/tz.js +110 -0
- package/dist/timeframe/tz.js.map +1 -0
- package/dist/translations/de.js +78 -0
- package/dist/translations/de.js.map +1 -0
- package/dist/translations/en.js +5 -2
- package/dist/translations/en.js.map +1 -1
- package/dist/translations/index.d.ts +2 -1
- package/dist/translations/index.js +6 -2
- package/dist/translations/index.js.map +1 -1
- package/dist/translations/keys.d.ts +5 -2
- package/dist/translations/keys.js +5 -2
- package/dist/translations/keys.js.map +1 -1
- package/dist/translations/metricKeys.js +0 -2
- package/dist/translations/metricKeys.js.map +1 -1
- package/dist/widgets/AnalyticsBreakdownWidget.js +1 -1
- package/dist/widgets/AnalyticsMetricWidget.js +9 -1
- package/dist/widgets/AnalyticsMetricWidget.js.map +1 -1
- package/dist/widgets/AnalyticsRealtimeWidget.js +1 -1
- package/dist/widgets/AnalyticsTrendWidget.js +10 -2
- package/dist/widgets/AnalyticsTrendWidget.js.map +1 -1
- package/dist/widgets/ComparisonDelta.js +83 -0
- package/dist/widgets/ComparisonDelta.js.map +1 -0
- package/dist/widgets/cardChrome.d.ts +8 -0
- package/dist/widgets/comparison.js +43 -0
- package/dist/widgets/comparison.js.map +1 -0
- package/dist/widgets/readForWidget.d.ts +4 -0
- package/dist/widgets/readForWidget.js +26 -11
- package/dist/widgets/readForWidget.js.map +1 -1
- package/dist/widgets/readForWidgetBreakdown.js +13 -8
- package/dist/widgets/readForWidgetBreakdown.js.map +1 -1
- package/dist/widgets/readForWidgetRealtime.js +1 -1
- package/dist/widgets/readForWidgetRealtime.js.map +1 -1
- package/dist/widgets/readForWidgetSeries.d.ts +6 -0
- package/dist/widgets/readForWidgetSeries.js +60 -40
- package/dist/widgets/readForWidgetSeries.js.map +1 -1
- package/dist/widgets/registerWidgets.js +148 -169
- package/dist/widgets/registerWidgets.js.map +1 -1
- package/package.json +6 -6
|
@@ -5,9 +5,10 @@ import { TranslationKey, keys } from "./keys.js";
|
|
|
5
5
|
type TranslationsOption = {
|
|
6
6
|
[locale: string]: Partial<Record<TranslationKey, string>>;
|
|
7
7
|
};
|
|
8
|
-
/** Per-locale messages merged into `config.i18n.translations`.
|
|
8
|
+
/** Per-locale messages merged into `config.i18n.translations`. */
|
|
9
9
|
declare const translations: {
|
|
10
10
|
en: Record<string, Record<string, string>>;
|
|
11
|
+
de: Record<string, Record<string, string>>;
|
|
11
12
|
};
|
|
12
13
|
//#endregion
|
|
13
14
|
export { TranslationsOption, translations };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import "./keys.js";
|
|
2
|
+
import { de } from "./de.js";
|
|
2
3
|
import { en } from "./en.js";
|
|
3
4
|
//#region src/translations/index.ts
|
|
4
5
|
/**
|
|
@@ -18,8 +19,11 @@ const toNested = (flat) => {
|
|
|
18
19
|
}
|
|
19
20
|
return out;
|
|
20
21
|
};
|
|
21
|
-
/** Per-locale messages merged into `config.i18n.translations`.
|
|
22
|
-
const translations = {
|
|
22
|
+
/** Per-locale messages merged into `config.i18n.translations`. */
|
|
23
|
+
const translations = {
|
|
24
|
+
en: toNested(en),
|
|
25
|
+
de: toNested(de)
|
|
26
|
+
};
|
|
23
27
|
//#endregion
|
|
24
28
|
export { toNested, translations };
|
|
25
29
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/translations/index.ts"],"sourcesContent":["import { en } from './en'\nimport type { TranslationKey } from './keys'\n\nexport type { TranslationKey } from './keys'\nexport { keys } from './keys'\n\n/** Per-locale string overrides keyed by this plugin's typed translation keys. */\nexport type TranslationsOption = {\n\t[locale: string]: Partial<Record<TranslationKey, string>>\n}\n\n/**\n * Flat `analytics:foo` entries to the nested `{ analytics: { foo } }`\n * shape Payload resolves `t('analytics:foo')` against (it splits on `:`).\n * Undefined values are skipped so `Partial` override maps pass through.\n */\nexport const toNested = (flat: {\n\t[key: string]: string | undefined\n}): Record<string, Record<string, string>> => {\n\tconst out: Record<string, Record<string, string>> = {}\n\tfor (const [fullKey, value] of Object.entries(flat)) {\n\t\tif (typeof value !== 'string') {\n\t\t\tcontinue\n\t\t}\n\t\tconst separator = fullKey.indexOf(':')\n\t\tconst namespace = fullKey.slice(0, separator)\n\t\tconst bucket = out[namespace] ?? {}\n\t\tbucket[fullKey.slice(separator + 1)] = value\n\t\tout[namespace] = bucket\n\t}\n\treturn out\n}\n\n/** Per-locale messages merged into `config.i18n.translations`.
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/translations/index.ts"],"sourcesContent":["import { de } from './de'\nimport { en } from './en'\nimport type { TranslationKey } from './keys'\n\nexport type { TranslationKey } from './keys'\nexport { keys } from './keys'\n\n/** Per-locale string overrides keyed by this plugin's typed translation keys. */\nexport type TranslationsOption = {\n\t[locale: string]: Partial<Record<TranslationKey, string>>\n}\n\n/**\n * Flat `analytics:foo` entries to the nested `{ analytics: { foo } }`\n * shape Payload resolves `t('analytics:foo')` against (it splits on `:`).\n * Undefined values are skipped so `Partial` override maps pass through.\n */\nexport const toNested = (flat: {\n\t[key: string]: string | undefined\n}): Record<string, Record<string, string>> => {\n\tconst out: Record<string, Record<string, string>> = {}\n\tfor (const [fullKey, value] of Object.entries(flat)) {\n\t\tif (typeof value !== 'string') {\n\t\t\tcontinue\n\t\t}\n\t\tconst separator = fullKey.indexOf(':')\n\t\tconst namespace = fullKey.slice(0, separator)\n\t\tconst bucket = out[namespace] ?? {}\n\t\tbucket[fullKey.slice(separator + 1)] = value\n\t\tout[namespace] = bucket\n\t}\n\treturn out\n}\n\n/** Per-locale messages merged into `config.i18n.translations`. */\nexport const translations = {\n\ten: toNested(en),\n\tde: toNested(de),\n}\n"],"mappings":";;;;;;;;;AAiBA,MAAa,YAAY,SAEqB;CAC7C,MAAM,MAA8C,CAAC;CACrD,KAAK,MAAM,CAAC,SAAS,UAAU,OAAO,QAAQ,IAAI,GAAG;EACpD,IAAI,OAAO,UAAU,UACpB;EAED,MAAM,YAAY,QAAQ,QAAQ,GAAG;EACrC,MAAM,YAAY,QAAQ,MAAM,GAAG,SAAS;EAC5C,MAAM,SAAS,IAAI,cAAc,CAAC;EAClC,OAAO,QAAQ,MAAM,YAAY,CAAC,KAAK;EACvC,IAAI,aAAa;CAClB;CACA,OAAO;AACR;;AAGA,MAAa,eAAe;CAC3B,IAAI,SAAS,EAAE;CACf,IAAI,SAAS,EAAE;AAChB"}
|
|
@@ -13,12 +13,11 @@ declare const keys: {
|
|
|
13
13
|
readonly metricEvents: "analytics:metricEvents";
|
|
14
14
|
readonly metricAvgDuration: "analytics:metricAvgDuration";
|
|
15
15
|
readonly metricBounceRate: "analytics:metricBounceRate";
|
|
16
|
-
readonly metricEntries: "analytics:metricEntries";
|
|
17
|
-
readonly metricExits: "analytics:metricExits";
|
|
18
16
|
readonly metricScrollDepth: "analytics:metricScrollDepth";
|
|
19
17
|
readonly metricConversions: "analytics:metricConversions";
|
|
20
18
|
readonly metricRevenue: "analytics:metricRevenue";
|
|
21
19
|
readonly stateNoData: "analytics:stateNoData";
|
|
20
|
+
readonly stateNew: "analytics:stateNew";
|
|
22
21
|
readonly stateNotBound: "analytics:stateNotBound";
|
|
23
22
|
readonly stateNotConfigured: "analytics:stateNotConfigured";
|
|
24
23
|
readonly stateUnavailable: "analytics:stateUnavailable";
|
|
@@ -51,6 +50,10 @@ declare const keys: {
|
|
|
51
50
|
readonly widgetRealtimeLabel: "analytics:widgetRealtimeLabel";
|
|
52
51
|
readonly widgetRealtimeCaption: "analytics:widgetRealtimeCaption";
|
|
53
52
|
readonly widgetRealtimePaused: "analytics:widgetRealtimePaused";
|
|
53
|
+
readonly comparisonVsPrevious: "analytics:comparisonVsPrevious";
|
|
54
|
+
readonly comparisonIncrease: "analytics:comparisonIncrease";
|
|
55
|
+
readonly comparisonDecrease: "analytics:comparisonDecrease";
|
|
56
|
+
readonly comparisonNoChange: "analytics:comparisonNoChange";
|
|
54
57
|
readonly providersCollectionSingular: "analytics:providersCollectionSingular";
|
|
55
58
|
readonly providersCollectionPlural: "analytics:providersCollectionPlural";
|
|
56
59
|
readonly providerFieldProvider: "analytics:providerFieldProvider";
|
|
@@ -13,12 +13,11 @@ const keys = {
|
|
|
13
13
|
metricEvents: "analytics:metricEvents",
|
|
14
14
|
metricAvgDuration: "analytics:metricAvgDuration",
|
|
15
15
|
metricBounceRate: "analytics:metricBounceRate",
|
|
16
|
-
metricEntries: "analytics:metricEntries",
|
|
17
|
-
metricExits: "analytics:metricExits",
|
|
18
16
|
metricScrollDepth: "analytics:metricScrollDepth",
|
|
19
17
|
metricConversions: "analytics:metricConversions",
|
|
20
18
|
metricRevenue: "analytics:metricRevenue",
|
|
21
19
|
stateNoData: "analytics:stateNoData",
|
|
20
|
+
stateNew: "analytics:stateNew",
|
|
22
21
|
stateNotBound: "analytics:stateNotBound",
|
|
23
22
|
stateNotConfigured: "analytics:stateNotConfigured",
|
|
24
23
|
stateUnavailable: "analytics:stateUnavailable",
|
|
@@ -51,6 +50,10 @@ const keys = {
|
|
|
51
50
|
widgetRealtimeLabel: "analytics:widgetRealtimeLabel",
|
|
52
51
|
widgetRealtimeCaption: "analytics:widgetRealtimeCaption",
|
|
53
52
|
widgetRealtimePaused: "analytics:widgetRealtimePaused",
|
|
53
|
+
comparisonVsPrevious: "analytics:comparisonVsPrevious",
|
|
54
|
+
comparisonIncrease: "analytics:comparisonIncrease",
|
|
55
|
+
comparisonDecrease: "analytics:comparisonDecrease",
|
|
56
|
+
comparisonNoChange: "analytics:comparisonNoChange",
|
|
54
57
|
providersCollectionSingular: "analytics:providersCollectionSingular",
|
|
55
58
|
providersCollectionPlural: "analytics:providersCollectionPlural",
|
|
56
59
|
providerFieldProvider: "analytics:providerFieldProvider",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keys.js","names":[],"sources":["../../src/translations/keys.ts"],"sourcesContent":["/**\n * Typed translation keys. Lookups must go through these constants, not string\n * literals (enforced by requireI18nKeysTyped.grit). Every key here must have a\n * value in every locale (`en.ts`), or it is a type error.\n */\nexport const keys = {\n\tpluginName: 'analytics:pluginName',\n\ttabAnalytics: 'analytics:tabAnalytics',\n\tmetricPageviews: 'analytics:metricPageviews',\n\tmetricVisitors: 'analytics:metricVisitors',\n\tmetricSessions: 'analytics:metricSessions',\n\tmetricEvents: 'analytics:metricEvents',\n\tmetricAvgDuration: 'analytics:metricAvgDuration',\n\tmetricBounceRate: 'analytics:metricBounceRate',\n\
|
|
1
|
+
{"version":3,"file":"keys.js","names":[],"sources":["../../src/translations/keys.ts"],"sourcesContent":["/**\n * Typed translation keys. Lookups must go through these constants, not string\n * literals (enforced by requireI18nKeysTyped.grit). Every key here must have a\n * value in every locale (`en.ts`), or it is a type error.\n */\nexport const keys = {\n\tpluginName: 'analytics:pluginName',\n\ttabAnalytics: 'analytics:tabAnalytics',\n\tmetricPageviews: 'analytics:metricPageviews',\n\tmetricVisitors: 'analytics:metricVisitors',\n\tmetricSessions: 'analytics:metricSessions',\n\tmetricEvents: 'analytics:metricEvents',\n\tmetricAvgDuration: 'analytics:metricAvgDuration',\n\tmetricBounceRate: 'analytics:metricBounceRate',\n\tmetricScrollDepth: 'analytics:metricScrollDepth',\n\tmetricConversions: 'analytics:metricConversions',\n\tmetricRevenue: 'analytics:metricRevenue',\n\tstateNoData: 'analytics:stateNoData',\n\tstateNew: 'analytics:stateNew',\n\tstateNotBound: 'analytics:stateNotBound',\n\tstateNotConfigured: 'analytics:stateNotConfigured',\n\tstateUnavailable: 'analytics:stateUnavailable',\n\ttimeframeToday: 'analytics:timeframeToday',\n\ttimeframeLast7Days: 'analytics:timeframeLast7Days',\n\ttimeframeLast30Days: 'analytics:timeframeLast30Days',\n\ttimeframeLast90Days: 'analytics:timeframeLast90Days',\n\ttimeframeThisMonth: 'analytics:timeframeThisMonth',\n\ttimeframeThisYear: 'analytics:timeframeThisYear',\n\ttimeframeLastYear: 'analytics:timeframeLastYear',\n\ttimeframeAllTime: 'analytics:timeframeAllTime',\n\twidgetMetricLabel: 'analytics:widgetMetricLabel',\n\twidgetTrendLabel: 'analytics:widgetTrendLabel',\n\twidgetBreakdownPages: 'analytics:widgetBreakdownPages',\n\twidgetBreakdownSources: 'analytics:widgetBreakdownSources',\n\twidgetBreakdownDevices: 'analytics:widgetBreakdownDevices',\n\twidgetBreakdownCountries: 'analytics:widgetBreakdownCountries',\n\twidgetFieldLimit: 'analytics:widgetFieldLimit',\n\tstateNoBreakdown: 'analytics:stateNoBreakdown',\n\tstateClamped: 'analytics:stateClamped',\n\twidgetFieldTitle: 'analytics:widgetFieldTitle',\n\twidgetFieldMetric: 'analytics:widgetFieldMetric',\n\twidgetFieldTimeframe: 'analytics:widgetFieldTimeframe',\n\twidgetFieldDataSource: 'analytics:widgetFieldDataSource',\n\twidgetFieldTitlePlaceholder: 'analytics:widgetFieldTitlePlaceholder',\n\twidgetTimeframeCustom: 'analytics:widgetTimeframeCustom',\n\twidgetFieldRange: 'analytics:widgetFieldRange',\n\tdateRangePlaceholder: 'analytics:dateRangePlaceholder',\n\twidgetFieldWindow: 'analytics:widgetFieldWindow',\n\twidgetRealtimeLabel: 'analytics:widgetRealtimeLabel',\n\twidgetRealtimeCaption: 'analytics:widgetRealtimeCaption',\n\twidgetRealtimePaused: 'analytics:widgetRealtimePaused',\n\tcomparisonVsPrevious: 'analytics:comparisonVsPrevious',\n\tcomparisonIncrease: 'analytics:comparisonIncrease',\n\tcomparisonDecrease: 'analytics:comparisonDecrease',\n\tcomparisonNoChange: 'analytics:comparisonNoChange',\n\tprovidersCollectionSingular: 'analytics:providersCollectionSingular',\n\tprovidersCollectionPlural: 'analytics:providersCollectionPlural',\n\tproviderFieldProvider: 'analytics:providerFieldProvider',\n\tproviderFieldEnabled: 'analytics:providerFieldEnabled',\n\tproviderFieldScope: 'analytics:providerFieldScope',\n\tproviderFieldSiteId: 'analytics:providerFieldSiteId',\n\tproviderFieldApiKey: 'analytics:providerFieldApiKey',\n\tproviderFieldHost: 'analytics:providerFieldHost',\n\tproviderFieldHostHelp: 'analytics:providerFieldHostHelp',\n\tproviderFieldWebsiteId: 'analytics:providerFieldWebsiteId',\n\tproviderFieldToken: 'analytics:providerFieldToken',\n\tproviderFieldPropertyId: 'analytics:providerFieldPropertyId',\n\tproviderFieldClientEmail: 'analytics:providerFieldClientEmail',\n\tproviderFieldPrivateKey: 'analytics:providerFieldPrivateKey',\n\tproviderFieldProjectId: 'analytics:providerFieldProjectId',\n\tproviderSecretHelp: 'analytics:providerSecretHelp',\n\tproviderNamePlausible: 'analytics:providerNamePlausible',\n\tproviderNameUmami: 'analytics:providerNameUmami',\n\tproviderNameGa4: 'analytics:providerNameGa4',\n\tproviderNamePosthog: 'analytics:providerNamePosthog',\n} as const\n\nexport type TranslationKey = (typeof keys)[keyof typeof keys]\n"],"mappings":";;;;;;AAKA,MAAa,OAAO;CACnB,YAAY;CACZ,cAAc;CACd,iBAAiB;CACjB,gBAAgB;CAChB,gBAAgB;CAChB,cAAc;CACd,mBAAmB;CACnB,kBAAkB;CAClB,mBAAmB;CACnB,mBAAmB;CACnB,eAAe;CACf,aAAa;CACb,UAAU;CACV,eAAe;CACf,oBAAoB;CACpB,kBAAkB;CAClB,gBAAgB;CAChB,oBAAoB;CACpB,qBAAqB;CACrB,qBAAqB;CACrB,oBAAoB;CACpB,mBAAmB;CACnB,mBAAmB;CACnB,kBAAkB;CAClB,mBAAmB;CACnB,kBAAkB;CAClB,sBAAsB;CACtB,wBAAwB;CACxB,wBAAwB;CACxB,0BAA0B;CAC1B,kBAAkB;CAClB,kBAAkB;CAClB,cAAc;CACd,kBAAkB;CAClB,mBAAmB;CACnB,sBAAsB;CACtB,uBAAuB;CACvB,6BAA6B;CAC7B,uBAAuB;CACvB,kBAAkB;CAClB,sBAAsB;CACtB,mBAAmB;CACnB,qBAAqB;CACrB,uBAAuB;CACvB,sBAAsB;CACtB,sBAAsB;CACtB,oBAAoB;CACpB,oBAAoB;CACpB,oBAAoB;CACpB,6BAA6B;CAC7B,2BAA2B;CAC3B,uBAAuB;CACvB,sBAAsB;CACtB,oBAAoB;CACpB,qBAAqB;CACrB,qBAAqB;CACrB,mBAAmB;CACnB,uBAAuB;CACvB,wBAAwB;CACxB,oBAAoB;CACpB,yBAAyB;CACzB,0BAA0B;CAC1B,yBAAyB;CACzB,wBAAwB;CACxB,oBAAoB;CACpB,uBAAuB;CACvB,mBAAmB;CACnB,iBAAiB;CACjB,qBAAqB;AACtB"}
|
|
@@ -8,8 +8,6 @@ const METRIC_KEYS = {
|
|
|
8
8
|
events: keys.metricEvents,
|
|
9
9
|
avgDuration: keys.metricAvgDuration,
|
|
10
10
|
bounceRate: keys.metricBounceRate,
|
|
11
|
-
entries: keys.metricEntries,
|
|
12
|
-
exits: keys.metricExits,
|
|
13
11
|
scrollDepth: keys.metricScrollDepth,
|
|
14
12
|
conversions: keys.metricConversions,
|
|
15
13
|
revenue: keys.metricRevenue
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"metricKeys.js","names":[],"sources":["../../src/translations/metricKeys.ts"],"sourcesContent":["import type { MetricKey } from '../core/contract'\nimport type { TimeframePreset } from '../timeframe/presets'\nimport type { TranslationKey } from './keys'\nimport { keys } from './keys'\n\nexport const METRIC_KEYS: Record<MetricKey, TranslationKey> = {\n\tpageviews: keys.metricPageviews,\n\tvisitors: keys.metricVisitors,\n\tvisits: keys.metricVisitors,\n\tsessions: keys.metricSessions,\n\tevents: keys.metricEvents,\n\tavgDuration: keys.metricAvgDuration,\n\tbounceRate: keys.metricBounceRate,\n\
|
|
1
|
+
{"version":3,"file":"metricKeys.js","names":[],"sources":["../../src/translations/metricKeys.ts"],"sourcesContent":["import type { MetricKey } from '../core/contract'\nimport type { TimeframePreset } from '../timeframe/presets'\nimport type { TranslationKey } from './keys'\nimport { keys } from './keys'\n\nexport const METRIC_KEYS: Record<MetricKey, TranslationKey> = {\n\tpageviews: keys.metricPageviews,\n\tvisitors: keys.metricVisitors,\n\tvisits: keys.metricVisitors,\n\tsessions: keys.metricSessions,\n\tevents: keys.metricEvents,\n\tavgDuration: keys.metricAvgDuration,\n\tbounceRate: keys.metricBounceRate,\n\tscrollDepth: keys.metricScrollDepth,\n\tconversions: keys.metricConversions,\n\trevenue: keys.metricRevenue,\n}\n\nexport const TIMEFRAME_KEYS: Record<TimeframePreset, TranslationKey> = {\n\ttoday: keys.timeframeToday,\n\tlast7days: keys.timeframeLast7Days,\n\tlast30days: keys.timeframeLast30Days,\n\tlast90days: keys.timeframeLast90Days,\n\tthisMonth: keys.timeframeThisMonth,\n\tthisYear: keys.timeframeThisYear,\n\tlastYear: keys.timeframeLastYear,\n\tallTime: keys.timeframeAllTime,\n}\n"],"mappings":";;AAKA,MAAa,cAAiD;CAC7D,WAAW,KAAK;CAChB,UAAU,KAAK;CACf,QAAQ,KAAK;CACb,UAAU,KAAK;CACf,QAAQ,KAAK;CACb,aAAa,KAAK;CAClB,YAAY,KAAK;CACjB,aAAa,KAAK;CAClB,aAAa,KAAK;CAClB,SAAS,KAAK;AACf;AAEA,MAAa,iBAA0D;CACtE,OAAO,KAAK;CACZ,WAAW,KAAK;CAChB,YAAY,KAAK;CACjB,YAAY,KAAK;CACjB,WAAW,KAAK;CAChB,UAAU,KAAK;CACf,UAAU,KAAK;CACf,SAAS,KAAK;AACf"}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { keys } from "../translations/keys.js";
|
|
2
2
|
import { asTranslate } from "../translations/server.js";
|
|
3
|
+
import { TIMEFRAME_KEYS } from "../translations/metricKeys.js";
|
|
3
4
|
import { breakdownSpecBySlug } from "./breakdownTypes.js";
|
|
4
5
|
import { formatRangeCaption, resolveCustomRange } from "./range.js";
|
|
5
6
|
import { readForWidgetBreakdown } from "./readForWidgetBreakdown.js";
|
|
6
|
-
import { TIMEFRAME_KEYS } from "../translations/metricKeys.js";
|
|
7
7
|
import { BarList } from "../charts/BarList.js";
|
|
8
8
|
import { formatMetricValue } from "../fields/format.js";
|
|
9
9
|
import { cardStyle, labelStyle } from "./cardChrome.js";
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { keys } from "../translations/keys.js";
|
|
2
2
|
import { asTranslate } from "../translations/server.js";
|
|
3
|
+
import { METRIC_KEYS, TIMEFRAME_KEYS } from "../translations/metricKeys.js";
|
|
3
4
|
import { formatRangeCaption, resolveCustomRange } from "./range.js";
|
|
4
5
|
import { readForWidget } from "./readForWidget.js";
|
|
5
|
-
import { METRIC_KEYS, TIMEFRAME_KEYS } from "../translations/metricKeys.js";
|
|
6
6
|
import { formatMetricValue } from "../fields/format.js";
|
|
7
7
|
import { cardStyle, labelStyle } from "./cardChrome.js";
|
|
8
|
+
import { ComparisonDelta } from "./ComparisonDelta.js";
|
|
8
9
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
9
10
|
//#region src/widgets/AnalyticsMetricWidget.tsx
|
|
10
11
|
const STATE_KEY = {
|
|
@@ -65,6 +66,13 @@ async function AnalyticsMetricWidget(props) {
|
|
|
65
66
|
},
|
|
66
67
|
children: caption
|
|
67
68
|
}),
|
|
69
|
+
result.comparisonRange ? /* @__PURE__ */ jsx(ComparisonDelta, {
|
|
70
|
+
current: value,
|
|
71
|
+
previous: result.previousMetrics?.[metric],
|
|
72
|
+
metric,
|
|
73
|
+
locale,
|
|
74
|
+
t
|
|
75
|
+
}) : null,
|
|
68
76
|
result.clamped ? /* @__PURE__ */ jsx("span", {
|
|
69
77
|
style: {
|
|
70
78
|
fontSize: "0.6875rem",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnalyticsMetricWidget.js","names":[],"sources":["../../src/widgets/AnalyticsMetricWidget.tsx"],"sourcesContent":["import type { WidgetServerProps } from 'payload'\nimport type { MetricKey } from '../core/contract'\nimport { formatMetricValue } from '../fields/format'\nimport type { TimeframePreset } from '../timeframe/presets'\nimport { keys, type TranslationKey } from '../translations/keys'\nimport { METRIC_KEYS, TIMEFRAME_KEYS } from '../translations/metricKeys'\nimport { asTranslate } from '../translations/server'\nimport { cardStyle, labelStyle } from './cardChrome'\nimport { formatRangeCaption, resolveCustomRange } from './range'\nimport { readForWidget, type WidgetReadStatus } from './readForWidget'\nimport type { MetricWidgetData } from './types'\n\nconst STATE_KEY: Record<Exclude<WidgetReadStatus, 'ok'>, TranslationKey> = {\n\t'not-configured': keys.stateNotConfigured,\n\tunavailable: keys.stateUnavailable,\n}\n\nexport default async function AnalyticsMetricWidget(props: WidgetServerProps) {\n\tconst data = (props.widgetData ?? {}) as MetricWidgetData\n\tconst metric: MetricKey = data.metric ?? 'pageviews'\n\tconst rawTimeframe = data.timeframe ?? 'last30days'\n\tconst customRange = resolveCustomRange(rawTimeframe, data.range)\n\tconst timeframe: TimeframePreset = rawTimeframe === 'custom' ? 'last30days' : rawTimeframe\n\tconst t = asTranslate(props.req.i18n.t)\n\tconst locale = props.req.i18n.language ?? 'en-US'\n\tconst title = data.title?.trim() || t(METRIC_KEYS[metric])\n\tconst caption = customRange\n\t\t? formatRangeCaption(customRange, locale)\n\t\t: t(TIMEFRAME_KEYS[timeframe])\n\n\tconst result = await readForWidget({\n\t\treq: props.req,\n\t\tmetrics: [metric],\n\t\ttimeframe,\n\t\tadapterId: data.dataSource,\n\t\tnow: new Date(),\n\t\trange: customRange,\n\t})\n\n\tif (result.status !== 'ok') {\n\t\treturn (\n\t\t\t<div className=\"analytics-metric-widget\" style={cardStyle}>\n\t\t\t\t<span style={labelStyle}>{title}</span>\n\t\t\t\t<span style={{ color: 'var(--theme-elevation-400)' }}>{t(STATE_KEY[result.status])}</span>\n\t\t\t</div>\n\t\t)\n\t}\n\n\tconst value = result.metrics[metric]\n\treturn (\n\t\t<div className=\"analytics-metric-widget\" style={cardStyle}>\n\t\t\t<span style={labelStyle}>{title}</span>\n\t\t\t<span\n\t\t\t\tstyle={{\n\t\t\t\t\tfontSize: '2rem',\n\t\t\t\t\tfontWeight: 700,\n\t\t\t\t\tlineHeight: 1.1,\n\t\t\t\t\tcolor: 'var(--theme-elevation-800)',\n\t\t\t\t}}\n\t\t\t>\n\t\t\t\t{value === undefined ? '–' : formatMetricValue(metric, value, locale)}\n\t\t\t</span>\n\t\t\t<span style={{ fontSize: '0.75rem', color: 'var(--theme-elevation-400)' }}>{caption}</span>\n\t\t\t{result.clamped ? (\n\t\t\t\t<span style={{ fontSize: '0.6875rem', color: 'var(--theme-elevation-400)' }}>\n\t\t\t\t\t{t(keys.stateClamped)}\n\t\t\t\t</span>\n\t\t\t) : null}\n\t\t</div>\n\t)\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"AnalyticsMetricWidget.js","names":[],"sources":["../../src/widgets/AnalyticsMetricWidget.tsx"],"sourcesContent":["import type { WidgetServerProps } from 'payload'\nimport type { MetricKey } from '../core/contract'\nimport { formatMetricValue } from '../fields/format'\nimport type { TimeframePreset } from '../timeframe/presets'\nimport { keys, type TranslationKey } from '../translations/keys'\nimport { METRIC_KEYS, TIMEFRAME_KEYS } from '../translations/metricKeys'\nimport { asTranslate } from '../translations/server'\nimport { ComparisonDelta } from './ComparisonDelta'\nimport { cardStyle, labelStyle } from './cardChrome'\nimport { formatRangeCaption, resolveCustomRange } from './range'\nimport { readForWidget, type WidgetReadStatus } from './readForWidget'\nimport type { MetricWidgetData } from './types'\n\nconst STATE_KEY: Record<Exclude<WidgetReadStatus, 'ok'>, TranslationKey> = {\n\t'not-configured': keys.stateNotConfigured,\n\tunavailable: keys.stateUnavailable,\n}\n\nexport default async function AnalyticsMetricWidget(props: WidgetServerProps) {\n\tconst data = (props.widgetData ?? {}) as MetricWidgetData\n\tconst metric: MetricKey = data.metric ?? 'pageviews'\n\tconst rawTimeframe = data.timeframe ?? 'last30days'\n\tconst customRange = resolveCustomRange(rawTimeframe, data.range)\n\tconst timeframe: TimeframePreset = rawTimeframe === 'custom' ? 'last30days' : rawTimeframe\n\tconst t = asTranslate(props.req.i18n.t)\n\tconst locale = props.req.i18n.language ?? 'en-US'\n\tconst title = data.title?.trim() || t(METRIC_KEYS[metric])\n\tconst caption = customRange\n\t\t? formatRangeCaption(customRange, locale)\n\t\t: t(TIMEFRAME_KEYS[timeframe])\n\n\tconst result = await readForWidget({\n\t\treq: props.req,\n\t\tmetrics: [metric],\n\t\ttimeframe,\n\t\tadapterId: data.dataSource,\n\t\tnow: new Date(),\n\t\trange: customRange,\n\t})\n\n\tif (result.status !== 'ok') {\n\t\treturn (\n\t\t\t<div className=\"analytics-metric-widget\" style={cardStyle}>\n\t\t\t\t<span style={labelStyle}>{title}</span>\n\t\t\t\t<span style={{ color: 'var(--theme-elevation-400)' }}>{t(STATE_KEY[result.status])}</span>\n\t\t\t</div>\n\t\t)\n\t}\n\n\tconst value = result.metrics[metric]\n\treturn (\n\t\t<div className=\"analytics-metric-widget\" style={cardStyle}>\n\t\t\t<span style={labelStyle}>{title}</span>\n\t\t\t<span\n\t\t\t\tstyle={{\n\t\t\t\t\tfontSize: '2rem',\n\t\t\t\t\tfontWeight: 700,\n\t\t\t\t\tlineHeight: 1.1,\n\t\t\t\t\tcolor: 'var(--theme-elevation-800)',\n\t\t\t\t}}\n\t\t\t>\n\t\t\t\t{value === undefined ? '–' : formatMetricValue(metric, value, locale)}\n\t\t\t</span>\n\t\t\t<span style={{ fontSize: '0.75rem', color: 'var(--theme-elevation-400)' }}>{caption}</span>\n\t\t\t{result.comparisonRange ? (\n\t\t\t\t<ComparisonDelta\n\t\t\t\t\tcurrent={value}\n\t\t\t\t\tprevious={result.previousMetrics?.[metric]}\n\t\t\t\t\tmetric={metric}\n\t\t\t\t\tlocale={locale}\n\t\t\t\t\tt={t}\n\t\t\t\t/>\n\t\t\t) : null}\n\t\t\t{result.clamped ? (\n\t\t\t\t<span style={{ fontSize: '0.6875rem', color: 'var(--theme-elevation-400)' }}>\n\t\t\t\t\t{t(keys.stateClamped)}\n\t\t\t\t</span>\n\t\t\t) : null}\n\t\t</div>\n\t)\n}\n"],"mappings":";;;;;;;;;;AAaA,MAAM,YAAqE;CAC1E,kBAAkB,KAAK;CACvB,aAAa,KAAK;AACnB;AAEA,eAA8B,sBAAsB,OAA0B;CAC7E,MAAM,OAAQ,MAAM,cAAc,CAAC;CACnC,MAAM,SAAoB,KAAK,UAAU;CACzC,MAAM,eAAe,KAAK,aAAa;CACvC,MAAM,cAAc,mBAAmB,cAAc,KAAK,KAAK;CAC/D,MAAM,YAA6B,iBAAiB,WAAW,eAAe;CAC9E,MAAM,IAAI,YAAY,MAAM,IAAI,KAAK,CAAC;CACtC,MAAM,SAAS,MAAM,IAAI,KAAK,YAAY;CAC1C,MAAM,QAAQ,KAAK,OAAO,KAAK,KAAK,EAAE,YAAY,OAAO;CACzD,MAAM,UAAU,cACb,mBAAmB,aAAa,MAAM,IACtC,EAAE,eAAe,UAAU;CAE9B,MAAM,SAAS,MAAM,cAAc;EAClC,KAAK,MAAM;EACX,SAAS,CAAC,MAAM;EAChB;EACA,WAAW,KAAK;EAChB,qBAAK,IAAI,KAAK;EACd,OAAO;CACR,CAAC;CAED,IAAI,OAAO,WAAW,MACrB,OACC,qBAAC,OAAD;EAAK,WAAU;EAA0B,OAAO;YAAhD,CACC,oBAAC,QAAD;GAAM,OAAO;aAAa;EAAY,CAAA,GACtC,oBAAC,QAAD;GAAM,OAAO,EAAE,OAAO,6BAA6B;aAAI,EAAE,UAAU,OAAO,OAAO;EAAQ,CAAA,CACrF;;CAIP,MAAM,QAAQ,OAAO,QAAQ;CAC7B,OACC,qBAAC,OAAD;EAAK,WAAU;EAA0B,OAAO;YAAhD;GACC,oBAAC,QAAD;IAAM,OAAO;cAAa;GAAY,CAAA;GACtC,oBAAC,QAAD;IACC,OAAO;KACN,UAAU;KACV,YAAY;KACZ,YAAY;KACZ,OAAO;IACR;cAEC,UAAU,KAAA,IAAY,MAAM,kBAAkB,QAAQ,OAAO,MAAM;GAC/D,CAAA;GACN,oBAAC,QAAD;IAAM,OAAO;KAAE,UAAU;KAAW,OAAO;IAA6B;cAAI;GAAc,CAAA;GACzF,OAAO,kBACP,oBAAC,iBAAD;IACC,SAAS;IACT,UAAU,OAAO,kBAAkB;IAC3B;IACA;IACL;GACH,CAAA,IACE;GACH,OAAO,UACP,oBAAC,QAAD;IAAM,OAAO;KAAE,UAAU;KAAa,OAAO;IAA6B;cACxE,EAAE,KAAK,YAAY;GACf,CAAA,IACH;EACA;;AAEP"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { keys } from "../translations/keys.js";
|
|
2
2
|
import { asTranslate } from "../translations/server.js";
|
|
3
|
+
import { METRIC_KEYS } from "../translations/metricKeys.js";
|
|
3
4
|
import { readForWidgetRealtime } from "./readForWidgetRealtime.js";
|
|
4
5
|
import { REALTIME_PATH } from "../plugin/realtimeEndpoint.js";
|
|
5
|
-
import { METRIC_KEYS } from "../translations/metricKeys.js";
|
|
6
6
|
import { RealtimeCounter } from "./RealtimeCounter.js";
|
|
7
7
|
import { cardStyle, labelStyle } from "./cardChrome.js";
|
|
8
8
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { keys } from "../translations/keys.js";
|
|
2
2
|
import { asTranslate } from "../translations/server.js";
|
|
3
|
-
import { formatRangeCaption, resolveCustomRange } from "./range.js";
|
|
4
3
|
import { readForWidgetSeries } from "./readForWidgetSeries.js";
|
|
5
4
|
import { METRIC_KEYS, TIMEFRAME_KEYS } from "../translations/metricKeys.js";
|
|
5
|
+
import { formatRangeCaption, resolveCustomRange } from "./range.js";
|
|
6
6
|
import { TrendChart } from "../charts/TrendChart.js";
|
|
7
7
|
import { formatMetricValue } from "../fields/format.js";
|
|
8
8
|
import { cardStyle, labelStyle } from "./cardChrome.js";
|
|
9
|
+
import { ComparisonDelta } from "./ComparisonDelta.js";
|
|
9
10
|
import { bucketByRange, bucketSeries } from "../charts/bucket.js";
|
|
10
11
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
11
12
|
//#region src/widgets/AnalyticsTrendWidget.tsx
|
|
@@ -42,7 +43,7 @@ async function AnalyticsTrendWidget(props) {
|
|
|
42
43
|
})]
|
|
43
44
|
});
|
|
44
45
|
const caption = customRange ? formatRangeCaption(customRange, locale) : t(TIMEFRAME_KEYS[timeframe]);
|
|
45
|
-
const trendPoints = (customRange ? bucketByRange(result.points, customRange) : bucketSeries(result.points, timeframe,
|
|
46
|
+
const trendPoints = (customRange ? bucketByRange(result.points, customRange, result.timezone) : bucketSeries(result.points, timeframe, result.timezone)).map((b) => ({
|
|
46
47
|
...b,
|
|
47
48
|
display: formatMetricValue(metric, b.value, locale)
|
|
48
49
|
}));
|
|
@@ -75,6 +76,13 @@ async function AnalyticsTrendWidget(props) {
|
|
|
75
76
|
},
|
|
76
77
|
children: caption
|
|
77
78
|
}),
|
|
79
|
+
result.comparisonRange ? /* @__PURE__ */ jsx(ComparisonDelta, {
|
|
80
|
+
current: result.total,
|
|
81
|
+
previous: result.previousTotal,
|
|
82
|
+
metric,
|
|
83
|
+
locale,
|
|
84
|
+
t
|
|
85
|
+
}) : null,
|
|
78
86
|
result.clamped ? /* @__PURE__ */ jsx("span", {
|
|
79
87
|
style: {
|
|
80
88
|
fontSize: "0.6875rem",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnalyticsTrendWidget.js","names":[],"sources":["../../src/widgets/AnalyticsTrendWidget.tsx"],"sourcesContent":["import type { WidgetServerProps } from 'payload'\nimport { bucketByRange, bucketSeries } from '../charts/bucket'\nimport { TrendChart } from '../charts/TrendChart'\nimport type { MetricKey } from '../core/contract'\nimport { formatMetricValue } from '../fields/format'\nimport type { TimeframePreset } from '../timeframe/presets'\nimport { keys, type TranslationKey } from '../translations/keys'\nimport { METRIC_KEYS, TIMEFRAME_KEYS } from '../translations/metricKeys'\nimport { asTranslate } from '../translations/server'\nimport { cardStyle, labelStyle } from './cardChrome'\nimport { formatRangeCaption, resolveCustomRange } from './range'\nimport type { WidgetReadStatus } from './readForWidget'\nimport { readForWidgetSeries } from './readForWidgetSeries'\nimport type { MetricWidgetData } from './types'\n\nconst STATE_KEY: Record<Exclude<WidgetReadStatus, 'ok'>, TranslationKey> = {\n\t'not-configured': keys.stateNotConfigured,\n\tunavailable: keys.stateUnavailable,\n}\n\nexport default async function AnalyticsTrendWidget(props: WidgetServerProps) {\n\tconst data = (props.widgetData ?? {}) as MetricWidgetData\n\tconst metric: MetricKey = data.metric ?? 'pageviews'\n\tconst rawTimeframe = data.timeframe ?? 'last30days'\n\tconst customRange = resolveCustomRange(rawTimeframe, data.range)\n\tconst timeframe: TimeframePreset = rawTimeframe === 'custom' ? 'last30days' : rawTimeframe\n\tconst t = asTranslate(props.req.i18n.t)\n\tconst locale = props.req.i18n.language ?? 'en-US'\n\tconst title = data.title?.trim() || t(METRIC_KEYS[metric])\n\n\tconst result = await readForWidgetSeries({\n\t\treq: props.req,\n\t\tmetric,\n\t\ttimeframe,\n\t\tadapterId: data.dataSource,\n\t\tnow: new Date(),\n\t\trange: customRange,\n\t})\n\n\tif (result.status !== 'ok') {\n\t\treturn (\n\t\t\t<div className=\"analytics-trend-widget\" style={cardStyle}>\n\t\t\t\t<span style={labelStyle}>{title}</span>\n\t\t\t\t<span style={{ color: 'var(--theme-elevation-400)' }}>{t(STATE_KEY[result.status])}</span>\n\t\t\t</div>\n\t\t)\n\t}\n\n\tconst caption = customRange\n\t\t? formatRangeCaption(customRange, locale)\n\t\t: t(TIMEFRAME_KEYS[timeframe])\n\tconst buckets = customRange\n\t\t? bucketByRange(result.points, customRange)\n\t\t: bucketSeries(result.points, timeframe,
|
|
1
|
+
{"version":3,"file":"AnalyticsTrendWidget.js","names":[],"sources":["../../src/widgets/AnalyticsTrendWidget.tsx"],"sourcesContent":["import type { WidgetServerProps } from 'payload'\nimport { bucketByRange, bucketSeries } from '../charts/bucket'\nimport { TrendChart } from '../charts/TrendChart'\nimport type { MetricKey } from '../core/contract'\nimport { formatMetricValue } from '../fields/format'\nimport type { TimeframePreset } from '../timeframe/presets'\nimport { keys, type TranslationKey } from '../translations/keys'\nimport { METRIC_KEYS, TIMEFRAME_KEYS } from '../translations/metricKeys'\nimport { asTranslate } from '../translations/server'\nimport { ComparisonDelta } from './ComparisonDelta'\nimport { cardStyle, labelStyle } from './cardChrome'\nimport { formatRangeCaption, resolveCustomRange } from './range'\nimport type { WidgetReadStatus } from './readForWidget'\nimport { readForWidgetSeries } from './readForWidgetSeries'\nimport type { MetricWidgetData } from './types'\n\nconst STATE_KEY: Record<Exclude<WidgetReadStatus, 'ok'>, TranslationKey> = {\n\t'not-configured': keys.stateNotConfigured,\n\tunavailable: keys.stateUnavailable,\n}\n\nexport default async function AnalyticsTrendWidget(props: WidgetServerProps) {\n\tconst data = (props.widgetData ?? {}) as MetricWidgetData\n\tconst metric: MetricKey = data.metric ?? 'pageviews'\n\tconst rawTimeframe = data.timeframe ?? 'last30days'\n\tconst customRange = resolveCustomRange(rawTimeframe, data.range)\n\tconst timeframe: TimeframePreset = rawTimeframe === 'custom' ? 'last30days' : rawTimeframe\n\tconst t = asTranslate(props.req.i18n.t)\n\tconst locale = props.req.i18n.language ?? 'en-US'\n\tconst title = data.title?.trim() || t(METRIC_KEYS[metric])\n\n\tconst result = await readForWidgetSeries({\n\t\treq: props.req,\n\t\tmetric,\n\t\ttimeframe,\n\t\tadapterId: data.dataSource,\n\t\tnow: new Date(),\n\t\trange: customRange,\n\t})\n\n\tif (result.status !== 'ok') {\n\t\treturn (\n\t\t\t<div className=\"analytics-trend-widget\" style={cardStyle}>\n\t\t\t\t<span style={labelStyle}>{title}</span>\n\t\t\t\t<span style={{ color: 'var(--theme-elevation-400)' }}>{t(STATE_KEY[result.status])}</span>\n\t\t\t</div>\n\t\t)\n\t}\n\n\tconst caption = customRange\n\t\t? formatRangeCaption(customRange, locale)\n\t\t: t(TIMEFRAME_KEYS[timeframe])\n\tconst buckets = customRange\n\t\t? bucketByRange(result.points, customRange, result.timezone)\n\t\t: bucketSeries(result.points, timeframe, result.timezone)\n\tconst trendPoints = buckets.map((b) => ({\n\t\t...b,\n\t\tdisplay: formatMetricValue(metric, b.value, locale),\n\t}))\n\treturn (\n\t\t<div className=\"analytics-trend-widget\" style={cardStyle}>\n\t\t\t<span style={labelStyle}>{title}</span>\n\t\t\t<span\n\t\t\t\tstyle={{\n\t\t\t\t\tfontSize: '1.75rem',\n\t\t\t\t\tfontWeight: 700,\n\t\t\t\t\tlineHeight: 1.1,\n\t\t\t\t\tcolor: 'var(--theme-elevation-800)',\n\t\t\t\t}}\n\t\t\t>\n\t\t\t\t{formatMetricValue(metric, result.total, locale)}\n\t\t\t</span>\n\t\t\t<TrendChart buckets={trendPoints} ariaLabel={`${title} ${caption}`} minHeight={180} />\n\t\t\t<span style={{ fontSize: '0.75rem', color: 'var(--theme-elevation-400)' }}>{caption}</span>\n\t\t\t{result.comparisonRange ? (\n\t\t\t\t<ComparisonDelta\n\t\t\t\t\tcurrent={result.total}\n\t\t\t\t\tprevious={result.previousTotal}\n\t\t\t\t\tmetric={metric}\n\t\t\t\t\tlocale={locale}\n\t\t\t\t\tt={t}\n\t\t\t\t/>\n\t\t\t) : null}\n\t\t\t{result.clamped ? (\n\t\t\t\t<span style={{ fontSize: '0.6875rem', color: 'var(--theme-elevation-400)' }}>\n\t\t\t\t\t{t(keys.stateClamped)}\n\t\t\t\t</span>\n\t\t\t) : null}\n\t\t</div>\n\t)\n}\n"],"mappings":";;;;;;;;;;;;AAgBA,MAAM,YAAqE;CAC1E,kBAAkB,KAAK;CACvB,aAAa,KAAK;AACnB;AAEA,eAA8B,qBAAqB,OAA0B;CAC5E,MAAM,OAAQ,MAAM,cAAc,CAAC;CACnC,MAAM,SAAoB,KAAK,UAAU;CACzC,MAAM,eAAe,KAAK,aAAa;CACvC,MAAM,cAAc,mBAAmB,cAAc,KAAK,KAAK;CAC/D,MAAM,YAA6B,iBAAiB,WAAW,eAAe;CAC9E,MAAM,IAAI,YAAY,MAAM,IAAI,KAAK,CAAC;CACtC,MAAM,SAAS,MAAM,IAAI,KAAK,YAAY;CAC1C,MAAM,QAAQ,KAAK,OAAO,KAAK,KAAK,EAAE,YAAY,OAAO;CAEzD,MAAM,SAAS,MAAM,oBAAoB;EACxC,KAAK,MAAM;EACX;EACA;EACA,WAAW,KAAK;EAChB,qBAAK,IAAI,KAAK;EACd,OAAO;CACR,CAAC;CAED,IAAI,OAAO,WAAW,MACrB,OACC,qBAAC,OAAD;EAAK,WAAU;EAAyB,OAAO;YAA/C,CACC,oBAAC,QAAD;GAAM,OAAO;aAAa;EAAY,CAAA,GACtC,oBAAC,QAAD;GAAM,OAAO,EAAE,OAAO,6BAA6B;aAAI,EAAE,UAAU,OAAO,OAAO;EAAQ,CAAA,CACrF;;CAIP,MAAM,UAAU,cACb,mBAAmB,aAAa,MAAM,IACtC,EAAE,eAAe,UAAU;CAI9B,MAAM,eAHU,cACb,cAAc,OAAO,QAAQ,aAAa,OAAO,QAAQ,IACzD,aAAa,OAAO,QAAQ,WAAW,OAAO,QAAQ,GAC7B,KAAK,OAAO;EACvC,GAAG;EACH,SAAS,kBAAkB,QAAQ,EAAE,OAAO,MAAM;CACnD,EAAE;CACF,OACC,qBAAC,OAAD;EAAK,WAAU;EAAyB,OAAO;YAA/C;GACC,oBAAC,QAAD;IAAM,OAAO;cAAa;GAAY,CAAA;GACtC,oBAAC,QAAD;IACC,OAAO;KACN,UAAU;KACV,YAAY;KACZ,YAAY;KACZ,OAAO;IACR;cAEC,kBAAkB,QAAQ,OAAO,OAAO,MAAM;GAC1C,CAAA;GACN,oBAAC,YAAD;IAAY,SAAS;IAAa,WAAW,GAAG,MAAM,GAAG;IAAW,WAAW;GAAM,CAAA;GACrF,oBAAC,QAAD;IAAM,OAAO;KAAE,UAAU;KAAW,OAAO;IAA6B;cAAI;GAAc,CAAA;GACzF,OAAO,kBACP,oBAAC,iBAAD;IACC,SAAS,OAAO;IAChB,UAAU,OAAO;IACT;IACA;IACL;GACH,CAAA,IACE;GACH,OAAO,UACP,oBAAC,QAAD;IAAM,OAAO;KAAE,UAAU;KAAa,OAAO;IAA6B;cACxE,EAAE,KAAK,YAAY;GACf,CAAA,IACH;EACA;;AAEP"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { keys } from "../translations/keys.js";
|
|
2
|
+
import { computeDelta } from "./comparison.js";
|
|
3
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
4
|
+
//#region src/widgets/ComparisonDelta.tsx
|
|
5
|
+
const SUCCESS = "var(--theme-success-500, rgb(21, 135, 186))";
|
|
6
|
+
const ERROR = "var(--theme-error-500, rgb(218, 75, 72))";
|
|
7
|
+
const NEUTRAL = "var(--theme-elevation-400)";
|
|
8
|
+
/** Metrics where a decrease is the good outcome, inverting the delta colors. */
|
|
9
|
+
const LOWER_IS_BETTER = new Set(["bounceRate"]);
|
|
10
|
+
const colorFor = (direction, metric) => {
|
|
11
|
+
if (direction === "none") return NEUTRAL;
|
|
12
|
+
const goodWhenDown = metric !== void 0 && LOWER_IS_BETTER.has(metric);
|
|
13
|
+
return direction === "up" !== goodWhenDown ? SUCCESS : ERROR;
|
|
14
|
+
};
|
|
15
|
+
const DIRECTION_LABEL = {
|
|
16
|
+
up: keys.comparisonIncrease,
|
|
17
|
+
down: keys.comparisonDecrease,
|
|
18
|
+
none: keys.comparisonNoChange
|
|
19
|
+
};
|
|
20
|
+
const DeltaArrow = ({ direction }) => /* @__PURE__ */ jsx("svg", {
|
|
21
|
+
"aria-hidden": "true",
|
|
22
|
+
focusable: "false",
|
|
23
|
+
width: "8",
|
|
24
|
+
height: "8",
|
|
25
|
+
viewBox: "0 0 8 8",
|
|
26
|
+
style: {
|
|
27
|
+
flexShrink: 0,
|
|
28
|
+
transform: direction === "down" ? "rotate(180deg)" : void 0
|
|
29
|
+
},
|
|
30
|
+
children: /* @__PURE__ */ jsx("path", {
|
|
31
|
+
d: "M4 1.2 7.3 6.8H0.7Z",
|
|
32
|
+
fill: "currentColor"
|
|
33
|
+
})
|
|
34
|
+
});
|
|
35
|
+
/**
|
|
36
|
+
* Period-over-period delta chip: a colored arrow, the percentage change, and a localized
|
|
37
|
+
* "vs. previous period" caption. Renders nothing when there is no comparable previous
|
|
38
|
+
* value; when the previous value is 0 (no percentage baseline) the localized direction
|
|
39
|
+
* word stands in for the number. All text routes through typed translation keys; the
|
|
40
|
+
* arrow is decorative and carried by an aria-label with the localized direction word.
|
|
41
|
+
*/
|
|
42
|
+
function ComparisonDelta({ current, previous, metric, locale, t }) {
|
|
43
|
+
const delta = computeDelta(current, previous);
|
|
44
|
+
if (!delta) return null;
|
|
45
|
+
const percentText = delta.percent === null ? null : new Intl.NumberFormat(locale, {
|
|
46
|
+
style: "percent",
|
|
47
|
+
maximumFractionDigits: 1,
|
|
48
|
+
signDisplay: "never"
|
|
49
|
+
}).format(Math.abs(delta.percent) / 100);
|
|
50
|
+
const visible = percentText ?? t(DIRECTION_LABEL[delta.direction]);
|
|
51
|
+
return /* @__PURE__ */ jsxs("span", {
|
|
52
|
+
role: "img",
|
|
53
|
+
"aria-label": `${t(DIRECTION_LABEL[delta.direction])} ${percentText ?? ""} ${t(keys.comparisonVsPrevious)}`.replace(/\s+/g, " ").trim(),
|
|
54
|
+
style: {
|
|
55
|
+
display: "inline-flex",
|
|
56
|
+
gap: "0.375rem",
|
|
57
|
+
alignItems: "center",
|
|
58
|
+
fontSize: "0.75rem",
|
|
59
|
+
lineHeight: 1.25
|
|
60
|
+
},
|
|
61
|
+
children: [/* @__PURE__ */ jsxs("span", {
|
|
62
|
+
style: {
|
|
63
|
+
display: "inline-flex",
|
|
64
|
+
alignItems: "center",
|
|
65
|
+
gap: "0.25rem",
|
|
66
|
+
color: colorFor(delta.direction, metric),
|
|
67
|
+
fontWeight: 600,
|
|
68
|
+
lineHeight: "inherit"
|
|
69
|
+
},
|
|
70
|
+
children: [delta.direction !== "none" ? /* @__PURE__ */ jsx(DeltaArrow, { direction: delta.direction }) : null, visible]
|
|
71
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
72
|
+
style: {
|
|
73
|
+
color: "var(--theme-elevation-400)",
|
|
74
|
+
lineHeight: "inherit"
|
|
75
|
+
},
|
|
76
|
+
children: t(keys.comparisonVsPrevious)
|
|
77
|
+
})]
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
//#endregion
|
|
81
|
+
export { ComparisonDelta };
|
|
82
|
+
|
|
83
|
+
//# sourceMappingURL=ComparisonDelta.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ComparisonDelta.js","names":[],"sources":["../../src/widgets/ComparisonDelta.tsx"],"sourcesContent":["import type { MetricKey } from '../core/contract'\nimport { keys, type TranslationKey } from '../translations/keys'\nimport { computeDelta, type DeltaDirection } from './comparison'\n\n// Payload's admin palette maps success to its blue ramp and error to its red ramp; the\n// fallbacks mirror @payloadcms/ui colors.scss so the chip renders identically outside\n// a themed admin shell.\nconst SUCCESS = 'var(--theme-success-500, rgb(21, 135, 186))'\nconst ERROR = 'var(--theme-error-500, rgb(218, 75, 72))'\nconst NEUTRAL = 'var(--theme-elevation-400)'\n\n/** Metrics where a decrease is the good outcome, inverting the delta colors. */\nconst LOWER_IS_BETTER: ReadonlySet<MetricKey> = new Set<MetricKey>(['bounceRate'])\n\nconst colorFor = (direction: DeltaDirection, metric?: MetricKey): string => {\n\tif (direction === 'none') {\n\t\treturn NEUTRAL\n\t}\n\tconst goodWhenDown = metric !== undefined && LOWER_IS_BETTER.has(metric)\n\tconst improved = (direction === 'up') !== goodWhenDown\n\treturn improved ? SUCCESS : ERROR\n}\n\nconst DIRECTION_LABEL: Record<DeltaDirection, TranslationKey> = {\n\tup: keys.comparisonIncrease,\n\tdown: keys.comparisonDecrease,\n\tnone: keys.comparisonNoChange,\n}\n\nconst DeltaArrow = ({ direction }: { direction: 'up' | 'down' }) => (\n\t<svg\n\t\taria-hidden=\"true\"\n\t\tfocusable=\"false\"\n\t\twidth=\"8\"\n\t\theight=\"8\"\n\t\tviewBox=\"0 0 8 8\"\n\t\tstyle={{\n\t\t\tflexShrink: 0,\n\t\t\ttransform: direction === 'down' ? 'rotate(180deg)' : undefined,\n\t\t}}\n\t>\n\t\t<path d=\"M4 1.2 7.3 6.8H0.7Z\" fill=\"currentColor\" />\n\t</svg>\n)\n\nexport interface ComparisonDeltaProps {\n\tcurrent?: number\n\tprevious?: number\n\t/** Inverts good/bad coloring for metrics in {@link LOWER_IS_BETTER}. */\n\tmetric?: MetricKey\n\tlocale: string\n\tt: (key: TranslationKey) => string\n}\n\n/**\n * Period-over-period delta chip: a colored arrow, the percentage change, and a localized\n * \"vs. previous period\" caption. Renders nothing when there is no comparable previous\n * value; when the previous value is 0 (no percentage baseline) the localized direction\n * word stands in for the number. All text routes through typed translation keys; the\n * arrow is decorative and carried by an aria-label with the localized direction word.\n */\nexport function ComparisonDelta({ current, previous, metric, locale, t }: ComparisonDeltaProps) {\n\tconst delta = computeDelta(current, previous)\n\tif (!delta) {\n\t\treturn null\n\t}\n\tconst percentText =\n\t\tdelta.percent === null\n\t\t\t? null\n\t\t\t: new Intl.NumberFormat(locale, {\n\t\t\t\t\tstyle: 'percent',\n\t\t\t\t\tmaximumFractionDigits: 1,\n\t\t\t\t\tsignDisplay: 'never',\n\t\t\t\t}).format(Math.abs(delta.percent) / 100)\n\t// An unchanged value is a real 0%, so it reads as one; only a zero previous value\n\t// (no percentage exists to report) falls back to the direction word.\n\tconst visible = percentText ?? t(DIRECTION_LABEL[delta.direction])\n\tconst ariaLabel = `${t(DIRECTION_LABEL[delta.direction])} ${percentText ?? ''} ${t(\n\t\tkeys.comparisonVsPrevious\n\t)}`\n\t\t.replace(/\\s+/g, ' ')\n\t\t.trim()\n\treturn (\n\t\t<span\n\t\t\trole=\"img\"\n\t\t\taria-label={ariaLabel}\n\t\t\tstyle={{\n\t\t\t\tdisplay: 'inline-flex',\n\t\t\t\tgap: '0.375rem',\n\t\t\t\t// Not `baseline`: the value's own flex box takes its baseline from the arrow\n\t\t\t\t// glyph rather than its text, which lifts the value off the caption's line.\n\t\t\t\talignItems: 'center',\n\t\t\t\tfontSize: '0.75rem',\n\t\t\t\tlineHeight: 1.25,\n\t\t\t}}\n\t\t>\n\t\t\t<span\n\t\t\t\tstyle={{\n\t\t\t\t\tdisplay: 'inline-flex',\n\t\t\t\t\talignItems: 'center',\n\t\t\t\t\tgap: '0.25rem',\n\t\t\t\t\tcolor: colorFor(delta.direction, metric),\n\t\t\t\t\tfontWeight: 600,\n\t\t\t\t\tlineHeight: 'inherit',\n\t\t\t\t}}\n\t\t\t>\n\t\t\t\t{delta.direction !== 'none' ? <DeltaArrow direction={delta.direction} /> : null}\n\t\t\t\t{visible}\n\t\t\t</span>\n\t\t\t<span style={{ color: 'var(--theme-elevation-400)', lineHeight: 'inherit' }}>\n\t\t\t\t{t(keys.comparisonVsPrevious)}\n\t\t\t</span>\n\t\t</span>\n\t)\n}\n"],"mappings":";;;;AAOA,MAAM,UAAU;AAChB,MAAM,QAAQ;AACd,MAAM,UAAU;;AAGhB,MAAM,kBAA0C,IAAI,IAAe,CAAC,YAAY,CAAC;AAEjF,MAAM,YAAY,WAA2B,WAA+B;CAC3E,IAAI,cAAc,QACjB,OAAO;CAER,MAAM,eAAe,WAAW,KAAA,KAAa,gBAAgB,IAAI,MAAM;CAEvE,OADkB,cAAc,SAAU,eACxB,UAAU;AAC7B;AAEA,MAAM,kBAA0D;CAC/D,IAAI,KAAK;CACT,MAAM,KAAK;CACX,MAAM,KAAK;AACZ;AAEA,MAAM,cAAc,EAAE,gBACrB,oBAAC,OAAD;CACC,eAAY;CACZ,WAAU;CACV,OAAM;CACN,QAAO;CACP,SAAQ;CACR,OAAO;EACN,YAAY;EACZ,WAAW,cAAc,SAAS,mBAAmB,KAAA;CACtD;WAEA,oBAAC,QAAD;EAAM,GAAE;EAAsB,MAAK;CAAgB,CAAA;AAC/C,CAAA;;;;;;;;AAmBN,SAAgB,gBAAgB,EAAE,SAAS,UAAU,QAAQ,QAAQ,KAA2B;CAC/F,MAAM,QAAQ,aAAa,SAAS,QAAQ;CAC5C,IAAI,CAAC,OACJ,OAAO;CAER,MAAM,cACL,MAAM,YAAY,OACf,OACA,IAAI,KAAK,aAAa,QAAQ;EAC9B,OAAO;EACP,uBAAuB;EACvB,aAAa;CACd,CAAC,EAAE,OAAO,KAAK,IAAI,MAAM,OAAO,IAAI,GAAG;CAG1C,MAAM,UAAU,eAAe,EAAE,gBAAgB,MAAM,UAAU;CAMjE,OACC,qBAAC,QAAD;EACC,MAAK;EACL,cARgB,GAAG,EAAE,gBAAgB,MAAM,UAAU,EAAE,GAAG,eAAe,GAAG,GAAG,EAChF,KAAK,oBACN,IACE,QAAQ,QAAQ,GAAG,EACnB,KAIoB;EACpB,OAAO;GACN,SAAS;GACT,KAAK;GAGL,YAAY;GACZ,UAAU;GACV,YAAY;EACb;YAXD,CAaC,qBAAC,QAAD;GACC,OAAO;IACN,SAAS;IACT,YAAY;IACZ,KAAK;IACL,OAAO,SAAS,MAAM,WAAW,MAAM;IACvC,YAAY;IACZ,YAAY;GACb;aARD,CAUE,MAAM,cAAc,SAAS,oBAAC,YAAD,EAAY,WAAW,MAAM,UAAY,CAAA,IAAI,MAC1E,OACI;MACN,oBAAC,QAAD;GAAM,OAAO;IAAE,OAAO;IAA8B,YAAY;GAAU;aACxE,EAAE,KAAK,oBAAoB;EACvB,CAAA,CACD;;AAER"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { addDaysInTz, startOfDayInTz } from "../timeframe/tz.js";
|
|
2
|
+
//#region src/widgets/comparison.ts
|
|
3
|
+
const DAY_MS = 864e5;
|
|
4
|
+
/**
|
|
5
|
+
* Comparison windows beyond a year are not meaningful period-over-period reads; the cap
|
|
6
|
+
* also rejects the unbounded `allTime` range, which would otherwise compare against a
|
|
7
|
+
* pre-1970 window.
|
|
8
|
+
*/
|
|
9
|
+
const MAX_COMPARISON_DAYS = 366;
|
|
10
|
+
/**
|
|
11
|
+
* The comparable window immediately preceding `range`, aligned to whole reporting-timezone
|
|
12
|
+
* days so daily rollup stores (which stamp each day at its local midnight) never lose their
|
|
13
|
+
* first day to a mid-day window start. It spans the same count of calendar days the current
|
|
14
|
+
* range touches (the current partial day counts as one) and ends 1ms before the current
|
|
15
|
+
* range's first day. Returns null when the range spans more than
|
|
16
|
+
* {@link MAX_COMPARISON_DAYS} days, where a previous period is not meaningful.
|
|
17
|
+
*/
|
|
18
|
+
const previousWindow = (range, tz = "UTC") => {
|
|
19
|
+
const firstDay = startOfDayInTz(range.start, tz);
|
|
20
|
+
const lastDay = startOfDayInTz(range.end, tz);
|
|
21
|
+
const days = Math.round((lastDay.getTime() - firstDay.getTime()) / DAY_MS) + 1;
|
|
22
|
+
if (!Number.isFinite(days) || days < 1 || days > MAX_COMPARISON_DAYS) return null;
|
|
23
|
+
return {
|
|
24
|
+
start: addDaysInTz(firstDay, -days, tz),
|
|
25
|
+
end: /* @__PURE__ */ new Date(firstDay.getTime() - 1)
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Period-over-period delta for one metric value against its previous-window value.
|
|
30
|
+
* Returns null when either side is missing so the caller can omit the comparison.
|
|
31
|
+
*/
|
|
32
|
+
const computeDelta = (current, previous) => {
|
|
33
|
+
if (current === void 0 || previous === void 0) return null;
|
|
34
|
+
const diff = current - previous;
|
|
35
|
+
return {
|
|
36
|
+
direction: diff > 0 ? "up" : diff < 0 ? "down" : "none",
|
|
37
|
+
percent: previous === 0 ? null : diff / previous * 100
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
//#endregion
|
|
41
|
+
export { computeDelta, previousWindow };
|
|
42
|
+
|
|
43
|
+
//# sourceMappingURL=comparison.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"comparison.js","names":[],"sources":["../../src/widgets/comparison.ts"],"sourcesContent":["import type { DateRange } from '../core/contract'\nimport { addDaysInTz, DEFAULT_TIMEZONE, startOfDayInTz } from '../timeframe/tz'\n\nexport type DeltaDirection = 'up' | 'down' | 'none'\n\nexport interface MetricDelta {\n\tdirection: DeltaDirection\n\t/**\n\t * Signed percentage change vs the previous value, or null when the previous value is\n\t * 0 (the baseline is undefined, so a percentage would be infinite/meaningless).\n\t */\n\tpercent: number | null\n}\n\nconst DAY_MS = 86_400_000\n\n/**\n * Comparison windows beyond a year are not meaningful period-over-period reads; the cap\n * also rejects the unbounded `allTime` range, which would otherwise compare against a\n * pre-1970 window.\n */\nconst MAX_COMPARISON_DAYS = 366\n\n/**\n * The comparable window immediately preceding `range`, aligned to whole reporting-timezone\n * days so daily rollup stores (which stamp each day at its local midnight) never lose their\n * first day to a mid-day window start. It spans the same count of calendar days the current\n * range touches (the current partial day counts as one) and ends 1ms before the current\n * range's first day. Returns null when the range spans more than\n * {@link MAX_COMPARISON_DAYS} days, where a previous period is not meaningful.\n */\nexport const previousWindow = (\n\trange: DateRange,\n\ttz: string = DEFAULT_TIMEZONE\n): DateRange | null => {\n\tconst firstDay = startOfDayInTz(range.start, tz)\n\tconst lastDay = startOfDayInTz(range.end, tz)\n\tconst days = Math.round((lastDay.getTime() - firstDay.getTime()) / DAY_MS) + 1\n\tif (!Number.isFinite(days) || days < 1 || days > MAX_COMPARISON_DAYS) {\n\t\treturn null\n\t}\n\treturn { start: addDaysInTz(firstDay, -days, tz), end: new Date(firstDay.getTime() - 1) }\n}\n\n/**\n * Period-over-period delta for one metric value against its previous-window value.\n * Returns null when either side is missing so the caller can omit the comparison.\n */\nexport const computeDelta = (current?: number, previous?: number): MetricDelta | null => {\n\tif (current === undefined || previous === undefined) {\n\t\treturn null\n\t}\n\tconst diff = current - previous\n\tconst direction: DeltaDirection = diff > 0 ? 'up' : diff < 0 ? 'down' : 'none'\n\tconst percent = previous === 0 ? null : (diff / previous) * 100\n\treturn { direction, percent }\n}\n"],"mappings":";;AAcA,MAAM,SAAS;;;;;;AAOf,MAAM,sBAAsB;;;;;;;;;AAU5B,MAAa,kBACZ,OACA,KAAA,UACsB;CACtB,MAAM,WAAW,eAAe,MAAM,OAAO,EAAE;CAC/C,MAAM,UAAU,eAAe,MAAM,KAAK,EAAE;CAC5C,MAAM,OAAO,KAAK,OAAO,QAAQ,QAAQ,IAAI,SAAS,QAAQ,KAAK,MAAM,IAAI;CAC7E,IAAI,CAAC,OAAO,SAAS,IAAI,KAAK,OAAO,KAAK,OAAO,qBAChD,OAAO;CAER,OAAO;EAAE,OAAO,YAAY,UAAU,CAAC,MAAM,EAAE;EAAG,qBAAK,IAAI,KAAK,SAAS,QAAQ,IAAI,CAAC;CAAE;AACzF;;;;;AAMA,MAAa,gBAAgB,SAAkB,aAA0C;CACxF,IAAI,YAAY,KAAA,KAAa,aAAa,KAAA,GACzC,OAAO;CAER,MAAM,OAAO,UAAU;CAGvB,OAAO;EAAE,WAFyB,OAAO,IAAI,OAAO,OAAO,IAAI,SAAS;EAEpD,SADJ,aAAa,IAAI,OAAQ,OAAO,WAAY;CAChC;AAC7B"}
|
|
@@ -10,6 +10,10 @@ interface WidgetReadResult {
|
|
|
10
10
|
dateRange: DateRange;
|
|
11
11
|
metrics: Partial<Record<MetricKey, number>>;
|
|
12
12
|
clamped?: boolean;
|
|
13
|
+
/** Previous-window totals, present only when the adapter supports comparison. */
|
|
14
|
+
previousMetrics?: Partial<Record<MetricKey, number>>;
|
|
15
|
+
/** The previous comparable window, present only when comparison ran. */
|
|
16
|
+
comparisonRange?: DateRange;
|
|
13
17
|
}
|
|
14
18
|
interface ReadForWidgetArgs {
|
|
15
19
|
req: PayloadRequest;
|
|
@@ -1,20 +1,18 @@
|
|
|
1
1
|
import { satisfiesCapabilities } from "../core/capabilities.js";
|
|
2
|
-
import { getRuntime } from "../plugin/runtime.js";
|
|
2
|
+
import { getRuntime, resolveTimezoneFor } from "../plugin/runtime.js";
|
|
3
3
|
import { resolveReadContext } from "../core/scopedRead.js";
|
|
4
4
|
import { resolveTimeframe } from "../timeframe/presets.js";
|
|
5
|
+
import { previousWindow } from "./comparison.js";
|
|
5
6
|
//#region src/widgets/readForWidget.ts
|
|
6
7
|
const readForWidget = async (args) => {
|
|
7
8
|
const { req, metrics, timeframe, adapterId, now, range } = args;
|
|
8
|
-
const
|
|
9
|
-
const base = {
|
|
10
|
-
dateRange,
|
|
11
|
-
metrics: {}
|
|
12
|
-
};
|
|
9
|
+
const emptyMetrics = {};
|
|
13
10
|
const runtime = getRuntime(req.payload);
|
|
14
11
|
if (!runtime) return {
|
|
15
12
|
status: "unavailable",
|
|
16
13
|
adapterId: adapterId ?? "",
|
|
17
|
-
|
|
14
|
+
dateRange: range ?? resolveTimeframe(timeframe, now),
|
|
15
|
+
metrics: emptyMetrics
|
|
18
16
|
};
|
|
19
17
|
const ctx = await resolveReadContext({
|
|
20
18
|
runtime,
|
|
@@ -25,7 +23,14 @@ const readForWidget = async (args) => {
|
|
|
25
23
|
if (!ctx.ok) return {
|
|
26
24
|
status: "unavailable",
|
|
27
25
|
adapterId: adapterId ?? "",
|
|
28
|
-
|
|
26
|
+
dateRange: range ?? resolveTimeframe(timeframe, now),
|
|
27
|
+
metrics: emptyMetrics
|
|
28
|
+
};
|
|
29
|
+
const tz = await resolveTimezoneFor(runtime, req, ctx.scope);
|
|
30
|
+
const dateRange = range ?? resolveTimeframe(timeframe, now, tz);
|
|
31
|
+
const base = {
|
|
32
|
+
dateRange,
|
|
33
|
+
metrics: emptyMetrics
|
|
29
34
|
};
|
|
30
35
|
const adapter = ctx.adapter;
|
|
31
36
|
if (!adapter.isConfigured()) return {
|
|
@@ -38,17 +43,27 @@ const readForWidget = async (args) => {
|
|
|
38
43
|
adapterId: adapter.id,
|
|
39
44
|
...base
|
|
40
45
|
};
|
|
41
|
-
const
|
|
46
|
+
const comparisonRange = runtime.comparison && adapter.capabilities.comparison ? previousWindow(dateRange, tz) ?? void 0 : void 0;
|
|
47
|
+
const [result, previous] = await Promise.all([runtime.engine.read(adapter, {
|
|
42
48
|
metrics,
|
|
43
49
|
dateRange,
|
|
50
|
+
timezone: tz,
|
|
44
51
|
scope: ctx.queryScope
|
|
45
|
-
})
|
|
52
|
+
}), comparisonRange ? runtime.engine.read(adapter, {
|
|
53
|
+
metrics,
|
|
54
|
+
dateRange: comparisonRange,
|
|
55
|
+
timezone: tz,
|
|
56
|
+
scope: ctx.queryScope
|
|
57
|
+
}) : void 0]);
|
|
58
|
+
const previousMetrics = previous ? previous.totals ?? {} : void 0;
|
|
46
59
|
return {
|
|
47
60
|
status: "ok",
|
|
48
61
|
adapterId: adapter.id,
|
|
49
62
|
dateRange,
|
|
50
63
|
metrics: result.totals ?? {},
|
|
51
|
-
clamped: result.meta.clamped ?? false
|
|
64
|
+
clamped: result.meta.clamped ?? false,
|
|
65
|
+
previousMetrics,
|
|
66
|
+
comparisonRange
|
|
52
67
|
};
|
|
53
68
|
};
|
|
54
69
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"readForWidget.js","names":[],"sources":["../../src/widgets/readForWidget.ts"],"sourcesContent":["import type { PayloadRequest } from 'payload'\nimport { satisfiesCapabilities } from '../core/capabilities'\nimport type { AnalyticsAdapter, DateRange, MetricKey } from '../core/contract'\nimport { resolveReadContext } from '../core/scopedRead'\nimport { getRuntime } from '../plugin/runtime'\nimport { resolveTimeframe, type TimeframePreset } from '../timeframe/presets'\n\nexport type WidgetReadStatus = 'ok' | 'not-configured' | 'unavailable'\n\nexport interface WidgetReadResult {\n\tstatus: WidgetReadStatus\n\tadapterId: string\n\tdateRange: DateRange\n\tmetrics: Partial<Record<MetricKey, number>>\n\tclamped?: boolean\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}\n\nexport const readForWidget = async (args: ReadForWidgetArgs): Promise<WidgetReadResult> => {\n\tconst { req, metrics, timeframe, adapterId, now, range } = args\n\tconst
|
|
1
|
+
{"version":3,"file":"readForWidget.js","names":[],"sources":["../../src/widgets/readForWidget.ts"],"sourcesContent":["import type { PayloadRequest } from 'payload'\nimport { satisfiesCapabilities } from '../core/capabilities'\nimport type { AnalyticsAdapter, DateRange, MetricKey } from '../core/contract'\nimport { resolveReadContext } from '../core/scopedRead'\nimport { getRuntime, resolveTimezoneFor } from '../plugin/runtime'\nimport { resolveTimeframe, type TimeframePreset } from '../timeframe/presets'\nimport { previousWindow } from './comparison'\n\nexport type WidgetReadStatus = 'ok' | 'not-configured' | 'unavailable'\n\nexport interface WidgetReadResult {\n\tstatus: WidgetReadStatus\n\tadapterId: string\n\tdateRange: DateRange\n\tmetrics: Partial<Record<MetricKey, number>>\n\tclamped?: 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}\n\nexport const readForWidget = async (args: ReadForWidgetArgs): Promise<WidgetReadResult> => {\n\tconst { req, metrics, timeframe, adapterId, now, range } = args\n\tconst emptyMetrics = {} as Partial<Record<MetricKey, number>>\n\n\tconst runtime = getRuntime(req.payload)\n\tif (!runtime) {\n\t\treturn {\n\t\t\tstatus: 'unavailable',\n\t\t\tadapterId: adapterId ?? '',\n\t\t\tdateRange: range ?? resolveTimeframe(timeframe, now),\n\t\t\tmetrics: emptyMetrics,\n\t\t}\n\t}\n\tconst ctx = await resolveReadContext({ runtime, req, adapterId, scope: args.scope })\n\tif (!ctx.ok) {\n\t\treturn {\n\t\t\tstatus: 'unavailable',\n\t\t\tadapterId: adapterId ?? '',\n\t\t\tdateRange: range ?? resolveTimeframe(timeframe, now),\n\t\t\tmetrics: emptyMetrics,\n\t\t}\n\t}\n\tconst tz = await resolveTimezoneFor(runtime, req, ctx.scope)\n\tconst dateRange = range ?? resolveTimeframe(timeframe, now, tz)\n\tconst base = { dateRange, metrics: emptyMetrics }\n\tconst adapter: AnalyticsAdapter = ctx.adapter\n\tif (!adapter.isConfigured()) {\n\t\treturn { status: 'not-configured', adapterId: adapter.id, ...base }\n\t}\n\tif (!satisfiesCapabilities(adapter.capabilities, { metrics })) {\n\t\treturn { status: 'unavailable', adapterId: adapter.id, ...base }\n\t}\n\tconst comparisonRange =\n\t\truntime.comparison && adapter.capabilities.comparison\n\t\t\t? (previousWindow(dateRange, tz) ?? undefined)\n\t\t\t: undefined\n\tconst [result, previous] = await Promise.all([\n\t\truntime.engine.read(adapter, { metrics, dateRange, timezone: tz, scope: ctx.queryScope }),\n\t\tcomparisonRange\n\t\t\t? runtime.engine.read(adapter, {\n\t\t\t\t\tmetrics,\n\t\t\t\t\tdateRange: comparisonRange,\n\t\t\t\t\ttimezone: tz,\n\t\t\t\t\tscope: ctx.queryScope,\n\t\t\t\t})\n\t\t\t: undefined,\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\tclamped: result.meta.clamped ?? false,\n\t\tpreviousMetrics,\n\t\tcomparisonRange,\n\t}\n}\n"],"mappings":";;;;;;AAiCA,MAAa,gBAAgB,OAAO,SAAuD;CAC1F,MAAM,EAAE,KAAK,SAAS,WAAW,WAAW,KAAK,UAAU;CAC3D,MAAM,eAAe,CAAC;CAEtB,MAAM,UAAU,WAAW,IAAI,OAAO;CACtC,IAAI,CAAC,SACJ,OAAO;EACN,QAAQ;EACR,WAAW,aAAa;EACxB,WAAW,SAAS,iBAAiB,WAAW,GAAG;EACnD,SAAS;CACV;CAED,MAAM,MAAM,MAAM,mBAAmB;EAAE;EAAS;EAAK;EAAW,OAAO,KAAK;CAAM,CAAC;CACnF,IAAI,CAAC,IAAI,IACR,OAAO;EACN,QAAQ;EACR,WAAW,aAAa;EACxB,WAAW,SAAS,iBAAiB,WAAW,GAAG;EACnD,SAAS;CACV;CAED,MAAM,KAAK,MAAM,mBAAmB,SAAS,KAAK,IAAI,KAAK;CAC3D,MAAM,YAAY,SAAS,iBAAiB,WAAW,KAAK,EAAE;CAC9D,MAAM,OAAO;EAAE;EAAW,SAAS;CAAa;CAChD,MAAM,UAA4B,IAAI;CACtC,IAAI,CAAC,QAAQ,aAAa,GACzB,OAAO;EAAE,QAAQ;EAAkB,WAAW,QAAQ;EAAI,GAAG;CAAK;CAEnE,IAAI,CAAC,sBAAsB,QAAQ,cAAc,EAAE,QAAQ,CAAC,GAC3D,OAAO;EAAE,QAAQ;EAAe,WAAW,QAAQ;EAAI,GAAG;CAAK;CAEhE,MAAM,kBACL,QAAQ,cAAc,QAAQ,aAAa,aACvC,eAAe,WAAW,EAAE,KAAK,KAAA,IAClC,KAAA;CACJ,MAAM,CAAC,QAAQ,YAAY,MAAM,QAAQ,IAAI,CAC5C,QAAQ,OAAO,KAAK,SAAS;EAAE;EAAS;EAAW,UAAU;EAAI,OAAO,IAAI;CAAW,CAAC,GACxF,kBACG,QAAQ,OAAO,KAAK,SAAS;EAC7B;EACA,WAAW;EACX,UAAU;EACV,OAAO,IAAI;CACZ,CAAC,IACA,KAAA,CACJ,CAAC;CACD,MAAM,kBAAkB,WAAY,SAAS,UAAU,CAAC,IAAK,KAAA;CAC7D,OAAO;EACN,QAAQ;EACR,WAAW,QAAQ;EACnB;EACA,SAAS,OAAO,UAAU,CAAC;EAC3B,SAAS,OAAO,KAAK,WAAW;EAChC;EACA;CACD;AACD"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { satisfiesCapabilities } from "../core/capabilities.js";
|
|
2
|
-
import { getRuntime } from "../plugin/runtime.js";
|
|
2
|
+
import { getRuntime, resolveTimezoneFor } from "../plugin/runtime.js";
|
|
3
3
|
import { resolveReadContext } from "../core/scopedRead.js";
|
|
4
4
|
import { resolveTimeframe } from "../timeframe/presets.js";
|
|
5
5
|
//#region src/widgets/readForWidgetBreakdown.ts
|
|
@@ -10,16 +10,13 @@ import { resolveTimeframe } from "../timeframe/presets.js";
|
|
|
10
10
|
*/
|
|
11
11
|
const readForWidgetBreakdown = async (args) => {
|
|
12
12
|
const { req, metric, dimension, timeframe, limit, adapterId, now, range } = args;
|
|
13
|
-
const
|
|
14
|
-
const base = {
|
|
15
|
-
dateRange,
|
|
16
|
-
rows: []
|
|
17
|
-
};
|
|
13
|
+
const emptyRows = [];
|
|
18
14
|
const runtime = getRuntime(req.payload);
|
|
19
15
|
if (!runtime) return {
|
|
20
16
|
status: "unavailable",
|
|
21
17
|
adapterId: adapterId ?? "",
|
|
22
|
-
|
|
18
|
+
dateRange: range ?? resolveTimeframe(timeframe, now),
|
|
19
|
+
rows: emptyRows
|
|
23
20
|
};
|
|
24
21
|
const ctx = await resolveReadContext({
|
|
25
22
|
runtime,
|
|
@@ -30,7 +27,14 @@ const readForWidgetBreakdown = async (args) => {
|
|
|
30
27
|
if (!ctx.ok) return {
|
|
31
28
|
status: "unavailable",
|
|
32
29
|
adapterId: adapterId ?? "",
|
|
33
|
-
|
|
30
|
+
dateRange: range ?? resolveTimeframe(timeframe, now),
|
|
31
|
+
rows: emptyRows
|
|
32
|
+
};
|
|
33
|
+
const tz = await resolveTimezoneFor(runtime, req, ctx.scope);
|
|
34
|
+
const dateRange = range ?? resolveTimeframe(timeframe, now, tz);
|
|
35
|
+
const base = {
|
|
36
|
+
dateRange,
|
|
37
|
+
rows: emptyRows
|
|
34
38
|
};
|
|
35
39
|
const adapter = ctx.adapter;
|
|
36
40
|
if (!adapter.isConfigured()) return {
|
|
@@ -55,6 +59,7 @@ const readForWidgetBreakdown = async (args) => {
|
|
|
55
59
|
metric,
|
|
56
60
|
direction: "desc"
|
|
57
61
|
},
|
|
62
|
+
timezone: tz,
|
|
58
63
|
scope: ctx.queryScope
|
|
59
64
|
});
|
|
60
65
|
const rows = result.rows.map((row) => ({
|