@10x-media/analytics 1.0.0-beta.6 → 1.0.0-beta.8
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 +73 -0
- package/README.md +13 -3
- package/dist/adapters/ga4/ga4.js +3 -2
- 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 +42 -1
- package/dist/adapters/plausible/plausible.js.map +1 -1
- package/dist/adapters/posthog/posthog.d.ts +10 -1
- package/dist/adapters/posthog/posthog.js +47 -0
- package/dist/adapters/posthog/posthog.js.map +1 -1
- package/dist/adapters/umami/umami.d.ts +5 -1
- package/dist/adapters/umami/umami.js +26 -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 +54 -0
- package/dist/capture/slots.js.map +1 -0
- package/dist/capture/trackerConfig.d.ts +33 -0
- package/dist/capture/trackerConfig.js +95 -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/charts/BarList.d.ts +15 -1
- package/dist/charts/BarList.js +34 -18
- package/dist/charts/BarList.js.map +1 -1
- package/dist/charts/TrendChart.d.ts +16 -3
- package/dist/charts/TrendChart.js +172 -106
- package/dist/charts/TrendChart.js.map +1 -1
- package/dist/charts/bars.d.ts +2 -0
- package/dist/charts/bars.js +2 -1
- package/dist/charts/bars.js.map +1 -1
- package/dist/charts/bucket.js +10 -1
- package/dist/charts/bucket.js.map +1 -1
- package/dist/charts/spline.js +2 -2
- package/dist/charts/spline.js.map +1 -1
- package/dist/charts/styles.js +18 -1
- package/dist/charts/styles.js.map +1 -1
- package/dist/core/cacheKey.js +13 -1
- package/dist/core/cacheKey.js.map +1 -1
- package/dist/core/capture.d.ts +49 -0
- package/dist/core/contract.d.ts +35 -4
- package/dist/core/contract.js +29 -1
- package/dist/core/contract.js.map +1 -1
- package/dist/core/granularity.js +1 -1
- package/dist/core/options.d.ts +148 -4
- package/dist/core/options.js +121 -3
- 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 +45 -28
- package/dist/core/scopedRead.js.map +1 -1
- package/dist/core/serverEvent.d.ts +42 -0
- package/dist/core/serverEvent.js +12 -0
- package/dist/core/serverEvent.js.map +1 -0
- package/dist/exports/client.d.ts +8 -2
- package/dist/exports/client.js +6 -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 +8 -1
- package/dist/exports/rsc.js +7 -2
- package/dist/exports/tracker.d.ts +18 -0
- package/dist/exports/tracker.js +12 -0
- package/dist/exports/types.d.ts +14 -3
- package/dist/exports/types.js +2 -2
- package/dist/fields/AnalyticsPanelClient.js +2 -2
- package/dist/fields/AnalyticsPanelField.js +1 -1
- package/dist/fields/AnalyticsStatField.js +2 -2
- package/dist/fields/config/MetricSelectField.js +2 -2
- package/dist/fields/config/MetricSelectField.js.map +1 -1
- package/dist/fields/config/SourceSelectField.js +2 -2
- package/dist/fields/config/SourceSelectField.js.map +1 -1
- package/dist/fields/config/fetchSources.d.ts +5 -1
- package/dist/fields/config/narrow.js +2 -0
- package/dist/fields/config/narrow.js.map +1 -1
- package/dist/fields/readForDocument.d.ts +5 -0
- package/dist/fields/readForDocument.js +6 -6
- package/dist/fields/readForDocument.js.map +1 -1
- package/dist/goals/GoalSelectField.d.ts +7 -0
- package/dist/goals/GoalSelectField.js +77 -0
- package/dist/goals/GoalSelectField.js.map +1 -0
- package/dist/goals/collection.js +250 -0
- package/dist/goals/collection.js.map +1 -0
- package/dist/goals/currency.js +14 -0
- package/dist/goals/currency.js.map +1 -0
- package/dist/goals/fetchGoals.d.ts +18 -0
- package/dist/goals/fetchGoals.js +29 -0
- package/dist/goals/fetchGoals.js.map +1 -0
- package/dist/goals/goalField.d.ts +31 -0
- package/dist/goals/goalField.js +56 -0
- package/dist/goals/goalField.js.map +1 -0
- package/dist/goals/goalsEndpoint.d.ts +1 -0
- package/dist/goals/goalsEndpoint.js +55 -0
- package/dist/goals/goalsEndpoint.js.map +1 -0
- package/dist/goals/match.js +64 -0
- package/dist/goals/match.js.map +1 -0
- package/dist/goals/resolver.d.ts +8 -0
- package/dist/goals/resolver.js +147 -0
- package/dist/goals/resolver.js.map +1 -0
- package/dist/goals/trackGoalAction.d.ts +74 -0
- package/dist/goals/trackGoalAction.js +139 -0
- package/dist/goals/trackGoalAction.js.map +1 -0
- package/dist/goals/types.d.ts +33 -0
- package/dist/goals/types.js +7 -0
- package/dist/goals/types.js.map +1 -0
- package/dist/goals/useAnalyticsGoals.d.ts +20 -0
- package/dist/goals/useAnalyticsGoals.js +67 -0
- package/dist/goals/useAnalyticsGoals.js.map +1 -0
- package/dist/index.d.ts +11 -3
- package/dist/index.js +104 -17
- 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 +24 -0
- 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/device.js +11 -3
- package/dist/native/ingest/device.js.map +1 -1
- package/dist/native/ingest/endpoint.d.ts +1 -0
- package/dist/native/ingest/endpoint.js +25 -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.d.ts +20 -0
- package/dist/native/ingest/normalizeEvent.js +78 -10
- package/dist/native/ingest/normalizeEvent.js.map +1 -1
- package/dist/native/ingest/serverTrack.d.ts +20 -0
- package/dist/native/ingest/serverTrack.js +99 -0
- package/dist/native/ingest/serverTrack.js.map +1 -0
- package/dist/native/ingest/validate.js +27 -0
- package/dist/native/ingest/validate.js.map +1 -0
- package/dist/native/nativeAdapter.d.ts +1 -1
- package/dist/native/nativeAdapter.js +38 -11
- package/dist/native/nativeAdapter.js.map +1 -1
- package/dist/native/query/eventAgg.js +39 -13
- package/dist/native/query/eventAgg.js.map +1 -1
- package/dist/native/retention/pruneTask.js +1 -1
- package/dist/native/rollupAcc.js +12 -1
- package/dist/native/rollupAcc.js.map +1 -1
- package/dist/native/rollups/bumpRollup.js +5 -1
- package/dist/native/rollups/bumpRollup.js.map +1 -1
- package/dist/native/rollups/deltas.js +28 -6
- package/dist/native/rollups/deltas.js.map +1 -1
- package/dist/native/rollups/insertIfNew.js +36 -2
- package/dist/native/rollups/insertIfNew.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 +28 -10
- package/dist/plugin/documentEndpoint.js.map +1 -1
- package/dist/plugin/paths.js +15 -1
- package/dist/plugin/paths.js.map +1 -1
- package/dist/plugin/queryEndpoint.js +113 -0
- package/dist/plugin/queryEndpoint.js.map +1 -0
- package/dist/plugin/readContextForRequest.js +71 -0
- package/dist/plugin/readContextForRequest.js.map +1 -0
- package/dist/plugin/realtimeEndpoint.js +6 -3
- package/dist/plugin/realtimeEndpoint.js.map +1 -1
- package/dist/plugin/runtime.d.ts +1 -0
- package/dist/plugin/runtime.js +31 -1
- package/dist/plugin/runtime.js.map +1 -1
- package/dist/plugin/scopeFieldBoot.js +22 -0
- package/dist/plugin/scopeFieldBoot.js.map +1 -0
- package/dist/plugin/sourcesEndpoint.js +13 -39
- 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 +71 -25
- 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/providers/resolver.js.map +1 -1
- package/dist/query/dates.js +64 -0
- package/dist/query/dates.js.map +1 -0
- package/dist/query/errors.d.ts +17 -0
- package/dist/query/errors.js +10 -0
- package/dist/query/errors.js.map +1 -0
- package/dist/query/fetchQuery.d.ts +59 -0
- package/dist/query/fetchQuery.js +110 -0
- package/dist/query/fetchQuery.js.map +1 -0
- package/dist/query/parse.d.ts +22 -0
- package/dist/query/parse.js +168 -0
- package/dist/query/parse.js.map +1 -0
- package/dist/query/response.d.ts +36 -0
- 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/syncTask.js +60 -33
- package/dist/sync/syncTask.js.map +1 -1
- package/dist/testing/memoryAdapter.d.ts +2 -0
- package/dist/testing/memoryAdapter.js +10 -0
- package/dist/testing/memoryAdapter.js.map +1 -1
- package/dist/timeframe/tz.js +17 -6
- package/dist/timeframe/tz.js.map +1 -1
- package/dist/tracker/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 +104 -1
- package/dist/translations/ar.js.map +1 -1
- package/dist/translations/de.js +104 -1
- package/dist/translations/de.js.map +1 -1
- package/dist/translations/en.js +104 -1
- package/dist/translations/en.js.map +1 -1
- package/dist/translations/es.js +104 -1
- package/dist/translations/es.js.map +1 -1
- package/dist/translations/fr.js +104 -1
- package/dist/translations/fr.js.map +1 -1
- package/dist/translations/id.js +104 -1
- package/dist/translations/id.js.map +1 -1
- package/dist/translations/keys.d.ts +103 -0
- package/dist/translations/keys.js +104 -1
- package/dist/translations/keys.js.map +1 -1
- package/dist/translations/ko.js +104 -1
- package/dist/translations/ko.js.map +1 -1
- package/dist/translations/metricKeys.js +1 -1
- package/dist/translations/metricKeys.js.map +1 -1
- package/dist/translations/pt.js +104 -1
- package/dist/translations/pt.js.map +1 -1
- package/dist/translations/ru.js +104 -1
- package/dist/translations/ru.js.map +1 -1
- package/dist/translations/uk.js +104 -1
- package/dist/translations/uk.js.map +1 -1
- package/dist/translations/zh.js +104 -1
- package/dist/translations/zh.js.map +1 -1
- package/dist/view/AnalyticsNavLink.d.ts +19 -0
- package/dist/view/AnalyticsNavLink.js +40 -0
- package/dist/view/AnalyticsNavLink.js.map +1 -0
- package/dist/view/AnalyticsView.d.ts +23 -0
- package/dist/view/AnalyticsView.js +45 -0
- package/dist/view/AnalyticsView.js.map +1 -0
- package/dist/view/AnalyticsViewClient.d.ts +15 -0
- package/dist/view/AnalyticsViewClient.js +189 -0
- package/dist/view/AnalyticsViewClient.js.map +1 -0
- package/dist/view/authRedirect.js +32 -0
- package/dist/view/authRedirect.js.map +1 -0
- package/dist/view/conversionRate.js +18 -0
- package/dist/view/conversionRate.js.map +1 -0
- package/dist/view/dayRange.js +68 -0
- package/dist/view/dayRange.js.map +1 -0
- package/dist/view/gating.d.ts +1 -0
- package/dist/view/gating.js +105 -0
- package/dist/view/gating.js.map +1 -0
- package/dist/view/labels.js +44 -0
- package/dist/view/labels.js.map +1 -0
- package/dist/view/registerView.js +48 -0
- package/dist/view/registerView.js.map +1 -0
- package/dist/view/sections/Breakdowns.js +157 -0
- package/dist/view/sections/Breakdowns.js.map +1 -0
- package/dist/view/sections/EmptyStates.js +92 -0
- package/dist/view/sections/EmptyStates.js.map +1 -0
- package/dist/view/sections/FilterChips.js +41 -0
- package/dist/view/sections/FilterChips.js.map +1 -0
- package/dist/view/sections/GoalsPanel.js +81 -0
- package/dist/view/sections/GoalsPanel.js.map +1 -0
- package/dist/view/sections/OverviewCards.js +65 -0
- package/dist/view/sections/OverviewCards.js.map +1 -0
- package/dist/view/sections/RealtimeStrip.js +77 -0
- package/dist/view/sections/RealtimeStrip.js.map +1 -0
- package/dist/view/sections/Toolbar.js +179 -0
- package/dist/view/sections/Toolbar.js.map +1 -0
- package/dist/view/sections/Trend.js +119 -0
- package/dist/view/sections/Trend.js.map +1 -0
- package/dist/view/sections/styles.js +74 -0
- package/dist/view/sections/styles.js.map +1 -0
- package/dist/view/state.js +206 -0
- package/dist/view/state.js.map +1 -0
- package/dist/view/useViewQueries.js +150 -0
- package/dist/view/useViewQueries.js.map +1 -0
- package/dist/view/viewProps.d.ts +40 -0
- package/dist/view/viewProps.js +90 -0
- package/dist/view/viewProps.js.map +1 -0
- package/dist/widgets/AnalyticsBreakdownWidget.d.ts +2 -1
- package/dist/widgets/AnalyticsBreakdownWidget.js +22 -4
- package/dist/widgets/AnalyticsBreakdownWidget.js.map +1 -1
- package/dist/widgets/AnalyticsGoalsWidget.d.ts +14 -0
- package/dist/widgets/AnalyticsGoalsWidget.js +188 -0
- package/dist/widgets/AnalyticsGoalsWidget.js.map +1 -0
- package/dist/widgets/AnalyticsMetricWidget.d.ts +2 -1
- package/dist/widgets/AnalyticsMetricWidget.js +35 -12
- package/dist/widgets/AnalyticsMetricWidget.js.map +1 -1
- package/dist/widgets/AnalyticsRealtimeWidget.d.ts +2 -1
- package/dist/widgets/AnalyticsRealtimeWidget.js +42 -22
- package/dist/widgets/AnalyticsRealtimeWidget.js.map +1 -1
- package/dist/widgets/AnalyticsTrendWidget.d.ts +2 -1
- package/dist/widgets/AnalyticsTrendWidget.js +51 -15
- package/dist/widgets/AnalyticsTrendWidget.js.map +1 -1
- package/dist/widgets/WidgetViewLink.js +17 -0
- package/dist/widgets/WidgetViewLink.js.map +1 -0
- package/dist/widgets/breakdownTypes.js +32 -0
- package/dist/widgets/breakdownTypes.js.map +1 -1
- package/dist/widgets/cardChrome.js +31 -7
- package/dist/widgets/cardChrome.js.map +1 -1
- package/dist/widgets/range.js +43 -12
- package/dist/widgets/range.js.map +1 -1
- package/dist/widgets/readForWidget.d.ts +12 -0
- package/dist/widgets/readForWidget.js +30 -19
- package/dist/widgets/readForWidget.js.map +1 -1
- package/dist/widgets/readForWidgetBreakdown.d.ts +15 -0
- package/dist/widgets/readForWidgetBreakdown.js +32 -20
- package/dist/widgets/readForWidgetBreakdown.js.map +1 -1
- package/dist/widgets/readForWidgetGoals.d.ts +57 -0
- package/dist/widgets/readForWidgetGoals.js +126 -0
- package/dist/widgets/readForWidgetGoals.js.map +1 -0
- package/dist/widgets/readForWidgetRealtime.js +1 -1
- package/dist/widgets/readForWidgetSeries.d.ts +13 -0
- package/dist/widgets/readForWidgetSeries.js +56 -28
- package/dist/widgets/readForWidgetSeries.js.map +1 -1
- package/dist/widgets/registerWidgets.js +58 -9
- package/dist/widgets/registerWidgets.js.map +1 -1
- package/dist/widgets/types.js +16 -2
- package/dist/widgets/types.js.map +1 -1
- package/dist/widgets/viewLink.d.ts +22 -0
- package/dist/widgets/viewLink.js +83 -0
- package/dist/widgets/viewLink.js.map +1 -0
- package/package.json +25 -5
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { LoadScript, TrackerWindow } from "./types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/tracker/loadScript.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* The default `loadScript` seam: injects a snippet script into `<head>`, resolving once it
|
|
6
|
+
* has run. A script the page already carries is a no-op, which is what makes the RSC path
|
|
7
|
+
* safe: `AnalyticsScripts` renders the same snippets server-side, and the sink booting over
|
|
8
|
+
* them must not load a second copy. Inline scripts resolve as soon as they are appended,
|
|
9
|
+
* since appending runs them.
|
|
10
|
+
*/
|
|
11
|
+
declare const createScriptLoader: (win: TrackerWindow, nonce?: string) => LoadScript;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { createScriptLoader };
|
|
14
|
+
//# sourceMappingURL=loadScript.d.ts.map
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
//#region src/tracker/loadScript.ts
|
|
2
|
+
const alreadyPresent = (win, script) => {
|
|
3
|
+
for (const el of win.document.querySelectorAll("script")) {
|
|
4
|
+
if (script.src && el.getAttribute("src") === script.src) return true;
|
|
5
|
+
if (script.inline && el.textContent === script.inline) return true;
|
|
6
|
+
}
|
|
7
|
+
return false;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* The default `loadScript` seam: injects a snippet script into `<head>`, resolving once it
|
|
11
|
+
* has run. A script the page already carries is a no-op, which is what makes the RSC path
|
|
12
|
+
* safe: `AnalyticsScripts` renders the same snippets server-side, and the sink booting over
|
|
13
|
+
* them must not load a second copy. Inline scripts resolve as soon as they are appended,
|
|
14
|
+
* since appending runs them.
|
|
15
|
+
*/
|
|
16
|
+
const createScriptLoader = (win, nonce) => (script) => new Promise((resolve, reject) => {
|
|
17
|
+
if (!script.src && !script.inline || alreadyPresent(win, script)) {
|
|
18
|
+
resolve();
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const el = win.document.createElement("script");
|
|
22
|
+
for (const [name, value] of Object.entries(script.attrs ?? {})) el.setAttribute(name, value);
|
|
23
|
+
if (script.type) el.type = script.type;
|
|
24
|
+
if (nonce) el.setAttribute("nonce", nonce);
|
|
25
|
+
if (script.inline) {
|
|
26
|
+
el.text = script.inline;
|
|
27
|
+
win.document.head.appendChild(el);
|
|
28
|
+
resolve();
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
el.async = script.async ?? false;
|
|
32
|
+
el.defer = script.defer ?? false;
|
|
33
|
+
el.addEventListener("load", () => resolve());
|
|
34
|
+
el.addEventListener("error", () => reject(/* @__PURE__ */ new Error(`analytics: failed to load ${script.src}`)));
|
|
35
|
+
el.src = script.src;
|
|
36
|
+
win.document.head.appendChild(el);
|
|
37
|
+
});
|
|
38
|
+
//#endregion
|
|
39
|
+
export { createScriptLoader };
|
|
40
|
+
|
|
41
|
+
//# sourceMappingURL=loadScript.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loadScript.js","names":[],"sources":["../../src/tracker/loadScript.ts"],"sourcesContent":["import type { SnippetScript } from '../core/capture'\nimport type { LoadScript, TrackerWindow } from './types'\n\nconst alreadyPresent = (win: TrackerWindow, script: SnippetScript): boolean => {\n\tfor (const el of win.document.querySelectorAll('script')) {\n\t\tif (script.src && el.getAttribute('src') === script.src) {\n\t\t\treturn true\n\t\t}\n\t\tif (script.inline && el.textContent === script.inline) {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n\n/**\n * The default `loadScript` seam: injects a snippet script into `<head>`, resolving once it\n * has run. A script the page already carries is a no-op, which is what makes the RSC path\n * safe: `AnalyticsScripts` renders the same snippets server-side, and the sink booting over\n * them must not load a second copy. Inline scripts resolve as soon as they are appended,\n * since appending runs them.\n */\nexport const createScriptLoader =\n\t(win: TrackerWindow, nonce?: string): LoadScript =>\n\t(script) =>\n\t\tnew Promise((resolve, reject) => {\n\t\t\tif ((!script.src && !script.inline) || alreadyPresent(win, script)) {\n\t\t\t\tresolve()\n\t\t\t\treturn\n\t\t\t}\n\t\t\tconst el = win.document.createElement('script')\n\t\t\tfor (const [name, value] of Object.entries(script.attrs ?? {})) {\n\t\t\t\tel.setAttribute(name, value)\n\t\t\t}\n\t\t\tif (script.type) {\n\t\t\t\tel.type = script.type\n\t\t\t}\n\t\t\tif (nonce) {\n\t\t\t\tel.setAttribute('nonce', nonce)\n\t\t\t}\n\t\t\tif (script.inline) {\n\t\t\t\tel.text = script.inline\n\t\t\t\twin.document.head.appendChild(el)\n\t\t\t\tresolve()\n\t\t\t\treturn\n\t\t\t}\n\t\t\tel.async = script.async ?? false\n\t\t\tel.defer = script.defer ?? false\n\t\t\tel.addEventListener('load', () => resolve())\n\t\t\tel.addEventListener('error', () =>\n\t\t\t\treject(new Error(`analytics: failed to load ${script.src}`))\n\t\t\t)\n\t\t\tel.src = script.src as string\n\t\t\twin.document.head.appendChild(el)\n\t\t})\n"],"mappings":";AAGA,MAAM,kBAAkB,KAAoB,WAAmC;CAC9E,KAAK,MAAM,MAAM,IAAI,SAAS,iBAAiB,QAAQ,GAAG;EACzD,IAAI,OAAO,OAAO,GAAG,aAAa,KAAK,MAAM,OAAO,KACnD,OAAO;EAER,IAAI,OAAO,UAAU,GAAG,gBAAgB,OAAO,QAC9C,OAAO;CAET;CACA,OAAO;AACR;;;;;;;;AASA,MAAa,sBACX,KAAoB,WACpB,WACA,IAAI,SAAS,SAAS,WAAW;CAChC,IAAK,CAAC,OAAO,OAAO,CAAC,OAAO,UAAW,eAAe,KAAK,MAAM,GAAG;EACnE,QAAQ;EACR;CACD;CACA,MAAM,KAAK,IAAI,SAAS,cAAc,QAAQ;CAC9C,KAAK,MAAM,CAAC,MAAM,UAAU,OAAO,QAAQ,OAAO,SAAS,CAAC,CAAC,GAC5D,GAAG,aAAa,MAAM,KAAK;CAE5B,IAAI,OAAO,MACV,GAAG,OAAO,OAAO;CAElB,IAAI,OACH,GAAG,aAAa,SAAS,KAAK;CAE/B,IAAI,OAAO,QAAQ;EAClB,GAAG,OAAO,OAAO;EACjB,IAAI,SAAS,KAAK,YAAY,EAAE;EAChC,QAAQ;EACR;CACD;CACA,GAAG,QAAQ,OAAO,SAAS;CAC3B,GAAG,QAAQ,OAAO,SAAS;CAC3B,GAAG,iBAAiB,cAAc,QAAQ,CAAC;CAC3C,GAAG,iBAAiB,eACnB,uBAAO,IAAI,MAAM,6BAA6B,OAAO,KAAK,CAAC,CAC5D;CACA,GAAG,MAAM,OAAO;CAChB,IAAI,SAAS,KAAK,YAAY,EAAE;AACjC,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { TrackerWindow } from "./types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/tracker/pageTracking.d.ts
|
|
4
|
+
interface PageTracking {
|
|
5
|
+
destroy(): void;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Turns SPA navigations into page changes. `history.pushState`/`replaceState` fire no event
|
|
9
|
+
* of their own, so both are wrapped; `popstate` covers back and forward. Only a changed
|
|
10
|
+
* `location.pathname` counts, so a query or hash rewrite (a filter panel, an anchor link)
|
|
11
|
+
* does not report a second pageview. The originals are restored on destroy, which keeps a
|
|
12
|
+
* hot-reloaded or unmounted app from stacking wrappers.
|
|
13
|
+
*/
|
|
14
|
+
declare const createPageTracking: (win: TrackerWindow, onChange: () => void) => PageTracking;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { PageTracking, createPageTracking };
|
|
17
|
+
//# sourceMappingURL=pageTracking.d.ts.map
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
//#region src/tracker/pageTracking.ts
|
|
2
|
+
/**
|
|
3
|
+
* Turns SPA navigations into page changes. `history.pushState`/`replaceState` fire no event
|
|
4
|
+
* of their own, so both are wrapped; `popstate` covers back and forward. Only a changed
|
|
5
|
+
* `location.pathname` counts, so a query or hash rewrite (a filter panel, an anchor link)
|
|
6
|
+
* does not report a second pageview. The originals are restored on destroy, which keeps a
|
|
7
|
+
* hot-reloaded or unmounted app from stacking wrappers.
|
|
8
|
+
*/
|
|
9
|
+
const createPageTracking = (win, onChange) => {
|
|
10
|
+
const history = win.history;
|
|
11
|
+
const pushState = history.pushState;
|
|
12
|
+
const replaceState = history.replaceState;
|
|
13
|
+
let lastPath = win.location.pathname;
|
|
14
|
+
const check = () => {
|
|
15
|
+
const path = win.location.pathname;
|
|
16
|
+
if (path === lastPath) return;
|
|
17
|
+
lastPath = path;
|
|
18
|
+
onChange();
|
|
19
|
+
};
|
|
20
|
+
const wrap = (original) => (...args) => {
|
|
21
|
+
original.apply(history, args);
|
|
22
|
+
check();
|
|
23
|
+
};
|
|
24
|
+
const patchedPush = wrap(pushState);
|
|
25
|
+
const patchedReplace = wrap(replaceState);
|
|
26
|
+
history.pushState = patchedPush;
|
|
27
|
+
history.replaceState = patchedReplace;
|
|
28
|
+
win.addEventListener("popstate", check);
|
|
29
|
+
return { destroy() {
|
|
30
|
+
if (history.pushState === patchedPush) history.pushState = pushState;
|
|
31
|
+
if (history.replaceState === patchedReplace) history.replaceState = replaceState;
|
|
32
|
+
win.removeEventListener("popstate", check);
|
|
33
|
+
} };
|
|
34
|
+
};
|
|
35
|
+
//#endregion
|
|
36
|
+
export { createPageTracking };
|
|
37
|
+
|
|
38
|
+
//# sourceMappingURL=pageTracking.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pageTracking.js","names":[],"sources":["../../src/tracker/pageTracking.ts"],"sourcesContent":["import type { TrackerWindow } from './types'\n\nexport interface PageTracking {\n\tdestroy(): void\n}\n\n/**\n * Turns SPA navigations into page changes. `history.pushState`/`replaceState` fire no event\n * of their own, so both are wrapped; `popstate` covers back and forward. Only a changed\n * `location.pathname` counts, so a query or hash rewrite (a filter panel, an anchor link)\n * does not report a second pageview. The originals are restored on destroy, which keeps a\n * hot-reloaded or unmounted app from stacking wrappers.\n */\nexport const createPageTracking = (win: TrackerWindow, onChange: () => void): PageTracking => {\n\tconst history = win.history\n\tconst pushState = history.pushState\n\tconst replaceState = history.replaceState\n\tlet lastPath = win.location.pathname\n\n\tconst check = () => {\n\t\tconst path = win.location.pathname\n\t\tif (path === lastPath) {\n\t\t\treturn\n\t\t}\n\t\tlastPath = path\n\t\tonChange()\n\t}\n\n\tconst wrap =\n\t\t(original: typeof history.pushState) =>\n\t\t(...args: Parameters<typeof history.pushState>) => {\n\t\t\toriginal.apply(history, args)\n\t\t\tcheck()\n\t\t}\n\n\tconst patchedPush = wrap(pushState)\n\tconst patchedReplace = wrap(replaceState)\n\thistory.pushState = patchedPush\n\thistory.replaceState = patchedReplace\n\twin.addEventListener('popstate', check)\n\n\treturn {\n\t\tdestroy() {\n\t\t\t// Only unwind our own wrapper: another library may have patched on top of it,\n\t\t\t// and restoring the original underneath it would silently unhook that library.\n\t\t\tif (history.pushState === patchedPush) {\n\t\t\t\thistory.pushState = pushState\n\t\t\t}\n\t\t\tif (history.replaceState === patchedReplace) {\n\t\t\t\thistory.replaceState = replaceState\n\t\t\t}\n\t\t\twin.removeEventListener('popstate', check)\n\t\t},\n\t}\n}\n"],"mappings":";;;;;;;;AAaA,MAAa,sBAAsB,KAAoB,aAAuC;CAC7F,MAAM,UAAU,IAAI;CACpB,MAAM,YAAY,QAAQ;CAC1B,MAAM,eAAe,QAAQ;CAC7B,IAAI,WAAW,IAAI,SAAS;CAE5B,MAAM,cAAc;EACnB,MAAM,OAAO,IAAI,SAAS;EAC1B,IAAI,SAAS,UACZ;EAED,WAAW;EACX,SAAS;CACV;CAEA,MAAM,QACJ,cACA,GAAG,SAA+C;EAClD,SAAS,MAAM,SAAS,IAAI;EAC5B,MAAM;CACP;CAED,MAAM,cAAc,KAAK,SAAS;CAClC,MAAM,iBAAiB,KAAK,YAAY;CACxC,QAAQ,YAAY;CACpB,QAAQ,eAAe;CACvB,IAAI,iBAAiB,YAAY,KAAK;CAEtC,OAAO,EACN,UAAU;EAGT,IAAI,QAAQ,cAAc,aACzB,QAAQ,YAAY;EAErB,IAAI,QAAQ,iBAAiB,gBAC5B,QAAQ,eAAe;EAExB,IAAI,oBAAoB,YAAY,KAAK;CAC1C,EACD;AACD"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { TrackerConfig } from "../capture/trackerConfig.js";
|
|
2
|
+
import { Tracker, TrackerOptions } from "./types.js";
|
|
3
|
+
|
|
4
|
+
//#region src/tracker/registry.d.ts
|
|
5
|
+
interface TrackerLease {
|
|
6
|
+
tracker: Tracker;
|
|
7
|
+
/** Gives up this holder's claim. Safe to call more than once. */
|
|
8
|
+
release(): void;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Hands out the one tracker a window is allowed to have, reference counted.
|
|
12
|
+
*
|
|
13
|
+
* React mounts an effect, tears it down, and mounts it again on the same commit under
|
|
14
|
+
* StrictMode, so a component that created a tracker in its effect booted two of them and
|
|
15
|
+
* sent two boot pageviews. Here the second mount finds the live instance instead. The
|
|
16
|
+
* teardown is deferred to a macrotask and skipped if a new holder arrived in the meantime,
|
|
17
|
+
* which is what makes that synchronous remount free; a real unmount still destroys the
|
|
18
|
+
* tracker on the next tick.
|
|
19
|
+
*
|
|
20
|
+
* One tracker per window also means `<AnalyticsScripts />` and `<AnalyticsProvider>` can
|
|
21
|
+
* both be rendered without double-counting. The first holder's config wins for as long as
|
|
22
|
+
* the tracker lives.
|
|
23
|
+
*/
|
|
24
|
+
declare const acquireTracker: (config: TrackerConfig, options?: TrackerOptions) => TrackerLease;
|
|
25
|
+
//#endregion
|
|
26
|
+
export { TrackerLease, acquireTracker };
|
|
27
|
+
//# sourceMappingURL=registry.d.ts.map
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { createNoopTracker, createTracker } from "./createTracker.js";
|
|
2
|
+
//#region src/tracker/registry.ts
|
|
3
|
+
const entries = /* @__PURE__ */ new WeakMap();
|
|
4
|
+
/**
|
|
5
|
+
* The tracker a window already holds, or null. A peek, never a boot: a caller that has no
|
|
6
|
+
* config of its own (`useAnalytics` outside a provider) can reach the instance
|
|
7
|
+
* `<TrackerBoot />` or `<AnalyticsProvider>` acquired without taking a lease or creating a
|
|
8
|
+
* second one. An instance released this tick is still reachable until its teardown runs,
|
|
9
|
+
* and a destroyed tracker is inert rather than broken.
|
|
10
|
+
*/
|
|
11
|
+
const liveTracker = (window) => {
|
|
12
|
+
const win = window ?? globalThis.window;
|
|
13
|
+
return win ? entries.get(win)?.tracker ?? null : null;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Hands out the one tracker a window is allowed to have, reference counted.
|
|
17
|
+
*
|
|
18
|
+
* React mounts an effect, tears it down, and mounts it again on the same commit under
|
|
19
|
+
* StrictMode, so a component that created a tracker in its effect booted two of them and
|
|
20
|
+
* sent two boot pageviews. Here the second mount finds the live instance instead. The
|
|
21
|
+
* teardown is deferred to a macrotask and skipped if a new holder arrived in the meantime,
|
|
22
|
+
* which is what makes that synchronous remount free; a real unmount still destroys the
|
|
23
|
+
* tracker on the next tick.
|
|
24
|
+
*
|
|
25
|
+
* One tracker per window also means `<AnalyticsScripts />` and `<AnalyticsProvider>` can
|
|
26
|
+
* both be rendered without double-counting. The first holder's config wins for as long as
|
|
27
|
+
* the tracker lives.
|
|
28
|
+
*/
|
|
29
|
+
const acquireTracker = (config, options = {}) => {
|
|
30
|
+
const win = options.window ?? globalThis.window;
|
|
31
|
+
if (!win) return {
|
|
32
|
+
tracker: createNoopTracker(),
|
|
33
|
+
release: () => void 0
|
|
34
|
+
};
|
|
35
|
+
let entry = entries.get(win);
|
|
36
|
+
if (!entry) {
|
|
37
|
+
entry = {
|
|
38
|
+
tracker: createTracker(config, {
|
|
39
|
+
...options,
|
|
40
|
+
window: win
|
|
41
|
+
}),
|
|
42
|
+
refs: 0,
|
|
43
|
+
teardown: null
|
|
44
|
+
};
|
|
45
|
+
entries.set(win, entry);
|
|
46
|
+
}
|
|
47
|
+
if (entry.teardown !== null) {
|
|
48
|
+
win.clearTimeout(entry.teardown);
|
|
49
|
+
entry.teardown = null;
|
|
50
|
+
}
|
|
51
|
+
entry.refs += 1;
|
|
52
|
+
const held = entry;
|
|
53
|
+
let released = false;
|
|
54
|
+
return {
|
|
55
|
+
tracker: entry.tracker,
|
|
56
|
+
release() {
|
|
57
|
+
if (released) return;
|
|
58
|
+
released = true;
|
|
59
|
+
held.refs -= 1;
|
|
60
|
+
if (held.refs > 0 || held.teardown !== null) return;
|
|
61
|
+
held.teardown = win.setTimeout(() => {
|
|
62
|
+
held.teardown = null;
|
|
63
|
+
if (held.refs > 0) return;
|
|
64
|
+
held.tracker.destroy();
|
|
65
|
+
if (entries.get(win) === held) entries.delete(win);
|
|
66
|
+
}, 0);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
//#endregion
|
|
71
|
+
export { acquireTracker, liveTracker };
|
|
72
|
+
|
|
73
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.js","names":[],"sources":["../../src/tracker/registry.ts"],"sourcesContent":["import type { TrackerConfig } from '../capture/trackerConfig'\nimport { createNoopTracker, createTracker } from './createTracker'\nimport type { Tracker, TrackerOptions, TrackerWindow } from './types'\n\nexport interface TrackerLease {\n\ttracker: Tracker\n\t/** Gives up this holder's claim. Safe to call more than once. */\n\trelease(): void\n}\n\ninterface Entry {\n\ttracker: Tracker\n\trefs: number\n\tteardown: ReturnType<Window['setTimeout']> | null\n}\n\nconst entries = new WeakMap<TrackerWindow, Entry>()\n\n/**\n * The tracker a window already holds, or null. A peek, never a boot: a caller that has no\n * config of its own (`useAnalytics` outside a provider) can reach the instance\n * `<TrackerBoot />` or `<AnalyticsProvider>` acquired without taking a lease or creating a\n * second one. An instance released this tick is still reachable until its teardown runs,\n * and a destroyed tracker is inert rather than broken.\n */\nexport const liveTracker = (window?: TrackerWindow): Tracker | null => {\n\tconst win = window ?? (globalThis as { window?: TrackerWindow }).window\n\treturn win ? (entries.get(win)?.tracker ?? null) : null\n}\n\n/**\n * Hands out the one tracker a window is allowed to have, reference counted.\n *\n * React mounts an effect, tears it down, and mounts it again on the same commit under\n * StrictMode, so a component that created a tracker in its effect booted two of them and\n * sent two boot pageviews. Here the second mount finds the live instance instead. The\n * teardown is deferred to a macrotask and skipped if a new holder arrived in the meantime,\n * which is what makes that synchronous remount free; a real unmount still destroys the\n * tracker on the next tick.\n *\n * One tracker per window also means `<AnalyticsScripts />` and `<AnalyticsProvider>` can\n * both be rendered without double-counting. The first holder's config wins for as long as\n * the tracker lives.\n */\nexport const acquireTracker = (\n\tconfig: TrackerConfig,\n\toptions: TrackerOptions = {}\n): TrackerLease => {\n\tconst win = options.window ?? (globalThis as { window?: TrackerWindow }).window\n\tif (!win) {\n\t\treturn { tracker: createNoopTracker(), release: () => undefined }\n\t}\n\tlet entry = entries.get(win)\n\tif (!entry) {\n\t\tentry = {\n\t\t\ttracker: createTracker(config, { ...options, window: win }),\n\t\t\trefs: 0,\n\t\t\tteardown: null,\n\t\t}\n\t\tentries.set(win, entry)\n\t}\n\tif (entry.teardown !== null) {\n\t\twin.clearTimeout(entry.teardown)\n\t\tentry.teardown = null\n\t}\n\tentry.refs += 1\n\tconst held = entry\n\tlet released = false\n\n\treturn {\n\t\ttracker: entry.tracker,\n\t\trelease() {\n\t\t\tif (released) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\treleased = true\n\t\t\theld.refs -= 1\n\t\t\tif (held.refs > 0 || held.teardown !== null) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\theld.teardown = win.setTimeout(() => {\n\t\t\t\theld.teardown = null\n\t\t\t\tif (held.refs > 0) {\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t\theld.tracker.destroy()\n\t\t\t\tif (entries.get(win) === held) {\n\t\t\t\t\tentries.delete(win)\n\t\t\t\t}\n\t\t\t}, 0)\n\t\t},\n\t}\n}\n"],"mappings":";;AAgBA,MAAM,0BAAU,IAAI,QAA8B;;;;;;;;AASlD,MAAa,eAAe,WAA2C;CACtE,MAAM,MAAM,UAAW,WAA0C;CACjE,OAAO,MAAO,QAAQ,IAAI,GAAG,GAAG,WAAW,OAAQ;AACpD;;;;;;;;;;;;;;;AAgBA,MAAa,kBACZ,QACA,UAA0B,CAAC,MACT;CAClB,MAAM,MAAM,QAAQ,UAAW,WAA0C;CACzE,IAAI,CAAC,KACJ,OAAO;EAAE,SAAS,kBAAkB;EAAG,eAAe,KAAA;CAAU;CAEjE,IAAI,QAAQ,QAAQ,IAAI,GAAG;CAC3B,IAAI,CAAC,OAAO;EACX,QAAQ;GACP,SAAS,cAAc,QAAQ;IAAE,GAAG;IAAS,QAAQ;GAAI,CAAC;GAC1D,MAAM;GACN,UAAU;EACX;EACA,QAAQ,IAAI,KAAK,KAAK;CACvB;CACA,IAAI,MAAM,aAAa,MAAM;EAC5B,IAAI,aAAa,MAAM,QAAQ;EAC/B,MAAM,WAAW;CAClB;CACA,MAAM,QAAQ;CACd,MAAM,OAAO;CACb,IAAI,WAAW;CAEf,OAAO;EACN,SAAS,MAAM;EACf,UAAU;GACT,IAAI,UACH;GAED,WAAW;GACX,KAAK,QAAQ;GACb,IAAI,KAAK,OAAO,KAAK,KAAK,aAAa,MACtC;GAED,KAAK,WAAW,IAAI,iBAAiB;IACpC,KAAK,WAAW;IAChB,IAAI,KAAK,OAAO,GACf;IAED,KAAK,QAAQ,QAAQ;IACrB,IAAI,QAAQ,IAAI,GAAG,MAAM,MACxB,QAAQ,OAAO,GAAG;GAEpB,GAAG,CAAC;EACL;CACD;AACD"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Sink, TrackerWindow } from "../types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/tracker/sinks/native.d.ts
|
|
4
|
+
/** How long a pageview waits for its duration before it is sent anyway. */
|
|
5
|
+
declare const PAGEVIEW_BUFFER_MS = 10000;
|
|
6
|
+
/**
|
|
7
|
+
* Posts events to the plugin's own ingest endpoint. Events and goals go out immediately;
|
|
8
|
+
* a pageview is held so it can carry `durationMs` and the page's deepest scroll, and is
|
|
9
|
+
* sent on the first of: the next navigation, `flush()` (which the tracker calls on
|
|
10
|
+
* `pagehide`), or `PAGEVIEW_BUFFER_MS`. The tradeoff is bounded: a tab that dies inside
|
|
11
|
+
* the window is caught by `pagehide`, and one killed without firing it loses that single
|
|
12
|
+
* pageview rather than its duration.
|
|
13
|
+
*/
|
|
14
|
+
declare const createNativeSink: (args: {
|
|
15
|
+
slot: Sink["slot"];
|
|
16
|
+
win: TrackerWindow;
|
|
17
|
+
url: string; /** Deepest scroll on the current page, read at flush time. */
|
|
18
|
+
scrollDepth?: () => number | undefined;
|
|
19
|
+
}) => Sink;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { PAGEVIEW_BUFFER_MS, createNativeSink };
|
|
22
|
+
//# sourceMappingURL=native.d.ts.map
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { sendPayload } from "../transport.js";
|
|
2
|
+
//#region src/tracker/sinks/native.ts
|
|
3
|
+
/** How long a pageview waits for its duration before it is sent anyway. */
|
|
4
|
+
const PAGEVIEW_BUFFER_MS = 1e4;
|
|
5
|
+
/**
|
|
6
|
+
* Posts events to the plugin's own ingest endpoint. Events and goals go out immediately;
|
|
7
|
+
* a pageview is held so it can carry `durationMs` and the page's deepest scroll, and is
|
|
8
|
+
* sent on the first of: the next navigation, `flush()` (which the tracker calls on
|
|
9
|
+
* `pagehide`), or `PAGEVIEW_BUFFER_MS`. The tradeoff is bounded: a tab that dies inside
|
|
10
|
+
* the window is caught by `pagehide`, and one killed without firing it loses that single
|
|
11
|
+
* pageview rather than its duration.
|
|
12
|
+
*/
|
|
13
|
+
const createNativeSink = (args) => {
|
|
14
|
+
const { slot, win, url, scrollDepth } = args;
|
|
15
|
+
let buffered = null;
|
|
16
|
+
let timer = null;
|
|
17
|
+
const clear = () => {
|
|
18
|
+
if (timer !== null) {
|
|
19
|
+
win.clearTimeout(timer);
|
|
20
|
+
timer = null;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
const flush = () => {
|
|
24
|
+
const pending = buffered;
|
|
25
|
+
buffered = null;
|
|
26
|
+
clear();
|
|
27
|
+
if (!pending) return;
|
|
28
|
+
const depth = scrollDepth?.();
|
|
29
|
+
sendPayload(win, url, {
|
|
30
|
+
...pending.event,
|
|
31
|
+
durationMs: Math.max(0, Date.now() - pending.startedAt),
|
|
32
|
+
...depth === void 0 ? {} : { scrollDepth: depth }
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
return {
|
|
36
|
+
slot,
|
|
37
|
+
ready: () => Promise.resolve(),
|
|
38
|
+
send(event) {
|
|
39
|
+
if (event.type !== "pageview") {
|
|
40
|
+
sendPayload(win, url, event);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
flush();
|
|
44
|
+
buffered = {
|
|
45
|
+
event,
|
|
46
|
+
startedAt: Date.now()
|
|
47
|
+
};
|
|
48
|
+
timer = win.setTimeout(flush, PAGEVIEW_BUFFER_MS);
|
|
49
|
+
},
|
|
50
|
+
flush
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
//#endregion
|
|
54
|
+
export { PAGEVIEW_BUFFER_MS, createNativeSink };
|
|
55
|
+
|
|
56
|
+
//# sourceMappingURL=native.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"native.js","names":[],"sources":["../../../src/tracker/sinks/native.ts"],"sourcesContent":["import { sendPayload } from '../transport'\nimport type { Sink, TrackerEvent, TrackerWindow } from '../types'\n\n/** How long a pageview waits for its duration before it is sent anyway. */\nexport const PAGEVIEW_BUFFER_MS = 10_000\n\n/**\n * Posts events to the plugin's own ingest endpoint. Events and goals go out immediately;\n * a pageview is held so it can carry `durationMs` and the page's deepest scroll, and is\n * sent on the first of: the next navigation, `flush()` (which the tracker calls on\n * `pagehide`), or `PAGEVIEW_BUFFER_MS`. The tradeoff is bounded: a tab that dies inside\n * the window is caught by `pagehide`, and one killed without firing it loses that single\n * pageview rather than its duration.\n */\nexport const createNativeSink = (args: {\n\tslot: Sink['slot']\n\twin: TrackerWindow\n\turl: string\n\t/** Deepest scroll on the current page, read at flush time. */\n\tscrollDepth?: () => number | undefined\n}): Sink => {\n\tconst { slot, win, url, scrollDepth } = args\n\tlet buffered: { event: TrackerEvent; startedAt: number } | null = null\n\tlet timer: ReturnType<Window['setTimeout']> | null = null\n\n\tconst clear = () => {\n\t\tif (timer !== null) {\n\t\t\twin.clearTimeout(timer)\n\t\t\ttimer = null\n\t\t}\n\t}\n\n\tconst flush = () => {\n\t\tconst pending = buffered\n\t\tbuffered = null\n\t\tclear()\n\t\tif (!pending) {\n\t\t\treturn\n\t\t}\n\t\tconst depth = scrollDepth?.()\n\t\tsendPayload(win, url, {\n\t\t\t...pending.event,\n\t\t\tdurationMs: Math.max(0, Date.now() - pending.startedAt),\n\t\t\t...(depth === undefined ? {} : { scrollDepth: depth }),\n\t\t})\n\t}\n\n\treturn {\n\t\tslot,\n\t\tready: () => Promise.resolve(),\n\t\tsend(event) {\n\t\t\tif (event.type !== 'pageview') {\n\t\t\t\tsendPayload(win, url, event)\n\t\t\t\treturn\n\t\t\t}\n\t\t\tflush()\n\t\t\tbuffered = { event, startedAt: Date.now() }\n\t\t\ttimer = win.setTimeout(flush, PAGEVIEW_BUFFER_MS)\n\t\t},\n\t\tflush,\n\t}\n}\n"],"mappings":";;;AAIA,MAAa,qBAAqB;;;;;;;;;AAUlC,MAAa,oBAAoB,SAMrB;CACX,MAAM,EAAE,MAAM,KAAK,KAAK,gBAAgB;CACxC,IAAI,WAA8D;CAClE,IAAI,QAAiD;CAErD,MAAM,cAAc;EACnB,IAAI,UAAU,MAAM;GACnB,IAAI,aAAa,KAAK;GACtB,QAAQ;EACT;CACD;CAEA,MAAM,cAAc;EACnB,MAAM,UAAU;EAChB,WAAW;EACX,MAAM;EACN,IAAI,CAAC,SACJ;EAED,MAAM,QAAQ,cAAc;EAC5B,YAAY,KAAK,KAAK;GACrB,GAAG,QAAQ;GACX,YAAY,KAAK,IAAI,GAAG,KAAK,IAAI,IAAI,QAAQ,SAAS;GACtD,GAAI,UAAU,KAAA,IAAY,CAAC,IAAI,EAAE,aAAa,MAAM;EACrD,CAAC;CACF;CAEA,OAAO;EACN;EACA,aAAa,QAAQ,QAAQ;EAC7B,KAAK,OAAO;GACX,IAAI,MAAM,SAAS,YAAY;IAC9B,YAAY,KAAK,KAAK,KAAK;IAC3B;GACD;GACA,MAAM;GACN,WAAW;IAAE;IAAO,WAAW,KAAK,IAAI;GAAE;GAC1C,QAAQ,IAAI,WAAW,OAAO,kBAAkB;EACjD;EACA;CACD;AACD"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Sink } from "../types.js";
|
|
2
|
+
import { VendorSinkArgs } from "./vendor.js";
|
|
3
|
+
|
|
4
|
+
//#region src/tracker/sinks/plausible.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Forwards events and goals to the `plausible` queue function. Revenue uses Plausible's
|
|
7
|
+
* documented ecommerce shape, `{ revenue: { amount, currency } }`
|
|
8
|
+
* (https://plausible.io/docs/ecommerce-revenue-tracking). The per-site snippet publishes the
|
|
9
|
+
* `plausible.q` stub, so calls made before the tracker loads are replayed by it.
|
|
10
|
+
*/
|
|
11
|
+
declare const createPlausibleSink: (args: VendorSinkArgs) => Sink;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { createPlausibleSink };
|
|
14
|
+
//# sourceMappingURL=plausible.d.ts.map
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { createVendorSink, vendorEventName } from "./vendor.js";
|
|
2
|
+
//#region src/tracker/sinks/plausible.ts
|
|
3
|
+
/**
|
|
4
|
+
* Forwards events and goals to the `plausible` queue function. Revenue uses Plausible's
|
|
5
|
+
* documented ecommerce shape, `{ revenue: { amount, currency } }`
|
|
6
|
+
* (https://plausible.io/docs/ecommerce-revenue-tracking). The per-site snippet publishes the
|
|
7
|
+
* `plausible.q` stub, so calls made before the tracker loads are replayed by it.
|
|
8
|
+
*/
|
|
9
|
+
const createPlausibleSink = (args) => createVendorSink(args, {
|
|
10
|
+
has: () => typeof args.win.plausible === "function",
|
|
11
|
+
dispatch: (event) => {
|
|
12
|
+
const options = {
|
|
13
|
+
...event.props ? { props: event.props } : {},
|
|
14
|
+
...event.value === void 0 ? {} : { revenue: {
|
|
15
|
+
amount: event.value,
|
|
16
|
+
...event.currency === void 0 ? {} : { currency: event.currency }
|
|
17
|
+
} }
|
|
18
|
+
};
|
|
19
|
+
args.win.plausible?.(vendorEventName(event), options);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
//#endregion
|
|
23
|
+
export { createPlausibleSink };
|
|
24
|
+
|
|
25
|
+
//# sourceMappingURL=plausible.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plausible.js","names":[],"sources":["../../../src/tracker/sinks/plausible.ts"],"sourcesContent":["import type { Sink, TrackerWindow } from '../types'\nimport { createVendorSink, type VendorSinkArgs, vendorEventName } from './vendor'\n\ninterface PlausibleOptions {\n\tprops?: Record<string, unknown>\n\trevenue?: { amount: number; currency?: string }\n}\n\ntype PlausibleWindow = TrackerWindow & {\n\tplausible?: (name: string, options?: PlausibleOptions) => void\n}\n\n/**\n * Forwards events and goals to the `plausible` queue function. Revenue uses Plausible's\n * documented ecommerce shape, `{ revenue: { amount, currency } }`\n * (https://plausible.io/docs/ecommerce-revenue-tracking). The per-site snippet publishes the\n * `plausible.q` stub, so calls made before the tracker loads are replayed by it.\n */\nexport const createPlausibleSink = (args: VendorSinkArgs): Sink =>\n\tcreateVendorSink(args, {\n\t\thas: () => typeof (args.win as PlausibleWindow).plausible === 'function',\n\t\tdispatch: (event) => {\n\t\t\tconst options: PlausibleOptions = {\n\t\t\t\t...(event.props ? { props: event.props } : {}),\n\t\t\t\t...(event.value === undefined\n\t\t\t\t\t? {}\n\t\t\t\t\t: {\n\t\t\t\t\t\t\trevenue: {\n\t\t\t\t\t\t\t\tamount: event.value,\n\t\t\t\t\t\t\t\t...(event.currency === undefined ? {} : { currency: event.currency }),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t}),\n\t\t\t}\n\t\t\t;(args.win as PlausibleWindow).plausible?.(vendorEventName(event), options)\n\t\t},\n\t})\n"],"mappings":";;;;;;;;AAkBA,MAAa,uBAAuB,SACnC,iBAAiB,MAAM;CACtB,WAAW,OAAQ,KAAK,IAAwB,cAAc;CAC9D,WAAW,UAAU;EACpB,MAAM,UAA4B;GACjC,GAAI,MAAM,QAAQ,EAAE,OAAO,MAAM,MAAM,IAAI,CAAC;GAC5C,GAAI,MAAM,UAAU,KAAA,IACjB,CAAC,IACD,EACA,SAAS;IACR,QAAQ,MAAM;IACd,GAAI,MAAM,aAAa,KAAA,IAAY,CAAC,IAAI,EAAE,UAAU,MAAM,SAAS;GACpE,EACD;EACH;EACC,KAAM,IAAwB,YAAY,gBAAgB,KAAK,GAAG,OAAO;CAC3E;AACD,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Sink } from "../types.js";
|
|
2
|
+
import { VendorSinkArgs } from "./vendor.js";
|
|
3
|
+
|
|
4
|
+
//#region src/tracker/sinks/posthog.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Forwards events and goals to `posthog.capture`. Revenue rides along as the `value` and
|
|
7
|
+
* `currency` properties, which is what PostHog's own revenue analytics reads. The adapter's
|
|
8
|
+
* snippet publishes PostHog's queueing stub, so `capture` is callable (and replayed) from
|
|
9
|
+
* the moment the snippet runs, well before `array.js` lands.
|
|
10
|
+
*/
|
|
11
|
+
declare const createPosthogSink: (args: VendorSinkArgs) => Sink;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { createPosthogSink };
|
|
14
|
+
//# sourceMappingURL=posthog.d.ts.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { createVendorSink, flatProps, vendorEventName } from "./vendor.js";
|
|
2
|
+
//#region src/tracker/sinks/posthog.ts
|
|
3
|
+
/**
|
|
4
|
+
* Forwards events and goals to `posthog.capture`. Revenue rides along as the `value` and
|
|
5
|
+
* `currency` properties, which is what PostHog's own revenue analytics reads. The adapter's
|
|
6
|
+
* snippet publishes PostHog's queueing stub, so `capture` is callable (and replayed) from
|
|
7
|
+
* the moment the snippet runs, well before `array.js` lands.
|
|
8
|
+
*/
|
|
9
|
+
const createPosthogSink = (args) => createVendorSink(args, {
|
|
10
|
+
has: () => typeof args.win.posthog?.capture === "function",
|
|
11
|
+
dispatch: (event) => {
|
|
12
|
+
args.win.posthog?.capture(vendorEventName(event), flatProps(event));
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
//#endregion
|
|
16
|
+
export { createPosthogSink };
|
|
17
|
+
|
|
18
|
+
//# sourceMappingURL=posthog.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"posthog.js","names":[],"sources":["../../../src/tracker/sinks/posthog.ts"],"sourcesContent":["import type { Sink, TrackerWindow } from '../types'\nimport { createVendorSink, flatProps, type VendorSinkArgs, vendorEventName } from './vendor'\n\ninterface PosthogGlobal {\n\tcapture(name: string, properties?: Record<string, unknown>): void\n}\n\ntype PosthogWindow = TrackerWindow & { posthog?: PosthogGlobal }\n\n/**\n * Forwards events and goals to `posthog.capture`. Revenue rides along as the `value` and\n * `currency` properties, which is what PostHog's own revenue analytics reads. The adapter's\n * snippet publishes PostHog's queueing stub, so `capture` is callable (and replayed) from\n * the moment the snippet runs, well before `array.js` lands.\n */\nexport const createPosthogSink = (args: VendorSinkArgs): Sink =>\n\tcreateVendorSink(args, {\n\t\thas: () => typeof (args.win as PosthogWindow).posthog?.capture === 'function',\n\t\tdispatch: (event) => {\n\t\t\t;(args.win as PosthogWindow).posthog?.capture(vendorEventName(event), flatProps(event))\n\t\t},\n\t})\n"],"mappings":";;;;;;;;AAeA,MAAa,qBAAqB,SACjC,iBAAiB,MAAM;CACtB,WAAW,OAAQ,KAAK,IAAsB,SAAS,YAAY;CACnE,WAAW,UAAU;EACnB,KAAM,IAAsB,SAAS,QAAQ,gBAAgB,KAAK,GAAG,UAAU,KAAK,CAAC;CACvF;AACD,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Sink } from "../types.js";
|
|
2
|
+
import { VendorSinkArgs } from "./vendor.js";
|
|
3
|
+
|
|
4
|
+
//#region src/tracker/sinks/umami.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Forwards events and goals to `umami.track`. Umami ships no queueing stub, so `umami`
|
|
7
|
+
* exists only once its script has run and the sink waits for it. Umami has no revenue field
|
|
8
|
+
* of its own either, so `value` and `currency` travel as event data alongside the props.
|
|
9
|
+
*/
|
|
10
|
+
declare const createUmamiSink: (args: VendorSinkArgs) => Sink;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { createUmamiSink };
|
|
13
|
+
//# sourceMappingURL=umami.d.ts.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { createVendorSink, flatProps, vendorEventName } from "./vendor.js";
|
|
2
|
+
//#region src/tracker/sinks/umami.ts
|
|
3
|
+
/**
|
|
4
|
+
* Forwards events and goals to `umami.track`. Umami ships no queueing stub, so `umami`
|
|
5
|
+
* exists only once its script has run and the sink waits for it. Umami has no revenue field
|
|
6
|
+
* of its own either, so `value` and `currency` travel as event data alongside the props.
|
|
7
|
+
*/
|
|
8
|
+
const createUmamiSink = (args) => createVendorSink(args, {
|
|
9
|
+
has: () => typeof args.win.umami?.track === "function",
|
|
10
|
+
dispatch: (event) => {
|
|
11
|
+
args.win.umami?.track(vendorEventName(event), flatProps(event));
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
//#endregion
|
|
15
|
+
export { createUmamiSink };
|
|
16
|
+
|
|
17
|
+
//# sourceMappingURL=umami.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"umami.js","names":[],"sources":["../../../src/tracker/sinks/umami.ts"],"sourcesContent":["import type { Sink, TrackerWindow } from '../types'\nimport { createVendorSink, flatProps, type VendorSinkArgs, vendorEventName } from './vendor'\n\ninterface UmamiGlobal {\n\ttrack(name: string, data?: Record<string, unknown>): void\n}\n\ntype UmamiWindow = TrackerWindow & { umami?: UmamiGlobal }\n\n/**\n * Forwards events and goals to `umami.track`. Umami ships no queueing stub, so `umami`\n * exists only once its script has run and the sink waits for it. Umami has no revenue field\n * of its own either, so `value` and `currency` travel as event data alongside the props.\n */\nexport const createUmamiSink = (args: VendorSinkArgs): Sink =>\n\tcreateVendorSink(args, {\n\t\thas: () => typeof (args.win as UmamiWindow).umami?.track === 'function',\n\t\tdispatch: (event) => {\n\t\t\t;(args.win as UmamiWindow).umami?.track(vendorEventName(event), flatProps(event))\n\t\t},\n\t})\n"],"mappings":";;;;;;;AAcA,MAAa,mBAAmB,SAC/B,iBAAiB,MAAM;CACtB,WAAW,OAAQ,KAAK,IAAoB,OAAO,UAAU;CAC7D,WAAW,UAAU;EACnB,KAAM,IAAoB,OAAO,MAAM,gBAAgB,KAAK,GAAG,UAAU,KAAK,CAAC;CACjF;AACD,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { SnippetScript } from "../../core/capture.js";
|
|
2
|
+
import { LoadScript, Sink, TrackerWindow } from "../types.js";
|
|
3
|
+
|
|
4
|
+
//#region src/tracker/sinks/vendor.d.ts
|
|
5
|
+
interface VendorSinkArgs {
|
|
6
|
+
slot: Sink['slot'];
|
|
7
|
+
win: TrackerWindow;
|
|
8
|
+
/** The slot's snippet scripts, in the order the adapter declared them. */
|
|
9
|
+
scripts: readonly SnippetScript[];
|
|
10
|
+
loadScript: LoadScript;
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
13
|
+
export { VendorSinkArgs };
|
|
14
|
+
//# sourceMappingURL=vendor.d.ts.map
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/** How long it keeps checking before giving up on the event. */
|
|
2
|
+
const VENDOR_GLOBAL_TIMEOUT_MS = 1e4;
|
|
3
|
+
const whenPresent = (win, target) => {
|
|
4
|
+
if (target.has()) return Promise.resolve(true);
|
|
5
|
+
return new Promise((resolve) => {
|
|
6
|
+
const deadline = Date.now() + VENDOR_GLOBAL_TIMEOUT_MS;
|
|
7
|
+
const check = () => {
|
|
8
|
+
if (target.has()) {
|
|
9
|
+
resolve(true);
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
if (Date.now() >= deadline) {
|
|
13
|
+
resolve(false);
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
win.setTimeout(check, 100);
|
|
17
|
+
};
|
|
18
|
+
win.setTimeout(check, 100);
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Shared body of every vendor sink: load the slot's snippet scripts once, in order, then
|
|
23
|
+
* hand each event to the vendor global. Pageviews are dropped, because every supported
|
|
24
|
+
* vendor script tracks its own and a forwarded one would double-count.
|
|
25
|
+
*
|
|
26
|
+
* Dispatch waits for the global to exist rather than for the load to resolve. PostHog and
|
|
27
|
+
* Plausible publish a queueing stub the moment their inline snippet runs, so an event sent
|
|
28
|
+
* before the SDK lands is queued and replayed by the SDK itself; Umami has no stub and its
|
|
29
|
+
* `umami` appears only once the script has run, which is why this polls instead of assuming
|
|
30
|
+
* the load promise is enough. An event whose global never turns up (an ad blocker, a CSP
|
|
31
|
+
* rule, a 404 on the proxy) is dropped in silence after the timeout: never surfaced to the
|
|
32
|
+
* visitor, never retried into a loop.
|
|
33
|
+
*/
|
|
34
|
+
const createVendorSink = (args, target) => {
|
|
35
|
+
let loading = null;
|
|
36
|
+
const ready = () => {
|
|
37
|
+
loading ??= args.scripts.reduce((chain, script) => chain.then(() => args.loadScript(script)), Promise.resolve(void 0));
|
|
38
|
+
return loading;
|
|
39
|
+
};
|
|
40
|
+
return {
|
|
41
|
+
slot: args.slot,
|
|
42
|
+
ready,
|
|
43
|
+
send(event) {
|
|
44
|
+
if (event.type === "pageview") return;
|
|
45
|
+
if (target.has()) {
|
|
46
|
+
target.dispatch(event);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
ready().then(() => whenPresent(args.win, target)).then((present) => {
|
|
50
|
+
if (present) target.dispatch(event);
|
|
51
|
+
}).catch(() => void 0);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
/** Event name on the wire: a goal travels under its slug. */
|
|
56
|
+
const vendorEventName = (event) => event.name ?? "";
|
|
57
|
+
/** `props` merged with the revenue fields, each key present only when it has a value. */
|
|
58
|
+
const flatProps = (event) => ({
|
|
59
|
+
...event.props,
|
|
60
|
+
...event.value === void 0 ? {} : { value: event.value },
|
|
61
|
+
...event.currency === void 0 ? {} : { currency: event.currency }
|
|
62
|
+
});
|
|
63
|
+
//#endregion
|
|
64
|
+
export { createVendorSink, flatProps, vendorEventName };
|
|
65
|
+
|
|
66
|
+
//# sourceMappingURL=vendor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vendor.js","names":[],"sources":["../../../src/tracker/sinks/vendor.ts"],"sourcesContent":["import type { SnippetScript } from '../../core/capture'\nimport type { LoadScript, Sink, TrackerEvent, TrackerWindow } from '../types'\n\n/** How often the sink re-checks for a vendor global that has not appeared yet. */\nexport const VENDOR_GLOBAL_POLL_MS = 100\n/** How long it keeps checking before giving up on the event. */\nexport const VENDOR_GLOBAL_TIMEOUT_MS = 10_000\n\nexport interface VendorSinkArgs {\n\tslot: Sink['slot']\n\twin: TrackerWindow\n\t/** The slot's snippet scripts, in the order the adapter declared them. */\n\tscripts: readonly SnippetScript[]\n\tloadScript: LoadScript\n}\n\n/** What a vendor sink needs to know about its own global. */\nexport interface VendorTarget {\n\t/** True once the global is callable, stub or fully loaded SDK alike. */\n\thas(): boolean\n\tdispatch(event: TrackerEvent): void\n}\n\nconst whenPresent = (win: TrackerWindow, target: VendorTarget): Promise<boolean> => {\n\tif (target.has()) {\n\t\treturn Promise.resolve(true)\n\t}\n\treturn new Promise((resolve) => {\n\t\tconst deadline = Date.now() + VENDOR_GLOBAL_TIMEOUT_MS\n\t\tconst check = () => {\n\t\t\tif (target.has()) {\n\t\t\t\tresolve(true)\n\t\t\t\treturn\n\t\t\t}\n\t\t\tif (Date.now() >= deadline) {\n\t\t\t\tresolve(false)\n\t\t\t\treturn\n\t\t\t}\n\t\t\twin.setTimeout(check, VENDOR_GLOBAL_POLL_MS)\n\t\t}\n\t\twin.setTimeout(check, VENDOR_GLOBAL_POLL_MS)\n\t})\n}\n\n/**\n * Shared body of every vendor sink: load the slot's snippet scripts once, in order, then\n * hand each event to the vendor global. Pageviews are dropped, because every supported\n * vendor script tracks its own and a forwarded one would double-count.\n *\n * Dispatch waits for the global to exist rather than for the load to resolve. PostHog and\n * Plausible publish a queueing stub the moment their inline snippet runs, so an event sent\n * before the SDK lands is queued and replayed by the SDK itself; Umami has no stub and its\n * `umami` appears only once the script has run, which is why this polls instead of assuming\n * the load promise is enough. An event whose global never turns up (an ad blocker, a CSP\n * rule, a 404 on the proxy) is dropped in silence after the timeout: never surfaced to the\n * visitor, never retried into a loop.\n */\nexport const createVendorSink = (args: VendorSinkArgs, target: VendorTarget): Sink => {\n\tlet loading: Promise<void> | null = null\n\n\tconst ready = (): Promise<void> => {\n\t\tloading ??= args.scripts.reduce(\n\t\t\t(chain, script) => chain.then(() => args.loadScript(script)),\n\t\t\tPromise.resolve<void>(undefined)\n\t\t)\n\t\treturn loading\n\t}\n\n\treturn {\n\t\tslot: args.slot,\n\t\tready,\n\t\tsend(event) {\n\t\t\tif (event.type === 'pageview') {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tif (target.has()) {\n\t\t\t\ttarget.dispatch(event)\n\t\t\t\treturn\n\t\t\t}\n\t\t\tvoid ready()\n\t\t\t\t.then(() => whenPresent(args.win, target))\n\t\t\t\t.then((present) => {\n\t\t\t\t\tif (present) {\n\t\t\t\t\t\ttarget.dispatch(event)\n\t\t\t\t\t}\n\t\t\t\t})\n\t\t\t\t.catch(() => undefined)\n\t\t},\n\t}\n}\n\n/** Event name on the wire: a goal travels under its slug. */\nexport const vendorEventName = (event: TrackerEvent): string => event.name ?? ''\n\n/** `props` merged with the revenue fields, each key present only when it has a value. */\nexport const flatProps = (event: TrackerEvent): Record<string, unknown> => ({\n\t...event.props,\n\t...(event.value === undefined ? {} : { value: event.value }),\n\t...(event.currency === undefined ? {} : { currency: event.currency }),\n})\n"],"mappings":";AAMA,MAAa,2BAA2B;AAiBxC,MAAM,eAAe,KAAoB,WAA2C;CACnF,IAAI,OAAO,IAAI,GACd,OAAO,QAAQ,QAAQ,IAAI;CAE5B,OAAO,IAAI,SAAS,YAAY;EAC/B,MAAM,WAAW,KAAK,IAAI,IAAI;EAC9B,MAAM,cAAc;GACnB,IAAI,OAAO,IAAI,GAAG;IACjB,QAAQ,IAAI;IACZ;GACD;GACA,IAAI,KAAK,IAAI,KAAK,UAAU;IAC3B,QAAQ,KAAK;IACb;GACD;GACA,IAAI,WAAW,OAAA,GAA4B;EAC5C;EACA,IAAI,WAAW,OAAA,GAA4B;CAC5C,CAAC;AACF;;;;;;;;;;;;;;AAeA,MAAa,oBAAoB,MAAsB,WAA+B;CACrF,IAAI,UAAgC;CAEpC,MAAM,cAA6B;EAClC,YAAY,KAAK,QAAQ,QACvB,OAAO,WAAW,MAAM,WAAW,KAAK,WAAW,MAAM,CAAC,GAC3D,QAAQ,QAAc,KAAA,CAAS,CAChC;EACA,OAAO;CACR;CAEA,OAAO;EACN,MAAM,KAAK;EACX;EACA,KAAK,OAAO;GACX,IAAI,MAAM,SAAS,YAClB;GAED,IAAI,OAAO,IAAI,GAAG;IACjB,OAAO,SAAS,KAAK;IACrB;GACD;GACA,MAAW,EACT,WAAW,YAAY,KAAK,KAAK,MAAM,CAAC,EACxC,MAAM,YAAY;IAClB,IAAI,SACH,OAAO,SAAS,KAAK;GAEvB,CAAC,EACA,YAAY,KAAA,CAAS;EACxB;CACD;AACD;;AAGA,MAAa,mBAAmB,UAAgC,MAAM,QAAQ;;AAG9E,MAAa,aAAa,WAAkD;CAC3E,GAAG,MAAM;CACT,GAAI,MAAM,UAAU,KAAA,IAAY,CAAC,IAAI,EAAE,OAAO,MAAM,MAAM;CAC1D,GAAI,MAAM,aAAa,KAAA,IAAY,CAAC,IAAI,EAAE,UAAU,MAAM,SAAS;AACpE"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
//#region src/tracker/transport.ts
|
|
2
|
+
/**
|
|
3
|
+
* Posts one JSON payload to a first-party path. `sendBeacon` first, because it survives the
|
|
4
|
+
* unload the pageview flush happens in; `fetch` with `keepalive` covers the browsers (and
|
|
5
|
+
* the blocked-beacon cases) where it is missing or refuses the payload. Delivery is
|
|
6
|
+
* fire-and-forget: a failed send is dropped rather than retried, so analytics never
|
|
7
|
+
* interferes with the page.
|
|
8
|
+
*/
|
|
9
|
+
const sendPayload = (win, url, body) => {
|
|
10
|
+
const json = JSON.stringify(body);
|
|
11
|
+
const beacon = win.navigator?.sendBeacon;
|
|
12
|
+
if (typeof beacon === "function") try {
|
|
13
|
+
if (beacon.call(win.navigator, url, new Blob([json], { type: "application/json" }))) return;
|
|
14
|
+
} catch {}
|
|
15
|
+
try {
|
|
16
|
+
win.fetch?.(url, {
|
|
17
|
+
method: "POST",
|
|
18
|
+
headers: { "content-type": "application/json" },
|
|
19
|
+
body: json,
|
|
20
|
+
keepalive: true,
|
|
21
|
+
credentials: "same-origin"
|
|
22
|
+
})?.catch(() => void 0);
|
|
23
|
+
} catch {}
|
|
24
|
+
};
|
|
25
|
+
//#endregion
|
|
26
|
+
export { sendPayload };
|
|
27
|
+
|
|
28
|
+
//# sourceMappingURL=transport.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transport.js","names":[],"sources":["../../src/tracker/transport.ts"],"sourcesContent":["import type { TrackerWindow } from './types'\n\n/**\n * Posts one JSON payload to a first-party path. `sendBeacon` first, because it survives the\n * unload the pageview flush happens in; `fetch` with `keepalive` covers the browsers (and\n * the blocked-beacon cases) where it is missing or refuses the payload. Delivery is\n * fire-and-forget: a failed send is dropped rather than retried, so analytics never\n * interferes with the page.\n */\nexport const sendPayload = (win: TrackerWindow, url: string, body: unknown): void => {\n\tconst json = JSON.stringify(body)\n\tconst beacon = win.navigator?.sendBeacon\n\tif (typeof beacon === 'function') {\n\t\ttry {\n\t\t\tif (beacon.call(win.navigator, url, new Blob([json], { type: 'application/json' }))) {\n\t\t\t\treturn\n\t\t\t}\n\t\t} catch {\n\t\t\t// A CSP connect-src violation or a queue overflow: fall through to fetch.\n\t\t}\n\t}\n\ttry {\n\t\tvoid win\n\t\t\t.fetch?.(url, {\n\t\t\t\tmethod: 'POST',\n\t\t\t\theaders: { 'content-type': 'application/json' },\n\t\t\t\tbody: json,\n\t\t\t\tkeepalive: true,\n\t\t\t\tcredentials: 'same-origin',\n\t\t\t})\n\t\t\t?.catch(() => undefined)\n\t} catch {\n\t\t// No transport available (a non-browser host); drop the event.\n\t}\n}\n"],"mappings":";;;;;;;;AASA,MAAa,eAAe,KAAoB,KAAa,SAAwB;CACpF,MAAM,OAAO,KAAK,UAAU,IAAI;CAChC,MAAM,SAAS,IAAI,WAAW;CAC9B,IAAI,OAAO,WAAW,YACrB,IAAI;EACH,IAAI,OAAO,KAAK,IAAI,WAAW,KAAK,IAAI,KAAK,CAAC,IAAI,GAAG,EAAE,MAAM,mBAAmB,CAAC,CAAC,GACjF;CAEF,QAAQ,CAER;CAED,IAAI;EACH,IACE,QAAQ,KAAK;GACb,QAAQ;GACR,SAAS,EAAE,gBAAgB,mBAAmB;GAC9C,MAAM;GACN,WAAW;GACX,aAAa;EACd,CAAC,GACC,YAAY,KAAA,CAAS;CACzB,QAAQ,CAER;AACD"}
|