@aglyn/plugins-marketing 1.0.0-beta.221 → 1.0.0-beta.223
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/README.md +4 -1
- package/package.json +15 -15
- package/src/lib/components/campaign-conversions-card.js +53 -64
- package/src/lib/components/campaign-conversions-card.js.map +1 -1
- package/src/lib/components/campaign-detail-card.js +18 -3
- package/src/lib/components/campaign-detail-card.js.map +1 -1
- package/src/lib/components/campaign-edit-drawer.d.ts +6 -0
- package/src/lib/components/campaign-edit-drawer.js +13 -2
- package/src/lib/components/campaign-edit-drawer.js.map +1 -1
- package/src/lib/components/campaign-queries.d.ts +5 -0
- package/src/lib/components/campaign-queries.js +8 -1
- package/src/lib/components/campaign-queries.js.map +1 -1
- package/src/lib/components/campaign-reach-sections.d.ts +28 -0
- package/src/lib/components/campaign-reach-sections.js +96 -5
- package/src/lib/components/campaign-reach-sections.js.map +1 -1
- package/src/lib/components/campaign-report-card.js +5 -4
- package/src/lib/components/campaign-report-card.js.map +1 -1
- package/src/lib/components/campaigns-card.js +1 -1
- package/src/lib/components/campaigns-card.js.map +1 -1
- package/src/lib/components/conversion-attribution.component.d.ts +18 -0
- package/src/lib/components/conversion-attribution.component.js +177 -39
- package/src/lib/components/conversion-attribution.component.js.map +1 -1
- package/src/lib/components/email-compose-card.js +1 -1
- package/src/lib/components/email-compose-card.js.map +1 -1
- package/src/lib/components/email-detail.js +5 -4
- package/src/lib/components/email-detail.js.map +1 -1
- package/src/lib/components/email-template-report-card.d.ts +53 -0
- package/src/lib/components/email-template-report-card.js +469 -0
- package/src/lib/components/email-template-report-card.js.map +1 -0
- package/src/lib/components/emails-list-card.js +2 -2
- package/src/lib/components/emails-list-card.js.map +1 -1
- package/src/lib/components/host-experiments-card.component.js +3 -2
- package/src/lib/components/host-experiments-card.component.js.map +1 -1
- package/src/lib/components/record-attribution-widget.d.ts +5 -0
- package/src/lib/components/record-attribution-widget.js +6 -3
- package/src/lib/components/record-attribution-widget.js.map +1 -1
- package/src/lib/components/site-runtime.d.ts +5 -0
- package/src/lib/components/site-runtime.js +104 -3
- package/src/lib/components/site-runtime.js.map +1 -1
- package/src/lib/declarations.server.d.ts +9 -0
- package/src/lib/declarations.server.js +78 -0
- package/src/lib/declarations.server.js.map +1 -1
- package/src/lib/model/campaign-container.d.ts +531 -0
- package/src/lib/model/campaign-container.js +532 -0
- package/src/lib/model/campaign-container.js.map +1 -0
- package/src/lib/model/campaign-conversions.d.ts +55 -4
- package/src/lib/model/campaign-conversions.js +60 -4
- package/src/lib/model/campaign-conversions.js.map +1 -1
- package/src/lib/model/campaign-report.d.ts +158 -0
- package/src/lib/model/campaign-report.js +268 -0
- package/src/lib/model/campaign-report.js.map +1 -0
- package/src/lib/model/campaign-revenue.d.ts +3 -3
- package/src/lib/model/campaign-revenue.js +3 -3
- package/src/lib/model/campaign-revenue.js.map +1 -1
- package/src/lib/model/campaign-send-time.js +2 -2
- package/src/lib/model/campaign-send-time.js.map +1 -1
- package/src/lib/model/campaign-visits.d.ts +74 -0
- package/src/lib/model/campaign-visits.js +65 -0
- package/src/lib/model/campaign-visits.js.map +1 -0
- package/src/lib/model/compile-client-automations.d.ts +2 -2
- package/src/lib/model/compile-client-automations.js +3 -3
- package/src/lib/model/compile-client-automations.js.map +1 -1
- package/src/lib/model/email-record.d.ts +175 -0
- package/src/lib/model/email-record.js +206 -0
- package/src/lib/model/email-record.js.map +1 -0
- package/src/lib/model/site-contract.d.ts +2 -2
- package/src/lib/model/site-contract.js.map +1 -1
- package/src/lib/model/template-report.d.ts +158 -0
- package/src/lib/model/template-report.js +249 -0
- package/src/lib/model/template-report.js.map +1 -0
- package/src/lib/plugin.js +16 -0
- package/src/lib/plugin.js.map +1 -1
- package/src/lib/preview-props.js.map +1 -1
- package/src/lib/server/campaign-attribution-store.d.ts +55 -0
- package/src/lib/server/campaign-attribution-store.js +98 -0
- package/src/lib/server/campaign-attribution-store.js.map +1 -0
- package/src/lib/server/campaign-conversion-attribution.d.ts +314 -0
- package/src/lib/server/campaign-conversion-attribution.js +549 -0
- package/src/lib/server/campaign-conversion-attribution.js.map +1 -0
- package/src/lib/server/campaign-manage.js +1 -1
- package/src/lib/server/campaign-manage.js.map +1 -1
- package/src/lib/server/campaign-org-refs.js +1 -1
- package/src/lib/server/campaign-org-refs.js.map +1 -1
- package/src/lib/server/campaign-recipients.js +7 -7
- package/src/lib/server/campaign-recipients.js.map +1 -1
- package/src/lib/server/campaign-send.js +25 -19
- package/src/lib/server/campaign-send.js.map +1 -1
- package/src/lib/server/campaign-touch-targets.d.ts +61 -0
- package/src/lib/server/campaign-touch-targets.js +162 -0
- package/src/lib/server/campaign-touch-targets.js.map +1 -0
- package/src/lib/server/campaign-visit-beacon.d.ts +44 -0
- package/src/lib/server/campaign-visit-beacon.js +125 -0
- package/src/lib/server/campaign-visit-beacon.js.map +1 -0
- package/src/lib/server/conversion-creditor.d.ts +18 -0
- package/src/lib/server/conversion-creditor.js +238 -0
- package/src/lib/server/conversion-creditor.js.map +1 -0
- package/src/lib/server/email-campaign-touch.d.ts +81 -0
- package/src/lib/server/email-campaign-touch.js +227 -0
- package/src/lib/server/email-campaign-touch.js.map +1 -0
- package/src/lib/server/email-events.js +32 -31
- package/src/lib/server/email-events.js.map +1 -1
- package/src/lib/server/email-revenue-attribution.d.ts +186 -0
- package/src/lib/server/email-revenue-attribution.js +322 -0
- package/src/lib/server/email-revenue-attribution.js.map +1 -0
- package/src/lib/server/email-send-record-index.d.ts +34 -0
- package/src/lib/server/email-send-record-index.js +90 -0
- package/src/lib/server/email-send-record-index.js.map +1 -0
- package/src/lib/server/get-client-automations.js.map +1 -1
- package/src/lib/server/marketing-figures.js +3 -3
- package/src/lib/server/marketing-figures.js.map +1 -1
- package/src/lib/server/send-tally.d.ts +32 -0
- package/src/lib/server/send-tally.js +54 -0
- package/src/lib/server/send-tally.js.map +1 -0
- package/src/lib/server/site-page-enricher.js +18 -2
- package/src/lib/server/site-page-enricher.js.map +1 -1
- package/src/lib/testing/stand-in-person-records.d.ts +35 -0
- package/src/lib/testing/stand-in-person-records.js +67 -0
- package/src/lib/testing/stand-in-person-records.js.map +1 -0
- package/src/lib/testing/stand-in-record-counts.d.ts +26 -0
- package/src/lib/testing/stand-in-record-counts.js +70 -0
- package/src/lib/testing/stand-in-record-counts.js.map +1 -0
- package/src/lib/transfer/campaigns-package.d.ts +72 -0
- package/src/lib/transfer/campaigns-package.js +145 -0
- package/src/lib/transfer/campaigns-package.js.map +1 -0
- package/src/lib/transfer/campaigns-package.server.d.ts +33 -0
- package/src/lib/transfer/campaigns-package.server.js +305 -0
- package/src/lib/transfer/campaigns-package.server.js.map +1 -0
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
* See the License for the specific language governing permissions and
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
|
-
import type { CampaignCaveat } from '
|
|
17
|
+
import type { CampaignCaveat } from './campaign-report';
|
|
18
18
|
/**
|
|
19
19
|
* WHAT A CAMPAIGN CAUSED — the read half of the identify-moment join.
|
|
20
20
|
*
|
|
@@ -56,6 +56,15 @@ import type { CampaignCaveat } from '@aglyn/shared-ui-email-campaigns/model/camp
|
|
|
56
56
|
* this reader states that where the figure is drawn, and web-channel records
|
|
57
57
|
* are read as records — see {@link campaignConversionsCoverage}.
|
|
58
58
|
*
|
|
59
|
+
* ## A PAGE OR A DECLARED LABEL NAMES THE CAMPAIGN ITSELF (AGL-3461)
|
|
60
|
+
*
|
|
61
|
+
* Two more touches name a campaign CONTAINER rather than a send: a page filed
|
|
62
|
+
* under the campaign, and a `utm_campaign` label the campaign declares as its
|
|
63
|
+
* own. Their records carry the container's id as `campaignId`, so the
|
|
64
|
+
* campaign's page counts them with the same `kind` + `campaignId` aggregation
|
|
65
|
+
* it counts its sends' conversions with — keyed by a document id, so bounded
|
|
66
|
+
* by the campaigns that exist, and needing no rollup document of their own.
|
|
67
|
+
*
|
|
59
68
|
* ## A CONVERSION WITH NO TOUCH IS NOT IN HERE AT ALL
|
|
60
69
|
*
|
|
61
70
|
* Direct traffic writes no record. There is deliberately no
|
|
@@ -87,7 +96,43 @@ export type CampaignConversionKind = (typeof CAMPAIGN_CONVERSION_KINDS)[number];
|
|
|
87
96
|
* the campaign whose mail was clicked, plus the sequence and the enrollment
|
|
88
97
|
* it came through, so a record can say which rep's outreach it credits.
|
|
89
98
|
*/
|
|
90
|
-
export type CampaignTouchChannel = 'email' | 'web' | 'sequence';
|
|
99
|
+
export type CampaignTouchChannel = 'email' | 'web' | 'sequence' | 'page';
|
|
100
|
+
/**
|
|
101
|
+
* THE RULE an identify moment is credited under (AGL-3461), stamped on every
|
|
102
|
+
* record as `model` so a reader can print what was counted.
|
|
103
|
+
*
|
|
104
|
+
* `last-click` — the revenue join's rule, which an order still uses — named
|
|
105
|
+
* the later of a campaign email click and a labeled link. A page filed under a
|
|
106
|
+
* campaign is a touch too now, and it is not a click, so the identify moment
|
|
107
|
+
* says so:
|
|
108
|
+
*
|
|
109
|
+
* - the later of a campaign email click and a link carrying `utm_` labels
|
|
110
|
+
* wins, exactly as before;
|
|
111
|
+
* - a page filed under a campaign competes with that winner. Within
|
|
112
|
+
* {@link CAMPAIGN_VISIT_MINUTES} of it the two are one visit: a click that
|
|
113
|
+
* names a campaign — its mail, or a label the campaign declares — keeps
|
|
114
|
+
* the credit for the page it landed on, and a label that names none
|
|
115
|
+
* yields to the page. Further apart, the later one wins;
|
|
116
|
+
* - all of it inside the seven-day window, as before.
|
|
117
|
+
*/
|
|
118
|
+
export declare const CAMPAIGN_TOUCH_MODEL = "last-touch";
|
|
119
|
+
/** How close a page view and a click are to be one visit, in minutes. */
|
|
120
|
+
export declare const CAMPAIGN_VISIT_MINUTES = 30;
|
|
121
|
+
/** The same, in milliseconds. */
|
|
122
|
+
export declare const CAMPAIGN_VISIT_MS: number;
|
|
123
|
+
/**
|
|
124
|
+
* The rule a record or a rollup was credited under, as one sentence a reader
|
|
125
|
+
* can check the figure against. Keyed off the STORED model, so a conversion
|
|
126
|
+
* credited under an older rule prints the rule it was credited under.
|
|
127
|
+
*/
|
|
128
|
+
export declare function campaignCreditRule(model: string | null | undefined, windowDays: number | null | undefined): string;
|
|
129
|
+
/**
|
|
130
|
+
* The record each conversion kind credits, as its owner publishes the kind
|
|
131
|
+
* (`plugin-record-counts`, `plugin-record-lists`): a credited form names the
|
|
132
|
+
* SUBMISSION it credits, the rest the record of their own name. The owners
|
|
133
|
+
* count and read those records; this plugin only names the kind.
|
|
134
|
+
*/
|
|
135
|
+
export declare const CAMPAIGN_CONVERSION_RECORD_KINDS: Readonly<Record<CampaignConversionKind, string>>;
|
|
91
136
|
/**
|
|
92
137
|
* What a reader calls one kind, and what the count means.
|
|
93
138
|
*
|
|
@@ -147,6 +192,10 @@ export interface CampaignConversionRecord {
|
|
|
147
192
|
touchedAtMs?: number;
|
|
148
193
|
/** When the visitor became identifiable, epoch ms. */
|
|
149
194
|
convertedAtMs?: number;
|
|
195
|
+
/** The screen the visitor viewed, for a `page` touch (AGL-3461). */
|
|
196
|
+
screenId?: string;
|
|
197
|
+
/** That page's path, for a `page` touch. */
|
|
198
|
+
path?: string;
|
|
150
199
|
model?: string;
|
|
151
200
|
windowDays?: number;
|
|
152
201
|
}
|
|
@@ -290,8 +339,10 @@ export declare function campaignConversionsCoverage(options: {
|
|
|
290
339
|
*
|
|
291
340
|
* The email channel names a campaign document, so the screen can link to it,
|
|
292
341
|
* and so does a sequence touch: the campaign the sequence is in is a
|
|
293
|
-
* container with a page
|
|
294
|
-
*
|
|
342
|
+
* container with a page — as does a page touch, the campaign the viewed page
|
|
343
|
+
* is filed under (AGL-3461). The web channel names a label the marketer
|
|
344
|
+
* typed, which is text unless a campaign declares the label as its own; then
|
|
345
|
+
* the record carries that campaign's id too and the screen links it.
|
|
295
346
|
*
|
|
296
347
|
* The `utm_` triple is joined in the order a marketer set it, with the parts
|
|
297
348
|
* that are absent left out rather than filled with a placeholder.
|
|
@@ -60,6 +60,15 @@
|
|
|
60
60
|
* this reader states that where the figure is drawn, and web-channel records
|
|
61
61
|
* are read as records — see {@link campaignConversionsCoverage}.
|
|
62
62
|
*
|
|
63
|
+
* ## A PAGE OR A DECLARED LABEL NAMES THE CAMPAIGN ITSELF (AGL-3461)
|
|
64
|
+
*
|
|
65
|
+
* Two more touches name a campaign CONTAINER rather than a send: a page filed
|
|
66
|
+
* under the campaign, and a `utm_campaign` label the campaign declares as its
|
|
67
|
+
* own. Their records carry the container's id as `campaignId`, so the
|
|
68
|
+
* campaign's page counts them with the same `kind` + `campaignId` aggregation
|
|
69
|
+
* it counts its sends' conversions with — keyed by a document id, so bounded
|
|
70
|
+
* by the campaigns that exist, and needing no rollup document of their own.
|
|
71
|
+
*
|
|
63
72
|
* ## A CONVERSION WITH NO TOUCH IS NOT IN HERE AT ALL
|
|
64
73
|
*
|
|
65
74
|
* Direct traffic writes no record. There is deliberately no
|
|
@@ -91,6 +100,51 @@
|
|
|
91
100
|
'contact',
|
|
92
101
|
'booking'
|
|
93
102
|
];
|
|
103
|
+
/**
|
|
104
|
+
* THE RULE an identify moment is credited under (AGL-3461), stamped on every
|
|
105
|
+
* record as `model` so a reader can print what was counted.
|
|
106
|
+
*
|
|
107
|
+
* `last-click` — the revenue join's rule, which an order still uses — named
|
|
108
|
+
* the later of a campaign email click and a labeled link. A page filed under a
|
|
109
|
+
* campaign is a touch too now, and it is not a click, so the identify moment
|
|
110
|
+
* says so:
|
|
111
|
+
*
|
|
112
|
+
* - the later of a campaign email click and a link carrying `utm_` labels
|
|
113
|
+
* wins, exactly as before;
|
|
114
|
+
* - a page filed under a campaign competes with that winner. Within
|
|
115
|
+
* {@link CAMPAIGN_VISIT_MINUTES} of it the two are one visit: a click that
|
|
116
|
+
* names a campaign — its mail, or a label the campaign declares — keeps
|
|
117
|
+
* the credit for the page it landed on, and a label that names none
|
|
118
|
+
* yields to the page. Further apart, the later one wins;
|
|
119
|
+
* - all of it inside the seven-day window, as before.
|
|
120
|
+
*/ export const CAMPAIGN_TOUCH_MODEL = 'last-touch';
|
|
121
|
+
/** How close a page view and a click are to be one visit, in minutes. */ export const CAMPAIGN_VISIT_MINUTES = 30;
|
|
122
|
+
/** The same, in milliseconds. */ export const CAMPAIGN_VISIT_MS = CAMPAIGN_VISIT_MINUTES * 60 * 1000;
|
|
123
|
+
/**
|
|
124
|
+
* The rule a record or a rollup was credited under, as one sentence a reader
|
|
125
|
+
* can check the figure against. Keyed off the STORED model, so a conversion
|
|
126
|
+
* credited under an older rule prints the rule it was credited under.
|
|
127
|
+
*/ export function campaignCreditRule(model, windowDays) {
|
|
128
|
+
const days = Number(windowDays != null ? windowDays : 0) || EMAIL_ATTRIBUTION_WINDOW_DAYS;
|
|
129
|
+
if (model === CAMPAIGN_TOUCH_MODEL) {
|
|
130
|
+
return `Credited to the visitor’s latest campaign touch within ${days} days — a ` + 'campaign email they clicked, a link labeled for a campaign, or a page ' + `filed under one. A page viewed within ${CAMPAIGN_VISIT_MINUTES} minutes of ` + 'a click is that click’s visit, and the click keeps the credit when it ' + 'names a campaign.';
|
|
131
|
+
}
|
|
132
|
+
if (model === EMAIL_ATTRIBUTION_MODEL) {
|
|
133
|
+
return `Credited to the last campaign whose link they clicked, within ${days} days of that click.`;
|
|
134
|
+
}
|
|
135
|
+
return `Credited under the ${String(model != null ? model : 'recorded')} model, within ${days} days.`;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* The record each conversion kind credits, as its owner publishes the kind
|
|
139
|
+
* (`plugin-record-counts`, `plugin-record-lists`): a credited form names the
|
|
140
|
+
* SUBMISSION it credits, the rest the record of their own name. The owners
|
|
141
|
+
* count and read those records; this plugin only names the kind.
|
|
142
|
+
*/ export const CAMPAIGN_CONVERSION_RECORD_KINDS = {
|
|
143
|
+
form: 'formSubmission',
|
|
144
|
+
lead: 'lead',
|
|
145
|
+
contact: 'contact',
|
|
146
|
+
booking: 'booking'
|
|
147
|
+
};
|
|
94
148
|
export const CAMPAIGN_CONVERSION_KIND_COPY = {
|
|
95
149
|
form: {
|
|
96
150
|
label: 'Form submissions',
|
|
@@ -170,7 +224,7 @@ export const CAMPAIGN_CONVERSION_KIND_COPY = {
|
|
|
170
224
|
});
|
|
171
225
|
caveats.push({
|
|
172
226
|
id: 'conversions-web-not-rolled-up',
|
|
173
|
-
message: '
|
|
227
|
+
message: 'This email only. A conversion credited to a page filed under the ' + 'campaign, or to a link labeled for it, is counted on the ' + 'campaign’s own page; one credited to a utm_ label no campaign ' + 'declares is listed under Conversions in the marketing console.'
|
|
174
228
|
});
|
|
175
229
|
}
|
|
176
230
|
return {
|
|
@@ -228,8 +282,10 @@ export const CAMPAIGN_CONVERSION_KIND_COPY = {
|
|
|
228
282
|
*
|
|
229
283
|
* The email channel names a campaign document, so the screen can link to it,
|
|
230
284
|
* and so does a sequence touch: the campaign the sequence is in is a
|
|
231
|
-
* container with a page
|
|
232
|
-
*
|
|
285
|
+
* container with a page — as does a page touch, the campaign the viewed page
|
|
286
|
+
* is filed under (AGL-3461). The web channel names a label the marketer
|
|
287
|
+
* typed, which is text unless a campaign declares the label as its own; then
|
|
288
|
+
* the record carries that campaign's id too and the screen links it.
|
|
233
289
|
*
|
|
234
290
|
* The `utm_` triple is joined in the order a marketer set it, with the parts
|
|
235
291
|
* that are absent left out rather than filled with a placeholder.
|
|
@@ -237,7 +293,7 @@ export const CAMPAIGN_CONVERSION_KIND_COPY = {
|
|
|
237
293
|
* one of them invites the reader to look for a campaign called "(none)".
|
|
238
294
|
*/ export function campaignTouchLabel(record) {
|
|
239
295
|
if (!record) return '';
|
|
240
|
-
if (record.channel === 'email' || record.channel === 'sequence') {
|
|
296
|
+
if (record.channel === 'email' || record.channel === 'sequence' || record.channel === 'page') {
|
|
241
297
|
var _record_campaignId;
|
|
242
298
|
return String((_record_campaignId = record.campaignId) != null ? _record_campaignId : '');
|
|
243
299
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../../../libs/plugins/marketing/src/lib/model/campaign-conversions.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/*\n * The window and the model name come from `@aglyn/shared-util-email`, for the\n * reason `campaign-revenue.ts` gives: the writer is in `tenant-data-admin`,\n * which may not import a feature plugin, and a rule defined on both sides of\n * the join drifts into a figure credited under one and printed under another.\n */\nimport {\n EMAIL_ATTRIBUTION_MODEL,\n EMAIL_ATTRIBUTION_WINDOW_DAYS,\n} from '@aglyn/shared-util-email'\nimport type { CampaignCaveat } from '@aglyn/shared-ui-email-campaigns/model/campaign-report'\n\n/**\n * WHAT A CAMPAIGN CAUSED — the read half of the identify-moment join.\n *\n * `campaign-revenue.ts` answers what a campaign EARNED, from orders that name\n * their buyer. This answers what it CAUSED among people who were anonymous\n * until the moment they became somebody: they arrive from a campaign link,\n * browse, and only become identifiable when they submit a form, sign up, book\n * or check out. Same window, same last-click rule, same `model`/`windowDays`\n * stamped on every record, so the two figures on one screen are two readings\n * of one rule rather than two rules that happen to agree.\n *\n * ## THE KINDS ARE NEVER SUMMED, and this module makes that structural\n *\n * One form submission by a new person writes a submission record, a contact\n * and a lead — three true statements about one visitor action. Adding them\n * would treble every campaign's conversions, and the reader would have no way\n * to see it had happened, because the sum looks like a bigger version of a\n * real number.\n *\n * So {@link CampaignConversionsReport} carries no total, no `all`, no\n * `conversions` scalar, and there is no function here that reduces the kinds.\n * A screen that wanted one would have to write the addition itself. The\n * report also raises {@link CampaignCaveat} `conversions-kinds-overlap` so\n * the reader is told WHY the four figures stand apart, rather than being left\n * to assume the product forgot to add them up.\n *\n * ## THE WEB CHANNEL HAS NO ROLLUP, and cannot be given one\n *\n * A conversion is credited to one of two touches. An EMAIL touch names a\n * campaign document — a real entity with a real id — so its conversions roll\n * up under it beside the revenue the same join credits it with. A WEB touch\n * is a `utm_` label a marketer typed into a URL: no document, no id, and no\n * bound on how many distinct values exist, because anybody who can vary a\n * query string can mint another one.\n *\n * A rollup keyed on that label is a map a stranger can grow, which is the\n * same unbounded key space the analytics collector caps its per-day label map\n * against. So the writer increments the rollup for the email channel ONLY,\n * this reader states that where the figure is drawn, and web-channel records\n * are read as records — see {@link campaignConversionsCoverage}.\n *\n * ## A CONVERSION WITH NO TOUCH IS NOT IN HERE AT ALL\n *\n * Direct traffic writes no record. There is deliberately no\n * `utm_source=direct` placeholder, no referrer inference and no \"most recent\n * campaign on this site\" fallback: a conversion nobody can be credited with\n * is a conversion nobody is credited with.\n *\n * That makes the absence of a record load-bearing, and it makes a screen\n * showing only attributed conversions a lie by omission — it renders \"we\n * credited three of these\" as \"three of these happened\".\n * {@link campaignConversionsCoverage} exists so the unattributed count is a\n * figure on the page rather than an inference nobody makes.\n */\n\n/*\n * The constants are NOT re-exported here, though `campaign-revenue.ts`\n * re-exports the same two. Both modules sit behind one barrel, and a name\n * exported twice through it is a name a bundler has to disambiguate. Every\n * screen reads the rule off `model`/`windowDays` on the report anyway, which\n * is the stored value rather than today's constant — a campaign credited\n * under an older window has to print the window it was credited under.\n */\n\n/**\n * Which identify moment a record credits.\n *\n * An array first, because the ORDER is the reading order on every screen and\n * a second list of these is a second chance to leave one out. The union is\n * derived from it rather than written twice.\n */\nexport const CAMPAIGN_CONVERSION_KINDS = [\n 'form',\n 'lead',\n 'contact',\n 'booking',\n] as const\n\n/** One identify moment. Mirrors the writer's `CampaignConversionKind`. */\nexport type CampaignConversionKind = (typeof CAMPAIGN_CONVERSION_KINDS)[number]\n\n/**\n * Which channel the credited touch arrived through.\n *\n * `sequence` (AGL-3254) is a one-to-one email a rep's sequence sent: the\n * touch names the CAMPAIGN the sequence is in, like an email touch names\n * the campaign whose mail was clicked, plus the sequence and the enrollment\n * it came through, so a record can say which rep's outreach it credits.\n */\nexport type CampaignTouchChannel = 'email' | 'web' | 'sequence'\n\n/**\n * What a reader calls one kind, and what the count means.\n *\n * The note is the half that stops the addition. Each one names a DIFFERENT\n * population of the same visitors, and saying so under every figure is what\n * makes four numbers standing apart read as deliberate rather than as a\n * missing total.\n */\nexport interface CampaignConversionKindCopy {\n label: string\n note: string\n}\n\nexport const CAMPAIGN_CONVERSION_KIND_COPY: Readonly<\n Record<CampaignConversionKind, CampaignConversionKindCopy>\n> = {\n form: {\n label: 'Form submissions',\n note: 'submissions credited to this campaign',\n },\n lead: { label: 'Leads', note: 'new leads credited to this campaign' },\n contact: {\n label: 'Contacts',\n note: 'new contacts credited to this campaign',\n },\n booking: { label: 'Bookings', note: 'bookings credited to this campaign' },\n}\n\n/**\n * The stored shape of `campaigns/{campaignId}/reports/conversions`.\n *\n * Its own document beside `reports/revenue`, for the reason that one is: the\n * campaign document is read by the history list, the glance widget and the\n * send path, and a map that grows with what the campaign caused would enlarge\n * every one of those reads.\n */\nexport interface CampaignConversionsRollup {\n byKind?: Partial<Record<CampaignConversionKind, number>>\n /** The model these conversions were credited under. */\n model?: string\n /** The window, in days, they were credited inside. */\n windowDays?: number\n}\n\n/**\n * The stored shape of `hosts/{hostId}/campaignAttributions/{kind}:{refId}`.\n *\n * Read-side only, and every field optional: this is a document somebody\n * else's writer produced, and a reader that assumed a field was present would\n * throw on the first record written by an older version of it.\n */\nexport interface CampaignConversionRecord {\n kind?: CampaignConversionKind\n /** The submission, lead, contact or booking this credits. */\n refId?: string\n channel?: CampaignTouchChannel\n /**\n * The campaign document, when the touch was a click on our own mail — or,\n * for a `sequence` touch, the campaign container the sequence is in.\n */\n campaignId?: string\n /** The sequence, when the touch was a sequence email (AGL-3254). */\n sequenceId?: string\n /** The enrollment the sequence email went out under (AGL-3254). */\n enrollmentId?: string\n /** `utm_source`, when the touch was a link on the web. */\n source?: string\n /** `utm_medium`, when the touch was a link on the web. */\n medium?: string\n /** `utm_campaign`, when the touch was a link on the web. */\n campaign?: string\n /** When the visitor followed the campaign link, epoch ms. */\n touchedAtMs?: number\n /** When the visitor became identifiable, epoch ms. */\n convertedAtMs?: number\n model?: string\n windowDays?: number\n}\n\n/**\n * The document id for one conversion — `{kind}:{refId}`.\n *\n * The reader's half of the writer's `campaignConversionId`. It is restated\n * here rather than imported because the writer lives in `tenant-data-admin`,\n * which a UI library may not import, and it is one line whose shape is\n * asserted in this module's spec.\n *\n * Answers `null` for an unusable pair rather than building `form:undefined`,\n * which would be a valid document path pointing at a record that can never\n * exist — a keyed read that silently reports \"not attributed\" for every\n * record on the screen.\n */\nexport function campaignConversionId(\n kind: string | null | undefined,\n refId: string | null | undefined,\n): string | null {\n const k = String(kind ?? '')\n const ref = String(refId ?? '')\n if (!k || !ref) return null\n if (!(CAMPAIGN_CONVERSION_KINDS as readonly string[]).includes(k)) return null\n // A slash would leave the collection; the id scheme already spends the\n // colon, and a ref carrying one would make the pair ambiguous.\n if (ref.includes('/') || ref.includes(':')) return null\n return `${k}:${ref}`\n}\n\n/** A stored count as a non-negative integer. */\nfunction count(raw: unknown): number {\n const value = Math.floor(Number(raw ?? 0))\n return Number.isFinite(value) && value > 0 ? value : 0\n}\n\n/** One kind's figure on screen. Independent of every other kind's. */\nexport interface CampaignConversionKindReport {\n kind: CampaignConversionKind\n label: string\n /**\n * Conversions of this kind, or `null` when the rollup holds no entry.\n *\n * `null` rather than 0 for {@link CampaignConversionsReport.recorded}'s\n * reason, applied per kind: a site with no booking form has never written a\n * booking conversion, and rendering that as a measured zero invites the\n * reader to conclude the campaign failed at something it never attempted.\n */\n value: number | null\n note: string\n}\n\n/** Everything the conversions section renders. */\nexport interface CampaignConversionsReport {\n /**\n * One entry per kind, always all four and always in\n * {@link CAMPAIGN_CONVERSION_KINDS} order.\n *\n * There is deliberately NO total beside this array, and no field anywhere\n * on this report that holds one. See the module docblock.\n */\n kinds: CampaignConversionKindReport[]\n /**\n * Whether the rollup document exists at all.\n *\n * `false` is NOT \"this campaign caused nothing\" — it is also every campaign\n * sent before the join existed, and every campaign whose conversions all\n * arrived through the web channel, which writes no rollup. The screen\n * renders the difference rather than printing a zero for all three.\n */\n recorded: boolean\n /** At least one kind holds a figure. */\n any: boolean\n /** The model these figures were credited under, as stored. */\n model: string\n /** The window they were credited inside, as stored. */\n windowDays: number\n caveats: CampaignCaveat[]\n}\n\n/**\n * Turns the stored rollup into the conversions section.\n *\n * Takes the rollup and nothing else. Unlike the revenue report there is no\n * denominator to hand in: a conversion RATE over delivered messages would be\n * the same defect the revenue section refuses for orders — one visitor can\n * submit two forms, so the quotient passes 100% without anything being wrong\n * — and counting distinct people would need a document per person per\n * campaign, the per-recipient read this whole surface exists to refuse.\n */\nexport function campaignConversionsReport(options: {\n rollup: CampaignConversionsRollup | undefined\n}): CampaignConversionsReport {\n const { rollup } = options\n const stored = rollup?.byKind ?? {}\n const caveats: CampaignCaveat[] = []\n\n const kinds: CampaignConversionKindReport[] = CAMPAIGN_CONVERSION_KINDS.map(\n (kind) => {\n const raw = stored[kind]\n const copy = CAMPAIGN_CONVERSION_KIND_COPY[kind]\n return {\n kind,\n label: copy.label,\n // `undefined` means the rollup has no entry for this kind, which is\n // not a measured zero. A stored 0 is impossible — the writer only\n // ever increments — but it is read as unrecorded for the same reason.\n value: raw === undefined || count(raw) === 0 ? null : count(raw),\n note: copy.note,\n }\n },\n )\n\n const any = kinds.some((entry) => entry.value !== null)\n\n if (any) {\n caveats.push({\n id: 'conversions-kinds-overlap',\n message:\n 'These figures count different things about the same visits and are ' +\n 'deliberately not added together. One person filling in one form can ' +\n 'appear as a submission, a contact and a lead, so a total would ' +\n 'count that visit three times.',\n })\n caveats.push({\n id: 'conversions-web-not-rolled-up',\n message:\n 'Campaign emails only. A conversion credited to a link tagged with ' +\n 'utm_ parameters is recorded against that label rather than against ' +\n 'a campaign, so it is not in the figures above — those conversions ' +\n 'are listed under Conversions in the marketing console.',\n })\n }\n\n return {\n kinds,\n recorded: rollup !== undefined,\n any,\n model: String(rollup?.model ?? EMAIL_ATTRIBUTION_MODEL),\n windowDays: count(rollup?.windowDays) || EMAIL_ATTRIBUTION_WINDOW_DAYS,\n caveats,\n }\n}\n\n/**\n * How much of one kind was credited to anything at all.\n *\n * ## Why this figure has to exist\n *\n * The join writes nothing for a conversion it cannot credit, so a list of\n * attribution records is a list of the SUCCESSES. Rendering only that turns\n * \"we credited four of these\" into \"four of these happened\", and the reader\n * draws a conclusion about their campaigns from a number that is mostly a\n * fact about how many visitors arrived without a campaign link.\n *\n * So the screen shows both halves and this computes the second. `attributed`\n * is counted over the attribution records; `total` over the records the\n * conversions themselves live in.\n *\n * ## Why the difference is a CEILING and says so\n *\n * The two counts come from different collections with different histories,\n * and the gap between them holds three things that are not the same:\n *\n * - conversions by visitors who arrived directly, which is what the figure\n * is meant to describe;\n * - conversions from before the join existed, which were never eligible; and\n * - for contacts, conversions on the org's OTHER sites, because contacts are\n * shared across an org while attributions are per host.\n *\n * None of those can be separated from the others without a field nobody\n * writes, so `unattributed` is reported as an upper bound and\n * {@link CampaignConversionsCoverage.exact} is `false` whenever the reasons\n * apply. A figure presented as exact when it is not is the failure this\n * whole surface is built to avoid.\n */\nexport interface CampaignConversionsCoverage {\n kind: CampaignConversionKind\n /** Conversions of this kind credited to some campaign or label. */\n attributed: number\n /** Conversions of this kind that exist, credited or not. */\n total: number\n /** `total - attributed`, clamped at zero. An upper bound, not a count. */\n unattributed: number\n /**\n * Whether `unattributed` may be read as \"arrived directly\".\n *\n * Always `false` today — the three reasons in the docblock all apply to\n * every host — and kept as a field rather than hardcoded in the copy so a\n * screen asks the model rather than restating its conclusion.\n */\n exact: boolean\n caveats: CampaignCaveat[]\n}\n\n/**\n * The attributed/unattributed split for one kind.\n *\n * `total` is `null` when it could not be counted — the aggregation failed, or\n * the collection is not readable from this surface — and the split is then\n * withheld entirely rather than defaulting `total` to `attributed`, which\n * would render every conversion as attributed and is the single most\n * flattering wrong answer available here.\n */\nexport function campaignConversionsCoverage(options: {\n kind: CampaignConversionKind\n attributed: number | null | undefined\n total: number | null | undefined\n /** The kind's records live outside this host, so `total` over-counts. */\n crossHostTotal?: boolean\n}): CampaignConversionsCoverage | null {\n const { kind, crossHostTotal } = options\n if (options.total == null || options.attributed == null) return null\n const attributed = count(options.attributed)\n const total = count(options.total)\n const caveats: CampaignCaveat[] = []\n\n /*\n * CLAMPED, and only here at the point of display. The two counts are taken\n * from two collections a moment apart, so a conversion recorded between\n * them makes `attributed` briefly exceed `total`; a negative count of\n * things that did not happen is not a sentence anybody can act on.\n */\n const unattributed = Math.max(0, total - attributed)\n\n caveats.push({\n id: 'conversions-unattributed-is-a-ceiling',\n message:\n `${unattributed.toLocaleString()} of these are not credited to any ` +\n 'campaign. Most arrived without following a campaign link and are ' +\n 'recorded as direct — nothing is guessed from a referrer, and no ' +\n 'campaign is credited for being the most recent one to run. The rest ' +\n 'are records from before campaign attribution was recorded at all, so ' +\n 'read this as an upper bound rather than a count of direct arrivals.',\n })\n if (crossHostTotal) {\n caveats.push({\n id: 'conversions-total-crosses-hosts',\n message:\n 'Contacts are shared across every site in this organization, while ' +\n 'attributions belong to one site. The uncredited figure therefore ' +\n 'includes contacts that were created on another site and could ' +\n 'never have been credited here.',\n })\n }\n\n return {\n kind,\n attributed,\n total,\n unattributed,\n // Never true while the reasons above stand. The field exists so a screen\n // reads the model rather than restating its conclusion in JSX.\n exact: false,\n caveats,\n }\n}\n\n/**\n * How a record names the thing it was credited to.\n *\n * The email channel names a campaign document, so the screen can link to it,\n * and so does a sequence touch: the campaign the sequence is in is a\n * container with a page. The web channel names a label the marketer typed,\n * which is text and never a link — there is nothing at the other end of it.\n *\n * The `utm_` triple is joined in the order a marketer set it, with the parts\n * that are absent left out rather than filled with a placeholder.\n * `google / cpc` and `google / cpc / (none)` describe the same link, and only\n * one of them invites the reader to look for a campaign called \"(none)\".\n */\nexport function campaignTouchLabel(\n record: CampaignConversionRecord | null | undefined,\n): string {\n if (!record) return ''\n if (record.channel === 'email' || record.channel === 'sequence') {\n return String(record.campaignId ?? '')\n }\n const parts = [record.source, record.medium, record.campaign]\n .map((part) => String(part ?? '').trim())\n .filter(Boolean)\n return parts.join(' / ')\n}\n"],"names":["EMAIL_ATTRIBUTION_MODEL","EMAIL_ATTRIBUTION_WINDOW_DAYS","CAMPAIGN_CONVERSION_KINDS","CAMPAIGN_CONVERSION_KIND_COPY","form","label","note","lead","contact","booking","campaignConversionId","kind","refId","k","String","ref","includes","count","raw","value","Math","floor","Number","isFinite","campaignConversionsReport","options","rollup","stored","byKind","caveats","kinds","map","copy","undefined","any","some","entry","push","id","message","recorded","model","windowDays","campaignConversionsCoverage","crossHostTotal","total","attributed","unattributed","max","toLocaleString","exact","campaignTouchLabel","record","channel","campaignId","parts","source","medium","campaign","part","trim","filter","Boolean","join"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAED;;;;;CAKC,GACD,SACEA,uBAAuB,EACvBC,6BAA6B,QACxB,2BAA0B;AAGjC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAqDC,GAED;;;;;;;CAOC,GAED;;;;;;CAMC,GACD,OAAO,MAAMC,4BAA4B;IACvC;IACA;IACA;IACA;CACD,CAAS;AA4BV,OAAO,MAAMC,gCAET;IACFC,MAAM;QACJC,OAAO;QACPC,MAAM;IACR;IACAC,MAAM;QAAEF,OAAO;QAASC,MAAM;IAAsC;IACpEE,SAAS;QACPH,OAAO;QACPC,MAAM;IACR;IACAG,SAAS;QAAEJ,OAAO;QAAYC,MAAM;IAAqC;AAC3E,EAAC;AAqDD;;;;;;;;;;;;CAYC,GACD,OAAO,SAASI,qBACdC,IAA+B,EAC/BC,KAAgC;IAEhC,MAAMC,IAAIC,OAAOH,eAAAA,OAAQ;IACzB,MAAMI,MAAMD,OAAOF,gBAAAA,QAAS;IAC5B,IAAI,CAACC,KAAK,CAACE,KAAK,OAAO;IACvB,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"}
|
|
1
|
+
{"version":3,"sources":["../../../../../../../libs/plugins/marketing/src/lib/model/campaign-conversions.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/*\n * The window and the model name come from `@aglyn/shared-util-email`, for the\n * reason `campaign-revenue.ts` gives: the writer is in `tenant-data-admin`,\n * which may not import a feature plugin, and a rule defined on both sides of\n * the join drifts into a figure credited under one and printed under another.\n */\nimport {\n EMAIL_ATTRIBUTION_MODEL,\n EMAIL_ATTRIBUTION_WINDOW_DAYS,\n} from '@aglyn/shared-util-email'\nimport type { CampaignCaveat } from './campaign-report'\n\n/**\n * WHAT A CAMPAIGN CAUSED — the read half of the identify-moment join.\n *\n * `campaign-revenue.ts` answers what a campaign EARNED, from orders that name\n * their buyer. This answers what it CAUSED among people who were anonymous\n * until the moment they became somebody: they arrive from a campaign link,\n * browse, and only become identifiable when they submit a form, sign up, book\n * or check out. Same window, same last-click rule, same `model`/`windowDays`\n * stamped on every record, so the two figures on one screen are two readings\n * of one rule rather than two rules that happen to agree.\n *\n * ## THE KINDS ARE NEVER SUMMED, and this module makes that structural\n *\n * One form submission by a new person writes a submission record, a contact\n * and a lead — three true statements about one visitor action. Adding them\n * would treble every campaign's conversions, and the reader would have no way\n * to see it had happened, because the sum looks like a bigger version of a\n * real number.\n *\n * So {@link CampaignConversionsReport} carries no total, no `all`, no\n * `conversions` scalar, and there is no function here that reduces the kinds.\n * A screen that wanted one would have to write the addition itself. The\n * report also raises {@link CampaignCaveat} `conversions-kinds-overlap` so\n * the reader is told WHY the four figures stand apart, rather than being left\n * to assume the product forgot to add them up.\n *\n * ## THE WEB CHANNEL HAS NO ROLLUP, and cannot be given one\n *\n * A conversion is credited to one of two touches. An EMAIL touch names a\n * campaign document — a real entity with a real id — so its conversions roll\n * up under it beside the revenue the same join credits it with. A WEB touch\n * is a `utm_` label a marketer typed into a URL: no document, no id, and no\n * bound on how many distinct values exist, because anybody who can vary a\n * query string can mint another one.\n *\n * A rollup keyed on that label is a map a stranger can grow, which is the\n * same unbounded key space the analytics collector caps its per-day label map\n * against. So the writer increments the rollup for the email channel ONLY,\n * this reader states that where the figure is drawn, and web-channel records\n * are read as records — see {@link campaignConversionsCoverage}.\n *\n * ## A PAGE OR A DECLARED LABEL NAMES THE CAMPAIGN ITSELF (AGL-3461)\n *\n * Two more touches name a campaign CONTAINER rather than a send: a page filed\n * under the campaign, and a `utm_campaign` label the campaign declares as its\n * own. Their records carry the container's id as `campaignId`, so the\n * campaign's page counts them with the same `kind` + `campaignId` aggregation\n * it counts its sends' conversions with — keyed by a document id, so bounded\n * by the campaigns that exist, and needing no rollup document of their own.\n *\n * ## A CONVERSION WITH NO TOUCH IS NOT IN HERE AT ALL\n *\n * Direct traffic writes no record. There is deliberately no\n * `utm_source=direct` placeholder, no referrer inference and no \"most recent\n * campaign on this site\" fallback: a conversion nobody can be credited with\n * is a conversion nobody is credited with.\n *\n * That makes the absence of a record load-bearing, and it makes a screen\n * showing only attributed conversions a lie by omission — it renders \"we\n * credited three of these\" as \"three of these happened\".\n * {@link campaignConversionsCoverage} exists so the unattributed count is a\n * figure on the page rather than an inference nobody makes.\n */\n\n/*\n * The constants are NOT re-exported here, though `campaign-revenue.ts`\n * re-exports the same two. Both modules sit behind one barrel, and a name\n * exported twice through it is a name a bundler has to disambiguate. Every\n * screen reads the rule off `model`/`windowDays` on the report anyway, which\n * is the stored value rather than today's constant — a campaign credited\n * under an older window has to print the window it was credited under.\n */\n\n/**\n * Which identify moment a record credits.\n *\n * An array first, because the ORDER is the reading order on every screen and\n * a second list of these is a second chance to leave one out. The union is\n * derived from it rather than written twice.\n */\nexport const CAMPAIGN_CONVERSION_KINDS = [\n 'form',\n 'lead',\n 'contact',\n 'booking',\n] as const\n\n/** One identify moment. Mirrors the writer's `CampaignConversionKind`. */\nexport type CampaignConversionKind = (typeof CAMPAIGN_CONVERSION_KINDS)[number]\n\n/**\n * Which channel the credited touch arrived through.\n *\n * `sequence` (AGL-3254) is a one-to-one email a rep's sequence sent: the\n * touch names the CAMPAIGN the sequence is in, like an email touch names\n * the campaign whose mail was clicked, plus the sequence and the enrollment\n * it came through, so a record can say which rep's outreach it credits.\n */\nexport type CampaignTouchChannel = 'email' | 'web' | 'sequence' | 'page'\n\n/**\n * THE RULE an identify moment is credited under (AGL-3461), stamped on every\n * record as `model` so a reader can print what was counted.\n *\n * `last-click` — the revenue join's rule, which an order still uses — named\n * the later of a campaign email click and a labeled link. A page filed under a\n * campaign is a touch too now, and it is not a click, so the identify moment\n * says so:\n *\n * - the later of a campaign email click and a link carrying `utm_` labels\n * wins, exactly as before;\n * - a page filed under a campaign competes with that winner. Within\n * {@link CAMPAIGN_VISIT_MINUTES} of it the two are one visit: a click that\n * names a campaign — its mail, or a label the campaign declares — keeps\n * the credit for the page it landed on, and a label that names none\n * yields to the page. Further apart, the later one wins;\n * - all of it inside the seven-day window, as before.\n */\nexport const CAMPAIGN_TOUCH_MODEL = 'last-touch'\n\n/** How close a page view and a click are to be one visit, in minutes. */\nexport const CAMPAIGN_VISIT_MINUTES = 30\n\n/** The same, in milliseconds. */\nexport const CAMPAIGN_VISIT_MS = CAMPAIGN_VISIT_MINUTES * 60 * 1000\n\n/**\n * The rule a record or a rollup was credited under, as one sentence a reader\n * can check the figure against. Keyed off the STORED model, so a conversion\n * credited under an older rule prints the rule it was credited under.\n */\nexport function campaignCreditRule(\n model: string | null | undefined,\n windowDays: number | null | undefined,\n): string {\n const days = Number(windowDays ?? 0) || EMAIL_ATTRIBUTION_WINDOW_DAYS\n if (model === CAMPAIGN_TOUCH_MODEL) {\n return (\n `Credited to the visitor’s latest campaign touch within ${days} days — a ` +\n 'campaign email they clicked, a link labeled for a campaign, or a page ' +\n `filed under one. A page viewed within ${CAMPAIGN_VISIT_MINUTES} minutes of ` +\n 'a click is that click’s visit, and the click keeps the credit when it ' +\n 'names a campaign.'\n )\n }\n if (model === EMAIL_ATTRIBUTION_MODEL) {\n return `Credited to the last campaign whose link they clicked, within ${days} days of that click.`\n }\n return `Credited under the ${String(model ?? 'recorded')} model, within ${days} days.`\n}\n\n/**\n * The record each conversion kind credits, as its owner publishes the kind\n * (`plugin-record-counts`, `plugin-record-lists`): a credited form names the\n * SUBMISSION it credits, the rest the record of their own name. The owners\n * count and read those records; this plugin only names the kind.\n */\nexport const CAMPAIGN_CONVERSION_RECORD_KINDS: Readonly<\n Record<CampaignConversionKind, string>\n> = {\n form: 'formSubmission',\n lead: 'lead',\n contact: 'contact',\n booking: 'booking',\n}\n\n/**\n * What a reader calls one kind, and what the count means.\n *\n * The note is the half that stops the addition. Each one names a DIFFERENT\n * population of the same visitors, and saying so under every figure is what\n * makes four numbers standing apart read as deliberate rather than as a\n * missing total.\n */\nexport interface CampaignConversionKindCopy {\n label: string\n note: string\n}\n\nexport const CAMPAIGN_CONVERSION_KIND_COPY: Readonly<\n Record<CampaignConversionKind, CampaignConversionKindCopy>\n> = {\n form: {\n label: 'Form submissions',\n note: 'submissions credited to this campaign',\n },\n lead: { label: 'Leads', note: 'new leads credited to this campaign' },\n contact: {\n label: 'Contacts',\n note: 'new contacts credited to this campaign',\n },\n booking: { label: 'Bookings', note: 'bookings credited to this campaign' },\n}\n\n/**\n * The stored shape of `campaigns/{campaignId}/reports/conversions`.\n *\n * Its own document beside `reports/revenue`, for the reason that one is: the\n * campaign document is read by the history list, the glance widget and the\n * send path, and a map that grows with what the campaign caused would enlarge\n * every one of those reads.\n */\nexport interface CampaignConversionsRollup {\n byKind?: Partial<Record<CampaignConversionKind, number>>\n /** The model these conversions were credited under. */\n model?: string\n /** The window, in days, they were credited inside. */\n windowDays?: number\n}\n\n/**\n * The stored shape of `hosts/{hostId}/campaignAttributions/{kind}:{refId}`.\n *\n * Read-side only, and every field optional: this is a document somebody\n * else's writer produced, and a reader that assumed a field was present would\n * throw on the first record written by an older version of it.\n */\nexport interface CampaignConversionRecord {\n kind?: CampaignConversionKind\n /** The submission, lead, contact or booking this credits. */\n refId?: string\n channel?: CampaignTouchChannel\n /**\n * The campaign document, when the touch was a click on our own mail — or,\n * for a `sequence` touch, the campaign container the sequence is in.\n */\n campaignId?: string\n /** The sequence, when the touch was a sequence email (AGL-3254). */\n sequenceId?: string\n /** The enrollment the sequence email went out under (AGL-3254). */\n enrollmentId?: string\n /** `utm_source`, when the touch was a link on the web. */\n source?: string\n /** `utm_medium`, when the touch was a link on the web. */\n medium?: string\n /** `utm_campaign`, when the touch was a link on the web. */\n campaign?: string\n /** When the visitor followed the campaign link, epoch ms. */\n touchedAtMs?: number\n /** When the visitor became identifiable, epoch ms. */\n convertedAtMs?: number\n /** The screen the visitor viewed, for a `page` touch (AGL-3461). */\n screenId?: string\n /** That page's path, for a `page` touch. */\n path?: string\n model?: string\n windowDays?: number\n}\n\n/**\n * The document id for one conversion — `{kind}:{refId}`.\n *\n * The reader's half of the writer's `campaignConversionId`. It is restated\n * here rather than imported because the writer lives in `tenant-data-admin`,\n * which a UI library may not import, and it is one line whose shape is\n * asserted in this module's spec.\n *\n * Answers `null` for an unusable pair rather than building `form:undefined`,\n * which would be a valid document path pointing at a record that can never\n * exist — a keyed read that silently reports \"not attributed\" for every\n * record on the screen.\n */\nexport function campaignConversionId(\n kind: string | null | undefined,\n refId: string | null | undefined,\n): string | null {\n const k = String(kind ?? '')\n const ref = String(refId ?? '')\n if (!k || !ref) return null\n if (!(CAMPAIGN_CONVERSION_KINDS as readonly string[]).includes(k)) return null\n // A slash would leave the collection; the id scheme already spends the\n // colon, and a ref carrying one would make the pair ambiguous.\n if (ref.includes('/') || ref.includes(':')) return null\n return `${k}:${ref}`\n}\n\n/** A stored count as a non-negative integer. */\nfunction count(raw: unknown): number {\n const value = Math.floor(Number(raw ?? 0))\n return Number.isFinite(value) && value > 0 ? value : 0\n}\n\n/** One kind's figure on screen. Independent of every other kind's. */\nexport interface CampaignConversionKindReport {\n kind: CampaignConversionKind\n label: string\n /**\n * Conversions of this kind, or `null` when the rollup holds no entry.\n *\n * `null` rather than 0 for {@link CampaignConversionsReport.recorded}'s\n * reason, applied per kind: a site with no booking form has never written a\n * booking conversion, and rendering that as a measured zero invites the\n * reader to conclude the campaign failed at something it never attempted.\n */\n value: number | null\n note: string\n}\n\n/** Everything the conversions section renders. */\nexport interface CampaignConversionsReport {\n /**\n * One entry per kind, always all four and always in\n * {@link CAMPAIGN_CONVERSION_KINDS} order.\n *\n * There is deliberately NO total beside this array, and no field anywhere\n * on this report that holds one. See the module docblock.\n */\n kinds: CampaignConversionKindReport[]\n /**\n * Whether the rollup document exists at all.\n *\n * `false` is NOT \"this campaign caused nothing\" — it is also every campaign\n * sent before the join existed, and every campaign whose conversions all\n * arrived through the web channel, which writes no rollup. The screen\n * renders the difference rather than printing a zero for all three.\n */\n recorded: boolean\n /** At least one kind holds a figure. */\n any: boolean\n /** The model these figures were credited under, as stored. */\n model: string\n /** The window they were credited inside, as stored. */\n windowDays: number\n caveats: CampaignCaveat[]\n}\n\n/**\n * Turns the stored rollup into the conversions section.\n *\n * Takes the rollup and nothing else. Unlike the revenue report there is no\n * denominator to hand in: a conversion RATE over delivered messages would be\n * the same defect the revenue section refuses for orders — one visitor can\n * submit two forms, so the quotient passes 100% without anything being wrong\n * — and counting distinct people would need a document per person per\n * campaign, the per-recipient read this whole surface exists to refuse.\n */\nexport function campaignConversionsReport(options: {\n rollup: CampaignConversionsRollup | undefined\n}): CampaignConversionsReport {\n const { rollup } = options\n const stored = rollup?.byKind ?? {}\n const caveats: CampaignCaveat[] = []\n\n const kinds: CampaignConversionKindReport[] = CAMPAIGN_CONVERSION_KINDS.map(\n (kind) => {\n const raw = stored[kind]\n const copy = CAMPAIGN_CONVERSION_KIND_COPY[kind]\n return {\n kind,\n label: copy.label,\n // `undefined` means the rollup has no entry for this kind, which is\n // not a measured zero. A stored 0 is impossible — the writer only\n // ever increments — but it is read as unrecorded for the same reason.\n value: raw === undefined || count(raw) === 0 ? null : count(raw),\n note: copy.note,\n }\n },\n )\n\n const any = kinds.some((entry) => entry.value !== null)\n\n if (any) {\n caveats.push({\n id: 'conversions-kinds-overlap',\n message:\n 'These figures count different things about the same visits and are ' +\n 'deliberately not added together. One person filling in one form can ' +\n 'appear as a submission, a contact and a lead, so a total would ' +\n 'count that visit three times.',\n })\n caveats.push({\n id: 'conversions-web-not-rolled-up',\n message:\n 'This email only. A conversion credited to a page filed under the ' +\n 'campaign, or to a link labeled for it, is counted on the ' +\n 'campaign’s own page; one credited to a utm_ label no campaign ' +\n 'declares is listed under Conversions in the marketing console.',\n })\n }\n\n return {\n kinds,\n recorded: rollup !== undefined,\n any,\n model: String(rollup?.model ?? EMAIL_ATTRIBUTION_MODEL),\n windowDays: count(rollup?.windowDays) || EMAIL_ATTRIBUTION_WINDOW_DAYS,\n caveats,\n }\n}\n\n/**\n * How much of one kind was credited to anything at all.\n *\n * ## Why this figure has to exist\n *\n * The join writes nothing for a conversion it cannot credit, so a list of\n * attribution records is a list of the SUCCESSES. Rendering only that turns\n * \"we credited four of these\" into \"four of these happened\", and the reader\n * draws a conclusion about their campaigns from a number that is mostly a\n * fact about how many visitors arrived without a campaign link.\n *\n * So the screen shows both halves and this computes the second. `attributed`\n * is counted over the attribution records; `total` over the records the\n * conversions themselves live in.\n *\n * ## Why the difference is a CEILING and says so\n *\n * The two counts come from different collections with different histories,\n * and the gap between them holds three things that are not the same:\n *\n * - conversions by visitors who arrived directly, which is what the figure\n * is meant to describe;\n * - conversions from before the join existed, which were never eligible; and\n * - for contacts, conversions on the org's OTHER sites, because contacts are\n * shared across an org while attributions are per host.\n *\n * None of those can be separated from the others without a field nobody\n * writes, so `unattributed` is reported as an upper bound and\n * {@link CampaignConversionsCoverage.exact} is `false` whenever the reasons\n * apply. A figure presented as exact when it is not is the failure this\n * whole surface is built to avoid.\n */\nexport interface CampaignConversionsCoverage {\n kind: CampaignConversionKind\n /** Conversions of this kind credited to some campaign or label. */\n attributed: number\n /** Conversions of this kind that exist, credited or not. */\n total: number\n /** `total - attributed`, clamped at zero. An upper bound, not a count. */\n unattributed: number\n /**\n * Whether `unattributed` may be read as \"arrived directly\".\n *\n * Always `false` today — the three reasons in the docblock all apply to\n * every host — and kept as a field rather than hardcoded in the copy so a\n * screen asks the model rather than restating its conclusion.\n */\n exact: boolean\n caveats: CampaignCaveat[]\n}\n\n/**\n * The attributed/unattributed split for one kind.\n *\n * `total` is `null` when it could not be counted — the aggregation failed, or\n * the collection is not readable from this surface — and the split is then\n * withheld entirely rather than defaulting `total` to `attributed`, which\n * would render every conversion as attributed and is the single most\n * flattering wrong answer available here.\n */\nexport function campaignConversionsCoverage(options: {\n kind: CampaignConversionKind\n attributed: number | null | undefined\n total: number | null | undefined\n /** The kind's records live outside this host, so `total` over-counts. */\n crossHostTotal?: boolean\n}): CampaignConversionsCoverage | null {\n const { kind, crossHostTotal } = options\n if (options.total == null || options.attributed == null) return null\n const attributed = count(options.attributed)\n const total = count(options.total)\n const caveats: CampaignCaveat[] = []\n\n /*\n * CLAMPED, and only here at the point of display. The two counts are taken\n * from two collections a moment apart, so a conversion recorded between\n * them makes `attributed` briefly exceed `total`; a negative count of\n * things that did not happen is not a sentence anybody can act on.\n */\n const unattributed = Math.max(0, total - attributed)\n\n caveats.push({\n id: 'conversions-unattributed-is-a-ceiling',\n message:\n `${unattributed.toLocaleString()} of these are not credited to any ` +\n 'campaign. Most arrived without following a campaign link and are ' +\n 'recorded as direct — nothing is guessed from a referrer, and no ' +\n 'campaign is credited for being the most recent one to run. The rest ' +\n 'are records from before campaign attribution was recorded at all, so ' +\n 'read this as an upper bound rather than a count of direct arrivals.',\n })\n if (crossHostTotal) {\n caveats.push({\n id: 'conversions-total-crosses-hosts',\n message:\n 'Contacts are shared across every site in this organization, while ' +\n 'attributions belong to one site. The uncredited figure therefore ' +\n 'includes contacts that were created on another site and could ' +\n 'never have been credited here.',\n })\n }\n\n return {\n kind,\n attributed,\n total,\n unattributed,\n // Never true while the reasons above stand. The field exists so a screen\n // reads the model rather than restating its conclusion in JSX.\n exact: false,\n caveats,\n }\n}\n\n/**\n * How a record names the thing it was credited to.\n *\n * The email channel names a campaign document, so the screen can link to it,\n * and so does a sequence touch: the campaign the sequence is in is a\n * container with a page — as does a page touch, the campaign the viewed page\n * is filed under (AGL-3461). The web channel names a label the marketer\n * typed, which is text unless a campaign declares the label as its own; then\n * the record carries that campaign's id too and the screen links it.\n *\n * The `utm_` triple is joined in the order a marketer set it, with the parts\n * that are absent left out rather than filled with a placeholder.\n * `google / cpc` and `google / cpc / (none)` describe the same link, and only\n * one of them invites the reader to look for a campaign called \"(none)\".\n */\nexport function campaignTouchLabel(\n record: CampaignConversionRecord | null | undefined,\n): string {\n if (!record) return ''\n if (\n record.channel === 'email' ||\n record.channel === 'sequence' ||\n record.channel === 'page'\n ) {\n return String(record.campaignId ?? '')\n }\n const parts = [record.source, record.medium, record.campaign]\n .map((part) => String(part ?? '').trim())\n .filter(Boolean)\n return parts.join(' / ')\n}\n"],"names":["EMAIL_ATTRIBUTION_MODEL","EMAIL_ATTRIBUTION_WINDOW_DAYS","CAMPAIGN_CONVERSION_KINDS","CAMPAIGN_TOUCH_MODEL","CAMPAIGN_VISIT_MINUTES","CAMPAIGN_VISIT_MS","campaignCreditRule","model","windowDays","days","Number","String","CAMPAIGN_CONVERSION_RECORD_KINDS","form","lead","contact","booking","CAMPAIGN_CONVERSION_KIND_COPY","label","note","campaignConversionId","kind","refId","k","ref","includes","count","raw","value","Math","floor","isFinite","campaignConversionsReport","options","rollup","stored","byKind","caveats","kinds","map","copy","undefined","any","some","entry","push","id","message","recorded","campaignConversionsCoverage","crossHostTotal","total","attributed","unattributed","max","toLocaleString","exact","campaignTouchLabel","record","channel","campaignId","parts","source","medium","campaign","part","trim","filter","Boolean","join"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAED;;;;;CAKC,GACD,SACEA,uBAAuB,EACvBC,6BAA6B,QACxB,2BAA0B;AAGjC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA8DC,GAED;;;;;;;CAOC,GAED;;;;;;CAMC,GACD,OAAO,MAAMC,4BAA4B;IACvC;IACA;IACA;IACA;CACD,CAAS;AAeV;;;;;;;;;;;;;;;;;CAiBC,GACD,OAAO,MAAMC,uBAAuB,aAAY;AAEhD,uEAAuE,GACvE,OAAO,MAAMC,yBAAyB,GAAE;AAExC,+BAA+B,GAC/B,OAAO,MAAMC,oBAAoBD,yBAAyB,KAAK,KAAI;AAEnE;;;;CAIC,GACD,OAAO,SAASE,mBACdC,KAAgC,EAChCC,UAAqC;IAErC,MAAMC,OAAOC,OAAOF,qBAAAA,aAAc,MAAMP;IACxC,IAAIM,UAAUJ,sBAAsB;QAClC,OACE,CAAC,uDAAuD,EAAEM,KAAK,UAAU,CAAC,GAC1E,2EACA,CAAC,sCAAsC,EAAEL,uBAAuB,YAAY,CAAC,GAC7E,2EACA;IAEJ;IACA,IAAIG,UAAUP,yBAAyB;QACrC,OAAO,CAAC,8DAA8D,EAAES,KAAK,oBAAoB,CAAC;IACpG;IACA,OAAO,CAAC,mBAAmB,EAAEE,OAAOJ,gBAAAA,QAAS,YAAY,eAAe,EAAEE,KAAK,MAAM,CAAC;AACxF;AAEA;;;;;CAKC,GACD,OAAO,MAAMG,mCAET;IACFC,MAAM;IACNC,MAAM;IACNC,SAAS;IACTC,SAAS;AACX,EAAC;AAeD,OAAO,MAAMC,gCAET;IACFJ,MAAM;QACJK,OAAO;QACPC,MAAM;IACR;IACAL,MAAM;QAAEI,OAAO;QAASC,MAAM;IAAsC;IACpEJ,SAAS;QACPG,OAAO;QACPC,MAAM;IACR;IACAH,SAAS;QAAEE,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C,QAAO+C,qBAAAA,OAAOE,UAAU,YAAjBF,qBAAqB;IACrC;IACA,MAAMG,QAAQ;QAACH,OAAOI,MAAM;QAAEJ,OAAOK,MAAM;QAAEL,OAAOM,QAAQ;KAAC,CAC1DzB,GAAG,CAAC,CAAC0B,OAAStD,OAAOsD,eAAAA,OAAQ,IAAIC,IAAI,IACrCC,MAAM,CAACC;IACV,OAAOP,MAAMQ,IAAI,CAAC;AACpB"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Aglyn LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* WHAT ONE CAMPAIGN SEND DID, AND WHAT ITS SEQUENCES PRODUCED.
|
|
19
|
+
*
|
|
20
|
+
* The rate math and the link rollup every bulk sender divides by are the mail
|
|
21
|
+
* model's (`@aglyn/shared-ui-email-campaigns/model/send-report`); this is the
|
|
22
|
+
* campaign's reading of them — which population each rate is over, the
|
|
23
|
+
* caveats that qualify a figure, and the funnel a campaign's sequences report
|
|
24
|
+
* into. It keeps the rule that module states: a rate names its denominator,
|
|
25
|
+
* or it is not reported.
|
|
26
|
+
*/
|
|
27
|
+
import { type SendRate, type SendStats } from '@aglyn/shared-ui-email-campaigns/model/send-report';
|
|
28
|
+
/** Why a number the report would otherwise show is being withheld. */
|
|
29
|
+
export interface CampaignCaveat {
|
|
30
|
+
/** Stable id, so a spec can assert on the caveat rather than its prose. */
|
|
31
|
+
id: 'delivery-unrecorded' | 'click-tracking-unrecorded' | 'audience-truncated' | 'send-deferred' | 'revenue-denominator-unrecorded' | 'revenue-multi-currency' | 'revenue-mid-flight' | 'revenue-mixed-model' | 'conversions-kinds-overlap' | 'conversions-web-not-rolled-up' | 'conversions-unattributed-is-a-ceiling' | 'conversions-total-crosses-hosts' | 'sequences-funnel-not-summed';
|
|
32
|
+
message: string;
|
|
33
|
+
}
|
|
34
|
+
/** One population the send measured, for the audience breakdown. */
|
|
35
|
+
export interface CampaignPopulation {
|
|
36
|
+
id: string;
|
|
37
|
+
label: string;
|
|
38
|
+
count: number;
|
|
39
|
+
/** What this count is a part OF, named. */
|
|
40
|
+
ofLabel: string;
|
|
41
|
+
of: number;
|
|
42
|
+
}
|
|
43
|
+
/** Everything the report screen renders, decided here rather than in JSX. */
|
|
44
|
+
export interface CampaignReport {
|
|
45
|
+
sent: number;
|
|
46
|
+
recipients: number;
|
|
47
|
+
delivered: number | null;
|
|
48
|
+
opens: number;
|
|
49
|
+
clicks: number;
|
|
50
|
+
uniqueOpens: number | null;
|
|
51
|
+
uniqueClicks: number | null;
|
|
52
|
+
bounced: number;
|
|
53
|
+
complained: number;
|
|
54
|
+
unsubscribes: number;
|
|
55
|
+
/** Rates, each `null` when its denominator is zero or unrecorded. */
|
|
56
|
+
rates: {
|
|
57
|
+
/** Accepted by the receiving server, over what the provider accepted. */
|
|
58
|
+
delivery: SendRate | null;
|
|
59
|
+
/** Distinct readers who opened, over delivered. */
|
|
60
|
+
open: SendRate | null;
|
|
61
|
+
/** Distinct readers who clicked, over delivered. */
|
|
62
|
+
click: SendRate | null;
|
|
63
|
+
/**
|
|
64
|
+
* Distinct clickers over distinct OPENERS — a different question from
|
|
65
|
+
* `click`, and the one the two get confused for. It answers "of the
|
|
66
|
+
* people who read it, how many acted", not "of the people who received
|
|
67
|
+
* it". Reported separately and labelled separately, never as "click
|
|
68
|
+
* rate".
|
|
69
|
+
*/
|
|
70
|
+
clickToOpen: SendRate | null;
|
|
71
|
+
/** Bounced over what the provider accepted. */
|
|
72
|
+
bounce: SendRate | null;
|
|
73
|
+
/** Complaints over delivered — the number mailbox providers judge on. */
|
|
74
|
+
complaint: SendRate | null;
|
|
75
|
+
/** Unsubscribes through this campaign's link, over delivered. */
|
|
76
|
+
unsubscribe: SendRate | null;
|
|
77
|
+
};
|
|
78
|
+
populations: CampaignPopulation[];
|
|
79
|
+
caveats: CampaignCaveat[];
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Turns a stored `stats` map into the report.
|
|
83
|
+
*
|
|
84
|
+
* ## The denominator decisions, in one place
|
|
85
|
+
*
|
|
86
|
+
* - **`delivered`** carries the engagement rates — open, click, complaint,
|
|
87
|
+
* unsubscribe. Mail that bounced was never in front of a human, so
|
|
88
|
+
* including it in the denominator of an open rate depresses a number that
|
|
89
|
+
* describes the audience with a fact about the address list. This is also
|
|
90
|
+
* the convention every other tool reports, which matters: a merchant
|
|
91
|
+
* comparing our figure with their previous ESP's must be comparing the same
|
|
92
|
+
* quantity.
|
|
93
|
+
* - **`sent`** carries the delivery and bounce rates, because those describe
|
|
94
|
+
* what happened to what we handed the provider, and `delivered` is the
|
|
95
|
+
* numerator of one of them — a rate cannot be over itself.
|
|
96
|
+
* - **`uniqueOpens`** is the open-rate numerator, not `opens`. Open EVENTS
|
|
97
|
+
* over recipients can exceed 100% the moment one person opens twice, and a
|
|
98
|
+
* percentage above 100 is how a reader learns the number means something
|
|
99
|
+
* other than what it says. Both are shown; only the distinct count is
|
|
100
|
+
* divided.
|
|
101
|
+
*
|
|
102
|
+
* ## `delivered` unknown vs. zero
|
|
103
|
+
*
|
|
104
|
+
* A campaign predating the delivery webhook records no `delivered` at all.
|
|
105
|
+
* That is reported as `null` and every rate over it is withheld, with a
|
|
106
|
+
* caveat naming the reason — rather than substituting `sent`, which would
|
|
107
|
+
* silently publish the flattered number this module exists to refuse.
|
|
108
|
+
*/
|
|
109
|
+
export declare function campaignReport(stats: SendStats | undefined): CampaignReport;
|
|
110
|
+
/**
|
|
111
|
+
* The outcomes the runtime credits, in funnel order. An array first, because
|
|
112
|
+
* the ORDER is the reading order and a second list is a second chance to
|
|
113
|
+
* leave one out; the union is derived from it.
|
|
114
|
+
*/
|
|
115
|
+
export declare const CAMPAIGN_SEQUENCE_OUTCOMES: readonly ["enrolled", "sent", "replied", "meetings", "converted"];
|
|
116
|
+
export type CampaignSequenceOutcome = (typeof CAMPAIGN_SEQUENCE_OUTCOMES)[number];
|
|
117
|
+
/**
|
|
118
|
+
* The org collection holding one document per campaign container,
|
|
119
|
+
* `orgs/{orgId}/campaignSequenceReports/{campaignId}`.
|
|
120
|
+
*/
|
|
121
|
+
export declare const CAMPAIGN_SEQUENCE_REPORTS_COLLECTION = "campaignSequenceReports";
|
|
122
|
+
/** What a reader calls each outcome, and what the count means. */
|
|
123
|
+
export declare const CAMPAIGN_SEQUENCE_OUTCOME_COPY: Readonly<Record<CampaignSequenceOutcome, {
|
|
124
|
+
label: string;
|
|
125
|
+
note: string;
|
|
126
|
+
}>>;
|
|
127
|
+
/**
|
|
128
|
+
* The stored shape of `campaignSequenceReports/{campaignId}`.
|
|
129
|
+
*
|
|
130
|
+
* Read-side only, every field optional: another plugin's runtime wrote it,
|
|
131
|
+
* and a reader that assumed a field was present would throw on the first
|
|
132
|
+
* document written before the field existed.
|
|
133
|
+
*/
|
|
134
|
+
export interface CampaignSequencesRollup {
|
|
135
|
+
byOutcome?: Partial<Record<CampaignSequenceOutcome, number>>;
|
|
136
|
+
/** When the runtime last credited anything, epoch ms. */
|
|
137
|
+
updatedAtMs?: number;
|
|
138
|
+
}
|
|
139
|
+
/** One outcome's figure on screen. */
|
|
140
|
+
export interface CampaignSequenceFigure {
|
|
141
|
+
outcome: CampaignSequenceOutcome;
|
|
142
|
+
label: string;
|
|
143
|
+
/** The count, or `null` when the rollup holds no entry for the outcome. */
|
|
144
|
+
value: number | null;
|
|
145
|
+
note: string;
|
|
146
|
+
}
|
|
147
|
+
/** Everything the Sequences section renders. */
|
|
148
|
+
export interface CampaignSequencesReport {
|
|
149
|
+
/** Always all five, always in {@link CAMPAIGN_SEQUENCE_OUTCOMES} order. */
|
|
150
|
+
figures: CampaignSequenceFigure[];
|
|
151
|
+
/** Whether the rollup document exists at all — see the block header. */
|
|
152
|
+
recorded: boolean;
|
|
153
|
+
/** At least one outcome holds a figure. */
|
|
154
|
+
any: boolean;
|
|
155
|
+
caveats: CampaignCaveat[];
|
|
156
|
+
}
|
|
157
|
+
/** Turns the stored rollup into the Sequences section. */
|
|
158
|
+
export declare function campaignSequencesReport(rollup: CampaignSequencesRollup | undefined): CampaignSequencesReport;
|