@10x-media/analytics 0.1.0-beta.5 → 1.0.0-beta.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +40 -0
- package/LICENSE +21 -21
- package/README.md +5 -1
- package/dist/adapters/ga4/ga4.js +13 -0
- package/dist/adapters/ga4/ga4.js.map +1 -1
- package/dist/adapters/http/fetchJson.js +1 -1
- package/dist/adapters/plausible/plausible.d.ts +10 -0
- package/dist/adapters/plausible/plausible.js +51 -0
- package/dist/adapters/plausible/plausible.js.map +1 -1
- package/dist/adapters/posthog/posthog.d.ts +10 -1
- package/dist/adapters/posthog/posthog.js +84 -3
- package/dist/adapters/posthog/posthog.js.map +1 -1
- package/dist/adapters/series.js +11 -1
- package/dist/adapters/series.js.map +1 -1
- package/dist/adapters/umami/umami.d.ts +5 -1
- package/dist/adapters/umami/umami.js +28 -0
- package/dist/adapters/umami/umami.js.map +1 -1
- package/dist/capture/getTrackerConfig.d.ts +20 -0
- package/dist/capture/getTrackerConfig.js +31 -0
- package/dist/capture/getTrackerConfig.js.map +1 -0
- package/dist/capture/mountPath.js +14 -0
- package/dist/capture/mountPath.js.map +1 -0
- package/dist/capture/proxyEndpoint.js +123 -0
- package/dist/capture/proxyEndpoint.js.map +1 -0
- package/dist/capture/proxyRoutes.js +108 -0
- package/dist/capture/proxyRoutes.js.map +1 -0
- package/dist/capture/requestBody.js +68 -0
- package/dist/capture/requestBody.js.map +1 -0
- package/dist/capture/slots.d.ts +8 -0
- package/dist/capture/slots.js +43 -0
- package/dist/capture/slots.js.map +1 -0
- package/dist/capture/trackerConfig.d.ts +32 -0
- package/dist/capture/trackerConfig.js +70 -0
- package/dist/capture/trackerConfig.js.map +1 -0
- package/dist/capture/trackerEndpoint.js +35 -0
- package/dist/capture/trackerEndpoint.js.map +1 -0
- package/dist/core/capabilities.d.ts +5 -1
- package/dist/core/capabilities.js +4 -0
- package/dist/core/capabilities.js.map +1 -1
- package/dist/core/capture.d.ts +49 -0
- package/dist/core/contract.d.ts +33 -4
- package/dist/core/contract.js.map +1 -1
- package/dist/core/options.d.ts +97 -2
- package/dist/core/options.js +63 -2
- package/dist/core/options.js.map +1 -1
- package/dist/core/registry.d.ts +1 -0
- package/dist/core/scopeList.js +32 -0
- package/dist/core/scopeList.js.map +1 -0
- package/dist/core/scopedRead.js +13 -6
- package/dist/core/scopedRead.js.map +1 -1
- package/dist/exports/next.d.ts +2 -1
- package/dist/exports/next.js +2 -1
- package/dist/exports/react.d.ts +4 -0
- package/dist/exports/react.js +5 -0
- package/dist/exports/rsc.d.ts +5 -2
- package/dist/exports/rsc.js +3 -1
- package/dist/exports/tracker.d.ts +18 -0
- package/dist/exports/tracker.js +12 -0
- package/dist/exports/types.d.ts +6 -3
- package/dist/fields/AnalyticsPanelClient.js +1 -1
- package/dist/fields/config/narrow.js +2 -0
- package/dist/fields/config/narrow.js.map +1 -1
- package/dist/fields/readForDocument.js +1 -1
- package/dist/goals/match.js +64 -0
- package/dist/goals/match.js.map +1 -0
- package/dist/goals/types.d.ts +33 -0
- package/dist/goals/types.js +7 -0
- package/dist/goals/types.js.map +1 -0
- package/dist/index.d.ts +4 -3
- package/dist/index.js +39 -10
- package/dist/index.js.map +1 -1
- package/dist/native/collections/events.js +21 -1
- package/dist/native/collections/events.js.map +1 -1
- package/dist/native/collections/rollups.js +39 -3
- package/dist/native/collections/rollups.js.map +1 -1
- package/dist/native/geo/maxmindResolver.js +2 -4
- package/dist/native/geo/maxmindResolver.js.map +1 -1
- package/dist/native/ingest/clientIp.js +15 -0
- package/dist/native/ingest/clientIp.js.map +1 -0
- package/dist/native/ingest/endpoint.js +40 -4
- package/dist/native/ingest/endpoint.js.map +1 -1
- package/dist/native/ingest/flushBatch.js +4 -13
- package/dist/native/ingest/flushBatch.js.map +1 -1
- package/dist/native/ingest/normalizeEvent.js +76 -9
- package/dist/native/ingest/normalizeEvent.js.map +1 -1
- package/dist/native/nativeAdapter.js +100 -5
- package/dist/native/nativeAdapter.js.map +1 -1
- package/dist/native/query/eventAgg.js +161 -0
- package/dist/native/query/eventAgg.js.map +1 -0
- package/dist/native/rollupAcc.js +12 -1
- package/dist/native/rollupAcc.js.map +1 -1
- package/dist/native/rollups/applyDistinctDeltas.js +1 -1
- package/dist/native/rollups/applyDistinctDeltas.js.map +1 -1
- package/dist/native/rollups/bumpRollup.js +7 -2
- package/dist/native/rollups/bumpRollup.js.map +1 -1
- package/dist/native/rollups/deltas.js +70 -11
- package/dist/native/rollups/deltas.js.map +1 -1
- package/dist/next/captureRewrites.d.ts +41 -0
- package/dist/next/captureRewrites.js +56 -0
- package/dist/next/captureRewrites.js.map +1 -0
- package/dist/next/posthogProxyRewrites.d.ts +5 -0
- package/dist/next/posthogProxyRewrites.js +16 -21
- package/dist/next/posthogProxyRewrites.js.map +1 -1
- package/dist/plugin/documentEndpoint.js +1 -1
- package/dist/plugin/paths.js +11 -1
- package/dist/plugin/paths.js.map +1 -1
- package/dist/plugin/runtime.d.ts +1 -0
- package/dist/plugin/runtime.js.map +1 -1
- package/dist/plugin/sourcesEndpoint.js +16 -4
- package/dist/plugin/sourcesEndpoint.js.map +1 -1
- package/dist/plugin/warmTask.js +0 -0
- package/dist/plugin/warmTask.js.map +1 -1
- package/dist/providers/collection.js +56 -16
- package/dist/providers/collection.js.map +1 -1
- package/dist/providers/factory.js +8 -2
- package/dist/providers/factory.js.map +1 -1
- package/dist/react/AnalyticsProvider.d.ts +43 -0
- package/dist/react/AnalyticsProvider.js +66 -0
- package/dist/react/AnalyticsProvider.js.map +1 -0
- package/dist/react/TrackerBoot.d.ts +22 -0
- package/dist/react/TrackerBoot.js +30 -0
- package/dist/react/TrackerBoot.js.map +1 -0
- package/dist/react/useAnalytics.d.ts +17 -0
- package/dist/react/useAnalytics.js +36 -0
- package/dist/react/useAnalytics.js.map +1 -0
- package/dist/rsc/AnalyticsScripts.d.ts +30 -0
- package/dist/rsc/AnalyticsScripts.js +41 -0
- package/dist/rsc/AnalyticsScripts.js.map +1 -0
- package/dist/surfacing/cacheStore.d.ts +1 -0
- package/dist/surfacing/cacheStore.js +18 -2
- package/dist/surfacing/cacheStore.js.map +1 -1
- package/dist/surfacing/engine.d.ts +1 -0
- package/dist/surfacing/engine.js +44 -4
- package/dist/surfacing/engine.js.map +1 -1
- package/dist/surfacing/queue.js +22 -6
- package/dist/surfacing/queue.js.map +1 -1
- package/dist/surfacing/rateLimiter.js +130 -0
- package/dist/surfacing/rateLimiter.js.map +1 -0
- package/dist/surfacing/retryPolicy.js +15 -0
- package/dist/surfacing/retryPolicy.js.map +1 -0
- package/dist/sync/collection.js +23 -7
- package/dist/sync/collection.js.map +1 -1
- package/dist/sync/syncTask.js +68 -33
- package/dist/sync/syncTask.js.map +1 -1
- package/dist/testing/memoryAdapter.d.ts +2 -0
- package/dist/testing/memoryAdapter.js +17 -0
- package/dist/testing/memoryAdapter.js.map +1 -1
- package/dist/tracker/autoCapture.d.ts +48 -0
- package/dist/tracker/autoCapture.js +129 -0
- package/dist/tracker/autoCapture.js.map +1 -0
- package/dist/tracker/consent.d.ts +24 -0
- package/dist/tracker/consent.js +48 -0
- package/dist/tracker/consent.js.map +1 -0
- package/dist/tracker/createTracker.d.ts +18 -0
- package/dist/tracker/createTracker.js +163 -0
- package/dist/tracker/createTracker.js.map +1 -0
- package/dist/tracker/initTracker.d.ts +22 -0
- package/dist/tracker/initTracker.js +33 -0
- package/dist/tracker/initTracker.js.map +1 -0
- package/dist/tracker/loadScript.d.ts +14 -0
- package/dist/tracker/loadScript.js +41 -0
- package/dist/tracker/loadScript.js.map +1 -0
- package/dist/tracker/pageTracking.d.ts +17 -0
- package/dist/tracker/pageTracking.js +38 -0
- package/dist/tracker/pageTracking.js.map +1 -0
- package/dist/tracker/registry.d.ts +27 -0
- package/dist/tracker/registry.js +73 -0
- package/dist/tracker/registry.js.map +1 -0
- package/dist/tracker/sinks/native.d.ts +22 -0
- package/dist/tracker/sinks/native.js +56 -0
- package/dist/tracker/sinks/native.js.map +1 -0
- package/dist/tracker/sinks/plausible.d.ts +14 -0
- package/dist/tracker/sinks/plausible.js +25 -0
- package/dist/tracker/sinks/plausible.js.map +1 -0
- package/dist/tracker/sinks/posthog.d.ts +14 -0
- package/dist/tracker/sinks/posthog.js +18 -0
- package/dist/tracker/sinks/posthog.js.map +1 -0
- package/dist/tracker/sinks/umami.d.ts +13 -0
- package/dist/tracker/sinks/umami.js +17 -0
- package/dist/tracker/sinks/umami.js.map +1 -0
- package/dist/tracker/sinks/vendor.d.ts +14 -0
- package/dist/tracker/sinks/vendor.js +66 -0
- package/dist/tracker/sinks/vendor.js.map +1 -0
- package/dist/tracker/transport.js +28 -0
- package/dist/tracker/transport.js.map +1 -0
- package/dist/tracker/types.d.ts +66 -0
- package/dist/translations/ar.js +83 -0
- package/dist/translations/ar.js.map +1 -0
- package/dist/translations/de.js +6 -0
- package/dist/translations/de.js.map +1 -1
- package/dist/translations/en.js +6 -0
- package/dist/translations/en.js.map +1 -1
- package/dist/translations/es.js +83 -0
- package/dist/translations/es.js.map +1 -0
- package/dist/translations/fr.js +83 -0
- package/dist/translations/fr.js.map +1 -0
- package/dist/translations/id.js +83 -0
- package/dist/translations/id.js.map +1 -0
- package/dist/translations/index.d.ts +10 -1
- package/dist/translations/index.js +19 -1
- package/dist/translations/index.js.map +1 -1
- package/dist/translations/keys.d.ts +6 -0
- package/dist/translations/keys.js +6 -0
- package/dist/translations/keys.js.map +1 -1
- package/dist/translations/ko.js +83 -0
- package/dist/translations/ko.js.map +1 -0
- package/dist/translations/pt.js +83 -0
- package/dist/translations/pt.js.map +1 -0
- package/dist/translations/ru.js +83 -0
- package/dist/translations/ru.js.map +1 -0
- package/dist/translations/uk.js +83 -0
- package/dist/translations/uk.js.map +1 -0
- package/dist/translations/zh.js +83 -0
- package/dist/translations/zh.js.map +1 -0
- package/dist/widgets/breakdownTypes.js +5 -0
- package/dist/widgets/breakdownTypes.js.map +1 -1
- package/dist/widgets/readForWidget.d.ts +2 -1
- package/dist/widgets/readForWidget.js +32 -14
- package/dist/widgets/readForWidget.js.map +1 -1
- package/dist/widgets/readForWidgetBreakdown.d.ts +2 -1
- package/dist/widgets/readForWidgetBreakdown.js +29 -15
- package/dist/widgets/readForWidgetBreakdown.js.map +1 -1
- package/dist/widgets/readForWidgetRealtime.js +1 -1
- package/dist/widgets/readForWidgetSeries.d.ts +2 -1
- package/dist/widgets/readForWidgetSeries.js +34 -16
- package/dist/widgets/readForWidgetSeries.js.map +1 -1
- package/dist/widgets/registerWidgets.js +10 -4
- package/dist/widgets/registerWidgets.js.map +1 -1
- package/dist/widgets/types.js +4 -1
- package/dist/widgets/types.js.map +1 -1
- package/package.json +20 -5
|
@@ -16,6 +16,20 @@ const secretField = (args, opts) => args.buildSecret({
|
|
|
16
16
|
label: labelForKey(opts.label),
|
|
17
17
|
...opts.width ? { admin: { width: opts.width } } : {}
|
|
18
18
|
});
|
|
19
|
+
/**
|
|
20
|
+
* Public capture config: plain text, never a sealed secret, because the tracker config
|
|
21
|
+
* serves it to the browser. An adapter declares `capture` only when its public field is
|
|
22
|
+
* filled, so these are what let a provider document fill a capture slot.
|
|
23
|
+
*/
|
|
24
|
+
const captureField = (name, label, width) => ({
|
|
25
|
+
name,
|
|
26
|
+
type: "text",
|
|
27
|
+
label: labelForKey(label),
|
|
28
|
+
admin: {
|
|
29
|
+
description: labelForKey(keys.providerFieldCaptureHelp),
|
|
30
|
+
...width ? { width } : {}
|
|
31
|
+
}
|
|
32
|
+
});
|
|
19
33
|
const hostField = () => ({
|
|
20
34
|
name: "host",
|
|
21
35
|
type: "text",
|
|
@@ -121,14 +135,21 @@ const buildProvidersCollection = (args) => {
|
|
|
121
135
|
label: labelForKey(keys.providerFieldScope),
|
|
122
136
|
admin: { hidden: true }
|
|
123
137
|
},
|
|
124
|
-
providerGroup("plausible", keys.providerNamePlausible, [
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
138
|
+
providerGroup("plausible", keys.providerNamePlausible, [
|
|
139
|
+
{
|
|
140
|
+
type: "row",
|
|
141
|
+
fields: [textField("siteId", keys.providerFieldSiteId, "50%"), ...secretField(args, {
|
|
142
|
+
name: "apiKey",
|
|
143
|
+
label: keys.providerFieldApiKey,
|
|
144
|
+
width: "50%"
|
|
145
|
+
})]
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
type: "row",
|
|
149
|
+
fields: [captureField("domain", keys.providerFieldDomain, "50%"), captureField("scriptId", keys.providerFieldScriptId, "50%")]
|
|
150
|
+
},
|
|
151
|
+
hostField()
|
|
152
|
+
]),
|
|
132
153
|
providerGroup("umami", keys.providerNameUmami, [
|
|
133
154
|
{
|
|
134
155
|
type: "row",
|
|
@@ -156,14 +177,33 @@ const buildProvidersCollection = (args) => {
|
|
|
156
177
|
label: labelForKey(keys.providerFieldPrivateKey)
|
|
157
178
|
})
|
|
158
179
|
]),
|
|
159
|
-
providerGroup("posthog", keys.providerNamePosthog, [
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
180
|
+
providerGroup("posthog", keys.providerNamePosthog, [
|
|
181
|
+
{
|
|
182
|
+
type: "row",
|
|
183
|
+
fields: [textField("projectId", keys.providerFieldProjectId, "50%"), ...secretField(args, {
|
|
184
|
+
name: "apiKey",
|
|
185
|
+
label: keys.providerFieldApiKey,
|
|
186
|
+
width: "50%"
|
|
187
|
+
})]
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
type: "row",
|
|
191
|
+
fields: [captureField("projectToken", keys.providerFieldProjectToken, "50%"), {
|
|
192
|
+
name: "region",
|
|
193
|
+
type: "select",
|
|
194
|
+
label: labelForKey(keys.providerFieldRegion),
|
|
195
|
+
options: [{
|
|
196
|
+
label: "US",
|
|
197
|
+
value: "us"
|
|
198
|
+
}, {
|
|
199
|
+
label: "EU",
|
|
200
|
+
value: "eu"
|
|
201
|
+
}],
|
|
202
|
+
admin: { width: "50%" }
|
|
203
|
+
}]
|
|
204
|
+
},
|
|
205
|
+
hostField()
|
|
206
|
+
])
|
|
167
207
|
]
|
|
168
208
|
};
|
|
169
209
|
return args.overrides ? args.overrides(collection) : collection;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"collection.js","names":[],"sources":["../../src/providers/collection.ts"],"sourcesContent":["import type { CollectionConfig, Field, TextareaField, TextField } from 'payload'\nimport { keys, type TranslationKey } from '../translations/keys'\nimport { labelForKey } from '../translations/server'\nimport type { ProviderAccessArgs } from './access'\nimport { providerCreateAccess, providerRowAccess } from './access'\nimport type { ProviderId } from './factory'\nimport { stampScope } from './stampScope'\n\nexport const PROVIDERS_SLUG = 'analytics-providers'\n\n/** Builds the sealed variant of a secret source field; injected so the fields peer stays lazy. */\nexport type BuildSecretField = (source: TextField | TextareaField) => Field[]\n\nexport interface BuildProvidersCollectionArgs extends ProviderAccessArgs {\n\tslug: string\n\taccess?: Partial<CollectionConfig['access']>\n\toverrides?: (collection: CollectionConfig) => CollectionConfig\n\t/** Called after any change or delete so the per-scope registry cache drops stale adapters. */\n\tonChange: () => void\n\tbuildSecret: BuildSecretField\n}\n\nconst textField = (name: string, label: TranslationKey, width?: string): Field => ({\n\tname,\n\ttype: 'text',\n\tlabel: labelForKey(label),\n\t...(width ? { admin: { width } } : {}),\n})\n\nconst secretField = (\n\targs: BuildProvidersCollectionArgs,\n\topts: { name: string; label: TranslationKey; width?: string }\n): Field[] =>\n\targs.buildSecret({\n\t\tname: opts.name,\n\t\ttype: 'text',\n\t\tlabel: labelForKey(opts.label),\n\t\t...(opts.width ? { admin: { width: opts.width } } : {}),\n\t})\n\nconst hostField = (): Field => ({\n\tname: 'host',\n\ttype: 'text',\n\tlabel: labelForKey(keys.providerFieldHost),\n\tadmin: { description: labelForKey(keys.providerFieldHostHelp) },\n})\n\nconst providerGroup = (provider: ProviderId, label: TranslationKey, fields: Field[]): Field => ({\n\tname: provider,\n\ttype: 'group',\n\tlabel: labelForKey(label),\n\tadmin: { condition: (data) => data?.provider === provider },\n\tfields,\n})\n\n/**\n * The runtime provider-settings collection: one document per provider (and per scope\n * in scoped installs). Admin-only by default; `access` entries override individual\n * operations and `overrides` reshapes the whole config last, so slugs, labels,\n * fields, and access all stay overridable.\n */\nexport const buildProvidersCollection = (args: BuildProvidersCollectionArgs): CollectionConfig => {\n\tconst collection: CollectionConfig = {\n\t\tslug: args.slug,\n\t\tlabels: {\n\t\t\tsingular: labelForKey(keys.providersCollectionSingular),\n\t\t\tplural: labelForKey(keys.providersCollectionPlural),\n\t\t},\n\t\tdisableDuplicate: true,\n\t\tadmin: {\n\t\t\tuseAsTitle: 'name',\n\t\t\tdefaultColumns: ['name', 'provider', 'enabled'],\n\t\t\tgroup: 'Analytics',\n\t\t},\n\t\taccess: {\n\t\t\tread: providerRowAccess(args),\n\t\t\tcreate: providerCreateAccess(args),\n\t\t\tupdate: providerRowAccess(args),\n\t\t\tdelete: providerRowAccess(args),\n\t\t\t...args.access,\n\t\t},\n\t\thooks: {\n\t\t\tbeforeChange: [stampScope(args)],\n\t\t\tafterChange: [\n\t\t\t\t({ doc }) => {\n\t\t\t\t\targs.onChange()\n\t\t\t\t\treturn doc\n\t\t\t\t},\n\t\t\t],\n\t\t\tafterDelete: [\n\t\t\t\t({ doc }) => {\n\t\t\t\t\targs.onChange()\n\t\t\t\t\treturn doc\n\t\t\t\t},\n\t\t\t],\n\t\t},\n\t\tfields: [\n\t\t\t{\n\t\t\t\tname: 'name',\n\t\t\t\ttype: 'text',\n\t\t\t\trequired: true,\n\t\t\t\tlabel: labelForKey(keys.providerFieldName),\n\t\t\t},\n\t\t\t{\n\t\t\t\ttype: 'row',\n\t\t\t\tfields: [\n\t\t\t\t\t{\n\t\t\t\t\t\tname: 'provider',\n\t\t\t\t\t\ttype: 'select',\n\t\t\t\t\t\trequired: true,\n\t\t\t\t\t\tlabel: labelForKey(keys.providerFieldProvider),\n\t\t\t\t\t\toptions: [\n\t\t\t\t\t\t\t{ label: labelForKey(keys.providerNamePlausible), value: 'plausible' },\n\t\t\t\t\t\t\t{ label: labelForKey(keys.providerNameUmami), value: 'umami' },\n\t\t\t\t\t\t\t{ label: labelForKey(keys.providerNameGa4), value: 'ga4' },\n\t\t\t\t\t\t\t{ label: labelForKey(keys.providerNamePosthog), value: 'posthog' },\n\t\t\t\t\t\t],\n\t\t\t\t\t\tadmin: { width: '50%' },\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\tname: 'enabled',\n\t\t\t\t\t\ttype: 'checkbox',\n\t\t\t\t\t\tdefaultValue: true,\n\t\t\t\t\t\tlabel: labelForKey(keys.providerFieldEnabled),\n\t\t\t\t\t\tadmin: { width: '50%', style: { alignSelf: 'flex-end' } },\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t},\n\t\t\t{\n\t\t\t\t// Written by scoped setups (or a tenant plugin's own field via `scopeField`);\n\t\t\t\t// hidden because single-site installs never touch it.\n\t\t\t\tname: 'scope',\n\t\t\t\ttype: 'text',\n\t\t\t\tindex: true,\n\t\t\t\tlabel: labelForKey(keys.providerFieldScope),\n\t\t\t\tadmin: { hidden: true },\n\t\t\t},\n\t\t\tproviderGroup('plausible', keys.providerNamePlausible, [\n\t\t\t\t{\n\t\t\t\t\ttype: 'row',\n\t\t\t\t\tfields: [\n\t\t\t\t\t\ttextField('siteId', keys.providerFieldSiteId, '50%'),\n\t\t\t\t\t\t...secretField(args, { name: 'apiKey', label: keys.providerFieldApiKey, width: '50%' }),\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t\thostField(),\n\t\t\t]),\n\t\t\tproviderGroup('umami', keys.providerNameUmami, [\n\t\t\t\t{\n\t\t\t\t\ttype: 'row',\n\t\t\t\t\tfields: [\n\t\t\t\t\t\ttextField('websiteId', keys.providerFieldWebsiteId, '50%'),\n\t\t\t\t\t\t...secretField(args, { name: 'apiKey', label: keys.providerFieldApiKey, width: '50%' }),\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t\t...secretField(args, { name: 'token', label: keys.providerFieldToken }),\n\t\t\t\thostField(),\n\t\t\t]),\n\t\t\tproviderGroup('ga4', keys.providerNameGa4, [\n\t\t\t\t{\n\t\t\t\t\ttype: 'row',\n\t\t\t\t\tfields: [\n\t\t\t\t\t\ttextField('propertyId', keys.providerFieldPropertyId, '50%'),\n\t\t\t\t\t\ttextField('projectId', keys.providerFieldProjectId, '50%'),\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t\ttextField('clientEmail', keys.providerFieldClientEmail),\n\t\t\t\t...args.buildSecret({\n\t\t\t\t\tname: 'privateKey',\n\t\t\t\t\ttype: 'textarea',\n\t\t\t\t\tlabel: labelForKey(keys.providerFieldPrivateKey),\n\t\t\t\t}),\n\t\t\t]),\n\t\t\tproviderGroup('posthog', keys.providerNamePosthog, [\n\t\t\t\t{\n\t\t\t\t\ttype: 'row',\n\t\t\t\t\tfields: [\n\t\t\t\t\t\ttextField('projectId', keys.providerFieldProjectId, '50%'),\n\t\t\t\t\t\t...secretField(args, { name: 'apiKey', label: keys.providerFieldApiKey, width: '50%' }),\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t\thostField(),\n\t\t\t]),\n\t\t],\n\t}\n\treturn args.overrides ? args.overrides(collection) : collection\n}\n"],"mappings":";;;;;AAQA,MAAa,iBAAiB;AAc9B,MAAM,aAAa,MAAc,OAAuB,WAA2B;CAClF;CACA,MAAM;CACN,OAAO,YAAY,KAAK;CACxB,GAAI,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC;AACrC;AAEA,MAAM,eACL,MACA,SAEA,KAAK,YAAY;CAChB,MAAM,KAAK;CACX,MAAM;CACN,OAAO,YAAY,KAAK,KAAK;CAC7B,GAAI,KAAK,QAAQ,EAAE,OAAO,EAAE,OAAO,KAAK,MAAM,EAAE,IAAI,CAAC;AACtD,CAAC;AAEF,MAAM,mBAA0B;CAC/B,MAAM;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AAK;KAAqB,OAAO;IAAM,CAAC,CACvF;GACD,GACA,UAAU,CACX,CAAC;EACF;CACD;CACA,OAAO,KAAK,YAAY,KAAK,UAAU,UAAU,IAAI;AACtD"}
|
|
1
|
+
{"version":3,"file":"collection.js","names":[],"sources":["../../src/providers/collection.ts"],"sourcesContent":["import type { CollectionConfig, Field, TextareaField, TextField } from 'payload'\nimport { keys, type TranslationKey } from '../translations/keys'\nimport { labelForKey } from '../translations/server'\nimport type { ProviderAccessArgs } from './access'\nimport { providerCreateAccess, providerRowAccess } from './access'\nimport type { ProviderId } from './factory'\nimport { stampScope } from './stampScope'\n\nexport const PROVIDERS_SLUG = 'analytics-providers'\n\n/** Builds the sealed variant of a secret source field; injected so the fields peer stays lazy. */\nexport type BuildSecretField = (source: TextField | TextareaField) => Field[]\n\nexport interface BuildProvidersCollectionArgs extends ProviderAccessArgs {\n\tslug: string\n\taccess?: Partial<CollectionConfig['access']>\n\toverrides?: (collection: CollectionConfig) => CollectionConfig\n\t/** Called after any change or delete so the per-scope registry cache drops stale adapters. */\n\tonChange: () => void\n\tbuildSecret: BuildSecretField\n}\n\nconst textField = (name: string, label: TranslationKey, width?: string): Field => ({\n\tname,\n\ttype: 'text',\n\tlabel: labelForKey(label),\n\t...(width ? { admin: { width } } : {}),\n})\n\nconst secretField = (\n\targs: BuildProvidersCollectionArgs,\n\topts: { name: string; label: TranslationKey; width?: string }\n): Field[] =>\n\targs.buildSecret({\n\t\tname: opts.name,\n\t\ttype: 'text',\n\t\tlabel: labelForKey(opts.label),\n\t\t...(opts.width ? { admin: { width: opts.width } } : {}),\n\t})\n\n/**\n * Public capture config: plain text, never a sealed secret, because the tracker config\n * serves it to the browser. An adapter declares `capture` only when its public field is\n * filled, so these are what let a provider document fill a capture slot.\n */\nconst captureField = (name: string, label: TranslationKey, width?: string): Field => ({\n\tname,\n\ttype: 'text',\n\tlabel: labelForKey(label),\n\tadmin: {\n\t\tdescription: labelForKey(keys.providerFieldCaptureHelp),\n\t\t...(width ? { width } : {}),\n\t},\n})\n\nconst hostField = (): Field => ({\n\tname: 'host',\n\ttype: 'text',\n\tlabel: labelForKey(keys.providerFieldHost),\n\tadmin: { description: labelForKey(keys.providerFieldHostHelp) },\n})\n\nconst providerGroup = (provider: ProviderId, label: TranslationKey, fields: Field[]): Field => ({\n\tname: provider,\n\ttype: 'group',\n\tlabel: labelForKey(label),\n\tadmin: { condition: (data) => data?.provider === provider },\n\tfields,\n})\n\n/**\n * The runtime provider-settings collection: one document per provider (and per scope\n * in scoped installs). Admin-only by default; `access` entries override individual\n * operations and `overrides` reshapes the whole config last, so slugs, labels,\n * fields, and access all stay overridable.\n */\nexport const buildProvidersCollection = (args: BuildProvidersCollectionArgs): CollectionConfig => {\n\tconst collection: CollectionConfig = {\n\t\tslug: args.slug,\n\t\tlabels: {\n\t\t\tsingular: labelForKey(keys.providersCollectionSingular),\n\t\t\tplural: labelForKey(keys.providersCollectionPlural),\n\t\t},\n\t\tdisableDuplicate: true,\n\t\tadmin: {\n\t\t\tuseAsTitle: 'name',\n\t\t\tdefaultColumns: ['name', 'provider', 'enabled'],\n\t\t\tgroup: 'Analytics',\n\t\t},\n\t\taccess: {\n\t\t\tread: providerRowAccess(args),\n\t\t\tcreate: providerCreateAccess(args),\n\t\t\tupdate: providerRowAccess(args),\n\t\t\tdelete: providerRowAccess(args),\n\t\t\t...args.access,\n\t\t},\n\t\thooks: {\n\t\t\tbeforeChange: [stampScope(args)],\n\t\t\tafterChange: [\n\t\t\t\t({ doc }) => {\n\t\t\t\t\targs.onChange()\n\t\t\t\t\treturn doc\n\t\t\t\t},\n\t\t\t],\n\t\t\tafterDelete: [\n\t\t\t\t({ doc }) => {\n\t\t\t\t\targs.onChange()\n\t\t\t\t\treturn doc\n\t\t\t\t},\n\t\t\t],\n\t\t},\n\t\tfields: [\n\t\t\t{\n\t\t\t\tname: 'name',\n\t\t\t\ttype: 'text',\n\t\t\t\trequired: true,\n\t\t\t\tlabel: labelForKey(keys.providerFieldName),\n\t\t\t},\n\t\t\t{\n\t\t\t\ttype: 'row',\n\t\t\t\tfields: [\n\t\t\t\t\t{\n\t\t\t\t\t\tname: 'provider',\n\t\t\t\t\t\ttype: 'select',\n\t\t\t\t\t\trequired: true,\n\t\t\t\t\t\tlabel: labelForKey(keys.providerFieldProvider),\n\t\t\t\t\t\toptions: [\n\t\t\t\t\t\t\t{ label: labelForKey(keys.providerNamePlausible), value: 'plausible' },\n\t\t\t\t\t\t\t{ label: labelForKey(keys.providerNameUmami), value: 'umami' },\n\t\t\t\t\t\t\t{ label: labelForKey(keys.providerNameGa4), value: 'ga4' },\n\t\t\t\t\t\t\t{ label: labelForKey(keys.providerNamePosthog), value: 'posthog' },\n\t\t\t\t\t\t],\n\t\t\t\t\t\tadmin: { width: '50%' },\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\tname: 'enabled',\n\t\t\t\t\t\ttype: 'checkbox',\n\t\t\t\t\t\tdefaultValue: true,\n\t\t\t\t\t\tlabel: labelForKey(keys.providerFieldEnabled),\n\t\t\t\t\t\tadmin: { width: '50%', style: { alignSelf: 'flex-end' } },\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t},\n\t\t\t{\n\t\t\t\t// Written by scoped setups (or a tenant plugin's own field via `scopeField`);\n\t\t\t\t// hidden because single-site installs never touch it.\n\t\t\t\tname: 'scope',\n\t\t\t\ttype: 'text',\n\t\t\t\tindex: true,\n\t\t\t\tlabel: labelForKey(keys.providerFieldScope),\n\t\t\t\tadmin: { hidden: true },\n\t\t\t},\n\t\t\tproviderGroup('plausible', keys.providerNamePlausible, [\n\t\t\t\t{\n\t\t\t\t\ttype: 'row',\n\t\t\t\t\tfields: [\n\t\t\t\t\t\ttextField('siteId', keys.providerFieldSiteId, '50%'),\n\t\t\t\t\t\t...secretField(args, { name: 'apiKey', label: keys.providerFieldApiKey, width: '50%' }),\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\ttype: 'row',\n\t\t\t\t\tfields: [\n\t\t\t\t\t\tcaptureField('domain', keys.providerFieldDomain, '50%'),\n\t\t\t\t\t\tcaptureField('scriptId', keys.providerFieldScriptId, '50%'),\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t\thostField(),\n\t\t\t]),\n\t\t\tproviderGroup('umami', keys.providerNameUmami, [\n\t\t\t\t{\n\t\t\t\t\ttype: 'row',\n\t\t\t\t\tfields: [\n\t\t\t\t\t\ttextField('websiteId', keys.providerFieldWebsiteId, '50%'),\n\t\t\t\t\t\t...secretField(args, { name: 'apiKey', label: keys.providerFieldApiKey, width: '50%' }),\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t\t...secretField(args, { name: 'token', label: keys.providerFieldToken }),\n\t\t\t\thostField(),\n\t\t\t]),\n\t\t\tproviderGroup('ga4', keys.providerNameGa4, [\n\t\t\t\t{\n\t\t\t\t\ttype: 'row',\n\t\t\t\t\tfields: [\n\t\t\t\t\t\ttextField('propertyId', keys.providerFieldPropertyId, '50%'),\n\t\t\t\t\t\ttextField('projectId', keys.providerFieldProjectId, '50%'),\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t\ttextField('clientEmail', keys.providerFieldClientEmail),\n\t\t\t\t...args.buildSecret({\n\t\t\t\t\tname: 'privateKey',\n\t\t\t\t\ttype: 'textarea',\n\t\t\t\t\tlabel: labelForKey(keys.providerFieldPrivateKey),\n\t\t\t\t}),\n\t\t\t]),\n\t\t\tproviderGroup('posthog', keys.providerNamePosthog, [\n\t\t\t\t{\n\t\t\t\t\ttype: 'row',\n\t\t\t\t\tfields: [\n\t\t\t\t\t\ttextField('projectId', keys.providerFieldProjectId, '50%'),\n\t\t\t\t\t\t...secretField(args, { name: 'apiKey', label: keys.providerFieldApiKey, width: '50%' }),\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\ttype: 'row',\n\t\t\t\t\tfields: [\n\t\t\t\t\t\tcaptureField('projectToken', keys.providerFieldProjectToken, '50%'),\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tname: 'region',\n\t\t\t\t\t\t\ttype: 'select',\n\t\t\t\t\t\t\tlabel: labelForKey(keys.providerFieldRegion),\n\t\t\t\t\t\t\toptions: [\n\t\t\t\t\t\t\t\t{ label: 'US', value: 'us' },\n\t\t\t\t\t\t\t\t{ label: 'EU', value: 'eu' },\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\tadmin: { width: '50%' },\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t\thostField(),\n\t\t\t]),\n\t\t],\n\t}\n\treturn args.overrides ? args.overrides(collection) : collection\n}\n"],"mappings":";;;;;AAQA,MAAa,iBAAiB;AAc9B,MAAM,aAAa,MAAc,OAAuB,WAA2B;CAClF;CACA,MAAM;CACN,OAAO,YAAY,KAAK;CACxB,GAAI,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC;AACrC;AAEA,MAAM,eACL,MACA,SAEA,KAAK,YAAY;CAChB,MAAM,KAAK;CACX,MAAM;CACN,OAAO,YAAY,KAAK,KAAK;CAC7B,GAAI,KAAK,QAAQ,EAAE,OAAO,EAAE,OAAO,KAAK,MAAM,EAAE,IAAI,CAAC;AACtD,CAAC;;;;;;AAOF,MAAM,gBAAgB,MAAc,OAAuB,WAA2B;CACrF;CACA,MAAM;CACN,OAAO,YAAY,KAAK;CACxB,OAAO;EACN,aAAa,YAAY,KAAK,wBAAwB;EACtD,GAAI,QAAQ,EAAE,MAAM,IAAI,CAAC;CAC1B;AACD;AAEA,MAAM,mBAA0B;CAC/B,MAAM;CACN,MAAM;CACN,OAAO,YAAY,KAAK,iBAAiB;CACzC,OAAO,EAAE,aAAa,YAAY,KAAK,qBAAqB,EAAE;AAC/D;AAEA,MAAM,iBAAiB,UAAsB,OAAuB,YAA4B;CAC/F,MAAM;CACN,MAAM;CACN,OAAO,YAAY,KAAK;CACxB,OAAO,EAAE,YAAY,SAAS,MAAM,aAAa,SAAS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AAM;MACN,MAAM;MACN,OAAO,YAAY,KAAK,mBAAmB;MAC3C,SAAS,CACR;OAAE,OAAO;OAAM,OAAO;MAAK,GAC3B;OAAE,OAAO;OAAM,OAAO;MAAK,CAC5B;MACA,OAAO,EAAE,OAAO,MAAM;KACvB,CACD;IACD;IACA,UAAU;GACX,CAAC;EACF;CACD;CACA,OAAO,KAAK,YAAY,KAAK,UAAU,UAAU,IAAI;AACtD"}
|
|
@@ -4,6 +4,8 @@ import { posthog } from "../adapters/posthog/posthog.js";
|
|
|
4
4
|
import { umami } from "../adapters/umami/umami.js";
|
|
5
5
|
//#region src/providers/factory.ts
|
|
6
6
|
const orUndefined = (value) => value || void 0;
|
|
7
|
+
/** An unset or unrecognized stored region falls back to host-derived detection. */
|
|
8
|
+
const posthogRegion = (value) => value === "us" || value === "eu" ? value : void 0;
|
|
7
9
|
/**
|
|
8
10
|
* Keys pasted from a service-account JSON often carry escaped newlines; the gRPC
|
|
9
11
|
* client needs real ones.
|
|
@@ -26,7 +28,9 @@ const buildBaseAdapter = (doc) => {
|
|
|
26
28
|
return plausible({
|
|
27
29
|
siteId: cfg.siteId ?? "",
|
|
28
30
|
apiKey: cfg.apiKey ?? "",
|
|
29
|
-
host: orUndefined(cfg.host)
|
|
31
|
+
host: orUndefined(cfg.host),
|
|
32
|
+
domain: orUndefined(cfg.domain),
|
|
33
|
+
scriptId: orUndefined(cfg.scriptId)
|
|
30
34
|
});
|
|
31
35
|
}
|
|
32
36
|
case "umami": {
|
|
@@ -54,7 +58,9 @@ const buildBaseAdapter = (doc) => {
|
|
|
54
58
|
return posthog({
|
|
55
59
|
projectId: cfg.projectId ?? "",
|
|
56
60
|
apiKey: cfg.apiKey ?? "",
|
|
57
|
-
host: orUndefined(cfg.host)
|
|
61
|
+
host: orUndefined(cfg.host),
|
|
62
|
+
projectToken: orUndefined(cfg.projectToken),
|
|
63
|
+
region: posthogRegion(cfg.region)
|
|
58
64
|
});
|
|
59
65
|
}
|
|
60
66
|
default: return null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"factory.js","names":[],"sources":["../../src/providers/factory.ts"],"sourcesContent":["import { ga4 } from '../adapters/ga4/ga4'\nimport { plausible } from '../adapters/plausible/plausible'\nimport { posthog } from '../adapters/posthog/posthog'\nimport { umami } from '../adapters/umami/umami'\nimport type { AnalyticsAdapter } from '../core/contract'\n\nexport const PROVIDER_IDS = ['plausible', 'umami', 'ga4', 'posthog'] as const\nexport type ProviderId = (typeof PROVIDER_IDS)[number]\n\n/** The provider-settings document shape the adapter factory reads (depth 0, secrets revealed). */\nexport type ProviderDoc = {\n\tid?: number | string\n\tname?: string | null\n\tprovider?: string | null\n\tenabled?: boolean | null\n\tscope?: string | null\n\tplausible?: {
|
|
1
|
+
{"version":3,"file":"factory.js","names":[],"sources":["../../src/providers/factory.ts"],"sourcesContent":["import { ga4 } from '../adapters/ga4/ga4'\nimport { plausible } from '../adapters/plausible/plausible'\nimport { posthog } from '../adapters/posthog/posthog'\nimport { umami } from '../adapters/umami/umami'\nimport type { AnalyticsAdapter } from '../core/contract'\n\nexport const PROVIDER_IDS = ['plausible', 'umami', 'ga4', 'posthog'] as const\nexport type ProviderId = (typeof PROVIDER_IDS)[number]\n\n/** The provider-settings document shape the adapter factory reads (depth 0, secrets revealed). */\nexport type ProviderDoc = {\n\tid?: number | string\n\tname?: string | null\n\tprovider?: string | null\n\tenabled?: boolean | null\n\tscope?: string | null\n\tplausible?: {\n\t\tsiteId?: string | null\n\t\tapiKey?: string | null\n\t\thost?: string | null\n\t\tdomain?: string | null\n\t\tscriptId?: string | null\n\t} | null\n\tumami?: {\n\t\twebsiteId?: string | null\n\t\tapiKey?: string | null\n\t\ttoken?: string | null\n\t\thost?: string | null\n\t} | null\n\tga4?: {\n\t\tpropertyId?: string | null\n\t\tclientEmail?: string | null\n\t\tprivateKey?: string | null\n\t\tprojectId?: string | null\n\t} | null\n\tposthog?: {\n\t\tprojectId?: string | null\n\t\tapiKey?: string | null\n\t\thost?: string | null\n\t\tprojectToken?: string | null\n\t\tregion?: string | null\n\t} | null\n}\n\nconst orUndefined = (value: string | null | undefined): string | undefined => value || undefined\n\n/** An unset or unrecognized stored region falls back to host-derived detection. */\nconst posthogRegion = (value: string | null | undefined): 'us' | 'eu' | undefined =>\n\tvalue === 'us' || value === 'eu' ? value : undefined\n\n/**\n * Keys pasted from a service-account JSON often carry escaped newlines; the gRPC\n * client needs real ones.\n */\nexport const normalizePrivateKey = (key: string): string => key.replace(/\\\\n/g, '\\n')\n\nconst shortId = (id: number | string): string => String(id).slice(-6)\n\n/** Runtime adapters carry a per-document instance id so two projects of one provider type never collide in a registry; config adapters keep their plain ids. */\nconst withInstanceIdentity = (adapter: AnalyticsAdapter, doc: ProviderDoc): AnalyticsAdapter => {\n\tif (doc.id === undefined || doc.id === null || doc.id === '') {\n\t\treturn adapter\n\t}\n\treturn {\n\t\t...adapter,\n\t\tid: `${doc.provider}:${doc.id}`,\n\t\tlabel: doc.name || `${adapter.label} ${shortId(doc.id)}`,\n\t}\n}\n\nconst buildBaseAdapter = (doc: ProviderDoc): AnalyticsAdapter | null => {\n\tswitch (doc.provider) {\n\t\tcase 'plausible': {\n\t\t\tconst cfg = doc.plausible ?? {}\n\t\t\treturn plausible({\n\t\t\t\tsiteId: cfg.siteId ?? '',\n\t\t\t\tapiKey: cfg.apiKey ?? '',\n\t\t\t\thost: orUndefined(cfg.host),\n\t\t\t\tdomain: orUndefined(cfg.domain),\n\t\t\t\tscriptId: orUndefined(cfg.scriptId),\n\t\t\t})\n\t\t}\n\t\tcase 'umami': {\n\t\t\tconst cfg = doc.umami ?? {}\n\t\t\treturn umami({\n\t\t\t\twebsiteId: cfg.websiteId ?? '',\n\t\t\t\tapiKey: orUndefined(cfg.apiKey),\n\t\t\t\ttoken: orUndefined(cfg.token),\n\t\t\t\thost: orUndefined(cfg.host),\n\t\t\t})\n\t\t}\n\t\tcase 'ga4': {\n\t\t\tconst cfg = doc.ga4 ?? {}\n\t\t\treturn ga4({\n\t\t\t\tpropertyId: cfg.propertyId ?? '',\n\t\t\t\tcredentials: {\n\t\t\t\t\tclient_email: cfg.clientEmail ?? '',\n\t\t\t\t\tprivate_key: normalizePrivateKey(cfg.privateKey ?? ''),\n\t\t\t\t},\n\t\t\t\tprojectId: orUndefined(cfg.projectId),\n\t\t\t})\n\t\t}\n\t\tcase 'posthog': {\n\t\t\tconst cfg = doc.posthog ?? {}\n\t\t\treturn posthog({\n\t\t\t\tprojectId: cfg.projectId ?? '',\n\t\t\t\tapiKey: cfg.apiKey ?? '',\n\t\t\t\thost: orUndefined(cfg.host),\n\t\t\t\tprojectToken: orUndefined(cfg.projectToken),\n\t\t\t\tregion: posthogRegion(cfg.region),\n\t\t\t})\n\t\t}\n\t\tdefault:\n\t\t\treturn null\n\t}\n}\n\n/**\n * Build an adapter instance from a provider-settings document using the same\n * constructors as config-time adapters. Missing credentials are passed through as\n * empty strings so the adapter reports `isConfigured() === false` instead of the\n * factory throwing; an unknown provider value returns null and is skipped.\n */\nexport const adapterFromProviderDoc = (doc: ProviderDoc): AnalyticsAdapter | null => {\n\tconst base = buildBaseAdapter(doc)\n\treturn base ? withInstanceIdentity(base, doc) : null\n}\n"],"mappings":";;;;;AA4CA,MAAM,eAAe,UAAyD,SAAS,KAAA;;AAGvF,MAAM,iBAAiB,UACtB,UAAU,QAAQ,UAAU,OAAO,QAAQ,KAAA;;;;;AAM5C,MAAa,uBAAuB,QAAwB,IAAI,QAAQ,QAAQ,IAAI;AAEpF,MAAM,WAAW,OAAgC,OAAO,EAAE,EAAE,MAAM,EAAE;;AAGpE,MAAM,wBAAwB,SAA2B,QAAuC;CAC/F,IAAI,IAAI,OAAO,KAAA,KAAa,IAAI,OAAO,QAAQ,IAAI,OAAO,IACzD,OAAO;CAER,OAAO;EACN,GAAG;EACH,IAAI,GAAG,IAAI,SAAS,GAAG,IAAI;EAC3B,OAAO,IAAI,QAAQ,GAAG,QAAQ,MAAM,GAAG,QAAQ,IAAI,EAAE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{ ReactElement, ReactNode } from "react";
|
|
4
|
+
|
|
5
|
+
//#region src/react/AnalyticsProvider.d.ts
|
|
6
|
+
/** The tracker surface a component gets from `useAnalytics`. */
|
|
7
|
+
interface AnalyticsApi {
|
|
8
|
+
track: Tracker['track'];
|
|
9
|
+
trackGoal: Tracker['trackGoal'];
|
|
10
|
+
consent: Tracker['consent'];
|
|
11
|
+
}
|
|
12
|
+
interface AnalyticsProviderProps {
|
|
13
|
+
/** Resolved server-side by `getTrackerConfig`, or fetched from the tracker endpoint. */
|
|
14
|
+
config: TrackerConfig;
|
|
15
|
+
children?: ReactNode;
|
|
16
|
+
/** CSP nonce applied to the vendor scripts the tracker injects. */
|
|
17
|
+
nonce?: string;
|
|
18
|
+
/** Replaces script injection, for a consent manager or a framework script loader. */
|
|
19
|
+
loadScript?: LoadScript;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Shares the window's tracker with the tree through `useAnalytics`. Use it when components
|
|
23
|
+
* need to call `track`, `trackGoal`, or `consent` themselves; a page that only needs
|
|
24
|
+
* pageviews and auto-capture can render `<AnalyticsScripts />` (which boots
|
|
25
|
+
* `<TrackerBoot />`) and skip the provider. Rendering both is fine: the tracker is leased
|
|
26
|
+
* from a per-window registry, so there is still only one.
|
|
27
|
+
*
|
|
28
|
+
* The lease is taken lazily in the browser rather than during render, so server rendering
|
|
29
|
+
* stays inert and React's double-invoked render cannot leak a second tracker.
|
|
30
|
+
*
|
|
31
|
+
* Whichever component boots the tracker first owns its options, so a `nonce` or
|
|
32
|
+
* `loadScript` passed to a provider that mounts after `<TrackerBoot />` is ignored: give
|
|
33
|
+
* them to whatever boots first, or render only one of the two.
|
|
34
|
+
*/
|
|
35
|
+
declare const AnalyticsProvider: ({
|
|
36
|
+
config,
|
|
37
|
+
children,
|
|
38
|
+
nonce,
|
|
39
|
+
loadScript
|
|
40
|
+
}: AnalyticsProviderProps) => ReactElement;
|
|
41
|
+
//#endregion
|
|
42
|
+
export { AnalyticsApi, AnalyticsProvider, AnalyticsProviderProps };
|
|
43
|
+
//# sourceMappingURL=AnalyticsProvider.d.ts.map
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { acquireTracker } from "../tracker/registry.js";
|
|
3
|
+
import { createContext, useCallback, useEffect, useMemo, useRef } from "react";
|
|
4
|
+
import { jsx } from "react/jsx-runtime";
|
|
5
|
+
//#region src/react/AnalyticsProvider.tsx
|
|
6
|
+
const AnalyticsContext = createContext(null);
|
|
7
|
+
/**
|
|
8
|
+
* Shares the window's tracker with the tree through `useAnalytics`. Use it when components
|
|
9
|
+
* need to call `track`, `trackGoal`, or `consent` themselves; a page that only needs
|
|
10
|
+
* pageviews and auto-capture can render `<AnalyticsScripts />` (which boots
|
|
11
|
+
* `<TrackerBoot />`) and skip the provider. Rendering both is fine: the tracker is leased
|
|
12
|
+
* from a per-window registry, so there is still only one.
|
|
13
|
+
*
|
|
14
|
+
* The lease is taken lazily in the browser rather than during render, so server rendering
|
|
15
|
+
* stays inert and React's double-invoked render cannot leak a second tracker.
|
|
16
|
+
*
|
|
17
|
+
* Whichever component boots the tracker first owns its options, so a `nonce` or
|
|
18
|
+
* `loadScript` passed to a provider that mounts after `<TrackerBoot />` is ignored: give
|
|
19
|
+
* them to whatever boots first, or render only one of the two.
|
|
20
|
+
*/
|
|
21
|
+
const AnalyticsProvider = ({ config, children, nonce, loadScript }) => {
|
|
22
|
+
const boot = useRef({
|
|
23
|
+
config,
|
|
24
|
+
nonce,
|
|
25
|
+
loadScript
|
|
26
|
+
});
|
|
27
|
+
boot.current = {
|
|
28
|
+
config,
|
|
29
|
+
nonce,
|
|
30
|
+
loadScript
|
|
31
|
+
};
|
|
32
|
+
const lease = useRef(null);
|
|
33
|
+
const unmounted = useRef(false);
|
|
34
|
+
const ensure = useCallback(() => {
|
|
35
|
+
if (!lease.current && !unmounted.current && typeof window !== "undefined") {
|
|
36
|
+
const { config: current, nonce: currentNonce, loadScript: currentLoad } = boot.current;
|
|
37
|
+
lease.current = acquireTracker(current, {
|
|
38
|
+
nonce: currentNonce,
|
|
39
|
+
loadScript: currentLoad
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return lease.current?.tracker ?? null;
|
|
43
|
+
}, []);
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
unmounted.current = false;
|
|
46
|
+
ensure();
|
|
47
|
+
return () => {
|
|
48
|
+
unmounted.current = true;
|
|
49
|
+
lease.current?.release();
|
|
50
|
+
lease.current = null;
|
|
51
|
+
};
|
|
52
|
+
}, [ensure]);
|
|
53
|
+
const api = useMemo(() => ({
|
|
54
|
+
track: (name, props) => ensure()?.track(name, props),
|
|
55
|
+
trackGoal: (slug, opts) => ensure()?.trackGoal(slug, opts),
|
|
56
|
+
consent: (state) => ensure()?.consent(state)
|
|
57
|
+
}), [ensure]);
|
|
58
|
+
return /* @__PURE__ */ jsx(AnalyticsContext.Provider, {
|
|
59
|
+
value: api,
|
|
60
|
+
children
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
//#endregion
|
|
64
|
+
export { AnalyticsContext, AnalyticsProvider };
|
|
65
|
+
|
|
66
|
+
//# sourceMappingURL=AnalyticsProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AnalyticsProvider.js","names":[],"sources":["../../src/react/AnalyticsProvider.tsx"],"sourcesContent":["'use client'\n\nimport {\n\tcreateContext,\n\ttype ReactElement,\n\ttype ReactNode,\n\tuseCallback,\n\tuseEffect,\n\tuseMemo,\n\tuseRef,\n} from 'react'\nimport type { TrackerConfig } from '../capture/trackerConfig'\nimport { acquireTracker, type TrackerLease } from '../tracker/registry'\nimport type { LoadScript, Tracker } from '../tracker/types'\n\n/** The tracker surface a component gets from `useAnalytics`. */\nexport interface AnalyticsApi {\n\ttrack: Tracker['track']\n\ttrackGoal: Tracker['trackGoal']\n\tconsent: Tracker['consent']\n}\n\nexport const AnalyticsContext = createContext<AnalyticsApi | null>(null)\n\nexport interface AnalyticsProviderProps {\n\t/** Resolved server-side by `getTrackerConfig`, or fetched from the tracker endpoint. */\n\tconfig: TrackerConfig\n\tchildren?: ReactNode\n\t/** CSP nonce applied to the vendor scripts the tracker injects. */\n\tnonce?: string\n\t/** Replaces script injection, for a consent manager or a framework script loader. */\n\tloadScript?: LoadScript\n}\n\n/**\n * Shares the window's tracker with the tree through `useAnalytics`. Use it when components\n * need to call `track`, `trackGoal`, or `consent` themselves; a page that only needs\n * pageviews and auto-capture can render `<AnalyticsScripts />` (which boots\n * `<TrackerBoot />`) and skip the provider. Rendering both is fine: the tracker is leased\n * from a per-window registry, so there is still only one.\n *\n * The lease is taken lazily in the browser rather than during render, so server rendering\n * stays inert and React's double-invoked render cannot leak a second tracker.\n *\n * Whichever component boots the tracker first owns its options, so a `nonce` or\n * `loadScript` passed to a provider that mounts after `<TrackerBoot />` is ignored: give\n * them to whatever boots first, or render only one of the two.\n */\nexport const AnalyticsProvider = ({\n\tconfig,\n\tchildren,\n\tnonce,\n\tloadScript,\n}: AnalyticsProviderProps): ReactElement => {\n\tconst boot = useRef({ config, nonce, loadScript })\n\tboot.current = { config, nonce, loadScript }\n\tconst lease = useRef<TrackerLease | null>(null)\n\tconst unmounted = useRef(false)\n\n\tconst ensure = useCallback((): Tracker | null => {\n\t\tif (!lease.current && !unmounted.current && typeof window !== 'undefined') {\n\t\t\tconst { config: current, nonce: currentNonce, loadScript: currentLoad } = boot.current\n\t\t\tlease.current = acquireTracker(current, { nonce: currentNonce, loadScript: currentLoad })\n\t\t}\n\t\treturn lease.current?.tracker ?? null\n\t}, [])\n\n\tuseEffect(() => {\n\t\tunmounted.current = false\n\t\tensure()\n\t\treturn () => {\n\t\t\t// A stale `track` call held past unmount must not resurrect the tracker.\n\t\t\tunmounted.current = true\n\t\t\tlease.current?.release()\n\t\t\tlease.current = null\n\t\t}\n\t}, [ensure])\n\n\tconst api = useMemo<AnalyticsApi>(\n\t\t() => ({\n\t\t\ttrack: (name, props) => ensure()?.track(name, props),\n\t\t\ttrackGoal: (slug, opts) => ensure()?.trackGoal(slug, opts),\n\t\t\tconsent: (state) => ensure()?.consent(state),\n\t\t}),\n\t\t[ensure]\n\t)\n\n\treturn <AnalyticsContext.Provider value={api}>{children}</AnalyticsContext.Provider>\n}\n"],"mappings":";;;;;AAsBA,MAAa,mBAAmB,cAAmC,IAAI;;;;;;;;;;;;;;;AA0BvE,MAAa,qBAAqB,EACjC,QACA,UACA,OACA,iBAC2C;CAC3C,MAAM,OAAO,OAAO;EAAE;EAAQ;EAAO;CAAW,CAAC;CACjD,KAAK,UAAU;EAAE;EAAQ;EAAO;CAAW;CAC3C,MAAM,QAAQ,OAA4B,IAAI;CAC9C,MAAM,YAAY,OAAO,KAAK;CAE9B,MAAM,SAAS,kBAAkC;EAChD,IAAI,CAAC,MAAM,WAAW,CAAC,UAAU,WAAW,OAAO,WAAW,aAAa;GAC1E,MAAM,EAAE,QAAQ,SAAS,OAAO,cAAc,YAAY,gBAAgB,KAAK;GAC/E,MAAM,UAAU,eAAe,SAAS;IAAE,OAAO;IAAc,YAAY;GAAY,CAAC;EACzF;EACA,OAAO,MAAM,SAAS,WAAW;CAClC,GAAG,CAAC,CAAC;CAEL,gBAAgB;EACf,UAAU,UAAU;EACpB,OAAO;EACP,aAAa;GAEZ,UAAU,UAAU;GACpB,MAAM,SAAS,QAAQ;GACvB,MAAM,UAAU;EACjB;CACD,GAAG,CAAC,MAAM,CAAC;CAEX,MAAM,MAAM,eACJ;EACN,QAAQ,MAAM,UAAU,OAAO,GAAG,MAAM,MAAM,KAAK;EACnD,YAAY,MAAM,SAAS,OAAO,GAAG,UAAU,MAAM,IAAI;EACzD,UAAU,UAAU,OAAO,GAAG,QAAQ,KAAK;CAC5C,IACA,CAAC,MAAM,CACR;CAEA,OAAO,oBAAC,iBAAiB,UAAlB;EAA2B,OAAO;EAAM;CAAoC,CAAA;AACpF"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { TrackerConfig } from "../capture/trackerConfig.js";
|
|
2
|
+
|
|
3
|
+
//#region src/react/TrackerBoot.d.ts
|
|
4
|
+
interface TrackerBootProps {
|
|
5
|
+
config: TrackerConfig;
|
|
6
|
+
/** CSP nonce for any script this component injects. */
|
|
7
|
+
nonce?: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Boots the browser tracker from a server-resolved config and renders nothing. This is the
|
|
11
|
+
* RSC path: `<AnalyticsScripts />` renders it after the slot snippets, so a page gets
|
|
12
|
+
* pageviews and auto-capture with no provider and no client state. Wrap the tree in
|
|
13
|
+
* `<AnalyticsProvider>` as well when components need to call `track` themselves: both take
|
|
14
|
+
* a lease on the window's one tracker, so neither double-counts.
|
|
15
|
+
*/
|
|
16
|
+
declare const TrackerBoot: ({
|
|
17
|
+
config,
|
|
18
|
+
nonce
|
|
19
|
+
}: TrackerBootProps) => null;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { TrackerBoot, TrackerBootProps };
|
|
22
|
+
//# sourceMappingURL=TrackerBoot.d.ts.map
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { acquireTracker } from "../tracker/registry.js";
|
|
3
|
+
import { useEffect, useRef } from "react";
|
|
4
|
+
//#region src/react/TrackerBoot.tsx
|
|
5
|
+
/**
|
|
6
|
+
* Boots the browser tracker from a server-resolved config and renders nothing. This is the
|
|
7
|
+
* RSC path: `<AnalyticsScripts />` renders it after the slot snippets, so a page gets
|
|
8
|
+
* pageviews and auto-capture with no provider and no client state. Wrap the tree in
|
|
9
|
+
* `<AnalyticsProvider>` as well when components need to call `track` themselves: both take
|
|
10
|
+
* a lease on the window's one tracker, so neither double-counts.
|
|
11
|
+
*/
|
|
12
|
+
const TrackerBoot = ({ config, nonce }) => {
|
|
13
|
+
const boot = useRef({
|
|
14
|
+
config,
|
|
15
|
+
nonce
|
|
16
|
+
});
|
|
17
|
+
boot.current = {
|
|
18
|
+
config,
|
|
19
|
+
nonce
|
|
20
|
+
};
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
const lease = acquireTracker(boot.current.config, { nonce: boot.current.nonce });
|
|
23
|
+
return () => lease.release();
|
|
24
|
+
}, []);
|
|
25
|
+
return null;
|
|
26
|
+
};
|
|
27
|
+
//#endregion
|
|
28
|
+
export { TrackerBoot };
|
|
29
|
+
|
|
30
|
+
//# sourceMappingURL=TrackerBoot.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TrackerBoot.js","names":[],"sources":["../../src/react/TrackerBoot.tsx"],"sourcesContent":["'use client'\n\nimport { useEffect, useRef } from 'react'\nimport type { TrackerConfig } from '../capture/trackerConfig'\nimport { acquireTracker } from '../tracker/registry'\n\nexport interface TrackerBootProps {\n\tconfig: TrackerConfig\n\t/** CSP nonce for any script this component injects. */\n\tnonce?: string\n}\n\n/**\n * Boots the browser tracker from a server-resolved config and renders nothing. This is the\n * RSC path: `<AnalyticsScripts />` renders it after the slot snippets, so a page gets\n * pageviews and auto-capture with no provider and no client state. Wrap the tree in\n * `<AnalyticsProvider>` as well when components need to call `track` themselves: both take\n * a lease on the window's one tracker, so neither double-counts.\n */\nexport const TrackerBoot = ({ config, nonce }: TrackerBootProps): null => {\n\tconst boot = useRef({ config, nonce })\n\tboot.current = { config, nonce }\n\n\tuseEffect(() => {\n\t\tconst lease = acquireTracker(boot.current.config, { nonce: boot.current.nonce })\n\t\treturn () => lease.release()\n\t}, [])\n\n\treturn null\n}\n"],"mappings":";;;;;;;;;;;AAmBA,MAAa,eAAe,EAAE,QAAQ,YAAoC;CACzE,MAAM,OAAO,OAAO;EAAE;EAAQ;CAAM,CAAC;CACrC,KAAK,UAAU;EAAE;EAAQ;CAAM;CAE/B,gBAAgB;EACf,MAAM,QAAQ,eAAe,KAAK,QAAQ,QAAQ,EAAE,OAAO,KAAK,QAAQ,MAAM,CAAC;EAC/E,aAAa,MAAM,QAAQ;CAC5B,GAAG,CAAC,CAAC;CAEL,OAAO;AACR"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { AnalyticsApi } from "./AnalyticsProvider.js";
|
|
2
|
+
|
|
3
|
+
//#region src/react/useAnalytics.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* The window's tracker: the surrounding `<AnalyticsProvider>`'s when there is one, else
|
|
6
|
+
* whichever instance `<AnalyticsScripts />` booted, so the RSC path alone is enough for a
|
|
7
|
+
* client component to send its own events.
|
|
8
|
+
*
|
|
9
|
+
* The tracker is resolved per call, not per render: `<TrackerBoot />`'s effect may not have
|
|
10
|
+
* run when a sibling renders, and an eagerly resolved null would never recover. A call that
|
|
11
|
+
* finds neither a provider nor a booted tracker throws, because a silently dead `track` is
|
|
12
|
+
* worse than a build-time mistake.
|
|
13
|
+
*/
|
|
14
|
+
declare const useAnalytics: () => AnalyticsApi;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { useAnalytics };
|
|
17
|
+
//# sourceMappingURL=useAnalytics.d.ts.map
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { liveTracker } from "../tracker/registry.js";
|
|
3
|
+
import { AnalyticsContext } from "./AnalyticsProvider.js";
|
|
4
|
+
import { useContext, useMemo } from "react";
|
|
5
|
+
//#region src/react/useAnalytics.tsx
|
|
6
|
+
const NO_TRACKER = "analytics: useAnalytics found no tracker. Render <AnalyticsScripts /> (or <AnalyticsProvider>) above this component.";
|
|
7
|
+
/**
|
|
8
|
+
* The window's tracker: the surrounding `<AnalyticsProvider>`'s when there is one, else
|
|
9
|
+
* whichever instance `<AnalyticsScripts />` booted, so the RSC path alone is enough for a
|
|
10
|
+
* client component to send its own events.
|
|
11
|
+
*
|
|
12
|
+
* The tracker is resolved per call, not per render: `<TrackerBoot />`'s effect may not have
|
|
13
|
+
* run when a sibling renders, and an eagerly resolved null would never recover. A call that
|
|
14
|
+
* finds neither a provider nor a booted tracker throws, because a silently dead `track` is
|
|
15
|
+
* worse than a build-time mistake.
|
|
16
|
+
*/
|
|
17
|
+
const useAnalytics = () => {
|
|
18
|
+
const provided = useContext(AnalyticsContext);
|
|
19
|
+
return useMemo(() => {
|
|
20
|
+
if (provided) return provided;
|
|
21
|
+
const tracker = () => {
|
|
22
|
+
const live = liveTracker();
|
|
23
|
+
if (!live) throw new Error(NO_TRACKER);
|
|
24
|
+
return live;
|
|
25
|
+
};
|
|
26
|
+
return {
|
|
27
|
+
track: (name, props) => tracker().track(name, props),
|
|
28
|
+
trackGoal: (slug, opts) => tracker().trackGoal(slug, opts),
|
|
29
|
+
consent: (state) => tracker().consent(state)
|
|
30
|
+
};
|
|
31
|
+
}, [provided]);
|
|
32
|
+
};
|
|
33
|
+
//#endregion
|
|
34
|
+
export { useAnalytics };
|
|
35
|
+
|
|
36
|
+
//# sourceMappingURL=useAnalytics.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAnalytics.js","names":[],"sources":["../../src/react/useAnalytics.tsx"],"sourcesContent":["'use client'\n\nimport { useContext, useMemo } from 'react'\nimport { liveTracker } from '../tracker/registry'\nimport type { Tracker } from '../tracker/types'\nimport { type AnalyticsApi, AnalyticsContext } from './AnalyticsProvider'\n\nconst NO_TRACKER =\n\t'analytics: useAnalytics found no tracker. Render <AnalyticsScripts /> (or <AnalyticsProvider>) above this component.'\n\n/**\n * The window's tracker: the surrounding `<AnalyticsProvider>`'s when there is one, else\n * whichever instance `<AnalyticsScripts />` booted, so the RSC path alone is enough for a\n * client component to send its own events.\n *\n * The tracker is resolved per call, not per render: `<TrackerBoot />`'s effect may not have\n * run when a sibling renders, and an eagerly resolved null would never recover. A call that\n * finds neither a provider nor a booted tracker throws, because a silently dead `track` is\n * worse than a build-time mistake.\n */\nexport const useAnalytics = (): AnalyticsApi => {\n\tconst provided = useContext(AnalyticsContext)\n\treturn useMemo<AnalyticsApi>(() => {\n\t\tif (provided) {\n\t\t\treturn provided\n\t\t}\n\t\tconst tracker = (): Tracker => {\n\t\t\tconst live = liveTracker()\n\t\t\tif (!live) {\n\t\t\t\tthrow new Error(NO_TRACKER)\n\t\t\t}\n\t\t\treturn live\n\t\t}\n\t\treturn {\n\t\t\ttrack: (name, props) => tracker().track(name, props),\n\t\t\ttrackGoal: (slug, opts) => tracker().trackGoal(slug, opts),\n\t\t\tconsent: (state) => tracker().consent(state),\n\t\t}\n\t}, [provided])\n}\n"],"mappings":";;;;;AAOA,MAAM,aACL;;;;;;;;;;;AAYD,MAAa,qBAAmC;CAC/C,MAAM,WAAW,WAAW,gBAAgB;CAC5C,OAAO,cAA4B;EAClC,IAAI,UACH,OAAO;EAER,MAAM,gBAAyB;GAC9B,MAAM,OAAO,YAAY;GACzB,IAAI,CAAC,MACJ,MAAM,IAAI,MAAM,UAAU;GAE3B,OAAO;EACR;EACA,OAAO;GACN,QAAQ,MAAM,UAAU,QAAQ,EAAE,MAAM,MAAM,KAAK;GACnD,YAAY,MAAM,SAAS,QAAQ,EAAE,UAAU,MAAM,IAAI;GACzD,UAAU,UAAU,QAAQ,EAAE,QAAQ,KAAK;EAC5C;CACD,GAAG,CAAC,QAAQ,CAAC;AACd"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { TrackerConfig } from "../capture/trackerConfig.js";
|
|
2
|
+
import { ReactElement } from "react";
|
|
3
|
+
|
|
4
|
+
//#region src/rsc/AnalyticsScripts.d.ts
|
|
5
|
+
interface AnalyticsScriptsProps {
|
|
6
|
+
/** Resolved server-side, by `getTrackerConfig` or the tracker endpoint. */
|
|
7
|
+
config: TrackerConfig;
|
|
8
|
+
/** CSP nonce, applied to every script this renders. */
|
|
9
|
+
nonce?: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Renders each capture slot's vendor snippet, then boots the tracker over them. Snippet
|
|
13
|
+
* scripts come from the slot adapter's own `capture.snippet`, so they are plugin-authored
|
|
14
|
+
* strings rather than anything a request supplied; the inline ones are injected verbatim
|
|
15
|
+
* because a vendor init call has to parse as script, not as text.
|
|
16
|
+
*
|
|
17
|
+
* A slot that waits for consent renders nothing: shipping its script in the HTML would run
|
|
18
|
+
* the vendor's tracker before the visitor answered, which is the whole point of the gate.
|
|
19
|
+
* Its sink injects the same snippet once consent is granted (or immediately, when a granted
|
|
20
|
+
* decision was already persisted), and the loader skips scripts the page already carries.
|
|
21
|
+
*
|
|
22
|
+
* A native-only install renders no snippet at all: the tracker is the client.
|
|
23
|
+
*/
|
|
24
|
+
declare const AnalyticsScripts: ({
|
|
25
|
+
config,
|
|
26
|
+
nonce
|
|
27
|
+
}: AnalyticsScriptsProps) => ReactElement;
|
|
28
|
+
//#endregion
|
|
29
|
+
export { AnalyticsScripts, AnalyticsScriptsProps };
|
|
30
|
+
//# sourceMappingURL=AnalyticsScripts.d.ts.map
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { TrackerBoot } from "../react/TrackerBoot.js";
|
|
2
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
//#region src/rsc/AnalyticsScripts.tsx
|
|
4
|
+
/**
|
|
5
|
+
* Renders each capture slot's vendor snippet, then boots the tracker over them. Snippet
|
|
6
|
+
* scripts come from the slot adapter's own `capture.snippet`, so they are plugin-authored
|
|
7
|
+
* strings rather than anything a request supplied; the inline ones are injected verbatim
|
|
8
|
+
* because a vendor init call has to parse as script, not as text.
|
|
9
|
+
*
|
|
10
|
+
* A slot that waits for consent renders nothing: shipping its script in the HTML would run
|
|
11
|
+
* the vendor's tracker before the visitor answered, which is the whole point of the gate.
|
|
12
|
+
* Its sink injects the same snippet once consent is granted (or immediately, when a granted
|
|
13
|
+
* decision was already persisted), and the loader skips scripts the page already carries.
|
|
14
|
+
*
|
|
15
|
+
* A native-only install renders no snippet at all: the tracker is the client.
|
|
16
|
+
*/
|
|
17
|
+
const AnalyticsScripts = ({ config, nonce }) => /* @__PURE__ */ jsxs(Fragment, { children: [config.slots.flatMap((slot) => slot.requiresConsent ? [] : slot.snippet.scripts.map((script, index) => {
|
|
18
|
+
const key = `${slot.slot}-${index}`;
|
|
19
|
+
return script.inline ? /* @__PURE__ */ jsx("script", {
|
|
20
|
+
...script.attrs,
|
|
21
|
+
async: script.async,
|
|
22
|
+
defer: script.defer,
|
|
23
|
+
dangerouslySetInnerHTML: { __html: script.inline },
|
|
24
|
+
nonce,
|
|
25
|
+
type: script.type
|
|
26
|
+
}, key) : /* @__PURE__ */ jsx("script", {
|
|
27
|
+
...script.attrs,
|
|
28
|
+
async: script.async,
|
|
29
|
+
defer: script.defer,
|
|
30
|
+
nonce,
|
|
31
|
+
src: script.src,
|
|
32
|
+
type: script.type
|
|
33
|
+
}, key);
|
|
34
|
+
})), /* @__PURE__ */ jsx(TrackerBoot, {
|
|
35
|
+
config,
|
|
36
|
+
nonce
|
|
37
|
+
})] });
|
|
38
|
+
//#endregion
|
|
39
|
+
export { AnalyticsScripts };
|
|
40
|
+
|
|
41
|
+
//# sourceMappingURL=AnalyticsScripts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AnalyticsScripts.js","names":[],"sources":["../../src/rsc/AnalyticsScripts.tsx"],"sourcesContent":["import type { ReactElement } from 'react'\nimport type { TrackerConfig } from '../capture/trackerConfig'\nimport { TrackerBoot } from '../react/TrackerBoot'\n\nexport interface AnalyticsScriptsProps {\n\t/** Resolved server-side, by `getTrackerConfig` or the tracker endpoint. */\n\tconfig: TrackerConfig\n\t/** CSP nonce, applied to every script this renders. */\n\tnonce?: string\n}\n\n/**\n * Renders each capture slot's vendor snippet, then boots the tracker over them. Snippet\n * scripts come from the slot adapter's own `capture.snippet`, so they are plugin-authored\n * strings rather than anything a request supplied; the inline ones are injected verbatim\n * because a vendor init call has to parse as script, not as text.\n *\n * A slot that waits for consent renders nothing: shipping its script in the HTML would run\n * the vendor's tracker before the visitor answered, which is the whole point of the gate.\n * Its sink injects the same snippet once consent is granted (or immediately, when a granted\n * decision was already persisted), and the loader skips scripts the page already carries.\n *\n * A native-only install renders no snippet at all: the tracker is the client.\n */\nexport const AnalyticsScripts = ({ config, nonce }: AnalyticsScriptsProps): ReactElement => (\n\t<>\n\t\t{config.slots.flatMap((slot) =>\n\t\t\tslot.requiresConsent\n\t\t\t\t? []\n\t\t\t\t: slot.snippet.scripts.map((script, index) => {\n\t\t\t\t\t\tconst key = `${slot.slot}-${index}`\n\t\t\t\t\t\treturn script.inline ? (\n\t\t\t\t\t\t\t<script\n\t\t\t\t\t\t\t\tkey={key}\n\t\t\t\t\t\t\t\t{...script.attrs}\n\t\t\t\t\t\t\t\tasync={script.async}\n\t\t\t\t\t\t\t\tdefer={script.defer}\n\t\t\t\t\t\t\t\t// biome-ignore lint/security/noDangerouslySetInnerHtml: adapter-authored vendor init\n\t\t\t\t\t\t\t\tdangerouslySetInnerHTML={{ __html: script.inline }}\n\t\t\t\t\t\t\t\tnonce={nonce}\n\t\t\t\t\t\t\t\ttype={script.type}\n\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t) : (\n\t\t\t\t\t\t\t<script\n\t\t\t\t\t\t\t\tkey={key}\n\t\t\t\t\t\t\t\t{...script.attrs}\n\t\t\t\t\t\t\t\tasync={script.async}\n\t\t\t\t\t\t\t\tdefer={script.defer}\n\t\t\t\t\t\t\t\tnonce={nonce}\n\t\t\t\t\t\t\t\tsrc={script.src}\n\t\t\t\t\t\t\t\ttype={script.type}\n\t\t\t\t\t\t\t/>\n\t\t\t\t\t\t)\n\t\t\t\t\t})\n\t\t)}\n\t\t<TrackerBoot config={config} nonce={nonce} />\n\t</>\n)\n"],"mappings":";;;;;;;;;;;;;;;;AAwBA,MAAa,oBAAoB,EAAE,QAAQ,YAC1C,qBAAA,UAAA,EAAA,UAAA,CACE,OAAO,MAAM,SAAS,SACtB,KAAK,kBACF,CAAC,IACD,KAAK,QAAQ,QAAQ,KAAK,QAAQ,UAAU;CAC5C,MAAM,MAAM,GAAG,KAAK,KAAK,GAAG;CAC5B,OAAO,OAAO,SACb,oBAAC,UAAD;EAEC,GAAI,OAAO;EACX,OAAO,OAAO;EACd,OAAO,OAAO;EAEd,yBAAyB,EAAE,QAAQ,OAAO,OAAO;EAC1C;EACP,MAAM,OAAO;CACb,GARK,GAQL,IAED,oBAAC,UAAD;EAEC,GAAI,OAAO;EACX,OAAO,OAAO;EACd,OAAO,OAAO;EACP;EACP,KAAK,OAAO;EACZ,MAAM,OAAO;CACb,GAPK,GAOL;AAEH,CAAC,CACJ,GACA,oBAAC,aAAD;CAAqB;CAAe;AAAQ,CAAA,CAC3C,EAAA,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -1,20 +1,36 @@
|
|
|
1
1
|
//#region src/surfacing/cacheStore.ts
|
|
2
|
+
/** How long past expiry a value stays eligible for stale-while-error serving. */
|
|
3
|
+
const STALE_WINDOW_SECONDS = 86400;
|
|
2
4
|
function kvCacheStore(kv) {
|
|
3
5
|
const store = {
|
|
4
6
|
now: () => Date.now(),
|
|
5
7
|
async get(key) {
|
|
6
8
|
const hit = await kv.get(key);
|
|
7
9
|
if (!hit) return null;
|
|
8
|
-
if (hit.expiresAt <= store.now()) {
|
|
10
|
+
if ((hit.staleUntil ?? hit.expiresAt) <= store.now()) {
|
|
9
11
|
await kv.delete(key);
|
|
10
12
|
return null;
|
|
11
13
|
}
|
|
14
|
+
if (hit.expiresAt <= store.now()) return null;
|
|
15
|
+
return hit.value;
|
|
16
|
+
},
|
|
17
|
+
async getStale(key) {
|
|
18
|
+
const hit = await kv.get(key);
|
|
19
|
+
if (!hit) return null;
|
|
20
|
+
const now = store.now();
|
|
21
|
+
if ((hit.staleUntil ?? hit.expiresAt) <= now) {
|
|
22
|
+
await kv.delete(key);
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
if (hit.expiresAt > now) return null;
|
|
12
26
|
return hit.value;
|
|
13
27
|
},
|
|
14
28
|
async set(key, value, ttlSeconds) {
|
|
29
|
+
const expiresAt = store.now() + ttlSeconds * 1e3;
|
|
15
30
|
await kv.set(key, {
|
|
16
31
|
value,
|
|
17
|
-
expiresAt
|
|
32
|
+
expiresAt,
|
|
33
|
+
staleUntil: expiresAt + STALE_WINDOW_SECONDS * 1e3
|
|
18
34
|
});
|
|
19
35
|
}
|
|
20
36
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cacheStore.js","names":[],"sources":["../../src/surfacing/cacheStore.ts"],"sourcesContent":["import type { KVAdapter } from 'payload'\n\nexport interface CacheStore {\n\tget<T>(key: string): Promise<T | null>\n\tset<T>(key: string, value: T, ttlSeconds: number): Promise<void>\n\t// Injected for testing; defaults to Date.now\n\tnow: () => number\n}\n\ninterface Wrapped<T> {\n\tvalue: T\n\texpiresAt: number\n}\n\nexport function kvCacheStore(kv: KVAdapter): CacheStore {\n\tconst store: CacheStore = {\n\t\tnow: () => Date.now(),\n\t\tasync get<T>(key: string): Promise<T | null> {\n\t\t\tconst hit = await kv.get<Wrapped<T>>(key)\n\t\t\tif (!hit) return null\n\t\t\tif (hit.expiresAt <= store.now()) {\n\t\t\t\tawait kv.delete(key)\n\t\t\t\treturn null\n\t\t\t}\n\t\t\treturn hit.value\n\t\t},\n\t\tasync set<T>(key: string, value: T, ttlSeconds: number): Promise<void> {\n\t\t\tawait kv.set(key, {
|
|
1
|
+
{"version":3,"file":"cacheStore.js","names":[],"sources":["../../src/surfacing/cacheStore.ts"],"sourcesContent":["import type { KVAdapter } from 'payload'\n\nexport interface CacheStore {\n\tget<T>(key: string): Promise<T | null>\n\t/** Serves a value past `expiresAt` but still within the stale window; null once cold or past it. */\n\tgetStale<T>(key: string): Promise<T | null>\n\tset<T>(key: string, value: T, ttlSeconds: number): Promise<void>\n\t// Injected for testing; defaults to Date.now\n\tnow: () => number\n}\n\n/** How long past expiry a value stays eligible for stale-while-error serving. */\nexport const STALE_WINDOW_SECONDS = 86_400\n\ninterface Wrapped<T> {\n\tvalue: T\n\texpiresAt: number\n\t// Absent on entries written before stale-while-error shipped; treated as expiresAt (never stale-served).\n\tstaleUntil?: number\n}\n\nexport function kvCacheStore(kv: KVAdapter): CacheStore {\n\tconst store: CacheStore = {\n\t\tnow: () => Date.now(),\n\t\tasync get<T>(key: string): Promise<T | null> {\n\t\t\tconst hit = await kv.get<Wrapped<T>>(key)\n\t\t\tif (!hit) return null\n\t\t\tconst staleUntil = hit.staleUntil ?? hit.expiresAt\n\t\t\tif (staleUntil <= store.now()) {\n\t\t\t\tawait kv.delete(key)\n\t\t\t\treturn null\n\t\t\t}\n\t\t\tif (hit.expiresAt <= store.now()) return null\n\t\t\treturn hit.value\n\t\t},\n\t\tasync getStale<T>(key: string): Promise<T | null> {\n\t\t\tconst hit = await kv.get<Wrapped<T>>(key)\n\t\t\tif (!hit) return null\n\t\t\tconst now = store.now()\n\t\t\tconst staleUntil = hit.staleUntil ?? hit.expiresAt\n\t\t\tif (staleUntil <= now) {\n\t\t\t\tawait kv.delete(key)\n\t\t\t\treturn null\n\t\t\t}\n\t\t\tif (hit.expiresAt > now) return null\n\t\t\treturn hit.value\n\t\t},\n\t\tasync set<T>(key: string, value: T, ttlSeconds: number): Promise<void> {\n\t\t\tconst expiresAt = store.now() + ttlSeconds * 1000\n\t\t\tawait kv.set(key, {\n\t\t\t\tvalue,\n\t\t\t\texpiresAt,\n\t\t\t\tstaleUntil: expiresAt + STALE_WINDOW_SECONDS * 1000,\n\t\t\t} satisfies Wrapped<T>)\n\t\t},\n\t}\n\treturn store\n}\n"],"mappings":";;AAYA,MAAa,uBAAuB;AASpC,SAAgB,aAAa,IAA2B;CACvD,MAAM,QAAoB;EACzB,WAAW,KAAK,IAAI;EACpB,MAAM,IAAO,KAAgC;GAC5C,MAAM,MAAM,MAAM,GAAG,IAAgB,GAAG;GACxC,IAAI,CAAC,KAAK,OAAO;GAEjB,KADmB,IAAI,cAAc,IAAI,cACvB,MAAM,IAAI,GAAG;IAC9B,MAAM,GAAG,OAAO,GAAG;IACnB,OAAO;GACR;GACA,IAAI,IAAI,aAAa,MAAM,IAAI,GAAG,OAAO;GACzC,OAAO,IAAI;EACZ;EACA,MAAM,SAAY,KAAgC;GACjD,MAAM,MAAM,MAAM,GAAG,IAAgB,GAAG;GACxC,IAAI,CAAC,KAAK,OAAO;GACjB,MAAM,MAAM,MAAM,IAAI;GAEtB,KADmB,IAAI,cAAc,IAAI,cACvB,KAAK;IACtB,MAAM,GAAG,OAAO,GAAG;IACnB,OAAO;GACR;GACA,IAAI,IAAI,YAAY,KAAK,OAAO;GAChC,OAAO,IAAI;EACZ;EACA,MAAM,IAAO,KAAa,OAAU,YAAmC;GACtE,MAAM,YAAY,MAAM,IAAI,IAAI,aAAa;GAC7C,MAAM,GAAG,IAAI,KAAK;IACjB;IACA;IACA,YAAY,YAAY,uBAAuB;GAChD,CAAsB;EACvB;CACD;CACA,OAAO;AACR"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|