@10x-media/analytics 1.0.0-beta.14 → 1.0.0-beta.16
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 +55 -0
- package/README.md +1 -1
- package/dist/adapters/ga4/ga4.d.ts +7 -0
- package/dist/adapters/ga4/ga4.js +17 -3
- package/dist/adapters/ga4/ga4.js.map +1 -1
- package/dist/adapters/plausible/plausible.d.ts +6 -0
- package/dist/adapters/plausible/plausible.js +6 -3
- package/dist/adapters/plausible/plausible.js.map +1 -1
- package/dist/adapters/posthog/cloud.js +20 -0
- package/dist/adapters/posthog/cloud.js.map +1 -0
- package/dist/adapters/posthog/credentials.js +20 -0
- package/dist/adapters/posthog/credentials.js.map +1 -0
- package/dist/adapters/posthog/posthog.d.ts +10 -3
- package/dist/adapters/posthog/posthog.js +100 -27
- package/dist/adapters/posthog/posthog.js.map +1 -1
- package/dist/adapters/posthog/projectCheck.js +56 -0
- package/dist/adapters/posthog/projectCheck.js.map +1 -0
- package/dist/adapters/umami/umami.d.ts +6 -0
- package/dist/adapters/umami/umami.js +3 -1
- package/dist/adapters/umami/umami.js.map +1 -1
- package/dist/capture/trackerConfig.js +29 -6
- package/dist/capture/trackerConfig.js.map +1 -1
- package/dist/capture/withoutConsent.js +91 -0
- package/dist/capture/withoutConsent.js.map +1 -0
- package/dist/core/capture.d.ts +41 -11
- package/dist/core/captureOptions.js +97 -8
- package/dist/core/captureOptions.js.map +1 -1
- package/dist/core/contract.d.ts +25 -0
- package/dist/core/contract.js.map +1 -1
- package/dist/core/options.d.ts +24 -2
- package/dist/core/options.js.map +1 -1
- package/dist/core/sourceOptions.js +2 -1
- package/dist/core/sourceOptions.js.map +1 -1
- package/dist/exports/adapters/contract.d.ts +3 -3
- package/dist/exports/adapters/ga4.d.ts +4 -4
- package/dist/exports/adapters/native.d.ts +5 -5
- package/dist/exports/adapters/plausible.d.ts +4 -4
- package/dist/exports/adapters/posthog.d.ts +4 -4
- package/dist/exports/adapters/umami.d.ts +4 -4
- package/dist/exports/next.d.ts +4 -4
- package/dist/exports/react.d.ts +3 -2
- package/dist/exports/site.d.ts +2 -2
- package/dist/exports/testing.d.ts +4 -4
- package/dist/exports/tracker.d.ts +3 -2
- package/dist/exports/types.d.ts +5 -5
- package/dist/index.d.ts +5 -5
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/native/collections/hits.js +1 -0
- package/dist/native/collections/hits.js.map +1 -1
- package/dist/native/engine/engine.js +5 -1
- package/dist/native/engine/engine.js.map +1 -1
- package/dist/native/engine/planner.js +2 -1
- package/dist/native/engine/planner.js.map +1 -1
- package/dist/native/engine/toHit.js +4 -2
- package/dist/native/engine/toHit.js.map +1 -1
- package/dist/native/ingest/endpoint.js +5 -2
- package/dist/native/ingest/endpoint.js.map +1 -1
- package/dist/native/ingest/normalizeEvent.d.ts +6 -1
- package/dist/native/ingest/normalizeEvent.js +17 -7
- package/dist/native/ingest/normalizeEvent.js.map +1 -1
- package/dist/native/ingest/serverTrack.js +2 -1
- package/dist/native/ingest/serverTrack.js.map +1 -1
- package/dist/native/ingest/validate.js +3 -0
- package/dist/native/ingest/validate.js.map +1 -1
- package/dist/native/nativeAdapter.d.ts +10 -1
- package/dist/native/nativeAdapter.js +11 -1
- package/dist/native/nativeAdapter.js.map +1 -1
- package/dist/native/store/aggregate.js +1 -1
- package/dist/native/store/aggregate.js.map +1 -1
- package/dist/native/store/hitRow.js +3 -0
- package/dist/native/store/hitRow.js.map +1 -1
- package/dist/native/store/hits.js +3 -1
- package/dist/native/store/hits.js.map +1 -1
- package/dist/native/store/mongo/aggregatePipelines.js +10 -2
- package/dist/native/store/mongo/aggregatePipelines.js.map +1 -1
- package/dist/native/store/mongo/pipelines.js +15 -5
- package/dist/native/store/mongo/pipelines.js.map +1 -1
- package/dist/native/store/parts.js +9 -3
- package/dist/native/store/parts.js.map +1 -1
- package/dist/native/store/rawRead.js +6 -3
- package/dist/native/store/rawRead.js.map +1 -1
- package/dist/native/store/sql/aggregateSql.js +6 -2
- package/dist/native/store/sql/aggregateSql.js.map +1 -1
- package/dist/native/store/sql/columns.js +1 -0
- package/dist/native/store/sql/columns.js.map +1 -1
- package/dist/native/store/sql/compile.js +19 -7
- package/dist/native/store/sql/compile.js.map +1 -1
- package/dist/native/store/types.d.ts +7 -1
- package/dist/native/store/types.js +10 -0
- package/dist/native/store/types.js.map +1 -0
- package/dist/plugin/createRuntime.js +10 -1
- package/dist/plugin/createRuntime.js.map +1 -1
- package/dist/plugin/runtime.js.map +1 -1
- package/dist/providers/collection.d.ts +2 -0
- package/dist/providers/collection.js +36 -9
- package/dist/providers/collection.js.map +1 -1
- package/dist/providers/factory.d.ts +9 -1
- package/dist/providers/factory.js +14 -5
- package/dist/providers/factory.js.map +1 -1
- package/dist/providers/hosts.js +14 -6
- package/dist/providers/hosts.js.map +1 -1
- package/dist/providers/registry.js +2 -1
- package/dist/providers/registry.js.map +1 -1
- package/dist/react/AnalyticsProvider.d.ts +6 -0
- package/dist/react/AnalyticsProvider.js.map +1 -1
- package/dist/react/useAnalytics.js +9 -5
- package/dist/react/useAnalytics.js.map +1 -1
- package/dist/site/AnalyticsScripts.d.ts +4 -3
- package/dist/site/AnalyticsScripts.js +18 -4
- package/dist/site/AnalyticsScripts.js.map +1 -1
- package/dist/surfacing/todaySplit.js +7 -1
- package/dist/surfacing/todaySplit.js.map +1 -1
- package/dist/tracker/config.d.ts +26 -1
- package/dist/tracker/config.js.map +1 -1
- package/dist/tracker/createTracker.d.ts +2 -1
- package/dist/tracker/createTracker.js +91 -59
- package/dist/tracker/createTracker.js.map +1 -1
- package/dist/tracker/gate.d.ts +8 -0
- package/dist/tracker/gate.js +61 -11
- package/dist/tracker/gate.js.map +1 -1
- package/dist/tracker/loadScript.js +2 -1
- package/dist/tracker/loadScript.js.map +1 -1
- package/dist/tracker/registry.js +14 -3
- package/dist/tracker/registry.js.map +1 -1
- package/dist/tracker/sinks/createSink.js +27 -8
- package/dist/tracker/sinks/createSink.js.map +1 -1
- package/dist/tracker/sinks/ga4.js +38 -6
- package/dist/tracker/sinks/ga4.js.map +1 -1
- package/dist/tracker/sinks/native.js +17 -3
- package/dist/tracker/sinks/native.js.map +1 -1
- package/dist/tracker/sinks/plausible.js +8 -2
- package/dist/tracker/sinks/plausible.js.map +1 -1
- package/dist/tracker/sinks/posthog.js +108 -16
- package/dist/tracker/sinks/posthog.js.map +1 -1
- package/dist/tracker/sinks/umami.js +2 -2
- package/dist/tracker/sinks/umami.js.map +1 -1
- package/dist/tracker/sinks/vendor.js +25 -11
- package/dist/tracker/sinks/vendor.js.map +1 -1
- package/dist/tracker/types.d.ts +10 -3
- package/dist/translations/ar.js +10 -0
- package/dist/translations/ar.js.map +1 -1
- package/dist/translations/de.js +10 -0
- package/dist/translations/de.js.map +1 -1
- package/dist/translations/en.js +10 -0
- package/dist/translations/en.js.map +1 -1
- package/dist/translations/es.js +10 -0
- package/dist/translations/es.js.map +1 -1
- package/dist/translations/fr.js +10 -0
- package/dist/translations/fr.js.map +1 -1
- package/dist/translations/id.js +10 -0
- package/dist/translations/id.js.map +1 -1
- package/dist/translations/keys.d.ts +10 -0
- package/dist/translations/keys.js +11 -1
- package/dist/translations/keys.js.map +1 -1
- package/dist/translations/ko.js +10 -0
- package/dist/translations/ko.js.map +1 -1
- package/dist/translations/pt.js +10 -0
- package/dist/translations/pt.js.map +1 -1
- package/dist/translations/ru.js +10 -0
- package/dist/translations/ru.js.map +1 -1
- package/dist/translations/uk.js +10 -0
- package/dist/translations/uk.js.map +1 -1
- package/dist/translations/zh.js +10 -0
- package/dist/translations/zh.js.map +1 -1
- package/dist/view/consentShare.js +15 -0
- package/dist/view/consentShare.js.map +1 -0
- package/dist/view/sections/OverviewCards.js +38 -28
- package/dist/view/sections/OverviewCards.js.map +1 -1
- package/dist/widgets/notices.js +6 -1
- package/dist/widgets/notices.js.map +1 -1
- package/dist/widgets/readMeta.d.ts +4 -0
- package/dist/widgets/readMeta.js +5 -0
- package/dist/widgets/readMeta.js.map +1 -1
- package/package.json +3 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"planner.js","names":[],"sources":["../../../src/native/engine/planner.ts"],"sourcesContent":["import type { AnalyticsFilter, AnalyticsQuery, DimensionKey, MetricKey } from '../../core/contract'\nimport { addCalendarDays, type CalendarDay } from '../../timeframe/calendar'\nimport { AGGREGATION_VERSION, kindOf } from '../store/aggregate'\nimport { isRawDimension, type RawAxis, type RawDimension } from '../store/rawRead'\nimport { SETTLE_MS } from '../store/state'\nimport type { AggDim, DayMarker, FoldRead, RawOrder } from '../store/types'\nimport { type LocalDay, markerStart } from './days'\n\n/**\n * Whether a completed marker was counted the current way over the window its day is read in: the\n * current aggregation version, in `tz`, from where that window starts.\n */\nexport const countedOver = (marker: DayMarker, tz: string, window: LocalDay): boolean =>\n\tmarker.version === AGGREGATION_VERSION &&\n\tmarker.tz === tz &&\n\tmarkerStart(marker).getTime() === window.start.getTime()\n\n/**\n * Whether a day's rows hold the whole day, counted the current way over its window: aggregated\n * after it closed (`provisional`, `final`), or a snapshot taken once it had settled.\n */\nexport const holdsWholeDay = (\n\tmarker: DayMarker | undefined,\n\ttz: string,\n\twindow: LocalDay\n): boolean => {\n\tif (!marker || marker.gen === null || !countedOver(marker, tz, window)) {\n\t\treturn false\n\t}\n\tif (marker.status === 'provisional' || marker.status === 'final') {\n\t\treturn true\n\t}\n\treturn (\n\t\tmarker.status === 'partial' &&\n\t\tmarker.aggregatedAt !== null &&\n\t\tmarker.aggregatedAt.getTime() >= window.end.getTime() + SETTLE_MS\n\t)\n}\n\n/**\n * Where a closed day is read from: its rows, nowhere, a `probe` first, an `aggregate` first (rows\n * missing or counted another way), or `failed` (given up on: whatever an earlier count left).\n */\nexport type ClosedDaySource = 'aggregates' | 'empty' | 'probe' | 'aggregate' | 'failed'\n\nexport const closedDaySource = (args: {\n\tmarker: DayMarker | undefined\n\ttz: string\n\t/** The window the day is read over in `tz` (see `windowOf`). */\n\twindow: LocalDay\n\t/** Its count is fixed: part of what it counted is gone from the raw hits (see `isFixed`). */\n\tfixed: boolean\n}): ClosedDaySource => {\n\tconst { marker, tz, window, fixed } = args\n\tif (!marker) {\n\t\treturn 'probe'\n\t}\n\tif (marker.status === 'failed') {\n\t\treturn 'failed'\n\t}\n\t// The only numbers a fixed day has, whatever zone or version they were counted in.\n\tif (fixed) {\n\t\treturn marker.gen === null ? 'empty' : 'aggregates'\n\t}\n\tif (marker.status === 'empty') {\n\t\treturn countedOver(marker, tz, window) ? 'empty' : 'probe'\n\t}\n\treturn holdsWholeDay(marker, tz, window) ? 'aggregates' : 'aggregate'\n}\n\n/** Consecutive dates as runs `[first, last]`, in the order given, which must ascend. */\nexport const runsOf = (days: readonly CalendarDay[]): Array<[CalendarDay, CalendarDay]> => {\n\tconst runs: Array<[CalendarDay, CalendarDay]> = []\n\tfor (const day of days) {\n\t\tconst run = runs.at(-1)\n\t\tif (run && addCalendarDays(run[1], 1) === day) {\n\t\t\trun[1] = day\n\t\t} else {\n\t\t\truns.push([day, day])\n\t\t}\n\t}\n\treturn runs\n}\n\n/**\n * How many hits a closed day's marker says it holds, when its count is of the whole day: taken\n * once the day had ended. Null for a day never counted, or counted only while it ran.\n */\nexport const wholeDayHits = (marker: DayMarker | undefined, window: LocalDay): number | null =>\n\tmarker?.hits !== undefined &&\n\tmarker.hits !== null &&\n\tmarker.aggregatedAt !== null &&\n\tmarker.aggregatedAt >= window.end\n\t\t? marker.hits\n\t\t: null\n\n/**\n * A running day's snapshot: `fresh` within `maxAgeMs` of its count, `stale` past it (served while a\n * new one is taken), `none` without one counted the current way over its window.\n */\nexport const snapshotState = (\n\tmarker: DayMarker | undefined,\n\tat: { tz: string; window: LocalDay; now: Date; maxAgeMs: (hits: number) => number }\n): 'fresh' | 'stale' | 'none' => {\n\tconst { tz, window, now, maxAgeMs } = at\n\tif (\n\t\tmarker?.status !== 'partial' ||\n\t\tmarker.gen === null ||\n\t\tmarker.aggregatedAt === null ||\n\t\t!countedOver(marker, tz, window)\n\t) {\n\t\treturn 'none'\n\t}\n\treturn now.getTime() - marker.aggregatedAt.getTime() <= maxAgeMs(marker.hits ?? 0)\n\t\t? 'fresh'\n\t\t: 'stale'\n}\n\n/** The aggregate rows one part of a read sums: a dimension's, one value's, or a pair's. */\nexport interface AggregateTarget {\n\tdim: AggDim\n\tvalue?: string\n\tdim2?: AggDim\n}\n\n/**\n * The aggregate rows a read's totals, day series and breakdown use. `raw` when only raw hits can\n * answer its rows, with `total` still set when its filters alone have rows of their own.\n */\nexport type ReadShape =\n\t| {\n\t\t\tsource: 'aggregates'\n\t\t\ttotal: AggregateTarget\n\t\t\tbreakdown?: AggregateTarget & { groupBy: 'value' | 'value2' }\n\t }\n\t| { source: 'raw'; total?: AggregateTarget }\n\n/** The breakdowns a goal's pair rows are kept by. */\nconst GOAL_PAIRS: readonly RawDimension[] = ['channel', 'source', 'utmCampaign']\n\nconst RAW: ReadShape = { source: 'raw' }\n\n/**\n * Aggregates answer unfiltered reads, one `eq` filter, a goal by channel, source or campaign and an\n * item by page; the rest and hours read raw, a lone `eq` filter's totals still from its own rows.\n */\nexport const readShape = (args: {\n\tfilters: readonly AnalyticsFilter[]\n\tbreakdown?: RawDimension\n\thours: boolean\n}): ReadShape => {\n\tconst { filters, breakdown, hours } = args\n\tif (hours) {\n\t\treturn RAW\n\t}\n\tif (filters.length === 0) {\n\t\tif (breakdown === undefined) {\n\t\t\treturn { source: 'aggregates', total: { dim: '' } }\n\t\t}\n\t\treturn kindOf(breakdown, '')\n\t\t\t? {\n\t\t\t\t\tsource: 'aggregates',\n\t\t\t\t\ttotal: { dim: '' },\n\t\t\t\t\tbreakdown: { dim: breakdown, groupBy: 'value' },\n\t\t\t\t}\n\t\t\t: RAW\n\t}\n\tconst [only] = filters\n\tif (\n\t\tfilters.length !== 1 ||\n\t\t!only ||\n\t\tonly.operator !== 'eq' ||\n\t\tonly.value === '' ||\n\t\t!isRawDimension(only.dimension) ||\n\t\t!kindOf(only.dimension, '')\n\t) {\n\t\treturn RAW\n\t}\n\tconst total = { dim: only.dimension, value: only.value }\n\tif (breakdown === undefined) {\n\t\treturn { source: 'aggregates', total }\n\t}\n\tconst pair =\n\t\t(only.dimension === 'goal' && GOAL_PAIRS.includes(breakdown)) ||\n\t\t(only.dimension === 'item' && breakdown === 'page')\n\treturn pair && kindOf(only.dimension, breakdown)\n\t\t? { source: 'aggregates', total, breakdown: { ...total, dim2: breakdown, groupBy: 'value2' } }\n\t\t: { source: 'raw', total }\n}\n\n/** A value a read of daily totals asks about: one dimension's value, or a pair's first value. */\ntype FoldTarget = Pick<FoldRead, 'dim' | 'dim2' | 'value'>\n\n/**\n * The values a read of daily totals asks about where a cap can fold them: its one `eq` filter's\n * value, and a pair breakdown's first value. A day that folded one is read from its raw hits.\n */\nexport const foldTargets = (shape: ReadShape): FoldTarget[] => {\n\tif (shape.source !== 'aggregates') {\n\t\treturn []\n\t}\n\tconst targets: FoldTarget[] = []\n\tconst { total, breakdown } = shape\n\tif (total.value !== undefined && (kindOf(total.dim, '')?.cap ?? null) !== null) {\n\t\ttargets.push({ dim: total.dim, value: total.value })\n\t}\n\tif (\n\t\tbreakdown?.dim2 !== undefined &&\n\t\tbreakdown.value !== undefined &&\n\t\t(kindOf(breakdown.dim, breakdown.dim2)?.cap ?? null) !== null\n\t) {\n\t\ttargets.push({ dim: breakdown.dim, dim2: breakdown.dim2, value: breakdown.value })\n\t}\n\treturn targets\n}\n\nconst sameFilter = (a: AnalyticsFilter, b: AnalyticsFilter): boolean =>\n\ta.dimension === b.dimension && a.operator === b.operator && a.value === b.value\n\nconst equals = (dimension: DimensionKey, value: string | undefined): AnalyticsFilter[] =>\n\tvalue === undefined ? [] : [{ dimension, operator: 'eq', value }]\n\n/**\n * A read's filters with its page and its item folded in as `eq` filters, each filter once: a\n * read of one page or one item is a read filtered to it.\n */\nexport const readFilters = (\n\tquery: Pick<AnalyticsQuery, 'filters' | 'path' | 'item'>\n): AnalyticsFilter[] => {\n\tconst all = [\n\t\t...(query.filters ?? []),\n\t\t...equals('page', query.path),\n\t\t...equals('item', query.item),\n\t]\n\treturn all.filter(\n\t\t(filter, index) => all.findIndex((other) => sameFilter(filter, other)) === index\n\t)\n}\n\n/** What a read counts: goal completions, items seen and clicked, or hits. */\nexport const axisOf = (\n\tfilters: readonly AnalyticsFilter[],\n\tbreakdown: DimensionKey | undefined\n): RawAxis => {\n\tconst dimensions = [...filters.map((filter) => filter.dimension), breakdown]\n\treturn dimensions.includes('goal') ? 'goal' : dimensions.includes('item') ? 'item' : 'hit'\n}\n\nconst ORDER_PARTS: Record<MetricKey, Pick<RawOrder, 'part' | 'per'>> = {\n\tpageviews: { part: 'pageviews' },\n\tvisitors: { part: 'visitors' },\n\tvisits: { part: 'sessions' },\n\tsessions: { part: 'sessions' },\n\tbounceRate: { part: 'bounces', per: 'entries' },\n\tavgDuration: { part: 'durationMs', per: 'pageviews' },\n\tvisitDuration: { part: 'sessionMs', per: 'sessions' },\n\tscrollDepth: { part: 'scrollSum', per: 'scrollSamples' },\n\tevents: { part: 'events' },\n\tconversions: { part: 'conversions' },\n\trevenue: { part: 'revenue' },\n\timpressions: { part: 'impressions' },\n\tinteractions: { part: 'interactions' },\n}\n\n/**\n * How a breakdown ranks, as parts: an average by its own numerator over its own denominator. By\n * default items rank by how often they were seen, goals by completions, the rest by pageviews.\n */\nexport const orderOf = (query: Pick<AnalyticsQuery, 'order'>, axis: RawAxis): RawOrder => {\n\tconst metric =\n\t\tquery.order?.metric ??\n\t\t(axis === 'item' ? 'impressions' : axis === 'goal' ? 'conversions' : 'pageviews')\n\treturn { ...ORDER_PARTS[metric], dir: query.order?.direction ?? 'desc' }\n}\n\nconst VISIT_METRICS: ReadonlySet<MetricKey> = new Set([\n\t'visits',\n\t'sessions',\n\t'bounceRate',\n\t'visitDuration',\n])\n\n/** Whether a read counts visits, which a raw read only does when it cuts them. */\nexport const countsVisits = (query: Pick<AnalyticsQuery, 'metrics' | 'order'>): boolean =>\n\tquery.metrics.some((metric) => VISIT_METRICS.has(metric)) ||\n\t(query.order !== undefined && VISIT_METRICS.has(query.order.metric))\n\n/** The series a read asks for; a breakdown wins over a series. */\nexport type SeriesUnit = 'hour' | 'day' | 'week' | 'month'\n\nexport const seriesOf = (\n\tquery: Pick<AnalyticsQuery, 'granularity'>,\n\tbreakdown: DimensionKey | undefined\n): SeriesUnit | undefined => {\n\tconst unit = query.granularity\n\treturn breakdown === undefined &&\n\t\t(unit === 'hour' || unit === 'day' || unit === 'week' || unit === 'month')\n\t\t? unit\n\t\t: undefined\n}\n"],"mappings":";;;;;;;;;;AAYA,MAAa,eAAe,QAAmB,IAAY,WAC1D,OAAO,YAAA,KACP,OAAO,OAAO,MACd,YAAY,MAAM,EAAE,QAAQ,MAAM,OAAO,MAAM,QAAQ;;;;;AAMxD,MAAa,iBACZ,QACA,IACA,WACa;CACb,IAAI,CAAC,UAAU,OAAO,QAAQ,QAAQ,CAAC,YAAY,QAAQ,IAAI,MAAM,GACpE,OAAO;CAER,IAAI,OAAO,WAAW,iBAAiB,OAAO,WAAW,SACxD,OAAO;CAER,OACC,OAAO,WAAW,aAClB,OAAO,iBAAiB,QACxB,OAAO,aAAa,QAAQ,KAAK,OAAO,IAAI,QAAQ,IAAA;AAEtD;AAQA,MAAa,mBAAmB,SAOT;CACtB,MAAM,EAAE,QAAQ,IAAI,QAAQ,UAAU;CACtC,IAAI,CAAC,QACJ,OAAO;CAER,IAAI,OAAO,WAAW,UACrB,OAAO;CAGR,IAAI,OACH,OAAO,OAAO,QAAQ,OAAO,UAAU;CAExC,IAAI,OAAO,WAAW,SACrB,OAAO,YAAY,QAAQ,IAAI,MAAM,IAAI,UAAU;CAEpD,OAAO,cAAc,QAAQ,IAAI,MAAM,IAAI,eAAe;AAC3D;;AAGA,MAAa,UAAU,SAAoE;CAC1F,MAAM,OAA0C,CAAC;CACjD,KAAK,MAAM,OAAO,MAAM;EACvB,MAAM,MAAM,KAAK,GAAG,EAAE;E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../src/native/engine/planner.ts"],"sourcesContent":["import type { AnalyticsFilter, AnalyticsQuery, DimensionKey, MetricKey } from '../../core/contract'\nimport { addCalendarDays, type CalendarDay } from '../../timeframe/calendar'\nimport { AGGREGATION_VERSION, kindOf } from '../store/aggregate'\nimport { isRawDimension, type RawAxis, type RawDimension } from '../store/rawRead'\nimport { SETTLE_MS } from '../store/state'\nimport type { AggDim, DayMarker, FoldRead, RawOrder } from '../store/types'\nimport { type LocalDay, markerStart } from './days'\n\n/**\n * Whether a completed marker was counted the current way over the window its day is read in: the\n * current aggregation version, in `tz`, from where that window starts.\n */\nexport const countedOver = (marker: DayMarker, tz: string, window: LocalDay): boolean =>\n\tmarker.version === AGGREGATION_VERSION &&\n\tmarker.tz === tz &&\n\tmarkerStart(marker).getTime() === window.start.getTime()\n\n/**\n * Whether a day's rows hold the whole day, counted the current way over its window: aggregated\n * after it closed (`provisional`, `final`), or a snapshot taken once it had settled.\n */\nexport const holdsWholeDay = (\n\tmarker: DayMarker | undefined,\n\ttz: string,\n\twindow: LocalDay\n): boolean => {\n\tif (!marker || marker.gen === null || !countedOver(marker, tz, window)) {\n\t\treturn false\n\t}\n\tif (marker.status === 'provisional' || marker.status === 'final') {\n\t\treturn true\n\t}\n\treturn (\n\t\tmarker.status === 'partial' &&\n\t\tmarker.aggregatedAt !== null &&\n\t\tmarker.aggregatedAt.getTime() >= window.end.getTime() + SETTLE_MS\n\t)\n}\n\n/**\n * Where a closed day is read from: its rows, nowhere, a `probe` first, an `aggregate` first (rows\n * missing or counted another way), or `failed` (given up on: whatever an earlier count left).\n */\nexport type ClosedDaySource = 'aggregates' | 'empty' | 'probe' | 'aggregate' | 'failed'\n\nexport const closedDaySource = (args: {\n\tmarker: DayMarker | undefined\n\ttz: string\n\t/** The window the day is read over in `tz` (see `windowOf`). */\n\twindow: LocalDay\n\t/** Its count is fixed: part of what it counted is gone from the raw hits (see `isFixed`). */\n\tfixed: boolean\n}): ClosedDaySource => {\n\tconst { marker, tz, window, fixed } = args\n\tif (!marker) {\n\t\treturn 'probe'\n\t}\n\tif (marker.status === 'failed') {\n\t\treturn 'failed'\n\t}\n\t// The only numbers a fixed day has, whatever zone or version they were counted in.\n\tif (fixed) {\n\t\treturn marker.gen === null ? 'empty' : 'aggregates'\n\t}\n\tif (marker.status === 'empty') {\n\t\treturn countedOver(marker, tz, window) ? 'empty' : 'probe'\n\t}\n\treturn holdsWholeDay(marker, tz, window) ? 'aggregates' : 'aggregate'\n}\n\n/** Consecutive dates as runs `[first, last]`, in the order given, which must ascend. */\nexport const runsOf = (days: readonly CalendarDay[]): Array<[CalendarDay, CalendarDay]> => {\n\tconst runs: Array<[CalendarDay, CalendarDay]> = []\n\tfor (const day of days) {\n\t\tconst run = runs.at(-1)\n\t\tif (run && addCalendarDays(run[1], 1) === day) {\n\t\t\trun[1] = day\n\t\t} else {\n\t\t\truns.push([day, day])\n\t\t}\n\t}\n\treturn runs\n}\n\n/**\n * How many hits a closed day's marker says it holds, when its count is of the whole day: taken\n * once the day had ended. Null for a day never counted, or counted only while it ran.\n */\nexport const wholeDayHits = (marker: DayMarker | undefined, window: LocalDay): number | null =>\n\tmarker?.hits !== undefined &&\n\tmarker.hits !== null &&\n\tmarker.aggregatedAt !== null &&\n\tmarker.aggregatedAt >= window.end\n\t\t? marker.hits\n\t\t: null\n\n/**\n * A running day's snapshot: `fresh` within `maxAgeMs` of its count, `stale` past it (served while a\n * new one is taken), `none` without one counted the current way over its window.\n */\nexport const snapshotState = (\n\tmarker: DayMarker | undefined,\n\tat: { tz: string; window: LocalDay; now: Date; maxAgeMs: (hits: number) => number }\n): 'fresh' | 'stale' | 'none' => {\n\tconst { tz, window, now, maxAgeMs } = at\n\tif (\n\t\tmarker?.status !== 'partial' ||\n\t\tmarker.gen === null ||\n\t\tmarker.aggregatedAt === null ||\n\t\t!countedOver(marker, tz, window)\n\t) {\n\t\treturn 'none'\n\t}\n\treturn now.getTime() - marker.aggregatedAt.getTime() <= maxAgeMs(marker.hits ?? 0)\n\t\t? 'fresh'\n\t\t: 'stale'\n}\n\n/** The aggregate rows one part of a read sums: a dimension's, one value's, or a pair's. */\nexport interface AggregateTarget {\n\tdim: AggDim\n\tvalue?: string\n\tdim2?: AggDim\n}\n\n/**\n * The aggregate rows a read's totals, day series and breakdown use. `raw` when only raw hits can\n * answer its rows, with `total` still set when its filters alone have rows of their own.\n */\nexport type ReadShape =\n\t| {\n\t\t\tsource: 'aggregates'\n\t\t\ttotal: AggregateTarget\n\t\t\tbreakdown?: AggregateTarget & { groupBy: 'value' | 'value2' }\n\t }\n\t| { source: 'raw'; total?: AggregateTarget }\n\n/** The breakdowns a goal's pair rows are kept by. */\nconst GOAL_PAIRS: readonly RawDimension[] = ['channel', 'source', 'utmCampaign']\n\nconst RAW: ReadShape = { source: 'raw' }\n\n/**\n * Aggregates answer unfiltered reads, one `eq` filter, a goal by channel, source or campaign and an\n * item by page; the rest and hours read raw, a lone `eq` filter's totals still from its own rows.\n */\nexport const readShape = (args: {\n\tfilters: readonly AnalyticsFilter[]\n\tbreakdown?: RawDimension\n\thours: boolean\n}): ReadShape => {\n\tconst { filters, breakdown, hours } = args\n\tif (hours) {\n\t\treturn RAW\n\t}\n\tif (filters.length === 0) {\n\t\tif (breakdown === undefined) {\n\t\t\treturn { source: 'aggregates', total: { dim: '' } }\n\t\t}\n\t\treturn kindOf(breakdown, '')\n\t\t\t? {\n\t\t\t\t\tsource: 'aggregates',\n\t\t\t\t\ttotal: { dim: '' },\n\t\t\t\t\tbreakdown: { dim: breakdown, groupBy: 'value' },\n\t\t\t\t}\n\t\t\t: RAW\n\t}\n\tconst [only] = filters\n\tif (\n\t\tfilters.length !== 1 ||\n\t\t!only ||\n\t\tonly.operator !== 'eq' ||\n\t\tonly.value === '' ||\n\t\t!isRawDimension(only.dimension) ||\n\t\t!kindOf(only.dimension, '')\n\t) {\n\t\treturn RAW\n\t}\n\tconst total = { dim: only.dimension, value: only.value }\n\tif (breakdown === undefined) {\n\t\treturn { source: 'aggregates', total }\n\t}\n\tconst pair =\n\t\t(only.dimension === 'goal' && GOAL_PAIRS.includes(breakdown)) ||\n\t\t(only.dimension === 'item' && breakdown === 'page')\n\treturn pair && kindOf(only.dimension, breakdown)\n\t\t? { source: 'aggregates', total, breakdown: { ...total, dim2: breakdown, groupBy: 'value2' } }\n\t\t: { source: 'raw', total }\n}\n\n/** A value a read of daily totals asks about: one dimension's value, or a pair's first value. */\ntype FoldTarget = Pick<FoldRead, 'dim' | 'dim2' | 'value'>\n\n/**\n * The values a read of daily totals asks about where a cap can fold them: its one `eq` filter's\n * value, and a pair breakdown's first value. A day that folded one is read from its raw hits.\n */\nexport const foldTargets = (shape: ReadShape): FoldTarget[] => {\n\tif (shape.source !== 'aggregates') {\n\t\treturn []\n\t}\n\tconst targets: FoldTarget[] = []\n\tconst { total, breakdown } = shape\n\tif (total.value !== undefined && (kindOf(total.dim, '')?.cap ?? null) !== null) {\n\t\ttargets.push({ dim: total.dim, value: total.value })\n\t}\n\tif (\n\t\tbreakdown?.dim2 !== undefined &&\n\t\tbreakdown.value !== undefined &&\n\t\t(kindOf(breakdown.dim, breakdown.dim2)?.cap ?? null) !== null\n\t) {\n\t\ttargets.push({ dim: breakdown.dim, dim2: breakdown.dim2, value: breakdown.value })\n\t}\n\treturn targets\n}\n\nconst sameFilter = (a: AnalyticsFilter, b: AnalyticsFilter): boolean =>\n\ta.dimension === b.dimension && a.operator === b.operator && a.value === b.value\n\nconst equals = (dimension: DimensionKey, value: string | undefined): AnalyticsFilter[] =>\n\tvalue === undefined ? [] : [{ dimension, operator: 'eq', value }]\n\n/**\n * A read's filters with its page and its item folded in as `eq` filters, each filter once: a\n * read of one page or one item is a read filtered to it.\n */\nexport const readFilters = (\n\tquery: Pick<AnalyticsQuery, 'filters' | 'path' | 'item'>\n): AnalyticsFilter[] => {\n\tconst all = [\n\t\t...(query.filters ?? []),\n\t\t...equals('page', query.path),\n\t\t...equals('item', query.item),\n\t]\n\treturn all.filter(\n\t\t(filter, index) => all.findIndex((other) => sameFilter(filter, other)) === index\n\t)\n}\n\n/** What a read counts: goal completions, items seen and clicked, or hits. */\nexport const axisOf = (\n\tfilters: readonly AnalyticsFilter[],\n\tbreakdown: DimensionKey | undefined\n): RawAxis => {\n\tconst dimensions = [...filters.map((filter) => filter.dimension), breakdown]\n\treturn dimensions.includes('goal') ? 'goal' : dimensions.includes('item') ? 'item' : 'hit'\n}\n\nconst ORDER_PARTS: Record<MetricKey, Omit<RawOrder, 'dir'>> = {\n\tpageviews: { part: 'pageviews' },\n\tvisitors: { part: 'visitors' },\n\tvisits: { part: 'sessions' },\n\tsessions: { part: 'sessions' },\n\tbounceRate: { part: 'bounces', per: 'entries' },\n\tavgDuration: { part: 'durationMs', per: 'pageviews', less: 'pageviewsWithoutVisitor' },\n\tvisitDuration: { part: 'sessionMs', per: 'sessions' },\n\tscrollDepth: { part: 'scrollSum', per: 'scrollSamples' },\n\tevents: { part: 'events' },\n\tconversions: { part: 'conversions' },\n\trevenue: { part: 'revenue' },\n\timpressions: { part: 'impressions' },\n\tinteractions: { part: 'interactions' },\n}\n\n/**\n * How a breakdown ranks, as parts: an average by its own numerator over its own denominator. By\n * default items rank by how often they were seen, goals by completions, the rest by pageviews.\n */\nexport const orderOf = (query: Pick<AnalyticsQuery, 'order'>, axis: RawAxis): RawOrder => {\n\tconst metric =\n\t\tquery.order?.metric ??\n\t\t(axis === 'item' ? 'impressions' : axis === 'goal' ? 'conversions' : 'pageviews')\n\treturn { ...ORDER_PARTS[metric], dir: query.order?.direction ?? 'desc' }\n}\n\nconst VISIT_METRICS: ReadonlySet<MetricKey> = new Set([\n\t'visits',\n\t'sessions',\n\t'bounceRate',\n\t'visitDuration',\n])\n\n/** Whether a read counts visits, which a raw read only does when it cuts them. */\nexport const countsVisits = (query: Pick<AnalyticsQuery, 'metrics' | 'order'>): boolean =>\n\tquery.metrics.some((metric) => VISIT_METRICS.has(metric)) ||\n\t(query.order !== undefined && VISIT_METRICS.has(query.order.metric))\n\n/** The series a read asks for; a breakdown wins over a series. */\nexport type SeriesUnit = 'hour' | 'day' | 'week' | 'month'\n\nexport const seriesOf = (\n\tquery: Pick<AnalyticsQuery, 'granularity'>,\n\tbreakdown: DimensionKey | undefined\n): SeriesUnit | undefined => {\n\tconst unit = query.granularity\n\treturn breakdown === undefined &&\n\t\t(unit === 'hour' || unit === 'day' || unit === 'week' || unit === 'month')\n\t\t? unit\n\t\t: undefined\n}\n"],"mappings":";;;;;;;;;;AAYA,MAAa,eAAe,QAAmB,IAAY,WAC1D,OAAO,YAAA,KACP,OAAO,OAAO,MACd,YAAY,MAAM,EAAE,QAAQ,MAAM,OAAO,MAAM,QAAQ;;;;;AAMxD,MAAa,iBACZ,QACA,IACA,WACa;CACb,IAAI,CAAC,UAAU,OAAO,QAAQ,QAAQ,CAAC,YAAY,QAAQ,IAAI,MAAM,GACpE,OAAO;CAER,IAAI,OAAO,WAAW,iBAAiB,OAAO,WAAW,SACxD,OAAO;CAER,OACC,OAAO,WAAW,aAClB,OAAO,iBAAiB,QACxB,OAAO,aAAa,QAAQ,KAAK,OAAO,IAAI,QAAQ,IAAA;AAEtD;AAQA,MAAa,mBAAmB,SAOT;CACtB,MAAM,EAAE,QAAQ,IAAI,QAAQ,UAAU;CACtC,IAAI,CAAC,QACJ,OAAO;CAER,IAAI,OAAO,WAAW,UACrB,OAAO;CAGR,IAAI,OACH,OAAO,OAAO,QAAQ,OAAO,UAAU;CAExC,IAAI,OAAO,WAAW,SACrB,OAAO,YAAY,QAAQ,IAAI,MAAM,IAAI,UAAU;CAEpD,OAAO,cAAc,QAAQ,IAAI,MAAM,IAAI,eAAe;AAC3D;;AAGA,MAAa,UAAU,SAAoE;CAC1F,MAAM,OAA0C,CAAC;CACjD,KAAK,MAAM,OAAO,MAAM;EACvB,MAAM,MAAM,KAAK,GAAG,EAAE;EACtB,IAAI,OAAO,gBAAgB,IAAI,IAAI,CAAC,MAAM,KACzC,IAAI,KAAK;OAET,KAAK,KAAK,CAAC,KAAK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,GAAG,SAAS;CAC3E,OAAO,WAAW,SAAS,MAAM,IAAI,SAAS,WAAW,SAAS,MAAM,IAAI,SAAS;AACtF;AAEA,MAAM,cAAwD;CAC7D,WAAW,EAAE,MAAM,YAAY;CAC/B,UAAU,EAAE,MAAM,WAAW;CAC7B,QAAQ,EAAE,MAAM,WAAW;CAC3B,UAAU,EAAE,MAAM,WAAW;CAC7B,YAAY;EAAE,MAAM;EAAW,KAAK;CAAU;CAC9C,aAAa;EAAE,MAAM;EAAc,KAAK;EAAa,MAAM;CAA0B;CACrF,eAAe;EAAE,MAAM;EAAa,KAAK;CAAW;CACpD,aAAa;EAAE,MAAM;EAAa,KAAK;CAAgB;CACvD,QAAQ,EAAE,MAAM,SAAS;CACzB,aAAa,EAAE,MAAM,cAAc;CACnC,SAAS,EAAE,MAAM,UAAU;CAC3B,aAAa,EAAE,MAAM,cAAc;CACnC,cAAc,EAAE,MAAM,eAAe;AACtC;;;;;AAMA,MAAa,WAAW,OAAsC,SAA4B;CAIzF,OAAO;EAAE,GAAG,YAFX,MAAM,OAAO,WACZ,SAAS,SAAS,gBAAgB,SAAS,SAAS,gBAAgB;EACrC,KAAK,MAAM,OAAO,aAAa;CAAO;AACxE;AAEA,MAAM,gBAAwC,IAAI,IAAI;CACrD;CACA;CACA;CACA;AACD,CAAC;;AAGD,MAAa,gBAAgB,UAC5B,MAAM,QAAQ,MAAM,WAAW,cAAc,IAAI,MAAM,CAAC,KACvD,MAAM,UAAU,KAAA,KAAa,cAAc,IAAI,MAAM,MAAM,MAAM;AAKnE,MAAa,YACZ,OACA,cAC4B;CAC5B,MAAM,OAAO,MAAM;CACnB,OAAO,cAAc,KAAA,MACnB,SAAS,UAAU,SAAS,SAAS,SAAS,UAAU,SAAS,WAChE,OACA,KAAA;AACJ"}
|
|
@@ -10,7 +10,8 @@ import { isSelfReferral } from "../ingest/referrer.js";
|
|
|
10
10
|
const isLanding = (event) => event.type === "pageview" && event.navigation !== true && event.siteReferrer !== true && !isSelfReferral(event.referrer, event.hostname);
|
|
11
11
|
/**
|
|
12
12
|
* Where a visit came from: the referrer on a landing alone, tags and click id on any hit. A hit
|
|
13
|
-
* with no visitor claims no source unless it carries one: a webhook is not
|
|
13
|
+
* with no visitor claims no source unless it carries one, or is a page's landing: a webhook is not
|
|
14
|
+
* direct traffic, and an anonymous page further into a visit is not either.
|
|
14
15
|
*/
|
|
15
16
|
const acquisitionOf = (event, landing) => {
|
|
16
17
|
const ref = landing ? event.referrerHost : void 0;
|
|
@@ -21,7 +22,7 @@ const acquisitionOf = (event, landing) => {
|
|
|
21
22
|
event.utmContent,
|
|
22
23
|
event.utmTerm
|
|
23
24
|
].some((tag) => tag !== void 0);
|
|
24
|
-
if (event.visitorId === "" && !tagged) return {};
|
|
25
|
+
if (event.visitorId === "" && !tagged && (event.server === true || !landing)) return {};
|
|
25
26
|
return {
|
|
26
27
|
ref,
|
|
27
28
|
source: deriveSource({
|
|
@@ -64,6 +65,7 @@ const toHit = (event, context) => {
|
|
|
64
65
|
v: event.visitorId ?? "",
|
|
65
66
|
landing,
|
|
66
67
|
server: event.server === true,
|
|
68
|
+
mode: event.mode ?? "f",
|
|
67
69
|
durationMs: event.durationMs,
|
|
68
70
|
scrollDepth: event.scrollDepth
|
|
69
71
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toHit.js","names":[],"sources":["../../../src/native/engine/toHit.ts"],"sourcesContent":["import { MAX_ITEMS_PER_BEACON } from '../../goals/constants'\nimport type { NormalizedEvent } from '../ingest/normalizeEvent'\nimport { isSelfReferral } from '../ingest/referrer'\nimport { CHANNEL_TAXONOMY_VERSION, classifyChannel, deriveSource } from '../ingest/source'\nimport { orderHitId, type UlidFactory, ulid } from '../store/ids'\nimport type { HitRow } from '../store/types'\n\ninterface HitContext {\n\t/** When the beacon arrived, or the server call was made: where an engagement's reading ends. */\n\treceivedAt: Date\n\t/** Makes a hit's id out of its time; the process's own factory unless a test hands one in. */\n\tids?: UlidFactory\n}\n\n/**\n * Whether a hit opens a visit: a document-loading pageview no page of its scope's hostnames linked\n * to. Events, goals and engagements never do: their referrer is the one their page loaded with.\n */\nexport const isLanding = (\n\tevent: Pick<NormalizedEvent, 'type' | 'navigation' | 'referrer' | 'hostname' | 'siteReferrer'>\n): boolean =>\n\tevent.type === 'pageview' &&\n\tevent.navigation !== true &&\n\tevent.siteReferrer !== true &&\n\t!isSelfReferral(event.referrer, event.hostname)\n\n/**\n * Where a visit came from: the referrer on a landing alone, tags and click id on any hit. A hit\n * with no visitor claims no source unless it carries one: a webhook is not direct traffic.\n */\nconst acquisitionOf = (event: NormalizedEvent, landing: boolean): Partial<HitRow> => {\n\tconst ref = landing ? event.referrerHost : undefined\n\tconst tagged =\n\t\tref !== undefined ||\n\t\tevent.clickId !== undefined ||\n\t\t[event.utmSource, event.utmMedium, event.utmCampaign, event.utmContent, event.utmTerm].some(\n\t\t\t(tag) => tag !== undefined\n\t\t)\n\tif (event.visitorId === '' && !tagged) {\n\t\treturn {}\n\t}\n\treturn {\n\t\tref,\n\t\tsource: deriveSource({ referrerHost: ref, utmSource: event.utmSource }),\n\t\tchannel: classifyChannel({\n\t\t\treferrerHost: ref,\n\t\t\tutmSource: event.utmSource,\n\t\t\tutmMedium: event.utmMedium,\n\t\t\tclickId: event.clickId,\n\t\t}),\n\t\tchannelVersion: CHANNEL_TAXONOMY_VERSION,\n\t\tclickId: event.clickId,\n\t\tutmSource: event.utmSource,\n\t\tutmMedium: event.utmMedium,\n\t\tutmCampaign: event.utmCampaign,\n\t\tutmContent: event.utmContent,\n\t\tutmTerm: event.utmTerm,\n\t}\n}\n\n/** Drops the fields a row leaves out, so an absent one is missing rather than undefined. */\nconst withoutAbsent = (hit: HitRow): HitRow => {\n\tfor (const key of Object.keys(hit)) {\n\t\tif (Reflect.get(hit, key) === undefined) {\n\t\t\tReflect.deleteProperty(hit, key)\n\t\t}\n\t}\n\treturn hit\n}\n\n/**\n * The row a normalized event is stored as. An order's id is its dedupe key, so a retried order\n * meets its own primary key; an engagement keeps its pageview's `ts`, its arrival as `lastAt`.\n */\nexport const toHit = (event: NormalizedEvent, context: HitContext): HitRow => {\n\tconst landing = isLanding(event)\n\tconst hit: HitRow = {\n\t\tid:\n\t\t\tevent.dedupeKey === undefined\n\t\t\t\t? (context.ids ?? ulid)(event.timestamp)\n\t\t\t\t: orderHitId(event.dedupeKey),\n\t\tscope: event.scope ?? '',\n\t\tts: event.timestamp,\n\t\ttype: event.type,\n\t\thostname: event.hostname,\n\t\tpath: event.path,\n\t\tv: event.visitorId ?? '',\n\t\tlanding,\n\t\tserver: event.server === true,\n\t\tdurationMs: event.durationMs,\n\t\tscrollDepth: event.scrollDepth,\n\t}\n\tif (event.type === 'engagement') {\n\t\tconst seen = event.impressions?.slice(0, MAX_ITEMS_PER_BEACON) ?? []\n\t\treturn withoutAbsent({\n\t\t\t...hit,\n\t\t\tlastAt: new Date(Math.max(event.timestamp.getTime(), context.receivedAt.getTime())),\n\t\t\t...(seen.length > 0 ? { impressions: seen } : {}),\n\t\t})\n\t}\n\tconst goals = event.goals ?? []\n\tconst revenue = goals.reduce((sum, completion) => sum + completion.value, 0)\n\t// A pageview has no currency: a configured one is stamped on every event, money or not.\n\tconst money = event.value !== undefined || event.originalValue !== undefined || goals.length > 0\n\treturn withoutAbsent({\n\t\t...hit,\n\t\t...acquisitionOf(event, landing),\n\t\tname: event.name,\n\t\tcountry: event.country,\n\t\tregion: event.region,\n\t\tcity: event.city,\n\t\tlanguage: event.language,\n\t\tdevice: event.device,\n\t\tbrowser: event.browser,\n\t\tos: event.os,\n\t\tvalue: event.value,\n\t\toriginalValue: event.originalValue,\n\t\tcurrency: money ? event.currency : undefined,\n\t\toriginalCurrency: event.originalCurrency,\n\t\titem: event.item,\n\t\tprops: event.props,\n\t\t// A refund takes money back from a sale that still happened: revenue, and no conversion.\n\t\t...(goals.length > 0 ? { goals, conversions: event.refund ? 0 : goals.length, revenue } : {}),\n\t})\n}\n"],"mappings":";;;;;;;;;AAkBA,MAAa,aACZ,UAEA,MAAM,SAAS,cACf,MAAM,eAAe,QACrB,MAAM,iBAAiB,QACvB,CAAC,eAAe,MAAM,UAAU,MAAM,QAAQ
|
|
1
|
+
{"version":3,"file":"toHit.js","names":[],"sources":["../../../src/native/engine/toHit.ts"],"sourcesContent":["import { MAX_ITEMS_PER_BEACON } from '../../goals/constants'\nimport type { NormalizedEvent } from '../ingest/normalizeEvent'\nimport { isSelfReferral } from '../ingest/referrer'\nimport { CHANNEL_TAXONOMY_VERSION, classifyChannel, deriveSource } from '../ingest/source'\nimport { orderHitId, type UlidFactory, ulid } from '../store/ids'\nimport type { HitRow } from '../store/types'\n\ninterface HitContext {\n\t/** When the beacon arrived, or the server call was made: where an engagement's reading ends. */\n\treceivedAt: Date\n\t/** Makes a hit's id out of its time; the process's own factory unless a test hands one in. */\n\tids?: UlidFactory\n}\n\n/**\n * Whether a hit opens a visit: a document-loading pageview no page of its scope's hostnames linked\n * to. Events, goals and engagements never do: their referrer is the one their page loaded with.\n */\nexport const isLanding = (\n\tevent: Pick<NormalizedEvent, 'type' | 'navigation' | 'referrer' | 'hostname' | 'siteReferrer'>\n): boolean =>\n\tevent.type === 'pageview' &&\n\tevent.navigation !== true &&\n\tevent.siteReferrer !== true &&\n\t!isSelfReferral(event.referrer, event.hostname)\n\n/**\n * Where a visit came from: the referrer on a landing alone, tags and click id on any hit. A hit\n * with no visitor claims no source unless it carries one, or is a page's landing: a webhook is not\n * direct traffic, and an anonymous page further into a visit is not either.\n */\nconst acquisitionOf = (event: NormalizedEvent, landing: boolean): Partial<HitRow> => {\n\tconst ref = landing ? event.referrerHost : undefined\n\tconst tagged =\n\t\tref !== undefined ||\n\t\tevent.clickId !== undefined ||\n\t\t[event.utmSource, event.utmMedium, event.utmCampaign, event.utmContent, event.utmTerm].some(\n\t\t\t(tag) => tag !== undefined\n\t\t)\n\tif (event.visitorId === '' && !tagged && (event.server === true || !landing)) {\n\t\treturn {}\n\t}\n\treturn {\n\t\tref,\n\t\tsource: deriveSource({ referrerHost: ref, utmSource: event.utmSource }),\n\t\tchannel: classifyChannel({\n\t\t\treferrerHost: ref,\n\t\t\tutmSource: event.utmSource,\n\t\t\tutmMedium: event.utmMedium,\n\t\t\tclickId: event.clickId,\n\t\t}),\n\t\tchannelVersion: CHANNEL_TAXONOMY_VERSION,\n\t\tclickId: event.clickId,\n\t\tutmSource: event.utmSource,\n\t\tutmMedium: event.utmMedium,\n\t\tutmCampaign: event.utmCampaign,\n\t\tutmContent: event.utmContent,\n\t\tutmTerm: event.utmTerm,\n\t}\n}\n\n/** Drops the fields a row leaves out, so an absent one is missing rather than undefined. */\nconst withoutAbsent = (hit: HitRow): HitRow => {\n\tfor (const key of Object.keys(hit)) {\n\t\tif (Reflect.get(hit, key) === undefined) {\n\t\t\tReflect.deleteProperty(hit, key)\n\t\t}\n\t}\n\treturn hit\n}\n\n/**\n * The row a normalized event is stored as. An order's id is its dedupe key, so a retried order\n * meets its own primary key; an engagement keeps its pageview's `ts`, its arrival as `lastAt`.\n */\nexport const toHit = (event: NormalizedEvent, context: HitContext): HitRow => {\n\tconst landing = isLanding(event)\n\tconst hit: HitRow = {\n\t\tid:\n\t\t\tevent.dedupeKey === undefined\n\t\t\t\t? (context.ids ?? ulid)(event.timestamp)\n\t\t\t\t: orderHitId(event.dedupeKey),\n\t\tscope: event.scope ?? '',\n\t\tts: event.timestamp,\n\t\ttype: event.type,\n\t\thostname: event.hostname,\n\t\tpath: event.path,\n\t\tv: event.visitorId ?? '',\n\t\tlanding,\n\t\tserver: event.server === true,\n\t\tmode: event.mode ?? 'f',\n\t\tdurationMs: event.durationMs,\n\t\tscrollDepth: event.scrollDepth,\n\t}\n\tif (event.type === 'engagement') {\n\t\tconst seen = event.impressions?.slice(0, MAX_ITEMS_PER_BEACON) ?? []\n\t\treturn withoutAbsent({\n\t\t\t...hit,\n\t\t\tlastAt: new Date(Math.max(event.timestamp.getTime(), context.receivedAt.getTime())),\n\t\t\t...(seen.length > 0 ? { impressions: seen } : {}),\n\t\t})\n\t}\n\tconst goals = event.goals ?? []\n\tconst revenue = goals.reduce((sum, completion) => sum + completion.value, 0)\n\t// A pageview has no currency: a configured one is stamped on every event, money or not.\n\tconst money = event.value !== undefined || event.originalValue !== undefined || goals.length > 0\n\treturn withoutAbsent({\n\t\t...hit,\n\t\t...acquisitionOf(event, landing),\n\t\tname: event.name,\n\t\tcountry: event.country,\n\t\tregion: event.region,\n\t\tcity: event.city,\n\t\tlanguage: event.language,\n\t\tdevice: event.device,\n\t\tbrowser: event.browser,\n\t\tos: event.os,\n\t\tvalue: event.value,\n\t\toriginalValue: event.originalValue,\n\t\tcurrency: money ? event.currency : undefined,\n\t\toriginalCurrency: event.originalCurrency,\n\t\titem: event.item,\n\t\tprops: event.props,\n\t\t// A refund takes money back from a sale that still happened: revenue, and no conversion.\n\t\t...(goals.length > 0 ? { goals, conversions: event.refund ? 0 : goals.length, revenue } : {}),\n\t})\n}\n"],"mappings":";;;;;;;;;AAkBA,MAAa,aACZ,UAEA,MAAM,SAAS,cACf,MAAM,eAAe,QACrB,MAAM,iBAAiB,QACvB,CAAC,eAAe,MAAM,UAAU,MAAM,QAAQ;;;;;;AAO/C,MAAM,iBAAiB,OAAwB,YAAsC;CACpF,MAAM,MAAM,UAAU,MAAM,eAAe,KAAA;CAC3C,MAAM,SACL,QAAQ,KAAA,KACR,MAAM,YAAY,KAAA,KAClB;EAAC,MAAM;EAAW,MAAM;EAAW,MAAM;EAAa,MAAM;EAAY,MAAM;CAAO,EAAE,MACrF,QAAQ,QAAQ,KAAA,CAClB;CACD,IAAI,MAAM,cAAc,MAAM,CAAC,WAAW,MAAM,WAAW,QAAQ,CAAC,UACnE,OAAO,CAAC;CAET,OAAO;EACN;EACA,QAAQ,aAAa;GAAE,cAAc;GAAK,WAAW,MAAM;EAAU,CAAC;EACtE,SAAS,gBAAgB;GACxB,cAAc;GACd,WAAW,MAAM;GACjB,WAAW,MAAM;GACjB,SAAS,MAAM;EAChB,CAA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OAAO;EAC/B,MAAM,MAAM;EACZ,SAAS,MAAM;EACf,QAAQ,MAAM;EACd,MAAM,MAAM;EACZ,UAAU,MAAM;EAChB,QAAQ,MAAM;EACd,SAAS,MAAM;EACf,IAAI,MAAM;EACV,OAAO,MAAM;EACb,eAAe,MAAM;EACrB,UAAU,QAAQ,MAAM,WAAW,KAAA;EACnC,kBAAkB,MAAM;EACxB,MAAM,MAAM;EACZ,OAAO,MAAM;EAEb,GAAI,MAAM,SAAS,IAAI;GAAE;GAAO,aAAa,MAAM,SAAS,IAAI,MAAM;GAAQ;EAAQ,IAAI,CAAC;CAC5F,CAAC;AACF"}
|
|
@@ -115,6 +115,8 @@ const makeIngestHandler = ({ geoResolver, target, resolvers = {}, attribution =
|
|
|
115
115
|
if (raw === void 0) return invalidField("type");
|
|
116
116
|
const param = rawEventError(raw);
|
|
117
117
|
if (param !== void 0) return invalidField(param);
|
|
118
|
+
const mode = raw.m ?? "f";
|
|
119
|
+
if (mode === "a" && raw.type === "engagement") return accepted();
|
|
118
120
|
const receivedAt = currentDate();
|
|
119
121
|
const at = eventTime(raw, receivedAt);
|
|
120
122
|
const visitor = captureRequest(req);
|
|
@@ -134,7 +136,7 @@ const makeIngestHandler = ({ geoResolver, target, resolvers = {}, attribution =
|
|
|
134
136
|
const { salts, writer, items, nudge, siteHosts, engine, orders, keepAlive } = target(req.payload);
|
|
135
137
|
const order = wireOrderId(raw.orderId, scope ?? "", orders);
|
|
136
138
|
if (!order.ok) return drop(req, "order refused");
|
|
137
|
-
const salt = await saltForHit(salts, {
|
|
139
|
+
const salt = mode === "a" ? "" : await saltForHit(salts, {
|
|
138
140
|
scope: scope ?? "",
|
|
139
141
|
at,
|
|
140
142
|
zone: () => resolveTimezone ? resolveTimezone(visitor, scope ?? null) : "UTC"
|
|
@@ -168,7 +170,8 @@ const makeIngestHandler = ({ geoResolver, target, resolvers = {}, attribution =
|
|
|
168
170
|
...trust,
|
|
169
171
|
...items ? { items } : {},
|
|
170
172
|
...siblings ? { siblings } : {},
|
|
171
|
-
orderKey: orderDigestKey(req.payload?.secret)
|
|
173
|
+
orderKey: orderDigestKey(req.payload?.secret),
|
|
174
|
+
mode
|
|
172
175
|
});
|
|
173
176
|
if (event.type === "engagement" && !event.durationMs && !event.scrollDepth && !event.impressions?.length) return accepted();
|
|
174
177
|
const hit = toHit(event, { receivedAt });
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"endpoint.js","names":[],"sources":["../../../src/native/ingest/endpoint.ts"],"sourcesContent":["import type { Payload, PayloadHandler, PayloadRequest } from 'payload'\nimport { captureRequest } from '../../capture/captureRequest'\nimport { readCappedBody } from '../../capture/requestBody'\nimport { refusedCrossSite, warnUntrustedForwarding } from '../../capture/requestTrust'\nimport { refusedSameSite } from '../../capture/sameSite'\nimport { currentDate } from '../../core/clock'\nimport { monotonicMs } from '../../core/monotonic'\nimport type { CurrencyConverter } from '../../goals/currency'\nimport { type BrowserAmounts, DEFAULT_BROWSER_AMOUNTS } from '../../goals/match'\nimport type { Goal } from '../../goals/types'\nimport type { TrustedPlatform } from '../../net/platform'\nimport { analyticsError, errorResponse } from '../../plugin/errors'\nimport { DEFAULT_TIMEZONE } from '../../timeframe/tz'\nimport type { NativeEngine } from '../engine/engine'\nimport { type DailySalts, saltForHit } from '../engine/salt'\nimport type { SiteHosts } from '../engine/siteHosts'\nimport { toHit } from '../engine/toHit'\nimport type { HitWriter } from '../engine/writeBuffer'\nimport type { GeoResolver } from '../geo/geoResolver'\nimport { serverTwinOf } from '../store/ids'\nimport type { AppendResult } from '../store/types'\nimport { type BotFilter, isBot } from './bots'\nimport {\n\teventTime,\n\ttype ItemIngestOptions,\n\tnormalizeEvent,\n\ttype RawEventInput,\n} from './normalizeEvent'\nimport { type OrderSignatureCheck, wireOrderId } from './orderSignature'\nimport { referrerHost } from './referrer'\nimport { type ResolvedHostnameOption, resolveEventHostname } from './resolveHostname'\nimport { orderDigestKey, warnUnconvertedOnce } from './revenue'\nimport { type RawEventField, rawEventError } from './validate'\n\nexport interface IngestResolvers {\n\t/** `scope.resolve` at the `capture` call site. */\n\tscope?: (req: PayloadRequest) => Promise<string | null>\n\t/**\n\t * Reporting timezone, which picks the day whose salt hashes the visitor. Throws rather than\n\t * answer UTC when the host resolver fails; the scope's last good zone then stands in.\n\t */\n\ttimezone?: (req: PayloadRequest, scope?: string | null) => Promise<string>\n\tcurrency?: (req: PayloadRequest, scope?: string | null) => Promise<string | undefined>\n\tconvertCurrency?: CurrencyConverter\n\tgoals?: (req: PayloadRequest, scope?: string | null) => Promise<Goal[]>\n\t/**\n\t * Whether the scope's provider documents grant it the native engine; answered from the registry\n\t * cache, so it costs no query per beacon.\n\t */\n\tgranted?: (req: PayloadRequest, scope: string | null) => Promise<boolean>\n}\n\n/** How the handler reads the request behind an event, as opposed to what the body claims. */\nexport interface IngestAttribution {\n\ttrustedProxyHops?: number\n\t/** The platform whose edge headers carry the visitor's address and location. */\n\ttrustedPlatform?: TrustedPlatform\n\t/**\n\t * `capture.allowedOrigins`, normalized: the other sites whose pages may send beacons here.\n\t * Absent, every beacon a browser marks `Sec-Fetch-Site: cross-site` is dropped.\n\t */\n\tallowedOrigins?: ReadonlySet<string>\n\t/** Resolved `hostname` option; absent attributes the event to the request's own host. */\n\thostname?: ResolvedHostnameOption\n\t/** Hostnames that ingest on a scoped install even when the request resolves no scope. */\n\tplatformHostnames?: ReadonlySet<string>\n}\n\n/** What one Payload instance stores a beacon's hit through. */\nexport interface IngestTarget {\n\tsalts: Pick<DailySalts, 'forHit' | 'forDay'>\n\twriter: Pick<HitWriter, 'write'>\n\t/** The collections bound as items; absent, every item ref a beacon carries is dropped. */\n\titems?: ItemIngestOptions\n\t/** Asks for maintenance in the background after a hit; it never waits for it. */\n\tnudge?: () => void\n\t/** The hostnames each scope records hits under; absent, only the event's own is the site's. */\n\tsiteHosts?: SiteHosts\n\t/**\n\t * Told when a browser record of an order is stored, so a closed day holding the server's record\n\t * of that sale is counted again: the sale now counts on the browser's record.\n\t */\n\tengine?: Pick<NativeEngine, 'orderStored'>\n\t/** How an order id a page sends is checked; absent, no signature can be. */\n\torders?: OrderSignatureCheck\n\t/** Keeps work running past the response that started it (Next's `after()`). */\n\tkeepAlive?: (work: Promise<unknown>) => void\n}\n\n/**\n * Dropped beacons answer exactly like accepted ones, with nothing logged per event, so the endpoint\n * never tells a prober which hosts or tenants exist.\n */\nconst accepted = (): Response => Response.json({ ok: true }, { status: 202 })\n\nconst REQUEST_HOSTNAME: ResolvedHostnameOption = { kind: 'request' }\n\nconst NO_ORIGINS: ReadonlySet<string> = new Set()\n\n/**\n * Logged once each: a misconfigured resolver, broken filter or header-stripping proxy otherwise\n * costs an install its traffic in silence, since the response cannot say so.\n */\nconst WARN_REASONS = {\n\t'no scope resolved':\n\t\t'analytics: ingest is dropping events because scope.resolve answered no scope for them. Check that resolver, and list any platform domain in platformHostnames so it ingests under the null scope. Logged once per process.',\n\t'no native provider':\n\t\t'analytics: ingest is dropping events for a scope with no enabled native provider document. Add one for that tenant to store its visits here. Logged once per process.',\n\t'hostname refused':\n\t\t'analytics: ingest is dropping events because the hostname option refused the request host. Check that option, and platformHostnames for a platform domain. Logged once per process.',\n\t'bot filter threw':\n\t\t'analytics: the filterBots function threw, so the event was kept. Check that function; every beacon counts while it throws. Logged once per process.',\n\t'no user agent':\n\t\t'analytics: a beacon arrived with no user agent and was not recorded: an intermediary may be stripping the header. Logged once per process.',\n\t'cross-site':\n\t\t\"analytics: ingest is dropping beacons that a page on another site made a visitor's browser send (Sec-Fetch-Site: cross-site). If a site on another domain should send its visits here, list its origin in capture.allowedOrigins. Logged once per process.\",\n\t'other scope':\n\t\t\"analytics: ingest is dropping beacons that a page on a sibling subdomain sent (Sec-Fetch-Site: same-site) whose Origin scope.resolve maps to another scope than the request's. Each tenant's pages post to their own host. Logged once per process.\",\n\t'timezone unresolved':\n\t\t'analytics: ingest hashes visitors by the UTC day for a scope whose reportingTimezone has never resolved: the resolver throws or answers something that is not an IANA timezone. Check that resolver. Logged once per process.',\n\t'order recount failed':\n\t\t\"analytics: a browser's record of an order was stored, but the closed day holding the server's record of it could not be marked to be counted again, so that day keeps the sale until it is rebuilt. Logged once per process.\",\n\t'order refused':\n\t\t'analytics: ingest is dropping sales whose orderId is empty, too long, holds a control character, or lacks the signature signOrderId writes while signatures are required. Logged once per process.',\n} as const\n\ntype WarnReason = keyof typeof WARN_REASONS\n\n/** One line about failed beacons at most this often: a public path must not be a log amplifier. */\nconst FAILED_LOG_MS = 60_000\n\nexport type BeaconFailureLog = (payload: Payload, err: unknown) => void\n\n/** Counts beacons that could not be `what` and logs them in one line at most once a minute. */\nexport const beaconFailureLog = (what: 'stored' | 'taken'): BeaconFailureLog => {\n\tlet failed = 0\n\tlet loggedAt: number | null = null\n\treturn (payload, err) => {\n\t\tfailed += 1\n\t\tconst at = monotonicMs()\n\t\tif (loggedAt !== null && at - loggedAt < FAILED_LOG_MS) {\n\t\t\treturn\n\t\t}\n\t\tloggedAt = at\n\t\tpayload.logger?.error({\n\t\t\terr,\n\t\t\tmsg: `analytics: ${failed} beacons could not be ${what}. Logged at most once a minute.`,\n\t\t})\n\t\tfailed = 0\n\t}\n}\n\n/** One event, not a session replay: far above any legitimate payload, far below a DoS. */\nconst MAX_INGEST_BODY_BYTES = 64 * 1024\n\n/**\n * Undefined for anything unparseable, so a garbage POST is a 400 rather than a thrown error that\n * escapes into Payload's routeError as a logged 500.\n */\nconst parseBody = (bytes: ArrayBuffer): RawEventInput | undefined => {\n\ttry {\n\t\tconst parsed: unknown = JSON.parse(new TextDecoder().decode(bytes))\n\t\t// Unchecked until `rawEventError` runs; every field it skips is sanitized as unknown later.\n\t\treturn typeof parsed === 'object' && parsed !== null ? (parsed as RawEventInput) : undefined\n\t} catch {\n\t\treturn undefined\n\t}\n}\n\n/**\n * Naming the failed field saves a tracker author guessing. A non-object body fails the same check\n * a typeless event does, so it names `type`.\n */\nconst invalidField = (param: RawEventField): Response =>\n\terrorResponse(\n\t\t400,\n\t\tanalyticsError('invalid_param', `analytics: ${param} is missing or invalid`, param)\n\t)\n\nexport interface IngestHandlerOptions {\n\tgeoResolver: GeoResolver\n\t/** The salts, writer and items of the instance a beacon reached. */\n\ttarget: (payload: Payload) => IngestTarget\n\tresolvers?: IngestResolvers\n\tattribution?: IngestAttribution\n\t/** Which agents never reach the pipeline. Defaults to the built-in `isBot`. */\n\tfilterBots?: BotFilter\n\t/** `revenue.browserAmounts`: how far a beacon's amount is believed. Default `'fixed'`. */\n\tbrowserAmounts?: BrowserAmounts\n}\n\n/**\n * Native beacon endpoint: cheap refusals first, then everything resolved as a capture call site\n * sees the request. The only read on the public path is the day's salt, once a day per process.\n */\nexport const makeIngestHandler = ({\n\tgeoResolver,\n\ttarget,\n\tresolvers = {},\n\tattribution = {},\n\tfilterBots = isBot,\n\tbrowserAmounts = DEFAULT_BROWSER_AMOUNTS,\n}: IngestHandlerOptions): PayloadHandler => {\n\t// One line per reason for the life of the handler, which is the life of the process: a\n\t// public endpoint must not be a log amplifier, and an attacker chooses how often it drops.\n\tconst warned = new Set<WarnReason>()\n\tconst warnOnce = (req: PayloadRequest, reason: WarnReason): void => {\n\t\tif (!warned.has(reason)) {\n\t\t\twarned.add(reason)\n\t\t\treq.payload.logger?.warn(WARN_REASONS[reason])\n\t\t}\n\t}\n\tconst drop = (req: PayloadRequest, reason: WarnReason): Response => {\n\t\twarnOnce(req, reason)\n\t\treturn accepted()\n\t}\n\tconst writeFailed = beaconFailureLog('stored')\n\tconst takeFailed = beaconFailureLog('taken')\n\n\tconst take = async (req: PayloadRequest): Promise<Response> => {\n\t\t// Before the body is read, so a crawler costs one header lookup; answered like a kept beacon\n\t\t// and never logged, since bots are expected traffic.\n\t\tconst userAgent = req.headers.get('user-agent') ?? ''\n\t\tlet automated = false\n\t\ttry {\n\t\t\tautomated = filterBots(userAgent)\n\t\t} catch {\n\t\t\twarnOnce(req, 'bot filter threw')\n\t\t}\n\t\tif (automated) {\n\t\t\t// The exception to the silence: an agent-less beacon is indistinguishable from an\n\t\t\t// intermediary stripping the header, which would zero an install's numbers.\n\t\t\tif (userAgent.trim() === '') {\n\t\t\t\twarnOnce(req, 'no user agent')\n\t\t\t}\n\t\t\treturn accepted()\n\t\t}\n\t\t// Another site's page cannot make its visitors count here: the browser says where the\n\t\t// request came from, and only a listed origin may send from elsewhere.\n\t\tif (refusedCrossSite(req.headers, attribution.allowedOrigins ?? NO_ORIGINS)) {\n\t\t\treturn drop(req, 'cross-site')\n\t\t}\n\t\tconst {\n\t\t\tscope: resolveScope,\n\t\t\ttimezone: resolveTimezone,\n\t\t\tcurrency: resolveCurrency,\n\t\t\tgoals: resolveGoals,\n\t\t} = resolvers\n\t\t// A public path: the body is capped before it is buffered, and one that dies in transit (an\n\t\t// unloading tab's beacon) answers 400 rather than throwing.\n\t\tconst read = await readCappedBody(req, MAX_INGEST_BODY_BYTES)\n\t\tif (!read.ok) {\n\t\t\treturn read.reason === 'too-large'\n\t\t\t\t? errorResponse(\n\t\t\t\t\t\t413,\n\t\t\t\t\t\tanalyticsError('payload_too_large', 'analytics: the event body is too large')\n\t\t\t\t\t)\n\t\t\t\t: errorResponse(\n\t\t\t\t\t\t400,\n\t\t\t\t\t\tanalyticsError('invalid_param', 'analytics: the event body could not be read')\n\t\t\t\t\t)\n\t\t}\n\t\tconst raw = parseBody(read.body)\n\t\tif (raw === undefined) {\n\t\t\treturn invalidField('type')\n\t\t}\n\t\tconst param = rawEventError(raw)\n\t\tif (param !== undefined) {\n\t\t\treturn invalidField(param)\n\t\t}\n\t\tconst receivedAt = currentDate()\n\t\tconst at = eventTime(raw, receivedAt)\n\t\t// The host's resolvers are asked about the visit, never the signed-in editor whose session\n\t\t// a beacon carries: the tenant is the site it landed on, not the admin's selection.\n\t\tconst visitor = captureRequest(req)\n\t\tconst resolvedScope = resolveScope ? await resolveScope(visitor) : null\n\t\tconst scope = resolveScope ? (resolvedScope ?? '') : undefined\n\t\tconst hostname = await resolveEventHostname({\n\t\t\toption: attribution.hostname ?? REQUEST_HOSTNAME,\n\t\t\tclaimed: raw.hostname,\n\t\t\treq: visitor,\n\t\t\tscope: resolvedScope,\n\t\t\ttrustedProxyHops: attribution.trustedProxyHops,\n\t\t})\n\t\tif (hostname === null) {\n\t\t\treturn drop(req, 'hostname refused')\n\t\t}\n\t\t// A scoped install keeps only what a scope answers for. Its platform hostnames are\n\t\t// infrastructure rather than tenants, so they ingest under the null scope.\n\t\tconst unscoped =\n\t\t\tresolveScope !== undefined &&\n\t\t\tresolvedScope === null &&\n\t\t\t!(attribution.platformHostnames?.has(hostname) ?? false)\n\t\tif (unscoped) {\n\t\t\treturn drop(req, 'no scope resolved')\n\t\t}\n\t\t// A sibling subdomain is another tenant's site: its pages cannot count into this one.\n\t\tif (\n\t\t\tresolveScope &&\n\t\t\t(await refusedSameSite(visitor, () => Promise.resolve(resolvedScope), resolveScope))\n\t\t) {\n\t\t\treturn drop(req, 'other scope')\n\t\t}\n\t\t// A tenant stores visits only while it says so, checked after the hostname and scope rules\n\t\t// and answered 202 either way, so a forged host cannot probe which tenants exist.\n\t\tif (resolvers.granted && !(await resolvers.granted(visitor, resolvedScope))) {\n\t\t\treturn drop(req, 'no native provider')\n\t\t}\n\t\tconst { salts, writer, items, nudge, siteHosts, engine, orders, keepAlive } = target(\n\t\t\treq.payload\n\t\t)\n\t\t// An order id no key can be stored under is dropped rather than stored as a sale without one,\n\t\t// and answered 202 like any drop: a 400 here would tell a prober the tenant takes beacons.\n\t\tconst order = wireOrderId(raw.orderId, scope ?? '', orders)\n\t\tif (!order.ok) {\n\t\t\treturn drop(req, 'order refused')\n\t\t}\n\t\tconst salt = await saltForHit(\n\t\t\tsalts,\n\t\t\t{\n\t\t\t\tscope: scope ?? '',\n\t\t\t\tat,\n\t\t\t\tzone: () => (resolveTimezone ? resolveTimezone(visitor, scope ?? null) : DEFAULT_TIMEZONE),\n\t\t\t},\n\t\t\t() => warnOnce(req, 'timezone unresolved')\n\t\t)\n\t\t// Goals resolve under the same scope the event is stamped with, so a tenant's event\n\t\t// can only ever complete that tenant's goals. An engagement completes none.\n\t\tconst goals =\n\t\t\tresolveGoals && raw.type !== 'engagement'\n\t\t\t\t? await resolveGoals(visitor, scope ?? null)\n\t\t\t\t: undefined\n\t\tconst reportingCurrency = resolveCurrency\n\t\t\t? await resolveCurrency(visitor, scope ?? null)\n\t\t\t: undefined\n\t\tconst trust = {\n\t\t\ttrustedProxyHops: attribution.trustedProxyHops,\n\t\t\ttrustedPlatform: attribution.trustedPlatform,\n\t\t}\n\t\twarnUntrustedForwarding(req.payload, req.headers, trust)\n\t\t// Only a pageview from another host needs the scope's hostnames: is it a sibling's page?\n\t\tconst siblings =\n\t\t\tsiteHosts && raw.type === 'pageview' && referrerHost(raw.referrer, hostname) !== undefined\n\t\t\t\t? await siteHosts.of(scope ?? '')\n\t\t\t\t: undefined\n\t\tconst event = await normalizeEvent({\n\t\t\traw: { ...raw, orderId: order.orderId },\n\t\t\thostname,\n\t\t\theaders: req.headers,\n\t\t\tgeoResolver,\n\t\t\twarnGeo: (message) => req.payload.logger?.warn(message),\n\t\t\tsalt,\n\t\t\tnow: at,\n\t\t\tscope,\n\t\t\tgoals,\n\t\t\torigin: 'wire',\n\t\t\tbrowserAmounts,\n\t\t\treportingCurrency,\n\t\t\tconvertCurrency: resolvers.convertCurrency,\n\t\t\twarnUnconverted: () => warnUnconvertedOnce(req.payload.logger),\n\t\t\t...trust,\n\t\t\t...(items ? { items } : {}),\n\t\t\t...(siblings ? { siblings } : {}),\n\t\t\torderKey: orderDigestKey(req.payload?.secret),\n\t\t})\n\t\t// Nothing left to add once sanitized: no time, no scroll and no item seen moves no number.\n\t\tif (\n\t\t\tevent.type === 'engagement' &&\n\t\t\t!event.durationMs &&\n\t\t\t!event.scrollDepth &&\n\t\t\t!event.impressions?.length\n\t\t) {\n\t\t\treturn accepted()\n\t\t}\n\t\t// A retried order meets its own primary key and is refused there; nothing is read first,\n\t\t// since a read per beacon on a public path is a query anyone can repeat at will.\n\t\tconst hit = toHit(event, { receivedAt })\n\t\tlet stored: AppendResult | undefined\n\t\ttry {\n\t\t\tstored = await writer.write(hit)\n\t\t\tsiteHosts?.saw(scope ?? '', hostname)\n\t\t} catch (err) {\n\t\t\twriteFailed(req.payload, err)\n\t\t}\n\t\t// For an order the write just stored, never a repeat of one: one primary key lookup of the\n\t\t// server's record, past the response. Maintenance sweeps the orders again in case it is lost.\n\t\tif (engine && (stored?.written ?? 0) > 0 && serverTwinOf(hit.id) !== undefined) {\n\t\t\tconst probe = engine\n\t\t\t\t.orderStored(scope ?? '', hit.id)\n\t\t\t\t.catch(() => warnOnce(req, 'order recount failed'))\n\t\t\tkeepAlive?.(probe)\n\t\t}\n\t\tnudge?.()\n\t\treturn accepted()\n\t}\n\n\treturn async (req) => {\n\t\ttry {\n\t\t\treturn await take(req)\n\t\t} catch (err) {\n\t\t\t// A host resolver or a store failing must not reach Payload's routeError as a logged 500\n\t\t\t// per beacon: a public path answers its own envelope and logs at most once a minute.\n\t\t\ttakeFailed(req.payload, err)\n\t\t\treturn errorResponse(500, analyticsError('internal', 'analytics: the beacon failed'))\n\t\t}\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AA6FA,MAAM,iBAA2B,SAAS,KAAK,EAAE,IAAI,KAAK,GAAG,EAAE,QAAQ,IAAI,CAAC;AAE5E,MAAM,mBAA2C,EAAE,MAAM,UAAU;AAEnE,MAAM,6BAAkC,IAAI,IAAI;;;;;AAMhD,MAAM,eAAe;CACpB,qBACC;CACD,sBACC;CACD,oBACC;CACD,oBACC;CACD,iBACC;CACD,cACC;CACD,eACC;CACD,uBACC;CACD,wBACC;CACD,iBACC;AACF;;AAKA,MAAM,gBAAgB;;AAKtB,MAAa,oBAAoB,SAA+C;CAC/E,IAAI,SAAS;CACb,IAAI,WAA0B;CAC9B,QAAQ,SAAS,QAAQ;EACxB,UAAU;EACV,MAAM,KAAK,YAAY;EACvB,IAAI,aAAa,QAAQ,KAAK,WAAW,eACxC;EAED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B,WAAW,IAAI,SAAS,IAAI,QAAQ;EACrC,SAAS,KAAK;GACb,YAAY,IAAI,SAAS,GAAG;EAC7B;EAGA,IAAI,WAAW,QAAQ,WAAW,KAAK,KAAK,aAAa,IAAI,EAAE,MAAM,KAAA,GAAW;GAC/E,MAAM,QAAQ,OACZ,YAAY,SAAS,IAAI,IAAI,EAAE,EAC/B,YAAY,SAAS,KAAK,sBAAsB,CAAC;GACnD,YAAY,KAAK;EAClB;EACA,QAAQ;EACR,OAAO,SAAS;CACjB;CAEA,OAAO,OAAO,QAAQ;EACrB,IAAI;GACH,OAAO,MAAM,KAAK,GAAG;EACtB,SAAS,KAAK;GAGb,WAAW,IAAI,SAAS,GAAG;GAC3B,OAAO,cAAc,KAAK,eAAe,YAAY,8BAA8B,CAAC;EACrF;CACD;AACD"}
|
|
1
|
+
{"version":3,"file":"endpoint.js","names":[],"sources":["../../../src/native/ingest/endpoint.ts"],"sourcesContent":["import type { Payload, PayloadHandler, PayloadRequest } from 'payload'\nimport { captureRequest } from '../../capture/captureRequest'\nimport { readCappedBody } from '../../capture/requestBody'\nimport { refusedCrossSite, warnUntrustedForwarding } from '../../capture/requestTrust'\nimport { refusedSameSite } from '../../capture/sameSite'\nimport { currentDate } from '../../core/clock'\nimport { monotonicMs } from '../../core/monotonic'\nimport type { CurrencyConverter } from '../../goals/currency'\nimport { type BrowserAmounts, DEFAULT_BROWSER_AMOUNTS } from '../../goals/match'\nimport type { Goal } from '../../goals/types'\nimport type { TrustedPlatform } from '../../net/platform'\nimport { analyticsError, errorResponse } from '../../plugin/errors'\nimport { DEFAULT_TIMEZONE } from '../../timeframe/tz'\nimport type { NativeEngine } from '../engine/engine'\nimport { type DailySalts, saltForHit } from '../engine/salt'\nimport type { SiteHosts } from '../engine/siteHosts'\nimport { toHit } from '../engine/toHit'\nimport type { HitWriter } from '../engine/writeBuffer'\nimport type { GeoResolver } from '../geo/geoResolver'\nimport { serverTwinOf } from '../store/ids'\nimport type { AppendResult } from '../store/types'\nimport { type BotFilter, isBot } from './bots'\nimport {\n\teventTime,\n\ttype ItemIngestOptions,\n\tnormalizeEvent,\n\ttype RawEventInput,\n} from './normalizeEvent'\nimport { type OrderSignatureCheck, wireOrderId } from './orderSignature'\nimport { referrerHost } from './referrer'\nimport { type ResolvedHostnameOption, resolveEventHostname } from './resolveHostname'\nimport { orderDigestKey, warnUnconvertedOnce } from './revenue'\nimport { type RawEventField, rawEventError } from './validate'\n\nexport interface IngestResolvers {\n\t/** `scope.resolve` at the `capture` call site. */\n\tscope?: (req: PayloadRequest) => Promise<string | null>\n\t/**\n\t * Reporting timezone, which picks the day whose salt hashes the visitor. Throws rather than\n\t * answer UTC when the host resolver fails; the scope's last good zone then stands in.\n\t */\n\ttimezone?: (req: PayloadRequest, scope?: string | null) => Promise<string>\n\tcurrency?: (req: PayloadRequest, scope?: string | null) => Promise<string | undefined>\n\tconvertCurrency?: CurrencyConverter\n\tgoals?: (req: PayloadRequest, scope?: string | null) => Promise<Goal[]>\n\t/**\n\t * Whether the scope's provider documents grant it the native engine; answered from the registry\n\t * cache, so it costs no query per beacon.\n\t */\n\tgranted?: (req: PayloadRequest, scope: string | null) => Promise<boolean>\n}\n\n/** How the handler reads the request behind an event, as opposed to what the body claims. */\nexport interface IngestAttribution {\n\ttrustedProxyHops?: number\n\t/** The platform whose edge headers carry the visitor's address and location. */\n\ttrustedPlatform?: TrustedPlatform\n\t/**\n\t * `capture.allowedOrigins`, normalized: the other sites whose pages may send beacons here.\n\t * Absent, every beacon a browser marks `Sec-Fetch-Site: cross-site` is dropped.\n\t */\n\tallowedOrigins?: ReadonlySet<string>\n\t/** Resolved `hostname` option; absent attributes the event to the request's own host. */\n\thostname?: ResolvedHostnameOption\n\t/** Hostnames that ingest on a scoped install even when the request resolves no scope. */\n\tplatformHostnames?: ReadonlySet<string>\n}\n\n/** What one Payload instance stores a beacon's hit through. */\nexport interface IngestTarget {\n\tsalts: Pick<DailySalts, 'forHit' | 'forDay'>\n\twriter: Pick<HitWriter, 'write'>\n\t/** The collections bound as items; absent, every item ref a beacon carries is dropped. */\n\titems?: ItemIngestOptions\n\t/** Asks for maintenance in the background after a hit; it never waits for it. */\n\tnudge?: () => void\n\t/** The hostnames each scope records hits under; absent, only the event's own is the site's. */\n\tsiteHosts?: SiteHosts\n\t/**\n\t * Told when a browser record of an order is stored, so a closed day holding the server's record\n\t * of that sale is counted again: the sale now counts on the browser's record.\n\t */\n\tengine?: Pick<NativeEngine, 'orderStored'>\n\t/** How an order id a page sends is checked; absent, no signature can be. */\n\torders?: OrderSignatureCheck\n\t/** Keeps work running past the response that started it (Next's `after()`). */\n\tkeepAlive?: (work: Promise<unknown>) => void\n}\n\n/**\n * Dropped beacons answer exactly like accepted ones, with nothing logged per event, so the endpoint\n * never tells a prober which hosts or tenants exist.\n */\nconst accepted = (): Response => Response.json({ ok: true }, { status: 202 })\n\nconst REQUEST_HOSTNAME: ResolvedHostnameOption = { kind: 'request' }\n\nconst NO_ORIGINS: ReadonlySet<string> = new Set()\n\n/**\n * Logged once each: a misconfigured resolver, broken filter or header-stripping proxy otherwise\n * costs an install its traffic in silence, since the response cannot say so.\n */\nconst WARN_REASONS = {\n\t'no scope resolved':\n\t\t'analytics: ingest is dropping events because scope.resolve answered no scope for them. Check that resolver, and list any platform domain in platformHostnames so it ingests under the null scope. Logged once per process.',\n\t'no native provider':\n\t\t'analytics: ingest is dropping events for a scope with no enabled native provider document. Add one for that tenant to store its visits here. Logged once per process.',\n\t'hostname refused':\n\t\t'analytics: ingest is dropping events because the hostname option refused the request host. Check that option, and platformHostnames for a platform domain. Logged once per process.',\n\t'bot filter threw':\n\t\t'analytics: the filterBots function threw, so the event was kept. Check that function; every beacon counts while it throws. Logged once per process.',\n\t'no user agent':\n\t\t'analytics: a beacon arrived with no user agent and was not recorded: an intermediary may be stripping the header. Logged once per process.',\n\t'cross-site':\n\t\t\"analytics: ingest is dropping beacons that a page on another site made a visitor's browser send (Sec-Fetch-Site: cross-site). If a site on another domain should send its visits here, list its origin in capture.allowedOrigins. Logged once per process.\",\n\t'other scope':\n\t\t\"analytics: ingest is dropping beacons that a page on a sibling subdomain sent (Sec-Fetch-Site: same-site) whose Origin scope.resolve maps to another scope than the request's. Each tenant's pages post to their own host. Logged once per process.\",\n\t'timezone unresolved':\n\t\t'analytics: ingest hashes visitors by the UTC day for a scope whose reportingTimezone has never resolved: the resolver throws or answers something that is not an IANA timezone. Check that resolver. Logged once per process.',\n\t'order recount failed':\n\t\t\"analytics: a browser's record of an order was stored, but the closed day holding the server's record of it could not be marked to be counted again, so that day keeps the sale until it is rebuilt. Logged once per process.\",\n\t'order refused':\n\t\t'analytics: ingest is dropping sales whose orderId is empty, too long, holds a control character, or lacks the signature signOrderId writes while signatures are required. Logged once per process.',\n} as const\n\ntype WarnReason = keyof typeof WARN_REASONS\n\n/** One line about failed beacons at most this often: a public path must not be a log amplifier. */\nconst FAILED_LOG_MS = 60_000\n\nexport type BeaconFailureLog = (payload: Payload, err: unknown) => void\n\n/** Counts beacons that could not be `what` and logs them in one line at most once a minute. */\nexport const beaconFailureLog = (what: 'stored' | 'taken'): BeaconFailureLog => {\n\tlet failed = 0\n\tlet loggedAt: number | null = null\n\treturn (payload, err) => {\n\t\tfailed += 1\n\t\tconst at = monotonicMs()\n\t\tif (loggedAt !== null && at - loggedAt < FAILED_LOG_MS) {\n\t\t\treturn\n\t\t}\n\t\tloggedAt = at\n\t\tpayload.logger?.error({\n\t\t\terr,\n\t\t\tmsg: `analytics: ${failed} beacons could not be ${what}. Logged at most once a minute.`,\n\t\t})\n\t\tfailed = 0\n\t}\n}\n\n/** One event, not a session replay: far above any legitimate payload, far below a DoS. */\nconst MAX_INGEST_BODY_BYTES = 64 * 1024\n\n/**\n * Undefined for anything unparseable, so a garbage POST is a 400 rather than a thrown error that\n * escapes into Payload's routeError as a logged 500.\n */\nconst parseBody = (bytes: ArrayBuffer): RawEventInput | undefined => {\n\ttry {\n\t\tconst parsed: unknown = JSON.parse(new TextDecoder().decode(bytes))\n\t\t// Unchecked until `rawEventError` runs; every field it skips is sanitized as unknown later.\n\t\treturn typeof parsed === 'object' && parsed !== null ? (parsed as RawEventInput) : undefined\n\t} catch {\n\t\treturn undefined\n\t}\n}\n\n/**\n * Naming the failed field saves a tracker author guessing. A non-object body fails the same check\n * a typeless event does, so it names `type`.\n */\nconst invalidField = (param: RawEventField): Response =>\n\terrorResponse(\n\t\t400,\n\t\tanalyticsError('invalid_param', `analytics: ${param} is missing or invalid`, param)\n\t)\n\nexport interface IngestHandlerOptions {\n\tgeoResolver: GeoResolver\n\t/** The salts, writer and items of the instance a beacon reached. */\n\ttarget: (payload: Payload) => IngestTarget\n\tresolvers?: IngestResolvers\n\tattribution?: IngestAttribution\n\t/** Which agents never reach the pipeline. Defaults to the built-in `isBot`. */\n\tfilterBots?: BotFilter\n\t/** `revenue.browserAmounts`: how far a beacon's amount is believed. Default `'fixed'`. */\n\tbrowserAmounts?: BrowserAmounts\n}\n\n/**\n * Native beacon endpoint: cheap refusals first, then everything resolved as a capture call site\n * sees the request. The only read on the public path is the day's salt, once a day per process.\n */\nexport const makeIngestHandler = ({\n\tgeoResolver,\n\ttarget,\n\tresolvers = {},\n\tattribution = {},\n\tfilterBots = isBot,\n\tbrowserAmounts = DEFAULT_BROWSER_AMOUNTS,\n}: IngestHandlerOptions): PayloadHandler => {\n\t// One line per reason for the life of the handler, which is the life of the process: a\n\t// public endpoint must not be a log amplifier, and an attacker chooses how often it drops.\n\tconst warned = new Set<WarnReason>()\n\tconst warnOnce = (req: PayloadRequest, reason: WarnReason): void => {\n\t\tif (!warned.has(reason)) {\n\t\t\twarned.add(reason)\n\t\t\treq.payload.logger?.warn(WARN_REASONS[reason])\n\t\t}\n\t}\n\tconst drop = (req: PayloadRequest, reason: WarnReason): Response => {\n\t\twarnOnce(req, reason)\n\t\treturn accepted()\n\t}\n\tconst writeFailed = beaconFailureLog('stored')\n\tconst takeFailed = beaconFailureLog('taken')\n\n\tconst take = async (req: PayloadRequest): Promise<Response> => {\n\t\t// Before the body is read, so a crawler costs one header lookup; answered like a kept beacon\n\t\t// and never logged, since bots are expected traffic.\n\t\tconst userAgent = req.headers.get('user-agent') ?? ''\n\t\tlet automated = false\n\t\ttry {\n\t\t\tautomated = filterBots(userAgent)\n\t\t} catch {\n\t\t\twarnOnce(req, 'bot filter threw')\n\t\t}\n\t\tif (automated) {\n\t\t\t// The exception to the silence: an agent-less beacon is indistinguishable from an\n\t\t\t// intermediary stripping the header, which would zero an install's numbers.\n\t\t\tif (userAgent.trim() === '') {\n\t\t\t\twarnOnce(req, 'no user agent')\n\t\t\t}\n\t\t\treturn accepted()\n\t\t}\n\t\t// Another site's page cannot make its visitors count here: the browser says where the\n\t\t// request came from, and only a listed origin may send from elsewhere.\n\t\tif (refusedCrossSite(req.headers, attribution.allowedOrigins ?? NO_ORIGINS)) {\n\t\t\treturn drop(req, 'cross-site')\n\t\t}\n\t\tconst {\n\t\t\tscope: resolveScope,\n\t\t\ttimezone: resolveTimezone,\n\t\t\tcurrency: resolveCurrency,\n\t\t\tgoals: resolveGoals,\n\t\t} = resolvers\n\t\t// A public path: the body is capped before it is buffered, and one that dies in transit (an\n\t\t// unloading tab's beacon) answers 400 rather than throwing.\n\t\tconst read = await readCappedBody(req, MAX_INGEST_BODY_BYTES)\n\t\tif (!read.ok) {\n\t\t\treturn read.reason === 'too-large'\n\t\t\t\t? errorResponse(\n\t\t\t\t\t\t413,\n\t\t\t\t\t\tanalyticsError('payload_too_large', 'analytics: the event body is too large')\n\t\t\t\t\t)\n\t\t\t\t: errorResponse(\n\t\t\t\t\t\t400,\n\t\t\t\t\t\tanalyticsError('invalid_param', 'analytics: the event body could not be read')\n\t\t\t\t\t)\n\t\t}\n\t\tconst raw = parseBody(read.body)\n\t\tif (raw === undefined) {\n\t\t\treturn invalidField('type')\n\t\t}\n\t\tconst param = rawEventError(raw)\n\t\tif (param !== undefined) {\n\t\t\treturn invalidField(param)\n\t\t}\n\t\tconst mode = raw.m ?? 'f'\n\t\t// Anonymous time and scroll are never stored, so the beacon that carries them costs no more.\n\t\tif (mode === 'a' && raw.type === 'engagement') {\n\t\t\treturn accepted()\n\t\t}\n\t\tconst receivedAt = currentDate()\n\t\tconst at = eventTime(raw, receivedAt)\n\t\t// The host's resolvers are asked about the visit, never the signed-in editor whose session\n\t\t// a beacon carries: the tenant is the site it landed on, not the admin's selection.\n\t\tconst visitor = captureRequest(req)\n\t\tconst resolvedScope = resolveScope ? await resolveScope(visitor) : null\n\t\tconst scope = resolveScope ? (resolvedScope ?? '') : undefined\n\t\tconst hostname = await resolveEventHostname({\n\t\t\toption: attribution.hostname ?? REQUEST_HOSTNAME,\n\t\t\tclaimed: raw.hostname,\n\t\t\treq: visitor,\n\t\t\tscope: resolvedScope,\n\t\t\ttrustedProxyHops: attribution.trustedProxyHops,\n\t\t})\n\t\tif (hostname === null) {\n\t\t\treturn drop(req, 'hostname refused')\n\t\t}\n\t\t// A scoped install keeps only what a scope answers for. Its platform hostnames are\n\t\t// infrastructure rather than tenants, so they ingest under the null scope.\n\t\tconst unscoped =\n\t\t\tresolveScope !== undefined &&\n\t\t\tresolvedScope === null &&\n\t\t\t!(attribution.platformHostnames?.has(hostname) ?? false)\n\t\tif (unscoped) {\n\t\t\treturn drop(req, 'no scope resolved')\n\t\t}\n\t\t// A sibling subdomain is another tenant's site: its pages cannot count into this one.\n\t\tif (\n\t\t\tresolveScope &&\n\t\t\t(await refusedSameSite(visitor, () => Promise.resolve(resolvedScope), resolveScope))\n\t\t) {\n\t\t\treturn drop(req, 'other scope')\n\t\t}\n\t\t// A tenant stores visits only while it says so, checked after the hostname and scope rules\n\t\t// and answered 202 either way, so a forged host cannot probe which tenants exist.\n\t\tif (resolvers.granted && !(await resolvers.granted(visitor, resolvedScope))) {\n\t\t\treturn drop(req, 'no native provider')\n\t\t}\n\t\tconst { salts, writer, items, nudge, siteHosts, engine, orders, keepAlive } = target(\n\t\t\treq.payload\n\t\t)\n\t\t// An order id no key can be stored under is dropped rather than stored as a sale without one,\n\t\t// and answered 202 like any drop: a 400 here would tell a prober the tenant takes beacons.\n\t\tconst order = wireOrderId(raw.orderId, scope ?? '', orders)\n\t\tif (!order.ok) {\n\t\t\treturn drop(req, 'order refused')\n\t\t}\n\t\t// An anonymous hit hashes no visitor, so it needs no salt.\n\t\tconst salt =\n\t\t\tmode === 'a'\n\t\t\t\t? ''\n\t\t\t\t: await saltForHit(\n\t\t\t\t\t\tsalts,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tscope: scope ?? '',\n\t\t\t\t\t\t\tat,\n\t\t\t\t\t\t\tzone: () =>\n\t\t\t\t\t\t\t\tresolveTimezone ? resolveTimezone(visitor, scope ?? null) : DEFAULT_TIMEZONE,\n\t\t\t\t\t\t},\n\t\t\t\t\t\t() => warnOnce(req, 'timezone unresolved')\n\t\t\t\t\t)\n\t\t// Goals resolve under the same scope the event is stamped with, so a tenant's event\n\t\t// can only ever complete that tenant's goals. An engagement completes none.\n\t\tconst goals =\n\t\t\tresolveGoals && raw.type !== 'engagement'\n\t\t\t\t? await resolveGoals(visitor, scope ?? null)\n\t\t\t\t: undefined\n\t\tconst reportingCurrency = resolveCurrency\n\t\t\t? await resolveCurrency(visitor, scope ?? null)\n\t\t\t: undefined\n\t\tconst trust = {\n\t\t\ttrustedProxyHops: attribution.trustedProxyHops,\n\t\t\ttrustedPlatform: attribution.trustedPlatform,\n\t\t}\n\t\twarnUntrustedForwarding(req.payload, req.headers, trust)\n\t\t// Only a pageview from another host needs the scope's hostnames: is it a sibling's page?\n\t\tconst siblings =\n\t\t\tsiteHosts && raw.type === 'pageview' && referrerHost(raw.referrer, hostname) !== undefined\n\t\t\t\t? await siteHosts.of(scope ?? '')\n\t\t\t\t: undefined\n\t\tconst event = await normalizeEvent({\n\t\t\traw: { ...raw, orderId: order.orderId },\n\t\t\thostname,\n\t\t\theaders: req.headers,\n\t\t\tgeoResolver,\n\t\t\twarnGeo: (message) => req.payload.logger?.warn(message),\n\t\t\tsalt,\n\t\t\tnow: at,\n\t\t\tscope,\n\t\t\tgoals,\n\t\t\torigin: 'wire',\n\t\t\tbrowserAmounts,\n\t\t\treportingCurrency,\n\t\t\tconvertCurrency: resolvers.convertCurrency,\n\t\t\twarnUnconverted: () => warnUnconvertedOnce(req.payload.logger),\n\t\t\t...trust,\n\t\t\t...(items ? { items } : {}),\n\t\t\t...(siblings ? { siblings } : {}),\n\t\t\torderKey: orderDigestKey(req.payload?.secret),\n\t\t\tmode,\n\t\t})\n\t\t// Nothing left to add once sanitized: no time, no scroll and no item seen moves no number.\n\t\tif (\n\t\t\tevent.type === 'engagement' &&\n\t\t\t!event.durationMs &&\n\t\t\t!event.scrollDepth &&\n\t\t\t!event.impressions?.length\n\t\t) {\n\t\t\treturn accepted()\n\t\t}\n\t\t// A retried order meets its own primary key and is refused there; nothing is read first,\n\t\t// since a read per beacon on a public path is a query anyone can repeat at will.\n\t\tconst hit = toHit(event, { receivedAt })\n\t\tlet stored: AppendResult | undefined\n\t\ttry {\n\t\t\tstored = await writer.write(hit)\n\t\t\tsiteHosts?.saw(scope ?? '', hostname)\n\t\t} catch (err) {\n\t\t\twriteFailed(req.payload, err)\n\t\t}\n\t\t// For an order the write just stored, never a repeat of one: one primary key lookup of the\n\t\t// server's record, past the response. Maintenance sweeps the orders again in case it is lost.\n\t\tif (engine && (stored?.written ?? 0) > 0 && serverTwinOf(hit.id) !== undefined) {\n\t\t\tconst probe = engine\n\t\t\t\t.orderStored(scope ?? '', hit.id)\n\t\t\t\t.catch(() => warnOnce(req, 'order recount failed'))\n\t\t\tkeepAlive?.(probe)\n\t\t}\n\t\tnudge?.()\n\t\treturn accepted()\n\t}\n\n\treturn async (req) => {\n\t\ttry {\n\t\t\treturn await take(req)\n\t\t} catch (err) {\n\t\t\t// A host resolver or a store failing must not reach Payload's routeError as a logged 500\n\t\t\t// per beacon: a public path answers its own envelope and logs at most once a minute.\n\t\t\ttakeFailed(req.payload, err)\n\t\t\treturn errorResponse(500, analyticsError('internal', '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
|
1
|
+
import { HitMode, HitType } from "../store/types.js";
|
|
2
2
|
//#region src/native/ingest/normalizeEvent.d.ts
|
|
3
3
|
/**
|
|
4
4
|
* One event as the tracker or `trackServerEvent` sends it, before the ingest normalizes it.
|
|
@@ -77,6 +77,11 @@ interface RawEventInput {
|
|
|
77
77
|
* capped at one beacon's worth.
|
|
78
78
|
*/
|
|
79
79
|
impressions?: unknown;
|
|
80
|
+
/**
|
|
81
|
+
* How the page measured this beacon; absent is `f`, which a tracker from before modes sends.
|
|
82
|
+
* Ingest stores no more than the mode allows, whatever else the beacon carries.
|
|
83
|
+
*/
|
|
84
|
+
m?: HitMode;
|
|
80
85
|
}
|
|
81
86
|
//#endregion
|
|
82
87
|
export { RawEventInput };
|
|
@@ -146,6 +146,12 @@ const fieldsOfType = (raw) => {
|
|
|
146
146
|
const { durationMs: _durationMs, scrollDepth: _scrollDepth, impressions: _impressions, ...hit } = raw;
|
|
147
147
|
return hit;
|
|
148
148
|
};
|
|
149
|
+
/** An anonymous event keeps no time, scroll or items seen: engagement is a visitor's alone. */
|
|
150
|
+
const fieldsOfMode = (raw, mode) => {
|
|
151
|
+
if (mode !== "a") return raw;
|
|
152
|
+
const { durationMs: _durationMs, scrollDepth: _scrollDepth, impressions: _seen, ...kept } = raw;
|
|
153
|
+
return kept;
|
|
154
|
+
};
|
|
149
155
|
/**
|
|
150
156
|
* The ref an item value is kept under: signed by this server (or trusted, or unsigned where
|
|
151
157
|
* signatures are optional), of a bound collection, with an id that collection could hold.
|
|
@@ -169,8 +175,9 @@ const seenItems = (value, options, origin) => {
|
|
|
169
175
|
return [...refs];
|
|
170
176
|
};
|
|
171
177
|
/** One event sanitized, attributed and goal-matched, alike for a beacon and for server code. */
|
|
172
|
-
async function normalizeEvent({ raw: input, hostname: rawHostname, headers, geoResolver, warnGeo, salt, now, scope, goals, origin = "wire", browserAmounts = DEFAULT_BROWSER_AMOUNTS, reportingCurrency, convertCurrency, warnUnconverted, trustedProxyHops, trustedPlatform, items: itemOptions, siblings, orderKey }) {
|
|
173
|
-
const
|
|
178
|
+
async function normalizeEvent({ raw: input, hostname: rawHostname, headers, geoResolver, warnGeo, salt, now, scope, goals, origin = "wire", browserAmounts = DEFAULT_BROWSER_AMOUNTS, reportingCurrency, convertCurrency, warnUnconverted, trustedProxyHops, trustedPlatform, items: itemOptions, siblings, orderKey, mode = "f" }) {
|
|
179
|
+
const anonymous = mode === "a";
|
|
180
|
+
const raw = fieldsOfMode(fieldsOfType(input), mode);
|
|
174
181
|
const item = itemOptions ? keptItemRef(raw.item, itemOptions, origin) : void 0;
|
|
175
182
|
const impressions = seenItems(raw.impressions, itemOptions, origin);
|
|
176
183
|
const trust = {
|
|
@@ -181,7 +188,7 @@ async function normalizeEvent({ raw: input, hostname: rawHostname, headers, geoR
|
|
|
181
188
|
trust,
|
|
182
189
|
warn: warnGeo
|
|
183
190
|
});
|
|
184
|
-
const ip = clientIpFromHeaders(headers, trust) ?? "";
|
|
191
|
+
const ip = anonymous ? "" : clientIpFromHeaders(headers, trust) ?? "";
|
|
185
192
|
const ua = headers.get("user-agent") ?? "";
|
|
186
193
|
const path = raw.path.slice(0, MAX_PATH_LENGTH);
|
|
187
194
|
const hostname = rawHostname.slice(0, 253);
|
|
@@ -262,8 +269,10 @@ async function normalizeEvent({ raw: input, hostname: rawHostname, headers, geoR
|
|
|
262
269
|
...os ? { os } : {},
|
|
263
270
|
...utm,
|
|
264
271
|
country: geoValue(geo.country),
|
|
265
|
-
|
|
266
|
-
|
|
272
|
+
...anonymous ? {} : {
|
|
273
|
+
region: geoValue(geo.region),
|
|
274
|
+
city: geoValue(geo.city)
|
|
275
|
+
},
|
|
267
276
|
...language ? { language } : {},
|
|
268
277
|
props,
|
|
269
278
|
...durationMs !== void 0 ? { durationMs } : {},
|
|
@@ -286,7 +295,7 @@ async function normalizeEvent({ raw: input, hostname: rawHostname, headers, geoR
|
|
|
286
295
|
...scope !== void 0 ? { scope } : {},
|
|
287
296
|
...item !== void 0 ? { item } : {},
|
|
288
297
|
...impressions.length > 0 ? { impressions } : {},
|
|
289
|
-
visitorId: identifiesVisitor(ip, ua) ? dailyVisitorId({
|
|
298
|
+
visitorId: !anonymous && identifiesVisitor(ip, ua) ? dailyVisitorId({
|
|
290
299
|
ip,
|
|
291
300
|
ua,
|
|
292
301
|
site: hostname,
|
|
@@ -294,7 +303,8 @@ async function normalizeEvent({ raw: input, hostname: rawHostname, headers, geoR
|
|
|
294
303
|
}) : "",
|
|
295
304
|
...clickId ? { clickId } : {},
|
|
296
305
|
...input.type === "pageview" && isNavigation(input.nav) ? { navigation: true } : {},
|
|
297
|
-
...origin === "server" ? { server: true } : {}
|
|
306
|
+
...origin === "server" ? { server: true } : {},
|
|
307
|
+
mode
|
|
298
308
|
};
|
|
299
309
|
}
|
|
300
310
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"normalizeEvent.js","names":[],"sources":["../../../src/native/ingest/normalizeEvent.ts"],"sourcesContent":["import { isRecord } from '../../core/values'\nimport { MAX_ENGAGEMENT_MS, MAX_ITEMS_PER_BEACON, MAX_SINCE_MS } from '../../goals/constants'\nimport type { CurrencyConverter } from '../../goals/currency'\nimport { sanitizeItems } from '../../goals/items'\nimport {\n\ttype BrowserAmounts,\n\ttype CompletionAmount,\n\tcompletionAmounts,\n\tDEFAULT_BROWSER_AMOUNTS,\n\ttype GoalCompletion,\n} from '../../goals/match'\nimport type { Goal } from '../../goals/types'\nimport type { ItemIdType } from '../../items/idType'\nimport { boundItemRef } from '../../items/ref'\nimport type { TrustedPlatform } from '../../net/platform'\nimport { MAX_GEO_LENGTH, MAX_QUERY_LENGTH } from '../../query/limits'\nimport type { GeoResolver } from '../geo/geoResolver'\nimport { resolveGeo } from '../geo/resolveGeo'\nimport type { HitType } from '../store/types'\nimport { type BrowserName, classifyBrowser, classifyOs, type OsName } from './browser'\nimport { clientIpFromHeaders } from './clientIp'\nimport { classifyDevice, type DeviceType, SERVER_USER_AGENT } from './device'\nimport { type ItemSignatureCheck, verifiedItemRef } from './itemSignature'\nimport { primaryLanguage } from './language'\nimport { isSiteReferral, referrerHost, storedReferrer } from './referrer'\nimport { MAX_HOSTNAME_LENGTH } from './requestHost'\nimport {\n\tboundedValue,\n\tdedupeKeyFor,\n\ttype IngestOrigin,\n\tisRefund,\n\ttype Money,\n\treconcileMoney,\n\tsanitizeOrderId,\n} from './revenue'\nimport { clickIdOf } from './source'\nimport { extractUtm } from './utm'\nimport { boundedSinceMs, isNavigation } from './validate'\nimport { dailyVisitorId } from './visitorHash'\n\n/**\n * One event as the tracker or `trackServerEvent` sends it, before the ingest normalizes it.\n * @beta\n */\nexport interface RawEventInput {\n\t/** An `engagement` gets a row of its own so a filtered read finds it, never counted as a hit. */\n\ttype: HitType\n\t/** Event name, or the goal slug on a `goal`. Absent on a pageview and an engagement. */\n\tname?: string\n\tpath: string\n\t/**\n\t * The hostname the client claims. Ignored by the HTTP ingest, which attributes an event to\n\t * the request that carried it; `trackServerEvent` supplies its own and it is used as sent.\n\t */\n\thostname?: string\n\treferrer?: string\n\t/**\n\t * Query string without its leading `?`. Read for campaign keys and never stored, so a session\n\t * token or email address in the URL never lands in a hit.\n\t */\n\tquery?: string\n\t/**\n\t * Engaged time an engagement adds. Kept on a pageview too, which is what a tracker cached\n\t * from before engagement existed still sends; dropped on every other type.\n\t */\n\tdurationMs?: number\n\t/**\n\t * On an engagement only: ms since its pageview was sent, by the page's own clock so the device's\n\t * date never matters. Read by {@link eventTime}, never stored.\n\t */\n\tsinceMs?: number\n\t/**\n\t * How long the event waited in the page before it was sent, by the page's own clock: a pending\n\t * gate or a prerender held it. Read by {@link eventTime}, never stored.\n\t */\n\tageMs?: number\n\t/** `1` on a pageview a single-page app made in place, without loading a document. */\n\tnav?: number\n\tprops?: Record<string, unknown>\n\t/** Revenue for a goal completion, in `currency`. */\n\tvalue?: number\n\tcurrency?: string\n\t/**\n\t * The lines of a sale. Sanitized and stored under `props.items`, so a read of the event\n\t * carries the order without a second collection.\n\t */\n\titems?: unknown\n\t/**\n\t * Marks a server `purchase` or `refund`, the one path a negative `value` is allowed on. The wire\n\t * refuses it: a forged refund subtracts from revenue it never added to.\n\t */\n\trevenue?: boolean\n\t/**\n\t * The order this amount belongs to. The order's hit is keyed by a digest of it, so a retried\n\t * payment webhook counts once however often it arrives; the id itself is stored nowhere.\n\t */\n\torderId?: string\n\t/**\n\t * On a server refund, the refund's own id as the payment provider names it: it joins the order\n\t * id in the refund's key, so each partial refund counts once however often it is retried.\n\t */\n\trefundId?: string\n\t/**\n\t * On a pageview, its scroll depth, 0 when the tracker measures it afterwards; on an\n\t * engagement, how much deeper the page was scrolled since the previous one. 0-100.\n\t */\n\tscrollDepth?: number\n\t/**\n\t * Item ref (`<collection>:<id>`), server-signed when a page sends it (`offers:12@<signature>`).\n\t * Dropped, keeping the event, unless verified and of a collection bound as an item.\n\t */\n\titem?: string\n\t/**\n\t * On an engagement only: item values seen since the previous one, each checked like `item` and\n\t * capped at one beacon's worth.\n\t */\n\timpressions?: unknown\n}\n\n/**\n * An event once ingest has sanitized, attributed and matched it: what `toHit` shapes into the row\n * that is stored. Nothing here is written as it stands.\n */\nexport interface NormalizedEvent {\n\t/** When it arrived, or for an engagement its pageview's start; see {@link eventTime}. */\n\ttimestamp: Date\n\ttype: HitType\n\tname?: string\n\tpath: string\n\thostname: string\n\t/**\n\t * Origin and path only, the query string and fragment stripped: whether a pageview opens a\n\t * visit is decided by it. Never stored.\n\t */\n\treferrer?: string\n\t/** The referrer's bare host, which a landing stores as `ref`; absent for the site's own. */\n\treferrerHost?: string\n\t/**\n\t * The referrer is another hostname its scope records hits under: a link inside the site, which\n\t * opens no visit, whatever hostname it led to.\n\t */\n\tsiteReferrer?: boolean\n\tdevice?: DeviceType\n\tbrowser?: BrowserName\n\tos?: OsName\n\t/** The five campaign keys extracted from the wire `query`; absent when it carried none. */\n\tutmSource?: string\n\tutmMedium?: string\n\tutmCampaign?: string\n\tutmContent?: string\n\tutmTerm?: string\n\tcountry?: string\n\tregion?: string\n\tcity?: string\n\t/** Primary `Accept-Language` tag, lowercased (`de-de`). */\n\tlanguage?: string\n\tdurationMs?: number\n\tprops?: Record<string, unknown>\n\tvalue?: number\n\t/**\n\t * ISO 4217 code of `value`: as sent without `revenue.currency`, else always the reporting\n\t * currency, with the `original` fields holding what arrived.\n\t */\n\tcurrency?: string\n\t/** The amount as sent, present only when it is not what `value` holds. */\n\toriginalValue?: number\n\toriginalCurrency?: string\n\t/**\n\t * The order hit's primary key derives from it, so the database refuses a second row for one order\n\t * even when two retries race.\n\t */\n\tdedupeKey?: string\n\t/** A server refund: its completions count their negative amount in revenue and no conversion. */\n\trefund?: true\n\tscrollDepth?: number\n\t/**\n\t * Goals this event completed, resolved against the install's goals at ingest so a later\n\t * goal edit never rewrites history.\n\t */\n\tgoals?: GoalCompletion[]\n\t/** Set only in scoped installs; '' is the null scope. */\n\tscope?: string\n\t/** The content item this event or goal belongs to, as its ref. */\n\titem?: string\n\t/** Items seen on screen since the previous engagement, on an engagement only. */\n\timpressions?: string[]\n\t/**\n\t * The day's visitor id: 16 characters of a keyed hash of the address, the agent and the site,\n\t * or `''` for a server event with neither an address nor a user agent of its own.\n\t */\n\tvisitorId: string\n\t/** The ad click id the query carried, by name (`gclid`, `fbclid`). */\n\tclickId?: string\n\t/** A pageview a single-page app made in place (`nav: 1`). */\n\tnavigation?: boolean\n\t/** Recorded by server code rather than sent by a browser. */\n\tserver?: boolean\n}\n\ninterface NormalizeArgs {\n\traw: RawEventInput\n\t/** Already resolved and capped: what the event is attributed to, not what it claimed. */\n\thostname: string\n\theaders: Headers\n\tgeoResolver: GeoResolver\n\t/** Where a throwing resolver is reported; the event is stored without a location either way. */\n\twarnGeo?: (message: string) => void\n\t/** The salt of the visitor's day, which the visitor id is hashed with. */\n\tsalt: string\n\tnow: Date\n\tscope?: string\n\tgoals?: Goal[]\n\t/**\n\t * Revenue semantics belong to `'server'` alone, so the default is the unprivileged origin and\n\t * only `trackServerEvent` passes the other.\n\t */\n\torigin?: IngestOrigin\n\t/**\n\t * `revenue.browserAmounts`: how far an amount a browser sent is believed. Read for\n\t * `origin: 'wire'` alone; absent, `DEFAULT_BROWSER_AMOUNTS`.\n\t */\n\tbrowserAmounts?: BrowserAmounts\n\t/** The install's reporting currency; undefined stores and sums amounts exactly as sent. */\n\treportingCurrency?: string\n\tconvertCurrency?: CurrencyConverter\n\t/** Called at most once per process when an amount could not be converted. */\n\twarnUnconverted?: () => void\n\t/** Trusted proxies in front of the app, for the client address the hash and geo read. */\n\ttrustedProxyHops?: number\n\t/** The platform whose edge headers carry the client address and location, when trusted. */\n\ttrustedPlatform?: TrustedPlatform\n\t/** The collections bound as items, whose refs alone are kept. Absent keeps no item at all. */\n\titems?: ItemIngestOptions\n\t/**\n\t * The other hostnames the event's scope records hits under, bare: a referrer on one of them is\n\t * the site's own page, never a referral.\n\t */\n\tsiblings?: ReadonlySet<string>\n\t/** `orderDigestKey` of the instance's secret, which an order's key is digested with. */\n\torderKey?: Buffer\n}\n\nexport interface ItemIngestOptions {\n\tcollections: ReadonlySet<string>\n\t/**\n\t * Each bound collection's id type, known once the database adapter is (at init). A ref whose\n\t * id cannot be one of its collection's ids is dropped like an unbound one.\n\t */\n\tidTypes?: ReadonlyMap<string, ItemIdType>\n\t/**\n\t * How a ref's signature is checked, known once the instance's secret is. Absent, nothing a page\n\t * sends can be verified, so only server code's own items are kept.\n\t */\n\tsignature?: ItemSignatureCheck\n}\n\nconst MAX_PROPS = 20\nconst MAX_KEY_LENGTH = 64\nconst MAX_VALUE_LENGTH = 256\nconst MAX_NAME_LENGTH = 128\nconst MAX_PATH_LENGTH = 512\nconst CURRENCY = /^[A-Z]{3}$/\n\n/**\n * When an event happened: its arrival less its `ageMs`, and an engagement's `sinceMs` too, so it is\n * filed at its pageview's start. Each is believed from 0 to a day, and together reach back a day.\n */\nexport const eventTime = (\n\traw: Pick<RawEventInput, 'type' | 'sinceMs' | 'ageMs'>,\n\treceivedAt: Date\n): Date => {\n\tconst waited = boundedSinceMs(raw.ageMs) ?? 0\n\tconst since = raw.type === 'engagement' ? (boundedSinceMs(raw.sinceMs) ?? 0) : 0\n\tconst back = Math.min(MAX_SINCE_MS, waited + since)\n\treturn back === 0 ? receivedAt : new Date(receivedAt.getTime() - back)\n}\n\n/**\n * Whether an event comes with a visitor of its own: an address, or a user agent other than the\n * one server tracking stamps when it has none. A beacon always does; server code may not.\n */\nconst identifiesVisitor = (ip: string, ua: string): boolean =>\n\tip !== '' || (ua !== '' && ua !== SERVER_USER_AGENT)\n\n/**\n * Optional wire fields are sanitized rather than rejected, so one malformed attribute never costs\n * a pageview.\n */\nconst nonNegative = (value: unknown): number | undefined => {\n\tconst bounded = boundedValue(value)\n\treturn bounded !== undefined && bounded >= 0 ? bounded : undefined\n}\n\n/** A refund is the one negative amount ingest keeps, and only on a server `refund` call. */\nconst signed = (value: unknown): number | undefined => boundedValue(value)\n\nconst currencyCode = (value: unknown): string | undefined =>\n\ttypeof value === 'string' && CURRENCY.test(value) ? value : undefined\n\n/**\n * The event's amount as far as it is believed: as sent under `trust` and `fixed` (where a goal's\n * fixed value still wins for its own completion), nothing under `none`.\n */\nexport const sentAmount = (args: {\n\tvalue: number | undefined\n\tcurrency: string | undefined\n\tbelieved: BrowserAmounts\n}): { value?: number; currency?: string } =>\n\targs.believed === 'none'\n\t\t? {}\n\t\t: {\n\t\t\t\t...(args.value === undefined ? {} : { value: args.value }),\n\t\t\t\t...(args.currency === undefined ? {} : { currency: args.currency }),\n\t\t\t}\n\n/** One completion as counted, and the money its amount came to, absent for one worth nothing. */\ninterface CountedCompletion {\n\tcompletion: GoalCompletion\n\tmoney?: Money\n}\n\n/**\n * Each completion reconciled on its own (a fixed value in its goal's currency, else the event's),\n * one conversion per distinct amount however many goals share it.\n */\nconst countCompletions = async (\n\tamounts: readonly CompletionAmount[],\n\targs: {\n\t\teventCurrency: string | undefined\n\t\treconcile: (value: number, currency: string | undefined) => Promise<Money>\n\t}\n): Promise<CountedCompletion[]> => {\n\tconst counted: CountedCompletion[] = []\n\tfor (const { goal, source, amount } of amounts) {\n\t\tif (source === 'none') {\n\t\t\tcounted.push({ completion: { slug: goal.slug, value: 0 } })\n\t\t\tcontinue\n\t\t}\n\t\tconst currency = source === 'fixed' ? currencyCode(goal.currency) : args.eventCurrency\n\t\tconst money = await args.reconcile(amount, currency)\n\t\tcounted.push({\n\t\t\tcompletion: {\n\t\t\t\tslug: goal.slug,\n\t\t\t\tvalue: money.value ?? 0,\n\t\t\t\t...(money.originalValue === undefined ? {} : { sent: money.originalValue }),\n\t\t\t},\n\t\t\tmoney,\n\t\t})\n\t}\n\treturn counted\n}\n\n/**\n * Infinity would poison every average for good, and a string would throw out of the write and take\n * the buffered batch with it.\n */\nconst duration = (value: unknown): number | undefined => {\n\tconst n = nonNegative(value)\n\treturn n === undefined || n > MAX_ENGAGEMENT_MS ? undefined : n\n}\n\n/** The event name is a value the `event` dimension groups by, so it is length-capped. */\nconst eventName = (value: unknown): string | undefined =>\n\ttypeof value === 'string' ? value.slice(0, MAX_NAME_LENGTH) : undefined\n\n/**\n * Capped, not trusted: the resolver reads headers a client can set. An empty value stays absent,\n * since '' would be a value of its own.\n */\nconst geoValue = (value: string | undefined): string | undefined =>\n\tvalue ? value.slice(0, MAX_GEO_LENGTH) : undefined\n\n/** Capped before it is parsed, so a hostile query cannot make ingest do unbounded work. */\nconst queryString = (value: unknown): string | undefined =>\n\ttypeof value === 'string' ? value.slice(0, MAX_QUERY_LENGTH) : undefined\n\nconst depth = (value: unknown): number | undefined => {\n\tconst n = nonNegative(value)\n\treturn n === undefined ? undefined : Math.round(Math.min(n, 100))\n}\n\n/**\n * Flat JSON scalars only, first 20 short-keyed entries. An order's event drops an `orderId` prop:\n * an order is kept as its digest alone.\n */\nconst sanitizeProps = (raw: unknown, order: boolean): Record<string, unknown> | undefined => {\n\tif (!isRecord(raw) || Array.isArray(raw)) {\n\t\treturn undefined\n\t}\n\tconst out: Record<string, unknown> = {}\n\tlet kept = 0\n\tfor (const [key, value] of Object.entries(raw)) {\n\t\tif (kept >= MAX_PROPS || key.length > MAX_KEY_LENGTH || (order && key === 'orderId')) {\n\t\t\tcontinue\n\t\t}\n\t\tif (typeof value === 'string') {\n\t\t\tout[key] = value.slice(0, MAX_VALUE_LENGTH)\n\t\t} else if (\n\t\t\ttypeof value === 'boolean' ||\n\t\t\t(typeof value === 'number' && Number.isFinite(value))\n\t\t) {\n\t\t\tout[key] = value\n\t\t} else {\n\t\t\tcontinue\n\t\t}\n\t\tkept++\n\t}\n\treturn kept > 0 ? out : undefined\n}\n\n/**\n * An engagement adds only time, scroll and items seen; time and scroll count from a pageview or an\n * engagement alone, and only an event or a goal belongs to an item.\n */\nconst fieldsOfType = (raw: RawEventInput): RawEventInput => {\n\tif (raw.type === 'engagement') {\n\t\tconst { type, path, hostname, referrer, query, durationMs, scrollDepth, impressions } = raw\n\t\treturn { type, path, hostname, referrer, query, durationMs, scrollDepth, impressions }\n\t}\n\tif (raw.type === 'pageview') {\n\t\tconst { item: _item, impressions: _impressions, ...pageview } = raw\n\t\treturn pageview\n\t}\n\tconst {\n\t\tdurationMs: _durationMs,\n\t\tscrollDepth: _scrollDepth,\n\t\timpressions: _impressions,\n\t\t...hit\n\t} = raw\n\treturn hit\n}\n\n/**\n * The ref an item value is kept under: signed by this server (or trusted, or unsigned where\n * signatures are optional), of a bound collection, with an id that collection could hold.\n */\nconst keptItemRef = (\n\tvalue: unknown,\n\toptions: ItemIngestOptions,\n\torigin: IngestOrigin\n): string | undefined => {\n\tconst ref = verifiedItemRef(value, options.signature, origin === 'server')\n\treturn ref === undefined ? undefined : boundItemRef(ref, options.collections, options.idTypes)\n}\n\n/**\n * The refs of the items an engagement saw, each kept, once each, and no more than one beacon\n * carries. Anything else in the list is dropped rather than failing the engagement.\n */\nconst seenItems = (\n\tvalue: unknown,\n\toptions: ItemIngestOptions | undefined,\n\torigin: IngestOrigin\n): string[] => {\n\tif (!options || !Array.isArray(value)) {\n\t\treturn []\n\t}\n\tconst refs = new Set<string>()\n\tfor (const entry of value) {\n\t\tif (refs.size >= MAX_ITEMS_PER_BEACON) {\n\t\t\tbreak\n\t\t}\n\t\tconst ref = keptItemRef(entry, options, origin)\n\t\tif (ref !== undefined) {\n\t\t\trefs.add(ref)\n\t\t}\n\t}\n\treturn [...refs]\n}\n\n/** One event sanitized, attributed and goal-matched, alike for a beacon and for server code. */\nexport async function normalizeEvent({\n\traw: input,\n\thostname: rawHostname,\n\theaders,\n\tgeoResolver,\n\twarnGeo,\n\tsalt,\n\tnow,\n\tscope,\n\tgoals,\n\torigin = 'wire',\n\tbrowserAmounts = DEFAULT_BROWSER_AMOUNTS,\n\treportingCurrency,\n\tconvertCurrency,\n\twarnUnconverted,\n\ttrustedProxyHops,\n\ttrustedPlatform,\n\titems: itemOptions,\n\tsiblings,\n\torderKey,\n}: NormalizeArgs): Promise<NormalizedEvent> {\n\tconst raw = fieldsOfType(input)\n\tconst item = itemOptions ? keptItemRef(raw.item, itemOptions, origin) : undefined\n\tconst impressions = seenItems(raw.impressions, itemOptions, origin)\n\tconst trust = { trustedProxyHops, trustedPlatform }\n\tconst geo = await resolveGeo(geoResolver, headers, { trust, warn: warnGeo })\n\tconst ip = clientIpFromHeaders(headers, trust) ?? ''\n\tconst ua = headers.get('user-agent') ?? ''\n\t// Every value a daily total is keyed by is capped first: Mongo's index key limit and Postgres'\n\t// 2704-byte btree key both fail the write. Geo, name, query and referrer cap below.\n\tconst path = raw.path.slice(0, MAX_PATH_LENGTH)\n\tconst hostname = rawHostname.slice(0, MAX_HOSTNAME_LENGTH)\n\tconst items = sanitizeItems(raw.items)\n\tconst sanitized = sanitizeProps(raw.props, raw.orderId !== undefined)\n\t// The lines ride under props rather than in a column of their own: they are read back with\n\t// the event, never grouped or filtered on, and `sanitizeProps` keeps scalars only.\n\tconst props = items === undefined ? sanitized : { ...sanitized, items }\n\tconst name = eventName(raw.name)\n\tconst isRevenue = origin === 'server' && raw.revenue === true\n\tconst refund = isRefund(isRevenue, raw.value)\n\tconst believed: BrowserAmounts = origin === 'server' ? 'trust' : browserAmounts\n\t// A server-only goal (`browser: false`) is one a browser cannot complete, whatever it sends.\n\tconst candidates = origin === 'wire' ? goals?.filter((goal) => goal.browser !== false) : goals\n\tconst matching = Boolean(candidates?.length) && raw.type !== 'engagement'\n\tconst eventCurrency = currencyCode(raw.currency)\n\tconst sent = sentAmount({\n\t\tvalue: isRevenue ? signed(raw.value) : nonNegative(raw.value),\n\t\tcurrency: eventCurrency,\n\t\tbelieved,\n\t})\n\tconst reconciled = new Map<string, Promise<Money>>()\n\tconst reconcile = (value: number | undefined, currency: string | undefined): Promise<Money> => {\n\t\tconst key = JSON.stringify([value ?? null, currency ?? null])\n\t\tconst known = reconciled.get(key)\n\t\tif (known) {\n\t\t\treturn known\n\t\t}\n\t\tconst money = reconcileMoney({\n\t\t\t...(value === undefined ? {} : { value }),\n\t\t\t...(currency === undefined ? {} : { currency }),\n\t\t\treportingCurrency,\n\t\t\tconvertCurrency,\n\t\t\tat: now,\n\t\t\twarn: warnUnconverted,\n\t\t})\n\t\treconciled.set(key, money)\n\t\treturn money\n\t}\n\t// Match on the sanitized fields, each completion's amount reconciled on its own, so a rejected\n\t// value never reaches a goal's revenue and a converted one is what revenue sums.\n\tconst counted =\n\t\tmatching && candidates\n\t\t\t? await countCompletions(\n\t\t\t\t\tcompletionAmounts({ type: raw.type, name, path, props, value: sent.value }, candidates, {\n\t\t\t\t\t\tallowNegativeValue: isRevenue,\n\t\t\t\t\t\tprops: believed !== 'none',\n\t\t\t\t\t\tfixedFirst: believed !== 'trust',\n\t\t\t\t\t}),\n\t\t\t\t\t{ eventCurrency, reconcile }\n\t\t\t\t)\n\t\t\t: []\n\tconst completions = counted.map((each) => each.completion)\n\t// The row carries the money of its first completion worth something, else the event's own.\n\tconst money =\n\t\tcounted.find((each) => each.money !== undefined)?.money ??\n\t\t(await reconcile(sent.value, sent.currency))\n\tconst order = sanitizeOrderId(raw.orderId)\n\tconst refundId = refund ? sanitizeOrderId(raw.refundId) : undefined\n\tconst scrollDepth = depth(raw.scrollDepth)\n\tconst durationMs = duration(raw.durationMs)\n\tconst device = classifyDevice(ua)\n\tconst browser = classifyBrowser(ua)\n\tconst os = classifyOs(ua)\n\tconst language = primaryLanguage(headers.get('accept-language'))\n\tconst refHost = referrerHost(raw.referrer, hostname, siblings)\n\tconst siteReferrer =\n\t\t!isSiteReferral(raw.referrer, hostname) && isSiteReferral(raw.referrer, hostname, siblings)\n\tconst query = queryString(raw.query)\n\tconst utm = extractUtm(query)\n\tconst clickId = clickIdOf(query)\n\treturn {\n\t\ttimestamp: now,\n\t\ttype: raw.type,\n\t\tname,\n\t\tpath,\n\t\thostname,\n\t\treferrer: storedReferrer(raw.referrer),\n\t\t...(refHost ? { referrerHost: refHost } : {}),\n\t\t...(siteReferrer ? { siteReferrer: true } : {}),\n\t\t...(device ? { device } : {}),\n\t\t...(browser ? { browser } : {}),\n\t\t...(os ? { os } : {}),\n\t\t...utm,\n\t\tcountry: geoValue(geo.country),\n\t\tregion: geoValue(geo.region),\n\t\tcity: geoValue(geo.city),\n\t\t...(language ? { language } : {}),\n\t\tprops,\n\t\t...(durationMs !== undefined ? { durationMs } : {}),\n\t\t...(money.value !== undefined ? { value: money.value } : {}),\n\t\t...(money.currency !== undefined ? { currency: money.currency } : {}),\n\t\t...(money.originalValue !== undefined ? { originalValue: money.originalValue } : {}),\n\t\t...(money.originalCurrency !== undefined ? { originalCurrency: money.originalCurrency } : {}),\n\t\t...(order\n\t\t\t? {\n\t\t\t\t\tdedupeKey: dedupeKeyFor({\n\t\t\t\t\t\torigin,\n\t\t\t\t\t\t...(scope ? { tenant: scope } : {}),\n\t\t\t\t\t\tgoal: name ?? raw.type,\n\t\t\t\t\t\torderId: order,\n\t\t\t\t\t\trefund,\n\t\t\t\t\t\t...(refundId === undefined ? {} : { refundId }),\n\t\t\t\t\t\t...(orderKey ? { key: orderKey } : {}),\n\t\t\t\t\t}),\n\t\t\t\t}\n\t\t\t: {}),\n\t\t...(refund ? { refund: true } : {}),\n\t\t...(scrollDepth !== undefined ? { scrollDepth } : {}),\n\t\t...(completions.length > 0 ? { goals: completions } : {}),\n\t\t...(scope !== undefined ? { scope } : {}),\n\t\t...(item !== undefined ? { item } : {}),\n\t\t...(impressions.length > 0 ? { impressions } : {}),\n\t\tvisitorId: identifiesVisitor(ip, ua) ? dailyVisitorId({ ip, ua, site: hostname, salt }) : '',\n\t\t...(clickId ? { clickId } : {}),\n\t\t...(input.type === 'pageview' && isNavigation(input.nav) ? { navigation: true } : {}),\n\t\t...(origin === 'server' ? { server: true } : {}),\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAgQA,MAAM,YAAY;AAClB,MAAM,iBAAiB;AACvB,MAAM,mBAAmB;AACzB,MAAM,kBAAkB;AACxB,MAAM,kBAAkB;AACxB,MAAM,WAAW;;;;;AAMjB,MAAa,aACZ,KACA,eACU;CACV,MAAM,SAAS,eAAe,IAAI,KAAK,KAAK;CAC5C,MAAM,QAAQ,IAAI,SAAS,eAAgB,eAAe,IAAI,OAAO,KAAK,IAAK;CAC/E,MAAM,OAAO,KAAK,IAAI,cAAc,SAAS,KAAK;CAClD,OAAO,SAAS,IAAI,aAAa,IAAI,KAAK,WAAW,QAAQ,IAAI,IAAI;AACtE;;;;;AAMA,MAAM,qBAAqB,IAAY,OACtC,OAAO,MAAO,OAAO,MAAM,OAAA;;;;;AAM5B,MAAM,eAAe,UAAuC;CAC3D,MAAM,UAAU,aAAa,KAAK;CAClC,OAAO,YAAY,KAAA,KAAa,WAAW,IAAI,UAAU,KAAA;AAC1D;;AAGA,MAAM,UAAU,UAAuC,aAAa,KAAK;AAEzE,MAAM,gBAAgB,UACrB,OAAO,UAAU,YAAY,SAAS,KAAK,KAAK,IAAI,QAAQ,KAAA;;;;;AAM7D,MAAa,cAAc,SAK1B,KAAK,aAAa,SACf,CAAC,IACD;CACA,GAAI,KAAK,UAAU,KAAA,IAAY,CAA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C,CAAC,EACF,IACC,CAAC;EACJ,GAAI,SAAS,EAAE,QAAQ,KAAK,IAAI,CAAC;EACjC,GAAI,gBAAgB,KAAA,IAAY,EAAE,YAAY,IAAI,CAAC;EACnD,GAAI,YAAY,SAAS,IAAI,EAAE,OAAO,YAAY,IAAI,CAAC;EACvD,GAAI,UAAU,KAAA,IAAY,EAAE,MAAM,IAAI,CAAC;EACvC,GAAI,SAAS,KAAA,IAAY,EAAE,KAAK,IAAI,CAAC;EACrC,GAAI,YAAY,SAAS,IAAI,EAAE,YAAY,IAAI,CAAC;EAChD,WAAW,kBAAkB,IAAI,EAAE,IAAI,eAAe;GAAE;GAAI;GAAI,MAAM;GAAU;EAAK,CAAC,IAAI;EAC1F,GAAI,UAAU,EAAE,QAAQ,IAAI,CAAC;EAC7B,GAAI,MAAM,SAAS,cAAc,aAAa,MAAM,GAAG,IAAI,EAAE,YAAY,KAAK,IAAI,CAAC;EACnF,GAAI,WAAW,WAAW,EAAE,QAAQ,KAAK,IAAI,CAAC;CAC/C;AACD"}
|
|
1
|
+
{"version":3,"file":"normalizeEvent.js","names":[],"sources":["../../../src/native/ingest/normalizeEvent.ts"],"sourcesContent":["import { isRecord } from '../../core/values'\nimport { MAX_ENGAGEMENT_MS, MAX_ITEMS_PER_BEACON, MAX_SINCE_MS } from '../../goals/constants'\nimport type { CurrencyConverter } from '../../goals/currency'\nimport { sanitizeItems } from '../../goals/items'\nimport {\n\ttype BrowserAmounts,\n\ttype CompletionAmount,\n\tcompletionAmounts,\n\tDEFAULT_BROWSER_AMOUNTS,\n\ttype GoalCompletion,\n} from '../../goals/match'\nimport type { Goal } from '../../goals/types'\nimport type { ItemIdType } from '../../items/idType'\nimport { boundItemRef } from '../../items/ref'\nimport type { TrustedPlatform } from '../../net/platform'\nimport { MAX_GEO_LENGTH, MAX_QUERY_LENGTH } from '../../query/limits'\nimport type { GeoResolver } from '../geo/geoResolver'\nimport { resolveGeo } from '../geo/resolveGeo'\nimport type { HitMode, HitType } from '../store/types'\nimport { type BrowserName, classifyBrowser, classifyOs, type OsName } from './browser'\nimport { clientIpFromHeaders } from './clientIp'\nimport { classifyDevice, type DeviceType, SERVER_USER_AGENT } from './device'\nimport { type ItemSignatureCheck, verifiedItemRef } from './itemSignature'\nimport { primaryLanguage } from './language'\nimport { isSiteReferral, referrerHost, storedReferrer } from './referrer'\nimport { MAX_HOSTNAME_LENGTH } from './requestHost'\nimport {\n\tboundedValue,\n\tdedupeKeyFor,\n\ttype IngestOrigin,\n\tisRefund,\n\ttype Money,\n\treconcileMoney,\n\tsanitizeOrderId,\n} from './revenue'\nimport { clickIdOf } from './source'\nimport { extractUtm } from './utm'\nimport { boundedSinceMs, isNavigation } from './validate'\nimport { dailyVisitorId } from './visitorHash'\n\n/**\n * One event as the tracker or `trackServerEvent` sends it, before the ingest normalizes it.\n * @beta\n */\nexport interface RawEventInput {\n\t/** An `engagement` gets a row of its own so a filtered read finds it, never counted as a hit. */\n\ttype: HitType\n\t/** Event name, or the goal slug on a `goal`. Absent on a pageview and an engagement. */\n\tname?: string\n\tpath: string\n\t/**\n\t * The hostname the client claims. Ignored by the HTTP ingest, which attributes an event to\n\t * the request that carried it; `trackServerEvent` supplies its own and it is used as sent.\n\t */\n\thostname?: string\n\treferrer?: string\n\t/**\n\t * Query string without its leading `?`. Read for campaign keys and never stored, so a session\n\t * token or email address in the URL never lands in a hit.\n\t */\n\tquery?: string\n\t/**\n\t * Engaged time an engagement adds. Kept on a pageview too, which is what a tracker cached\n\t * from before engagement existed still sends; dropped on every other type.\n\t */\n\tdurationMs?: number\n\t/**\n\t * On an engagement only: ms since its pageview was sent, by the page's own clock so the device's\n\t * date never matters. Read by {@link eventTime}, never stored.\n\t */\n\tsinceMs?: number\n\t/**\n\t * How long the event waited in the page before it was sent, by the page's own clock: a pending\n\t * gate or a prerender held it. Read by {@link eventTime}, never stored.\n\t */\n\tageMs?: number\n\t/** `1` on a pageview a single-page app made in place, without loading a document. */\n\tnav?: number\n\tprops?: Record<string, unknown>\n\t/** Revenue for a goal completion, in `currency`. */\n\tvalue?: number\n\tcurrency?: string\n\t/**\n\t * The lines of a sale. Sanitized and stored under `props.items`, so a read of the event\n\t * carries the order without a second collection.\n\t */\n\titems?: unknown\n\t/**\n\t * Marks a server `purchase` or `refund`, the one path a negative `value` is allowed on. The wire\n\t * refuses it: a forged refund subtracts from revenue it never added to.\n\t */\n\trevenue?: boolean\n\t/**\n\t * The order this amount belongs to. The order's hit is keyed by a digest of it, so a retried\n\t * payment webhook counts once however often it arrives; the id itself is stored nowhere.\n\t */\n\torderId?: string\n\t/**\n\t * On a server refund, the refund's own id as the payment provider names it: it joins the order\n\t * id in the refund's key, so each partial refund counts once however often it is retried.\n\t */\n\trefundId?: string\n\t/**\n\t * On a pageview, its scroll depth, 0 when the tracker measures it afterwards; on an\n\t * engagement, how much deeper the page was scrolled since the previous one. 0-100.\n\t */\n\tscrollDepth?: number\n\t/**\n\t * Item ref (`<collection>:<id>`), server-signed when a page sends it (`offers:12@<signature>`).\n\t * Dropped, keeping the event, unless verified and of a collection bound as an item.\n\t */\n\titem?: string\n\t/**\n\t * On an engagement only: item values seen since the previous one, each checked like `item` and\n\t * capped at one beacon's worth.\n\t */\n\timpressions?: unknown\n\t/**\n\t * How the page measured this beacon; absent is `f`, which a tracker from before modes sends.\n\t * Ingest stores no more than the mode allows, whatever else the beacon carries.\n\t */\n\tm?: HitMode\n}\n\n/**\n * An event once ingest has sanitized, attributed and matched it: what `toHit` shapes into the row\n * that is stored. Nothing here is written as it stands.\n */\nexport interface NormalizedEvent {\n\t/** When it arrived, or for an engagement its pageview's start; see {@link eventTime}. */\n\ttimestamp: Date\n\ttype: HitType\n\tname?: string\n\tpath: string\n\thostname: string\n\t/**\n\t * Origin and path only, the query string and fragment stripped: whether a pageview opens a\n\t * visit is decided by it. Never stored.\n\t */\n\treferrer?: string\n\t/** The referrer's bare host, which a landing stores as `ref`; absent for the site's own. */\n\treferrerHost?: string\n\t/**\n\t * The referrer is another hostname its scope records hits under: a link inside the site, which\n\t * opens no visit, whatever hostname it led to.\n\t */\n\tsiteReferrer?: boolean\n\tdevice?: DeviceType\n\tbrowser?: BrowserName\n\tos?: OsName\n\t/** The five campaign keys extracted from the wire `query`; absent when it carried none. */\n\tutmSource?: string\n\tutmMedium?: string\n\tutmCampaign?: string\n\tutmContent?: string\n\tutmTerm?: string\n\tcountry?: string\n\tregion?: string\n\tcity?: string\n\t/** Primary `Accept-Language` tag, lowercased (`de-de`). */\n\tlanguage?: string\n\tdurationMs?: number\n\tprops?: Record<string, unknown>\n\tvalue?: number\n\t/**\n\t * ISO 4217 code of `value`: as sent without `revenue.currency`, else always the reporting\n\t * currency, with the `original` fields holding what arrived.\n\t */\n\tcurrency?: string\n\t/** The amount as sent, present only when it is not what `value` holds. */\n\toriginalValue?: number\n\toriginalCurrency?: string\n\t/**\n\t * The order hit's primary key derives from it, so the database refuses a second row for one order\n\t * even when two retries race.\n\t */\n\tdedupeKey?: string\n\t/** A server refund: its completions count their negative amount in revenue and no conversion. */\n\trefund?: true\n\tscrollDepth?: number\n\t/**\n\t * Goals this event completed, resolved against the install's goals at ingest so a later\n\t * goal edit never rewrites history.\n\t */\n\tgoals?: GoalCompletion[]\n\t/** Set only in scoped installs; '' is the null scope. */\n\tscope?: string\n\t/** The content item this event or goal belongs to, as its ref. */\n\titem?: string\n\t/** Items seen on screen since the previous engagement, on an engagement only. */\n\timpressions?: string[]\n\t/**\n\t * The day's visitor id: 16 characters of a keyed hash of the address, the agent and the site,\n\t * or `''` for a server event with neither an address nor a user agent of its own.\n\t */\n\tvisitorId: string\n\t/** The ad click id the query carried, by name (`gclid`, `fbclid`). */\n\tclickId?: string\n\t/** A pageview a single-page app made in place (`nav: 1`). */\n\tnavigation?: boolean\n\t/** Recorded by server code rather than sent by a browser. */\n\tserver?: boolean\n\t/** Absent is `f`. */\n\tmode?: HitMode\n}\n\ninterface NormalizeArgs {\n\traw: RawEventInput\n\t/** Already resolved and capped: what the event is attributed to, not what it claimed. */\n\thostname: string\n\theaders: Headers\n\tgeoResolver: GeoResolver\n\t/** Where a throwing resolver is reported; the event is stored without a location either way. */\n\twarnGeo?: (message: string) => void\n\t/** The salt of the visitor's day, which the visitor id is hashed with. */\n\tsalt: string\n\tnow: Date\n\tscope?: string\n\tgoals?: Goal[]\n\t/**\n\t * Revenue semantics belong to `'server'` alone, so the default is the unprivileged origin and\n\t * only `trackServerEvent` passes the other.\n\t */\n\torigin?: IngestOrigin\n\t/**\n\t * `revenue.browserAmounts`: how far an amount a browser sent is believed. Read for\n\t * `origin: 'wire'` alone; absent, `DEFAULT_BROWSER_AMOUNTS`.\n\t */\n\tbrowserAmounts?: BrowserAmounts\n\t/** The install's reporting currency; undefined stores and sums amounts exactly as sent. */\n\treportingCurrency?: string\n\tconvertCurrency?: CurrencyConverter\n\t/** Called at most once per process when an amount could not be converted. */\n\twarnUnconverted?: () => void\n\t/** Trusted proxies in front of the app, for the client address the hash and geo read. */\n\ttrustedProxyHops?: number\n\t/** The platform whose edge headers carry the client address and location, when trusted. */\n\ttrustedPlatform?: TrustedPlatform\n\t/** The collections bound as items, whose refs alone are kept. Absent keeps no item at all. */\n\titems?: ItemIngestOptions\n\t/**\n\t * The other hostnames the event's scope records hits under, bare: a referrer on one of them is\n\t * the site's own page, never a referral.\n\t */\n\tsiblings?: ReadonlySet<string>\n\t/** `orderDigestKey` of the instance's secret, which an order's key is digested with. */\n\torderKey?: Buffer\n\t/**\n\t * What the event may store: `a` hashes no visitor and keeps country alone, no time, scroll or\n\t * items seen. Absent is `f`, as for a beacon without `m`.\n\t */\n\tmode?: HitMode\n}\n\nexport interface ItemIngestOptions {\n\tcollections: ReadonlySet<string>\n\t/**\n\t * Each bound collection's id type, known once the database adapter is (at init). A ref whose\n\t * id cannot be one of its collection's ids is dropped like an unbound one.\n\t */\n\tidTypes?: ReadonlyMap<string, ItemIdType>\n\t/**\n\t * How a ref's signature is checked, known once the instance's secret is. Absent, nothing a page\n\t * sends can be verified, so only server code's own items are kept.\n\t */\n\tsignature?: ItemSignatureCheck\n}\n\nconst MAX_PROPS = 20\nconst MAX_KEY_LENGTH = 64\nconst MAX_VALUE_LENGTH = 256\nconst MAX_NAME_LENGTH = 128\nconst MAX_PATH_LENGTH = 512\nconst CURRENCY = /^[A-Z]{3}$/\n\n/**\n * When an event happened: its arrival less its `ageMs`, and an engagement's `sinceMs` too, so it is\n * filed at its pageview's start. Each is believed from 0 to a day, and together reach back a day.\n */\nexport const eventTime = (\n\traw: Pick<RawEventInput, 'type' | 'sinceMs' | 'ageMs'>,\n\treceivedAt: Date\n): Date => {\n\tconst waited = boundedSinceMs(raw.ageMs) ?? 0\n\tconst since = raw.type === 'engagement' ? (boundedSinceMs(raw.sinceMs) ?? 0) : 0\n\tconst back = Math.min(MAX_SINCE_MS, waited + since)\n\treturn back === 0 ? receivedAt : new Date(receivedAt.getTime() - back)\n}\n\n/**\n * Whether an event comes with a visitor of its own: an address, or a user agent other than the\n * one server tracking stamps when it has none. A beacon always does; server code may not.\n */\nconst identifiesVisitor = (ip: string, ua: string): boolean =>\n\tip !== '' || (ua !== '' && ua !== SERVER_USER_AGENT)\n\n/**\n * Optional wire fields are sanitized rather than rejected, so one malformed attribute never costs\n * a pageview.\n */\nconst nonNegative = (value: unknown): number | undefined => {\n\tconst bounded = boundedValue(value)\n\treturn bounded !== undefined && bounded >= 0 ? bounded : undefined\n}\n\n/** A refund is the one negative amount ingest keeps, and only on a server `refund` call. */\nconst signed = (value: unknown): number | undefined => boundedValue(value)\n\nconst currencyCode = (value: unknown): string | undefined =>\n\ttypeof value === 'string' && CURRENCY.test(value) ? value : undefined\n\n/**\n * The event's amount as far as it is believed: as sent under `trust` and `fixed` (where a goal's\n * fixed value still wins for its own completion), nothing under `none`.\n */\nexport const sentAmount = (args: {\n\tvalue: number | undefined\n\tcurrency: string | undefined\n\tbelieved: BrowserAmounts\n}): { value?: number; currency?: string } =>\n\targs.believed === 'none'\n\t\t? {}\n\t\t: {\n\t\t\t\t...(args.value === undefined ? {} : { value: args.value }),\n\t\t\t\t...(args.currency === undefined ? {} : { currency: args.currency }),\n\t\t\t}\n\n/** One completion as counted, and the money its amount came to, absent for one worth nothing. */\ninterface CountedCompletion {\n\tcompletion: GoalCompletion\n\tmoney?: Money\n}\n\n/**\n * Each completion reconciled on its own (a fixed value in its goal's currency, else the event's),\n * one conversion per distinct amount however many goals share it.\n */\nconst countCompletions = async (\n\tamounts: readonly CompletionAmount[],\n\targs: {\n\t\teventCurrency: string | undefined\n\t\treconcile: (value: number, currency: string | undefined) => Promise<Money>\n\t}\n): Promise<CountedCompletion[]> => {\n\tconst counted: CountedCompletion[] = []\n\tfor (const { goal, source, amount } of amounts) {\n\t\tif (source === 'none') {\n\t\t\tcounted.push({ completion: { slug: goal.slug, value: 0 } })\n\t\t\tcontinue\n\t\t}\n\t\tconst currency = source === 'fixed' ? currencyCode(goal.currency) : args.eventCurrency\n\t\tconst money = await args.reconcile(amount, currency)\n\t\tcounted.push({\n\t\t\tcompletion: {\n\t\t\t\tslug: goal.slug,\n\t\t\t\tvalue: money.value ?? 0,\n\t\t\t\t...(money.originalValue === undefined ? {} : { sent: money.originalValue }),\n\t\t\t},\n\t\t\tmoney,\n\t\t})\n\t}\n\treturn counted\n}\n\n/**\n * Infinity would poison every average for good, and a string would throw out of the write and take\n * the buffered batch with it.\n */\nconst duration = (value: unknown): number | undefined => {\n\tconst n = nonNegative(value)\n\treturn n === undefined || n > MAX_ENGAGEMENT_MS ? undefined : n\n}\n\n/** The event name is a value the `event` dimension groups by, so it is length-capped. */\nconst eventName = (value: unknown): string | undefined =>\n\ttypeof value === 'string' ? value.slice(0, MAX_NAME_LENGTH) : undefined\n\n/**\n * Capped, not trusted: the resolver reads headers a client can set. An empty value stays absent,\n * since '' would be a value of its own.\n */\nconst geoValue = (value: string | undefined): string | undefined =>\n\tvalue ? value.slice(0, MAX_GEO_LENGTH) : undefined\n\n/** Capped before it is parsed, so a hostile query cannot make ingest do unbounded work. */\nconst queryString = (value: unknown): string | undefined =>\n\ttypeof value === 'string' ? value.slice(0, MAX_QUERY_LENGTH) : undefined\n\nconst depth = (value: unknown): number | undefined => {\n\tconst n = nonNegative(value)\n\treturn n === undefined ? undefined : Math.round(Math.min(n, 100))\n}\n\n/**\n * Flat JSON scalars only, first 20 short-keyed entries. An order's event drops an `orderId` prop:\n * an order is kept as its digest alone.\n */\nconst sanitizeProps = (raw: unknown, order: boolean): Record<string, unknown> | undefined => {\n\tif (!isRecord(raw) || Array.isArray(raw)) {\n\t\treturn undefined\n\t}\n\tconst out: Record<string, unknown> = {}\n\tlet kept = 0\n\tfor (const [key, value] of Object.entries(raw)) {\n\t\tif (kept >= MAX_PROPS || key.length > MAX_KEY_LENGTH || (order && key === 'orderId')) {\n\t\t\tcontinue\n\t\t}\n\t\tif (typeof value === 'string') {\n\t\t\tout[key] = value.slice(0, MAX_VALUE_LENGTH)\n\t\t} else if (\n\t\t\ttypeof value === 'boolean' ||\n\t\t\t(typeof value === 'number' && Number.isFinite(value))\n\t\t) {\n\t\t\tout[key] = value\n\t\t} else {\n\t\t\tcontinue\n\t\t}\n\t\tkept++\n\t}\n\treturn kept > 0 ? out : undefined\n}\n\n/**\n * An engagement adds only time, scroll and items seen; time and scroll count from a pageview or an\n * engagement alone, and only an event or a goal belongs to an item.\n */\nconst fieldsOfType = (raw: RawEventInput): RawEventInput => {\n\tif (raw.type === 'engagement') {\n\t\tconst { type, path, hostname, referrer, query, durationMs, scrollDepth, impressions } = raw\n\t\treturn { type, path, hostname, referrer, query, durationMs, scrollDepth, impressions }\n\t}\n\tif (raw.type === 'pageview') {\n\t\tconst { item: _item, impressions: _impressions, ...pageview } = raw\n\t\treturn pageview\n\t}\n\tconst {\n\t\tdurationMs: _durationMs,\n\t\tscrollDepth: _scrollDepth,\n\t\timpressions: _impressions,\n\t\t...hit\n\t} = raw\n\treturn hit\n}\n\n/** An anonymous event keeps no time, scroll or items seen: engagement is a visitor's alone. */\nconst fieldsOfMode = (raw: RawEventInput, mode: HitMode): RawEventInput => {\n\tif (mode !== 'a') {\n\t\treturn raw\n\t}\n\tconst { durationMs: _durationMs, scrollDepth: _scrollDepth, impressions: _seen, ...kept } = raw\n\treturn kept\n}\n\n/**\n * The ref an item value is kept under: signed by this server (or trusted, or unsigned where\n * signatures are optional), of a bound collection, with an id that collection could hold.\n */\nconst keptItemRef = (\n\tvalue: unknown,\n\toptions: ItemIngestOptions,\n\torigin: IngestOrigin\n): string | undefined => {\n\tconst ref = verifiedItemRef(value, options.signature, origin === 'server')\n\treturn ref === undefined ? undefined : boundItemRef(ref, options.collections, options.idTypes)\n}\n\n/**\n * The refs of the items an engagement saw, each kept, once each, and no more than one beacon\n * carries. Anything else in the list is dropped rather than failing the engagement.\n */\nconst seenItems = (\n\tvalue: unknown,\n\toptions: ItemIngestOptions | undefined,\n\torigin: IngestOrigin\n): string[] => {\n\tif (!options || !Array.isArray(value)) {\n\t\treturn []\n\t}\n\tconst refs = new Set<string>()\n\tfor (const entry of value) {\n\t\tif (refs.size >= MAX_ITEMS_PER_BEACON) {\n\t\t\tbreak\n\t\t}\n\t\tconst ref = keptItemRef(entry, options, origin)\n\t\tif (ref !== undefined) {\n\t\t\trefs.add(ref)\n\t\t}\n\t}\n\treturn [...refs]\n}\n\n/** One event sanitized, attributed and goal-matched, alike for a beacon and for server code. */\nexport async function normalizeEvent({\n\traw: input,\n\thostname: rawHostname,\n\theaders,\n\tgeoResolver,\n\twarnGeo,\n\tsalt,\n\tnow,\n\tscope,\n\tgoals,\n\torigin = 'wire',\n\tbrowserAmounts = DEFAULT_BROWSER_AMOUNTS,\n\treportingCurrency,\n\tconvertCurrency,\n\twarnUnconverted,\n\ttrustedProxyHops,\n\ttrustedPlatform,\n\titems: itemOptions,\n\tsiblings,\n\torderKey,\n\tmode = 'f',\n}: NormalizeArgs): Promise<NormalizedEvent> {\n\tconst anonymous = mode === 'a'\n\tconst raw = fieldsOfMode(fieldsOfType(input), mode)\n\tconst item = itemOptions ? keptItemRef(raw.item, itemOptions, origin) : undefined\n\tconst impressions = seenItems(raw.impressions, itemOptions, origin)\n\tconst trust = { trustedProxyHops, trustedPlatform }\n\tconst geo = await resolveGeo(geoResolver, headers, { trust, warn: warnGeo })\n\t// An anonymous event's address is read for its country alone, never hashed.\n\tconst ip = anonymous ? '' : (clientIpFromHeaders(headers, trust) ?? '')\n\tconst ua = headers.get('user-agent') ?? ''\n\t// Every value a daily total is keyed by is capped first: Mongo's index key limit and Postgres'\n\t// 2704-byte btree key both fail the write. Geo, name, query and referrer cap below.\n\tconst path = raw.path.slice(0, MAX_PATH_LENGTH)\n\tconst hostname = rawHostname.slice(0, MAX_HOSTNAME_LENGTH)\n\tconst items = sanitizeItems(raw.items)\n\tconst sanitized = sanitizeProps(raw.props, raw.orderId !== undefined)\n\t// The lines ride under props rather than in a column of their own: they are read back with\n\t// the event, never grouped or filtered on, and `sanitizeProps` keeps scalars only.\n\tconst props = items === undefined ? sanitized : { ...sanitized, items }\n\tconst name = eventName(raw.name)\n\tconst isRevenue = origin === 'server' && raw.revenue === true\n\tconst refund = isRefund(isRevenue, raw.value)\n\tconst believed: BrowserAmounts = origin === 'server' ? 'trust' : browserAmounts\n\t// A server-only goal (`browser: false`) is one a browser cannot complete, whatever it sends.\n\tconst candidates = origin === 'wire' ? goals?.filter((goal) => goal.browser !== false) : goals\n\tconst matching = Boolean(candidates?.length) && raw.type !== 'engagement'\n\tconst eventCurrency = currencyCode(raw.currency)\n\tconst sent = sentAmount({\n\t\tvalue: isRevenue ? signed(raw.value) : nonNegative(raw.value),\n\t\tcurrency: eventCurrency,\n\t\tbelieved,\n\t})\n\tconst reconciled = new Map<string, Promise<Money>>()\n\tconst reconcile = (value: number | undefined, currency: string | undefined): Promise<Money> => {\n\t\tconst key = JSON.stringify([value ?? null, currency ?? null])\n\t\tconst known = reconciled.get(key)\n\t\tif (known) {\n\t\t\treturn known\n\t\t}\n\t\tconst money = reconcileMoney({\n\t\t\t...(value === undefined ? {} : { value }),\n\t\t\t...(currency === undefined ? {} : { currency }),\n\t\t\treportingCurrency,\n\t\t\tconvertCurrency,\n\t\t\tat: now,\n\t\t\twarn: warnUnconverted,\n\t\t})\n\t\treconciled.set(key, money)\n\t\treturn money\n\t}\n\t// Match on the sanitized fields, each completion's amount reconciled on its own, so a rejected\n\t// value never reaches a goal's revenue and a converted one is what revenue sums.\n\tconst counted =\n\t\tmatching && candidates\n\t\t\t? await countCompletions(\n\t\t\t\t\tcompletionAmounts({ type: raw.type, name, path, props, value: sent.value }, candidates, {\n\t\t\t\t\t\tallowNegativeValue: isRevenue,\n\t\t\t\t\t\tprops: believed !== 'none',\n\t\t\t\t\t\tfixedFirst: believed !== 'trust',\n\t\t\t\t\t}),\n\t\t\t\t\t{ eventCurrency, reconcile }\n\t\t\t\t)\n\t\t\t: []\n\tconst completions = counted.map((each) => each.completion)\n\t// The row carries the money of its first completion worth something, else the event's own.\n\tconst money =\n\t\tcounted.find((each) => each.money !== undefined)?.money ??\n\t\t(await reconcile(sent.value, sent.currency))\n\tconst order = sanitizeOrderId(raw.orderId)\n\tconst refundId = refund ? sanitizeOrderId(raw.refundId) : undefined\n\tconst scrollDepth = depth(raw.scrollDepth)\n\tconst durationMs = duration(raw.durationMs)\n\tconst device = classifyDevice(ua)\n\tconst browser = classifyBrowser(ua)\n\tconst os = classifyOs(ua)\n\tconst language = primaryLanguage(headers.get('accept-language'))\n\tconst refHost = referrerHost(raw.referrer, hostname, siblings)\n\tconst siteReferrer =\n\t\t!isSiteReferral(raw.referrer, hostname) && isSiteReferral(raw.referrer, hostname, siblings)\n\tconst query = queryString(raw.query)\n\tconst utm = extractUtm(query)\n\tconst clickId = clickIdOf(query)\n\treturn {\n\t\ttimestamp: now,\n\t\ttype: raw.type,\n\t\tname,\n\t\tpath,\n\t\thostname,\n\t\treferrer: storedReferrer(raw.referrer),\n\t\t...(refHost ? { referrerHost: refHost } : {}),\n\t\t...(siteReferrer ? { siteReferrer: true } : {}),\n\t\t...(device ? { device } : {}),\n\t\t...(browser ? { browser } : {}),\n\t\t...(os ? { os } : {}),\n\t\t...utm,\n\t\tcountry: geoValue(geo.country),\n\t\t...(anonymous ? {} : { region: geoValue(geo.region), city: geoValue(geo.city) }),\n\t\t...(language ? { language } : {}),\n\t\tprops,\n\t\t...(durationMs !== undefined ? { durationMs } : {}),\n\t\t...(money.value !== undefined ? { value: money.value } : {}),\n\t\t...(money.currency !== undefined ? { currency: money.currency } : {}),\n\t\t...(money.originalValue !== undefined ? { originalValue: money.originalValue } : {}),\n\t\t...(money.originalCurrency !== undefined ? { originalCurrency: money.originalCurrency } : {}),\n\t\t...(order\n\t\t\t? {\n\t\t\t\t\tdedupeKey: dedupeKeyFor({\n\t\t\t\t\t\torigin,\n\t\t\t\t\t\t...(scope ? { tenant: scope } : {}),\n\t\t\t\t\t\tgoal: name ?? raw.type,\n\t\t\t\t\t\torderId: order,\n\t\t\t\t\t\trefund,\n\t\t\t\t\t\t...(refundId === undefined ? {} : { refundId }),\n\t\t\t\t\t\t...(orderKey ? { key: orderKey } : {}),\n\t\t\t\t\t}),\n\t\t\t\t}\n\t\t\t: {}),\n\t\t...(refund ? { refund: true } : {}),\n\t\t...(scrollDepth !== undefined ? { scrollDepth } : {}),\n\t\t...(completions.length > 0 ? { goals: completions } : {}),\n\t\t...(scope !== undefined ? { scope } : {}),\n\t\t...(item !== undefined ? { item } : {}),\n\t\t...(impressions.length > 0 ? { impressions } : {}),\n\t\tvisitorId:\n\t\t\t!anonymous && identifiesVisitor(ip, ua)\n\t\t\t\t? dailyVisitorId({ ip, ua, site: hostname, salt })\n\t\t\t\t: '',\n\t\t...(clickId ? { clickId } : {}),\n\t\t...(input.type === 'pageview' && isNavigation(input.nav) ? { navigation: true } : {}),\n\t\t...(origin === 'server' ? { server: true } : {}),\n\t\tmode,\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA4QA,MAAM,YAAY;AAClB,MAAM,iBAAiB;AACvB,MAAM,mBAAmB;AACzB,MAAM,kBAAk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AAA,IAAY,EAAE,KAAK,IAAI,CAAC;EACrC,GAAI,YAAY,SAAS,IAAI,EAAE,YAAY,IAAI,CAAC;EAChD,WACC,CAAC,aAAa,kBAAkB,IAAI,EAAE,IACnC,eAAe;GAAE;GAAI;GAAI,MAAM;GAAU;EAAK,CAAC,IAC/C;EACJ,GAAI,UAAU,EAAE,QAAQ,IAAI,CAAC;EAC7B,GAAI,MAAM,SAAS,cAAc,aAAa,MAAM,GAAG,IAAI,EAAE,YAAY,KAAK,IAAI,CAAC;EACnF,GAAI,WAAW,WAAW,EAAE,QAAQ,KAAK,IAAI,CAAC;EAC9C;CACD;AACD"}
|
|
@@ -169,7 +169,8 @@ const makeServerTrack = (deps) => async (payload, event, opts) => {
|
|
|
169
169
|
...trust,
|
|
170
170
|
...target.items ? { items: target.items } : {},
|
|
171
171
|
...siblings ? { siblings } : {},
|
|
172
|
-
orderKey
|
|
172
|
+
orderKey,
|
|
173
|
+
mode: identity === "none" ? "a" : "f"
|
|
173
174
|
});
|
|
174
175
|
if (event.revenue === true && normalized.goals === void 0) warnUnmatchedRevenueGoal(payload.logger, event.name ?? event.type);
|
|
175
176
|
const late = placed?.closed === true;
|