kanmi-perf-revenue 1.0.0 → 1.2.0
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 +347 -173
- package/dist/empirical/ab-testing.d.ts +83 -0
- package/dist/empirical/ab-testing.d.ts.map +1 -0
- package/dist/empirical/ab-testing.js +281 -0
- package/dist/empirical/ab-testing.js.map +1 -0
- package/dist/empirical/alerting.d.ts +85 -0
- package/dist/empirical/alerting.d.ts.map +1 -0
- package/dist/empirical/alerting.js +358 -0
- package/dist/empirical/alerting.js.map +1 -0
- package/dist/empirical/attribution.d.ts +80 -0
- package/dist/empirical/attribution.d.ts.map +1 -0
- package/dist/empirical/attribution.js +305 -0
- package/dist/empirical/attribution.js.map +1 -0
- package/dist/empirical/cohort.d.ts +75 -0
- package/dist/empirical/cohort.d.ts.map +1 -0
- package/dist/empirical/cohort.js +305 -0
- package/dist/empirical/cohort.js.map +1 -0
- package/dist/empirical/conversion-curve.d.ts +7 -10
- package/dist/empirical/conversion-curve.d.ts.map +1 -1
- package/dist/empirical/conversion-curve.js +37 -4
- package/dist/empirical/conversion-curve.js.map +1 -1
- package/dist/empirical/correlation.d.ts +91 -0
- package/dist/empirical/correlation.d.ts.map +1 -0
- package/dist/empirical/correlation.js +461 -0
- package/dist/empirical/correlation.js.map +1 -0
- package/dist/empirical/data-import.d.ts +22 -0
- package/dist/empirical/data-import.d.ts.map +1 -1
- package/dist/empirical/data-import.js +44 -0
- package/dist/empirical/data-import.js.map +1 -1
- package/dist/empirical/datadog-product-analytics.d.ts +192 -0
- package/dist/empirical/datadog-product-analytics.d.ts.map +1 -0
- package/dist/empirical/datadog-product-analytics.js +632 -0
- package/dist/empirical/datadog-product-analytics.js.map +1 -0
- package/dist/empirical/datadog-session-query.d.ts +32 -0
- package/dist/empirical/datadog-session-query.d.ts.map +1 -1
- package/dist/empirical/datadog-session-query.js +238 -17
- package/dist/empirical/datadog-session-query.js.map +1 -1
- package/dist/empirical/engagement-analysis.d.ts +112 -0
- package/dist/empirical/engagement-analysis.d.ts.map +1 -0
- package/dist/empirical/engagement-analysis.js +354 -0
- package/dist/empirical/engagement-analysis.js.map +1 -0
- package/dist/empirical/export.d.ts +75 -0
- package/dist/empirical/export.d.ts.map +1 -0
- package/dist/empirical/export.js +392 -0
- package/dist/empirical/export.js.map +1 -0
- package/dist/empirical/forecasting.d.ts +80 -0
- package/dist/empirical/forecasting.d.ts.map +1 -0
- package/dist/empirical/forecasting.js +287 -0
- package/dist/empirical/forecasting.js.map +1 -0
- package/dist/empirical/funnel.d.ts +66 -0
- package/dist/empirical/funnel.d.ts.map +1 -0
- package/dist/empirical/funnel.js +293 -0
- package/dist/empirical/funnel.js.map +1 -0
- package/dist/empirical/history.d.ts +198 -0
- package/dist/empirical/history.d.ts.map +1 -0
- package/dist/empirical/history.js +396 -0
- package/dist/empirical/history.js.map +1 -0
- package/dist/empirical/index.d.ts +41 -16
- package/dist/empirical/index.d.ts.map +1 -1
- package/dist/empirical/index.js +96 -13
- package/dist/empirical/index.js.map +1 -1
- package/dist/empirical/interactions.d.ts +89 -0
- package/dist/empirical/interactions.d.ts.map +1 -0
- package/dist/empirical/interactions.js +346 -0
- package/dist/empirical/interactions.js.map +1 -0
- package/dist/empirical/opportunity-calculator.d.ts +6 -18
- package/dist/empirical/opportunity-calculator.d.ts.map +1 -1
- package/dist/empirical/opportunity-calculator.js +19 -1
- package/dist/empirical/opportunity-calculator.js.map +1 -1
- package/dist/empirical/report.d.ts +3 -11
- package/dist/empirical/report.d.ts.map +1 -1
- package/dist/empirical/report.js +11 -7
- package/dist/empirical/report.js.map +1 -1
- package/dist/empirical/roi-calculator.d.ts +104 -0
- package/dist/empirical/roi-calculator.d.ts.map +1 -0
- package/dist/empirical/roi-calculator.js +403 -0
- package/dist/empirical/roi-calculator.js.map +1 -0
- package/dist/empirical/seasonality.d.ts +80 -0
- package/dist/empirical/seasonality.d.ts.map +1 -0
- package/dist/empirical/seasonality.js +340 -0
- package/dist/empirical/seasonality.js.map +1 -0
- package/dist/empirical/segmentation.d.ts +135 -0
- package/dist/empirical/segmentation.d.ts.map +1 -0
- package/dist/empirical/segmentation.js +379 -0
- package/dist/empirical/segmentation.js.map +1 -0
- package/dist/empirical/statistics.d.ts +118 -0
- package/dist/empirical/statistics.d.ts.map +1 -0
- package/dist/empirical/statistics.js +344 -0
- package/dist/empirical/statistics.js.map +1 -0
- package/dist/empirical/sweet-spot.d.ts +81 -0
- package/dist/empirical/sweet-spot.d.ts.map +1 -0
- package/dist/empirical/sweet-spot.js +198 -0
- package/dist/empirical/sweet-spot.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Alerting System
|
|
3
|
+
*
|
|
4
|
+
* Notify when performance metrics degrade or fall below thresholds.
|
|
5
|
+
* Provides configurable alerts and monitoring.
|
|
6
|
+
*
|
|
7
|
+
* @author Kanmi Obasa <i@kanmiobasa.com>
|
|
8
|
+
*/
|
|
9
|
+
// =============================================================================
|
|
10
|
+
// DEFAULT CONFIG
|
|
11
|
+
// =============================================================================
|
|
12
|
+
export const DEFAULT_THRESHOLDS = [
|
|
13
|
+
// Performance (higher is worse)
|
|
14
|
+
{ metric: 'lcp', warningThreshold: 2500, criticalThreshold: 4000, direction: 'above' },
|
|
15
|
+
{ metric: 'inp', warningThreshold: 200, criticalThreshold: 500, direction: 'above' },
|
|
16
|
+
{ metric: 'cls', warningThreshold: 0.1, criticalThreshold: 0.25, direction: 'above' },
|
|
17
|
+
{ metric: 'fcp', warningThreshold: 1800, criticalThreshold: 3000, direction: 'above' },
|
|
18
|
+
{ metric: 'ttfb', warningThreshold: 800, criticalThreshold: 1800, direction: 'above' },
|
|
19
|
+
// Conversion (lower is worse)
|
|
20
|
+
{ metric: 'cvr', warningThreshold: 0.02, criticalThreshold: 0.01, direction: 'below' },
|
|
21
|
+
// Bounce (higher is worse)
|
|
22
|
+
{ metric: 'bounce_rate', warningThreshold: 0.5, criticalThreshold: 0.7, direction: 'above' },
|
|
23
|
+
];
|
|
24
|
+
export const DEFAULT_ALERT_CONFIG = {
|
|
25
|
+
thresholds: DEFAULT_THRESHOLDS,
|
|
26
|
+
enableRegressionDetection: true,
|
|
27
|
+
regressionThreshold: 0.1, // 10% change
|
|
28
|
+
enableAnomalyDetection: true,
|
|
29
|
+
anomalyStdDevThreshold: 2,
|
|
30
|
+
};
|
|
31
|
+
// =============================================================================
|
|
32
|
+
// ALERTING FUNCTIONS
|
|
33
|
+
// =============================================================================
|
|
34
|
+
/**
|
|
35
|
+
* Check sessions against thresholds and generate alerts.
|
|
36
|
+
*/
|
|
37
|
+
export function checkThresholds(sessions, config = DEFAULT_ALERT_CONFIG) {
|
|
38
|
+
const alerts = [];
|
|
39
|
+
const now = new Date().toISOString();
|
|
40
|
+
// Calculate current metrics
|
|
41
|
+
const metrics = calculateCurrentMetrics(sessions);
|
|
42
|
+
for (const threshold of config.thresholds) {
|
|
43
|
+
const value = metrics[threshold.metric];
|
|
44
|
+
if (value === null)
|
|
45
|
+
continue;
|
|
46
|
+
const exceedsCritical = threshold.direction === 'above'
|
|
47
|
+
? value > threshold.criticalThreshold
|
|
48
|
+
: value < threshold.criticalThreshold;
|
|
49
|
+
const exceedsWarning = threshold.direction === 'above'
|
|
50
|
+
? value > threshold.warningThreshold
|
|
51
|
+
: value < threshold.warningThreshold;
|
|
52
|
+
if (exceedsCritical) {
|
|
53
|
+
alerts.push({
|
|
54
|
+
id: `threshold-${threshold.metric}-critical-${Date.now()}`,
|
|
55
|
+
type: 'threshold',
|
|
56
|
+
severity: 'critical',
|
|
57
|
+
metric: threshold.metric,
|
|
58
|
+
currentValue: value,
|
|
59
|
+
threshold: threshold.criticalThreshold,
|
|
60
|
+
message: `${threshold.metric.toUpperCase()} is critical: ${formatValue(threshold.metric, value)} (threshold: ${formatValue(threshold.metric, threshold.criticalThreshold)})`,
|
|
61
|
+
timestamp: now,
|
|
62
|
+
context: { sessions: sessions.length },
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
else if (exceedsWarning) {
|
|
66
|
+
alerts.push({
|
|
67
|
+
id: `threshold-${threshold.metric}-warning-${Date.now()}`,
|
|
68
|
+
type: 'threshold',
|
|
69
|
+
severity: 'warning',
|
|
70
|
+
metric: threshold.metric,
|
|
71
|
+
currentValue: value,
|
|
72
|
+
threshold: threshold.warningThreshold,
|
|
73
|
+
message: `${threshold.metric.toUpperCase()} needs attention: ${formatValue(threshold.metric, value)} (threshold: ${formatValue(threshold.metric, threshold.warningThreshold)})`,
|
|
74
|
+
timestamp: now,
|
|
75
|
+
context: { sessions: sessions.length },
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return alerts;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Detect regressions compared to baseline.
|
|
83
|
+
*/
|
|
84
|
+
export function detectRegressions(currentSessions, baselineSessions, config = DEFAULT_ALERT_CONFIG) {
|
|
85
|
+
if (!config.enableRegressionDetection)
|
|
86
|
+
return [];
|
|
87
|
+
const alerts = [];
|
|
88
|
+
const now = new Date().toISOString();
|
|
89
|
+
const currentMetrics = calculateCurrentMetrics(currentSessions);
|
|
90
|
+
const baselineMetrics = calculateCurrentMetrics(baselineSessions);
|
|
91
|
+
const metricsToCheck = [
|
|
92
|
+
{ key: 'lcp', higherIsWorse: true },
|
|
93
|
+
{ key: 'inp', higherIsWorse: true },
|
|
94
|
+
{ key: 'cls', higherIsWorse: true },
|
|
95
|
+
{ key: 'cvr', higherIsWorse: false },
|
|
96
|
+
{ key: 'bounce_rate', higherIsWorse: true },
|
|
97
|
+
];
|
|
98
|
+
for (const { key, higherIsWorse } of metricsToCheck) {
|
|
99
|
+
const current = currentMetrics[key];
|
|
100
|
+
const baseline = baselineMetrics[key];
|
|
101
|
+
if (current === null || baseline === null || baseline === 0)
|
|
102
|
+
continue;
|
|
103
|
+
const change = (current - baseline) / baseline;
|
|
104
|
+
const isRegression = higherIsWorse
|
|
105
|
+
? change > config.regressionThreshold
|
|
106
|
+
: change < -config.regressionThreshold;
|
|
107
|
+
if (isRegression) {
|
|
108
|
+
const severity = Math.abs(change) > config.regressionThreshold * 2
|
|
109
|
+
? 'critical'
|
|
110
|
+
: 'warning';
|
|
111
|
+
alerts.push({
|
|
112
|
+
id: `regression-${key}-${Date.now()}`,
|
|
113
|
+
type: 'regression',
|
|
114
|
+
severity,
|
|
115
|
+
metric: key,
|
|
116
|
+
currentValue: current,
|
|
117
|
+
threshold: baseline,
|
|
118
|
+
message: `${key.toUpperCase()} regressed ${(Math.abs(change) * 100).toFixed(1)}%: ${formatValue(key, baseline)} → ${formatValue(key, current)}`,
|
|
119
|
+
timestamp: now,
|
|
120
|
+
context: {
|
|
121
|
+
baseline,
|
|
122
|
+
change: change * 100,
|
|
123
|
+
baselineSessions: baselineSessions.length,
|
|
124
|
+
currentSessions: currentSessions.length,
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return alerts;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Detect anomalies using statistical methods.
|
|
133
|
+
*/
|
|
134
|
+
export function detectAnomalies(sessions, historicalMeans, config = DEFAULT_ALERT_CONFIG) {
|
|
135
|
+
if (!config.enableAnomalyDetection)
|
|
136
|
+
return [];
|
|
137
|
+
const alerts = [];
|
|
138
|
+
const now = new Date().toISOString();
|
|
139
|
+
const currentMetrics = calculateCurrentMetrics(sessions);
|
|
140
|
+
for (const [metric, stats] of Object.entries(historicalMeans)) {
|
|
141
|
+
const current = currentMetrics[metric];
|
|
142
|
+
if (current === null || stats.stdDev === 0)
|
|
143
|
+
continue;
|
|
144
|
+
const zScore = (current - stats.mean) / stats.stdDev;
|
|
145
|
+
if (Math.abs(zScore) > config.anomalyStdDevThreshold) {
|
|
146
|
+
const severity = Math.abs(zScore) > config.anomalyStdDevThreshold * 1.5
|
|
147
|
+
? 'critical'
|
|
148
|
+
: 'warning';
|
|
149
|
+
const direction = zScore > 0 ? 'above' : 'below';
|
|
150
|
+
alerts.push({
|
|
151
|
+
id: `anomaly-${metric}-${Date.now()}`,
|
|
152
|
+
type: 'anomaly',
|
|
153
|
+
severity,
|
|
154
|
+
metric,
|
|
155
|
+
currentValue: current,
|
|
156
|
+
threshold: stats.mean,
|
|
157
|
+
message: `${metric.toUpperCase()} anomaly detected: ${formatValue(metric, current)} is ${Math.abs(zScore).toFixed(1)} std devs ${direction} mean`,
|
|
158
|
+
timestamp: now,
|
|
159
|
+
context: {
|
|
160
|
+
mean: stats.mean,
|
|
161
|
+
stdDev: stats.stdDev,
|
|
162
|
+
zScore,
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return alerts;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Check for opportunities (positive alerts).
|
|
171
|
+
*/
|
|
172
|
+
export function detectOpportunities(sessions) {
|
|
173
|
+
const alerts = [];
|
|
174
|
+
const now = new Date().toISOString();
|
|
175
|
+
// Check if significant portion of sessions are slow
|
|
176
|
+
const lcpValues = sessions.map(s => s.lcp_ms).filter((v) => v !== null);
|
|
177
|
+
if (lcpValues.length >= 100) {
|
|
178
|
+
const slowSessions = lcpValues.filter(v => v > 2500).length;
|
|
179
|
+
const slowPercent = slowSessions / lcpValues.length;
|
|
180
|
+
if (slowPercent > 0.3) {
|
|
181
|
+
// Estimate revenue opportunity
|
|
182
|
+
const goodSessions = sessions.filter(s => s.lcp_ms !== null && s.lcp_ms <= 2500);
|
|
183
|
+
const poorSessions = sessions.filter(s => s.lcp_ms !== null && s.lcp_ms > 2500);
|
|
184
|
+
if (goodSessions.length >= 50 && poorSessions.length >= 50) {
|
|
185
|
+
const goodCvr = goodSessions.filter(s => s.has_purchase).length / goodSessions.length;
|
|
186
|
+
const poorCvr = poorSessions.filter(s => s.has_purchase).length / poorSessions.length;
|
|
187
|
+
if (goodCvr > poorCvr) {
|
|
188
|
+
const potentialLift = (goodCvr - poorCvr) * slowPercent;
|
|
189
|
+
alerts.push({
|
|
190
|
+
id: `opportunity-lcp-${Date.now()}`,
|
|
191
|
+
type: 'opportunity',
|
|
192
|
+
severity: 'info',
|
|
193
|
+
metric: 'lcp',
|
|
194
|
+
currentValue: slowPercent,
|
|
195
|
+
threshold: 0.3,
|
|
196
|
+
message: `Performance opportunity: ${(slowPercent * 100).toFixed(0)}% of sessions have slow LCP. Potential ${(potentialLift * 100).toFixed(2)}% CVR lift.`,
|
|
197
|
+
timestamp: now,
|
|
198
|
+
context: {
|
|
199
|
+
slowSessions,
|
|
200
|
+
goodCvr,
|
|
201
|
+
poorCvr,
|
|
202
|
+
potentialLift,
|
|
203
|
+
},
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return alerts;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Generate comprehensive alert report.
|
|
213
|
+
*/
|
|
214
|
+
export function generateAlertReport(sessions, config = DEFAULT_ALERT_CONFIG, baseline) {
|
|
215
|
+
const alerts = [];
|
|
216
|
+
// Threshold alerts
|
|
217
|
+
alerts.push(...checkThresholds(sessions, config));
|
|
218
|
+
// Regression alerts
|
|
219
|
+
if (baseline) {
|
|
220
|
+
alerts.push(...detectRegressions(sessions, baseline, config));
|
|
221
|
+
}
|
|
222
|
+
// Opportunity alerts
|
|
223
|
+
alerts.push(...detectOpportunities(sessions));
|
|
224
|
+
// Sort by severity
|
|
225
|
+
const severityOrder = {
|
|
226
|
+
critical: 0,
|
|
227
|
+
warning: 1,
|
|
228
|
+
info: 2,
|
|
229
|
+
};
|
|
230
|
+
alerts.sort((a, b) => severityOrder[a.severity] - severityOrder[b.severity]);
|
|
231
|
+
// Summary
|
|
232
|
+
const summary = {
|
|
233
|
+
critical: alerts.filter(a => a.severity === 'critical').length,
|
|
234
|
+
warning: alerts.filter(a => a.severity === 'warning').length,
|
|
235
|
+
info: alerts.filter(a => a.severity === 'info').length,
|
|
236
|
+
total: alerts.length,
|
|
237
|
+
};
|
|
238
|
+
// Metric status
|
|
239
|
+
const currentMetrics = calculateCurrentMetrics(sessions);
|
|
240
|
+
const metrics = [];
|
|
241
|
+
for (const threshold of config.thresholds) {
|
|
242
|
+
const value = currentMetrics[threshold.metric];
|
|
243
|
+
if (value === null)
|
|
244
|
+
continue;
|
|
245
|
+
let status = 'good';
|
|
246
|
+
if (threshold.direction === 'above') {
|
|
247
|
+
if (value > threshold.criticalThreshold)
|
|
248
|
+
status = 'critical';
|
|
249
|
+
else if (value > threshold.warningThreshold)
|
|
250
|
+
status = 'warning';
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
if (value < threshold.criticalThreshold)
|
|
254
|
+
status = 'critical';
|
|
255
|
+
else if (value < threshold.warningThreshold)
|
|
256
|
+
status = 'warning';
|
|
257
|
+
}
|
|
258
|
+
metrics.push({
|
|
259
|
+
metric: threshold.metric,
|
|
260
|
+
currentValue: value,
|
|
261
|
+
trend: 'stable', // Would need historical data for real trend
|
|
262
|
+
status,
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
return {
|
|
266
|
+
alerts,
|
|
267
|
+
summary,
|
|
268
|
+
metrics,
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
// =============================================================================
|
|
272
|
+
// REPORT GENERATION
|
|
273
|
+
// =============================================================================
|
|
274
|
+
/**
|
|
275
|
+
* Generate markdown report for alerts.
|
|
276
|
+
*/
|
|
277
|
+
export function generateAlertMarkdown(report) {
|
|
278
|
+
const lines = [];
|
|
279
|
+
lines.push('## Alert Report');
|
|
280
|
+
lines.push('');
|
|
281
|
+
// Summary
|
|
282
|
+
if (report.summary.critical > 0) {
|
|
283
|
+
lines.push(`### 🚨 ${report.summary.critical} Critical Alerts`);
|
|
284
|
+
}
|
|
285
|
+
else if (report.summary.warning > 0) {
|
|
286
|
+
lines.push(`### ⚠️ ${report.summary.warning} Warnings`);
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
lines.push('### ✅ All Clear');
|
|
290
|
+
}
|
|
291
|
+
lines.push('');
|
|
292
|
+
// Metric status
|
|
293
|
+
lines.push('### Metric Status');
|
|
294
|
+
lines.push('');
|
|
295
|
+
lines.push('| Metric | Value | Status |');
|
|
296
|
+
lines.push('|--------|-------|--------|');
|
|
297
|
+
for (const metric of report.metrics) {
|
|
298
|
+
const icon = metric.status === 'good' ? '✅' :
|
|
299
|
+
metric.status === 'warning' ? '⚠️' : '🚨';
|
|
300
|
+
lines.push(`| ${metric.metric.toUpperCase()} | ${formatValue(metric.metric, metric.currentValue)} | ${icon} ${metric.status} |`);
|
|
301
|
+
}
|
|
302
|
+
lines.push('');
|
|
303
|
+
// Active alerts
|
|
304
|
+
if (report.alerts.length > 0) {
|
|
305
|
+
lines.push('### Active Alerts');
|
|
306
|
+
lines.push('');
|
|
307
|
+
for (const alert of report.alerts) {
|
|
308
|
+
const icon = alert.severity === 'critical' ? '🚨' :
|
|
309
|
+
alert.severity === 'warning' ? '⚠️' : 'ℹ️';
|
|
310
|
+
lines.push(`- ${icon} **[${alert.type.toUpperCase()}]** ${alert.message}`);
|
|
311
|
+
}
|
|
312
|
+
lines.push('');
|
|
313
|
+
}
|
|
314
|
+
return lines.join('\n');
|
|
315
|
+
}
|
|
316
|
+
// =============================================================================
|
|
317
|
+
// HELPERS
|
|
318
|
+
// =============================================================================
|
|
319
|
+
function calculateCurrentMetrics(sessions) {
|
|
320
|
+
const lcpValues = sessions.map(s => s.lcp_ms).filter((v) => v !== null);
|
|
321
|
+
const inpValues = sessions.map(s => s.inp_ms).filter((v) => v !== null);
|
|
322
|
+
const clsValues = sessions.map(s => s.cls).filter((v) => v !== null);
|
|
323
|
+
const fcpValues = sessions.map(s => s.fcp_ms).filter((v) => v !== null);
|
|
324
|
+
const ttfbValues = sessions.map(s => s.ttfb_ms).filter((v) => v !== null);
|
|
325
|
+
const conversions = sessions.filter(s => s.has_purchase).length;
|
|
326
|
+
const bounces = sessions.filter(s => s.bounced).length;
|
|
327
|
+
return {
|
|
328
|
+
lcp: lcpValues.length > 0 ? percentile(lcpValues, 75) : null,
|
|
329
|
+
inp: inpValues.length > 0 ? percentile(inpValues, 75) : null,
|
|
330
|
+
cls: clsValues.length > 0 ? percentile(clsValues, 75) : null,
|
|
331
|
+
fcp: fcpValues.length > 0 ? percentile(fcpValues, 75) : null,
|
|
332
|
+
ttfb: ttfbValues.length > 0 ? percentile(ttfbValues, 75) : null,
|
|
333
|
+
cvr: sessions.length > 0 ? conversions / sessions.length : null,
|
|
334
|
+
bounce_rate: sessions.length > 0 ? bounces / sessions.length : null,
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
function percentile(values, p) {
|
|
338
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
339
|
+
const index = Math.ceil((p / 100) * sorted.length) - 1;
|
|
340
|
+
return sorted[Math.max(0, index)];
|
|
341
|
+
}
|
|
342
|
+
function formatValue(metric, value) {
|
|
343
|
+
switch (metric) {
|
|
344
|
+
case 'lcp':
|
|
345
|
+
case 'inp':
|
|
346
|
+
case 'fcp':
|
|
347
|
+
case 'ttfb':
|
|
348
|
+
return `${value.toFixed(0)}ms`;
|
|
349
|
+
case 'cls':
|
|
350
|
+
return value.toFixed(3);
|
|
351
|
+
case 'cvr':
|
|
352
|
+
case 'bounce_rate':
|
|
353
|
+
return `${(value * 100).toFixed(2)}%`;
|
|
354
|
+
default:
|
|
355
|
+
return value.toFixed(2);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
//# sourceMappingURL=alerting.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"alerting.js","sourceRoot":"","sources":["../../src/empirical/alerting.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AA2DH,gFAAgF;AAChF,iBAAiB;AACjB,gFAAgF;AAEhF,MAAM,CAAC,MAAM,kBAAkB,GAAqB;IAClD,gCAAgC;IAChC,EAAE,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
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attribution Models
|
|
3
|
+
*
|
|
4
|
+
* First-touch vs last-touch session attribution for understanding
|
|
5
|
+
* how performance at different touchpoints affects conversions.
|
|
6
|
+
*
|
|
7
|
+
* @author Kanmi Obasa <i@kanmiobasa.com>
|
|
8
|
+
*/
|
|
9
|
+
import type { SessionData } from './datadog-session-query.js';
|
|
10
|
+
export type AttributionModel = 'first_touch' | 'last_touch' | 'linear' | 'position_based';
|
|
11
|
+
export interface TouchpointStats {
|
|
12
|
+
/** Touchpoint identifier (e.g., page type, traffic source) */
|
|
13
|
+
touchpoint: string;
|
|
14
|
+
/** Total sessions at this touchpoint */
|
|
15
|
+
sessions: number;
|
|
16
|
+
/** First-touch attributions */
|
|
17
|
+
firstTouchConversions: number;
|
|
18
|
+
/** Last-touch attributions */
|
|
19
|
+
lastTouchConversions: number;
|
|
20
|
+
/** Linear attributions (partial credit) */
|
|
21
|
+
linearConversions: number;
|
|
22
|
+
/** Position-based attributions (40/20/40 model) */
|
|
23
|
+
positionBasedConversions: number;
|
|
24
|
+
/** Average performance at this touchpoint */
|
|
25
|
+
avgLcp: number | null;
|
|
26
|
+
/** Performance impact score */
|
|
27
|
+
performanceImpactScore: number;
|
|
28
|
+
}
|
|
29
|
+
export interface UserJourney {
|
|
30
|
+
userId: string;
|
|
31
|
+
touchpoints: Array<{
|
|
32
|
+
timestamp: string;
|
|
33
|
+
pageType: string;
|
|
34
|
+
trafficSource: string;
|
|
35
|
+
lcp: number | null;
|
|
36
|
+
converted: boolean;
|
|
37
|
+
value: number;
|
|
38
|
+
}>;
|
|
39
|
+
converted: boolean;
|
|
40
|
+
totalValue: number;
|
|
41
|
+
}
|
|
42
|
+
export interface AttributionReport {
|
|
43
|
+
model: AttributionModel;
|
|
44
|
+
touchpointStats: TouchpointStats[];
|
|
45
|
+
insights: string[];
|
|
46
|
+
recommendations: string[];
|
|
47
|
+
performanceInfluence: {
|
|
48
|
+
/** Correlation between first-touch performance and conversion */
|
|
49
|
+
firstTouchCorrelation: number;
|
|
50
|
+
/** Correlation between last-touch performance and conversion */
|
|
51
|
+
lastTouchCorrelation: number;
|
|
52
|
+
/** Which touchpoint's performance matters most? */
|
|
53
|
+
criticalTouchpoint: string;
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Build user journeys from session data.
|
|
58
|
+
*/
|
|
59
|
+
export declare function buildUserJourneys(sessions: SessionData[]): UserJourney[];
|
|
60
|
+
/**
|
|
61
|
+
* Calculate attribution for all touchpoints.
|
|
62
|
+
*/
|
|
63
|
+
export declare function calculateAttribution(journeys: UserJourney[], groupBy?: 'pageType' | 'trafficSource'): TouchpointStats[];
|
|
64
|
+
/**
|
|
65
|
+
* Analyze performance impact at different journey stages.
|
|
66
|
+
*/
|
|
67
|
+
export declare function analyzePerformanceInfluence(journeys: UserJourney[]): {
|
|
68
|
+
firstTouchCorrelation: number;
|
|
69
|
+
lastTouchCorrelation: number;
|
|
70
|
+
criticalTouchpoint: string;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Generate full attribution report.
|
|
74
|
+
*/
|
|
75
|
+
export declare function generateAttributionReport(sessions: SessionData[], model?: AttributionModel, groupBy?: 'pageType' | 'trafficSource'): AttributionReport;
|
|
76
|
+
/**
|
|
77
|
+
* Generate markdown report for attribution analysis.
|
|
78
|
+
*/
|
|
79
|
+
export declare function generateAttributionMarkdown(report: AttributionReport): string;
|
|
80
|
+
//# sourceMappingURL=attribution.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attribution.d.ts","sourceRoot":"","sources":["../../src/empirical/attribution.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AAM9D,MAAM,MAAM,gBAAgB,GAAG,aAAa,GAAG,YAAY,GAAG,QAAQ,GAAG,gBAAgB,CAAC;A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