@coursebuilder/analytics 1.1.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/LICENSE +21 -0
- package/dist/api/index.d.ts +158 -0
- package/dist/api/index.js +317 -0
- package/dist/api/index.js.map +1 -0
- package/dist/catalog.d.ts +14 -0
- package/dist/catalog.js +209 -0
- package/dist/catalog.js.map +1 -0
- package/dist/components/index.d.ts +172 -0
- package/dist/components/index.js +1258 -0
- package/dist/components/index.js.map +1 -0
- package/dist/engine.d.ts +20 -0
- package/dist/engine.js +350 -0
- package/dist/engine.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +353 -0
- package/dist/index.js.map +1 -0
- package/dist/providers/database.d.ts +79 -0
- package/dist/providers/database.js +533 -0
- package/dist/providers/database.js.map +1 -0
- package/dist/providers/derived.d.ts +45 -0
- package/dist/providers/derived.js +32 -0
- package/dist/providers/derived.js.map +1 -0
- package/dist/providers/ga4.d.ts +43 -0
- package/dist/providers/ga4.js +220 -0
- package/dist/providers/ga4.js.map +1 -0
- package/dist/providers/index.d.ts +8 -0
- package/dist/providers/index.js +1239 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/mux.d.ts +103 -0
- package/dist/providers/mux.js +241 -0
- package/dist/providers/mux.js.map +1 -0
- package/dist/providers/survey.d.ts +102 -0
- package/dist/providers/survey.js +233 -0
- package/dist/providers/survey.js.map +1 -0
- package/dist/types.d.ts +303 -0
- package/dist/types.js +1 -0
- package/dist/types.js.map +1 -0
- package/package.json +101 -0
- package/src/api/catalog-handler.ts +321 -0
- package/src/api/index.ts +4 -0
- package/src/api/token-handler.ts +71 -0
- package/src/catalog.ts +223 -0
- package/src/components/country-chart.tsx +114 -0
- package/src/components/index.ts +5 -0
- package/src/components/omnibus-dashboard.tsx +1460 -0
- package/src/components/revenue-chart.tsx +251 -0
- package/src/components/use-chart-colors.ts +75 -0
- package/src/engine.ts +201 -0
- package/src/index.ts +7 -0
- package/src/providers/database.ts +795 -0
- package/src/providers/derived.ts +79 -0
- package/src/providers/ga4.ts +173 -0
- package/src/providers/index.ts +44 -0
- package/src/providers/mux.ts +438 -0
- package/src/providers/survey.ts +487 -0
- package/src/types.ts +333 -0
package/dist/engine.js
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// src/catalog.ts
|
|
5
|
+
var catalog = {
|
|
6
|
+
summary: {
|
|
7
|
+
name: "summary",
|
|
8
|
+
description: "Revenue overview: total, purchase count, AOV",
|
|
9
|
+
category: "revenue",
|
|
10
|
+
provider: "database",
|
|
11
|
+
fn: "getRevenueSummary"
|
|
12
|
+
},
|
|
13
|
+
"revenue/daily": {
|
|
14
|
+
name: "revenue/daily",
|
|
15
|
+
description: "Revenue and purchase count per day",
|
|
16
|
+
category: "revenue",
|
|
17
|
+
provider: "database",
|
|
18
|
+
fn: "getRevenueByDay"
|
|
19
|
+
},
|
|
20
|
+
"revenue/products": {
|
|
21
|
+
name: "revenue/products",
|
|
22
|
+
description: "Revenue grouped by product",
|
|
23
|
+
category: "revenue",
|
|
24
|
+
provider: "database",
|
|
25
|
+
fn: "getRevenueByProduct"
|
|
26
|
+
},
|
|
27
|
+
"revenue/countries": {
|
|
28
|
+
name: "revenue/countries",
|
|
29
|
+
description: "Revenue grouped by country",
|
|
30
|
+
category: "revenue",
|
|
31
|
+
provider: "database",
|
|
32
|
+
fn: "getRevenueByCountry"
|
|
33
|
+
},
|
|
34
|
+
"purchases/recent": {
|
|
35
|
+
name: "purchases/recent",
|
|
36
|
+
description: "Last N purchases",
|
|
37
|
+
category: "revenue",
|
|
38
|
+
provider: "database",
|
|
39
|
+
fn: "getRecentPurchases"
|
|
40
|
+
},
|
|
41
|
+
attribution: {
|
|
42
|
+
name: "attribution",
|
|
43
|
+
description: "Attribution event counts by type",
|
|
44
|
+
category: "attribution",
|
|
45
|
+
provider: "database",
|
|
46
|
+
fn: "getAttributionSummary"
|
|
47
|
+
},
|
|
48
|
+
"attribution/shortlinks": {
|
|
49
|
+
name: "attribution/shortlinks",
|
|
50
|
+
description: "Per-shortlink click performance",
|
|
51
|
+
category: "attribution",
|
|
52
|
+
provider: "database",
|
|
53
|
+
fn: "getShortlinkPerformance"
|
|
54
|
+
},
|
|
55
|
+
"attribution/sources": {
|
|
56
|
+
name: "attribution/sources",
|
|
57
|
+
description: "Revenue by first-touch source/medium/campaign",
|
|
58
|
+
category: "attribution",
|
|
59
|
+
provider: "database",
|
|
60
|
+
fn: "getRevenueBySource"
|
|
61
|
+
},
|
|
62
|
+
"attribution/funnel": {
|
|
63
|
+
name: "attribution/funnel",
|
|
64
|
+
description: "Signup \u2192 purchase conversion funnel",
|
|
65
|
+
category: "attribution",
|
|
66
|
+
provider: "database",
|
|
67
|
+
fn: "getConversionFunnel"
|
|
68
|
+
},
|
|
69
|
+
"attribution/content": {
|
|
70
|
+
name: "attribution/content",
|
|
71
|
+
description: "Content consumed by purchasers",
|
|
72
|
+
category: "attribution",
|
|
73
|
+
provider: "database",
|
|
74
|
+
fn: "getContentPurchaseCorrelation"
|
|
75
|
+
},
|
|
76
|
+
"attribution/coverage": {
|
|
77
|
+
name: "attribution/coverage",
|
|
78
|
+
description: "Attributed vs dark revenue",
|
|
79
|
+
category: "attribution",
|
|
80
|
+
provider: "database",
|
|
81
|
+
fn: "getAttributedRevenueSummary"
|
|
82
|
+
},
|
|
83
|
+
traffic: {
|
|
84
|
+
name: "traffic",
|
|
85
|
+
description: "GA4 traffic overview",
|
|
86
|
+
category: "traffic",
|
|
87
|
+
provider: "ga4",
|
|
88
|
+
fn: "getTrafficOverview"
|
|
89
|
+
},
|
|
90
|
+
"traffic/daily": {
|
|
91
|
+
name: "traffic/daily",
|
|
92
|
+
description: "GA4 daily sessions",
|
|
93
|
+
category: "traffic",
|
|
94
|
+
provider: "ga4",
|
|
95
|
+
fn: "getSessionsByDay"
|
|
96
|
+
},
|
|
97
|
+
"traffic/pages": {
|
|
98
|
+
name: "traffic/pages",
|
|
99
|
+
description: "Top pages by pageviews",
|
|
100
|
+
category: "traffic",
|
|
101
|
+
provider: "ga4",
|
|
102
|
+
fn: "getTopPages"
|
|
103
|
+
},
|
|
104
|
+
"traffic/sources": {
|
|
105
|
+
name: "traffic/sources",
|
|
106
|
+
description: "Traffic sources",
|
|
107
|
+
category: "traffic",
|
|
108
|
+
provider: "ga4",
|
|
109
|
+
fn: "getTrafficSources"
|
|
110
|
+
},
|
|
111
|
+
youtube: {
|
|
112
|
+
name: "youtube",
|
|
113
|
+
description: "Channel overview (\u224848h lag)",
|
|
114
|
+
category: "youtube",
|
|
115
|
+
provider: "youtube",
|
|
116
|
+
fn: "getChannelOverview",
|
|
117
|
+
unavailableFix: "Complete OAuth at /api/analytics/youtube-auth"
|
|
118
|
+
},
|
|
119
|
+
"youtube/videos": {
|
|
120
|
+
name: "youtube/videos",
|
|
121
|
+
description: "Per-video performance (\u224848h lag)",
|
|
122
|
+
category: "youtube",
|
|
123
|
+
provider: "youtube",
|
|
124
|
+
fn: "getVideoPerformance",
|
|
125
|
+
unavailableFix: "Complete OAuth at /api/analytics/youtube-auth"
|
|
126
|
+
},
|
|
127
|
+
"youtube/daily": {
|
|
128
|
+
name: "youtube/daily",
|
|
129
|
+
description: "Daily views + watch minutes (\u224848h lag)",
|
|
130
|
+
category: "youtube",
|
|
131
|
+
provider: "youtube",
|
|
132
|
+
fn: "getChannelTimeseries",
|
|
133
|
+
unavailableFix: "Complete OAuth at /api/analytics/youtube-auth"
|
|
134
|
+
},
|
|
135
|
+
"youtube/sources": {
|
|
136
|
+
name: "youtube/sources",
|
|
137
|
+
description: "YouTube traffic sources (\u224848h lag)",
|
|
138
|
+
category: "youtube",
|
|
139
|
+
provider: "youtube",
|
|
140
|
+
fn: "getYouTubeTrafficSources",
|
|
141
|
+
unavailableFix: "Complete OAuth at /api/analytics/youtube-auth"
|
|
142
|
+
},
|
|
143
|
+
"correlation/traffic-revenue": {
|
|
144
|
+
name: "correlation/traffic-revenue",
|
|
145
|
+
description: "GA4 sessions + revenue by day",
|
|
146
|
+
category: "correlation",
|
|
147
|
+
provider: "derived",
|
|
148
|
+
fn: "getTrafficRevenueCorrelation"
|
|
149
|
+
},
|
|
150
|
+
"correlation/youtube-revenue": {
|
|
151
|
+
name: "correlation/youtube-revenue",
|
|
152
|
+
description: "YouTube (\u224848h lag) + GA4 + revenue overlay",
|
|
153
|
+
category: "correlation",
|
|
154
|
+
provider: "derived",
|
|
155
|
+
fn: "getYouTubeRevenueCorrelation"
|
|
156
|
+
},
|
|
157
|
+
surveys: {
|
|
158
|
+
name: "surveys",
|
|
159
|
+
description: "Survey overview: total surveys, responses, respondents",
|
|
160
|
+
category: "survey",
|
|
161
|
+
provider: "survey",
|
|
162
|
+
fn: "getSurveySummary"
|
|
163
|
+
},
|
|
164
|
+
"surveys/list": {
|
|
165
|
+
name: "surveys/list",
|
|
166
|
+
description: "All surveys with response counts",
|
|
167
|
+
category: "survey",
|
|
168
|
+
provider: "survey",
|
|
169
|
+
fn: "getSurveyList"
|
|
170
|
+
},
|
|
171
|
+
"surveys/daily": {
|
|
172
|
+
name: "surveys/daily",
|
|
173
|
+
description: "Daily survey response volume",
|
|
174
|
+
category: "survey",
|
|
175
|
+
provider: "survey",
|
|
176
|
+
fn: "getSurveyResponsesByDay"
|
|
177
|
+
},
|
|
178
|
+
"surveys/questions": {
|
|
179
|
+
name: "surveys/questions",
|
|
180
|
+
description: "Top questions by response count with answer distribution",
|
|
181
|
+
category: "survey",
|
|
182
|
+
provider: "survey",
|
|
183
|
+
fn: "getSurveyQuestionBreakdown"
|
|
184
|
+
},
|
|
185
|
+
"surveys/responses": {
|
|
186
|
+
name: "surveys/responses",
|
|
187
|
+
description: "Individual survey responses as flat rows (multi-choice and open-ended)",
|
|
188
|
+
category: "survey",
|
|
189
|
+
provider: "survey",
|
|
190
|
+
fn: "getSurveyResponses"
|
|
191
|
+
},
|
|
192
|
+
"attribution/email-campaigns": {
|
|
193
|
+
name: "attribution/email-campaigns",
|
|
194
|
+
description: "Kit email broadcasts \u2192 shortlink clicks \u2192 signups \u2192 purchases \u2192 revenue per campaign",
|
|
195
|
+
category: "attribution",
|
|
196
|
+
provider: "newsletter",
|
|
197
|
+
fn: "getEmailCampaignAttribution"
|
|
198
|
+
},
|
|
199
|
+
"correlation/survey-revenue": {
|
|
200
|
+
name: "correlation/survey-revenue",
|
|
201
|
+
description: "Survey respondents \u2192 purchase conversion by question/answer",
|
|
202
|
+
category: "correlation",
|
|
203
|
+
provider: "derived",
|
|
204
|
+
fn: "getSurveyRevenueCorrelation"
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
// src/engine.ts
|
|
209
|
+
function toGA4Range(range) {
|
|
210
|
+
if (range === "all")
|
|
211
|
+
return "90d";
|
|
212
|
+
return range;
|
|
213
|
+
}
|
|
214
|
+
__name(toGA4Range, "toGA4Range");
|
|
215
|
+
function toYouTubeRange(range) {
|
|
216
|
+
if (range === "all")
|
|
217
|
+
return "90d";
|
|
218
|
+
return range;
|
|
219
|
+
}
|
|
220
|
+
__name(toYouTubeRange, "toYouTubeRange");
|
|
221
|
+
async function invokeSurface(providers, surface, range, limit) {
|
|
222
|
+
const entry = catalog[surface];
|
|
223
|
+
const provider = providers[entry.provider];
|
|
224
|
+
if (!provider) {
|
|
225
|
+
return null;
|
|
226
|
+
}
|
|
227
|
+
if (typeof provider[entry.fn] !== "function") {
|
|
228
|
+
return null;
|
|
229
|
+
}
|
|
230
|
+
switch (surface) {
|
|
231
|
+
case "purchases/recent": {
|
|
232
|
+
const fn = provider[entry.fn];
|
|
233
|
+
return fn(limit);
|
|
234
|
+
}
|
|
235
|
+
case "youtube": {
|
|
236
|
+
const fn = provider[entry.fn];
|
|
237
|
+
return fn();
|
|
238
|
+
}
|
|
239
|
+
case "youtube/videos":
|
|
240
|
+
case "youtube/daily":
|
|
241
|
+
case "youtube/sources": {
|
|
242
|
+
const fn = provider[entry.fn];
|
|
243
|
+
if (surface === "youtube/videos") {
|
|
244
|
+
return fn(toYouTubeRange(range), limit);
|
|
245
|
+
}
|
|
246
|
+
return fn(toYouTubeRange(range));
|
|
247
|
+
}
|
|
248
|
+
case "traffic":
|
|
249
|
+
case "traffic/daily":
|
|
250
|
+
case "traffic/pages":
|
|
251
|
+
case "traffic/sources": {
|
|
252
|
+
const fn = provider[entry.fn];
|
|
253
|
+
if (surface === "traffic/pages" || surface === "traffic/sources") {
|
|
254
|
+
return fn(toGA4Range(range), limit);
|
|
255
|
+
}
|
|
256
|
+
return fn(toGA4Range(range));
|
|
257
|
+
}
|
|
258
|
+
case "surveys":
|
|
259
|
+
case "surveys/list":
|
|
260
|
+
case "surveys/daily": {
|
|
261
|
+
const fn = provider[entry.fn];
|
|
262
|
+
return fn(range);
|
|
263
|
+
}
|
|
264
|
+
case "attribution/email-campaigns": {
|
|
265
|
+
const fn = provider[entry.fn];
|
|
266
|
+
return fn(range, limit);
|
|
267
|
+
}
|
|
268
|
+
case "surveys/questions":
|
|
269
|
+
case "surveys/responses": {
|
|
270
|
+
const fn = provider[entry.fn];
|
|
271
|
+
return fn(range, limit);
|
|
272
|
+
}
|
|
273
|
+
default: {
|
|
274
|
+
const fn = provider[entry.fn];
|
|
275
|
+
if (surface === "attribution/content" || surface === "correlation/survey-revenue") {
|
|
276
|
+
return fn(range, limit);
|
|
277
|
+
}
|
|
278
|
+
return fn(range);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
__name(invokeSurface, "invokeSurface");
|
|
283
|
+
function createAnalyticsEngine(providers) {
|
|
284
|
+
async function query(surface, options) {
|
|
285
|
+
const range = options?.range ?? "30d";
|
|
286
|
+
const limit = options?.limit ?? 20;
|
|
287
|
+
const entry = catalog[surface];
|
|
288
|
+
const startMs = Date.now();
|
|
289
|
+
try {
|
|
290
|
+
const data = await invokeSurface(providers, surface, range, limit);
|
|
291
|
+
if (data === null) {
|
|
292
|
+
return {
|
|
293
|
+
ok: false,
|
|
294
|
+
surface,
|
|
295
|
+
error: {
|
|
296
|
+
message: `${entry.provider} is not available`,
|
|
297
|
+
code: `${entry.provider.toUpperCase()}_UNAVAILABLE`
|
|
298
|
+
},
|
|
299
|
+
fix: entry.unavailableFix ?? "Check provider configuration"
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
return {
|
|
303
|
+
ok: true,
|
|
304
|
+
surface,
|
|
305
|
+
range,
|
|
306
|
+
data,
|
|
307
|
+
meta: {
|
|
308
|
+
queryTimeMs: Date.now() - startMs,
|
|
309
|
+
truncated: Array.isArray(data) && data.length >= limit
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
} catch (error) {
|
|
313
|
+
return {
|
|
314
|
+
ok: false,
|
|
315
|
+
surface,
|
|
316
|
+
error: {
|
|
317
|
+
message: error instanceof Error ? error.message : String(error),
|
|
318
|
+
code: "QUERY_FAILED"
|
|
319
|
+
},
|
|
320
|
+
fix: `The ${surface} query failed. Try a different range or check server logs.`
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
__name(query, "query");
|
|
325
|
+
async function queryMany(surfaces, options) {
|
|
326
|
+
const results = await Promise.all(surfaces.map(async (surface) => [
|
|
327
|
+
surface,
|
|
328
|
+
await query(surface, options)
|
|
329
|
+
]));
|
|
330
|
+
return Object.fromEntries(results);
|
|
331
|
+
}
|
|
332
|
+
__name(queryMany, "queryMany");
|
|
333
|
+
function getCatalog() {
|
|
334
|
+
return Object.values(catalog).filter((entry) => {
|
|
335
|
+
const provider = providers[entry.provider];
|
|
336
|
+
return provider && typeof provider[entry.fn] === "function";
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
__name(getCatalog, "getCatalog");
|
|
340
|
+
return {
|
|
341
|
+
query,
|
|
342
|
+
queryMany,
|
|
343
|
+
getCatalog
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
__name(createAnalyticsEngine, "createAnalyticsEngine");
|
|
347
|
+
export {
|
|
348
|
+
createAnalyticsEngine
|
|
349
|
+
};
|
|
350
|
+
//# sourceMappingURL=engine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/catalog.ts","../src/engine.ts"],"sourcesContent":["import type { SurfaceName } from './types'\n\nexport interface SurfaceEntry {\n\tname: SurfaceName\n\tdescription: string\n\tcategory:\n\t\t| 'revenue'\n\t\t| 'attribution'\n\t\t| 'traffic'\n\t\t| 'youtube'\n\t\t| 'correlation'\n\t\t| 'survey'\n\tprovider: 'database' | 'ga4' | 'youtube' | 'derived' | 'newsletter' | 'survey'\n\tfn: string\n\tunavailableFix?: string\n}\n\nexport const catalog: Record<SurfaceName, SurfaceEntry> = {\n\tsummary: {\n\t\tname: 'summary',\n\t\tdescription: 'Revenue overview: total, purchase count, AOV',\n\t\tcategory: 'revenue',\n\t\tprovider: 'database',\n\t\tfn: 'getRevenueSummary',\n\t},\n\t'revenue/daily': {\n\t\tname: 'revenue/daily',\n\t\tdescription: 'Revenue and purchase count per day',\n\t\tcategory: 'revenue',\n\t\tprovider: 'database',\n\t\tfn: 'getRevenueByDay',\n\t},\n\t'revenue/products': {\n\t\tname: 'revenue/products',\n\t\tdescription: 'Revenue grouped by product',\n\t\tcategory: 'revenue',\n\t\tprovider: 'database',\n\t\tfn: 'getRevenueByProduct',\n\t},\n\t'revenue/countries': {\n\t\tname: 'revenue/countries',\n\t\tdescription: 'Revenue grouped by country',\n\t\tcategory: 'revenue',\n\t\tprovider: 'database',\n\t\tfn: 'getRevenueByCountry',\n\t},\n\t'purchases/recent': {\n\t\tname: 'purchases/recent',\n\t\tdescription: 'Last N purchases',\n\t\tcategory: 'revenue',\n\t\tprovider: 'database',\n\t\tfn: 'getRecentPurchases',\n\t},\n\tattribution: {\n\t\tname: 'attribution',\n\t\tdescription: 'Attribution event counts by type',\n\t\tcategory: 'attribution',\n\t\tprovider: 'database',\n\t\tfn: 'getAttributionSummary',\n\t},\n\t'attribution/shortlinks': {\n\t\tname: 'attribution/shortlinks',\n\t\tdescription: 'Per-shortlink click performance',\n\t\tcategory: 'attribution',\n\t\tprovider: 'database',\n\t\tfn: 'getShortlinkPerformance',\n\t},\n\t'attribution/sources': {\n\t\tname: 'attribution/sources',\n\t\tdescription: 'Revenue by first-touch source/medium/campaign',\n\t\tcategory: 'attribution',\n\t\tprovider: 'database',\n\t\tfn: 'getRevenueBySource',\n\t},\n\t'attribution/funnel': {\n\t\tname: 'attribution/funnel',\n\t\tdescription: 'Signup → purchase conversion funnel',\n\t\tcategory: 'attribution',\n\t\tprovider: 'database',\n\t\tfn: 'getConversionFunnel',\n\t},\n\t'attribution/content': {\n\t\tname: 'attribution/content',\n\t\tdescription: 'Content consumed by purchasers',\n\t\tcategory: 'attribution',\n\t\tprovider: 'database',\n\t\tfn: 'getContentPurchaseCorrelation',\n\t},\n\t'attribution/coverage': {\n\t\tname: 'attribution/coverage',\n\t\tdescription: 'Attributed vs dark revenue',\n\t\tcategory: 'attribution',\n\t\tprovider: 'database',\n\t\tfn: 'getAttributedRevenueSummary',\n\t},\n\ttraffic: {\n\t\tname: 'traffic',\n\t\tdescription: 'GA4 traffic overview',\n\t\tcategory: 'traffic',\n\t\tprovider: 'ga4',\n\t\tfn: 'getTrafficOverview',\n\t},\n\t'traffic/daily': {\n\t\tname: 'traffic/daily',\n\t\tdescription: 'GA4 daily sessions',\n\t\tcategory: 'traffic',\n\t\tprovider: 'ga4',\n\t\tfn: 'getSessionsByDay',\n\t},\n\t'traffic/pages': {\n\t\tname: 'traffic/pages',\n\t\tdescription: 'Top pages by pageviews',\n\t\tcategory: 'traffic',\n\t\tprovider: 'ga4',\n\t\tfn: 'getTopPages',\n\t},\n\t'traffic/sources': {\n\t\tname: 'traffic/sources',\n\t\tdescription: 'Traffic sources',\n\t\tcategory: 'traffic',\n\t\tprovider: 'ga4',\n\t\tfn: 'getTrafficSources',\n\t},\n\tyoutube: {\n\t\tname: 'youtube',\n\t\tdescription: 'Channel overview (≈48h lag)',\n\t\tcategory: 'youtube',\n\t\tprovider: 'youtube',\n\t\tfn: 'getChannelOverview',\n\t\tunavailableFix: 'Complete OAuth at /api/analytics/youtube-auth',\n\t},\n\t'youtube/videos': {\n\t\tname: 'youtube/videos',\n\t\tdescription: 'Per-video performance (≈48h lag)',\n\t\tcategory: 'youtube',\n\t\tprovider: 'youtube',\n\t\tfn: 'getVideoPerformance',\n\t\tunavailableFix: 'Complete OAuth at /api/analytics/youtube-auth',\n\t},\n\t'youtube/daily': {\n\t\tname: 'youtube/daily',\n\t\tdescription: 'Daily views + watch minutes (≈48h lag)',\n\t\tcategory: 'youtube',\n\t\tprovider: 'youtube',\n\t\tfn: 'getChannelTimeseries',\n\t\tunavailableFix: 'Complete OAuth at /api/analytics/youtube-auth',\n\t},\n\t'youtube/sources': {\n\t\tname: 'youtube/sources',\n\t\tdescription: 'YouTube traffic sources (≈48h lag)',\n\t\tcategory: 'youtube',\n\t\tprovider: 'youtube',\n\t\tfn: 'getYouTubeTrafficSources',\n\t\tunavailableFix: 'Complete OAuth at /api/analytics/youtube-auth',\n\t},\n\t'correlation/traffic-revenue': {\n\t\tname: 'correlation/traffic-revenue',\n\t\tdescription: 'GA4 sessions + revenue by day',\n\t\tcategory: 'correlation',\n\t\tprovider: 'derived',\n\t\tfn: 'getTrafficRevenueCorrelation',\n\t},\n\t'correlation/youtube-revenue': {\n\t\tname: 'correlation/youtube-revenue',\n\t\tdescription: 'YouTube (≈48h lag) + GA4 + revenue overlay',\n\t\tcategory: 'correlation',\n\t\tprovider: 'derived',\n\t\tfn: 'getYouTubeRevenueCorrelation',\n\t},\n\tsurveys: {\n\t\tname: 'surveys',\n\t\tdescription: 'Survey overview: total surveys, responses, respondents',\n\t\tcategory: 'survey',\n\t\tprovider: 'survey',\n\t\tfn: 'getSurveySummary',\n\t},\n\t'surveys/list': {\n\t\tname: 'surveys/list',\n\t\tdescription: 'All surveys with response counts',\n\t\tcategory: 'survey',\n\t\tprovider: 'survey',\n\t\tfn: 'getSurveyList',\n\t},\n\t'surveys/daily': {\n\t\tname: 'surveys/daily',\n\t\tdescription: 'Daily survey response volume',\n\t\tcategory: 'survey',\n\t\tprovider: 'survey',\n\t\tfn: 'getSurveyResponsesByDay',\n\t},\n\t'surveys/questions': {\n\t\tname: 'surveys/questions',\n\t\tdescription: 'Top questions by response count with answer distribution',\n\t\tcategory: 'survey',\n\t\tprovider: 'survey',\n\t\tfn: 'getSurveyQuestionBreakdown',\n\t},\n\t'surveys/responses': {\n\t\tname: 'surveys/responses',\n\t\tdescription:\n\t\t\t'Individual survey responses as flat rows (multi-choice and open-ended)',\n\t\tcategory: 'survey',\n\t\tprovider: 'survey',\n\t\tfn: 'getSurveyResponses',\n\t},\n\t'attribution/email-campaigns': {\n\t\tname: 'attribution/email-campaigns',\n\t\tdescription:\n\t\t\t'Kit email broadcasts → shortlink clicks → signups → purchases → revenue per campaign',\n\t\tcategory: 'attribution',\n\t\tprovider: 'newsletter',\n\t\tfn: 'getEmailCampaignAttribution',\n\t},\n\t'correlation/survey-revenue': {\n\t\tname: 'correlation/survey-revenue',\n\t\tdescription: 'Survey respondents → purchase conversion by question/answer',\n\t\tcategory: 'correlation',\n\t\tprovider: 'derived',\n\t\tfn: 'getSurveyRevenueCorrelation',\n\t},\n} as const satisfies Record<SurfaceName, SurfaceEntry>\n\nexport const ANALYTICS_CATALOG = catalog\n","import { catalog, type SurfaceEntry } from './catalog'\nimport type {\n\tAnalyticsRange,\n\tQueryOptions,\n\tQueryResult,\n\tSurfaceMap,\n\tSurfaceName,\n} from './types'\n\nexport type AnalyticsProvider = Record<\n\tstring,\n\t((...args: any[]) => Promise<any>) | unknown\n>\n\nexport type ProviderMap = Record<string, AnalyticsProvider>\n\nfunction toGA4Range(range: AnalyticsRange): '24h' | '7d' | '30d' | '90d' {\n\tif (range === 'all') return '90d'\n\treturn range\n}\n\nfunction toYouTubeRange(range: AnalyticsRange): '24h' | '7d' | '30d' | '90d' {\n\tif (range === 'all') return '90d'\n\treturn range\n}\n\nasync function invokeSurface<S extends SurfaceName>(\n\tproviders: ProviderMap,\n\tsurface: S,\n\trange: AnalyticsRange,\n\tlimit: number,\n): Promise<SurfaceMap[S] | null> {\n\tconst entry = catalog[surface]\n\tconst provider = providers[entry.provider]\n\n\tif (!provider) {\n\t\treturn null\n\t}\n\n\t// Check that the provider actually exposes the expected function.\n\t// A provider may exist but not support every surface in the catalog\n\t// (e.g. CWA's derived provider omits YouTube correlation).\n\tif (typeof provider[entry.fn] !== 'function') {\n\t\treturn null\n\t}\n\n\tswitch (surface) {\n\t\tcase 'purchases/recent': {\n\t\t\tconst fn = provider[entry.fn] as (limit: number) => Promise<SurfaceMap[S]>\n\t\t\treturn fn(limit)\n\t\t}\n\t\tcase 'youtube': {\n\t\t\tconst fn = provider[entry.fn] as () => Promise<SurfaceMap[S] | null>\n\t\t\treturn fn()\n\t\t}\n\t\tcase 'youtube/videos':\n\t\tcase 'youtube/daily':\n\t\tcase 'youtube/sources': {\n\t\t\tconst fn = provider[entry.fn] as (\n\t\t\t\trange: '24h' | '7d' | '30d' | '90d',\n\t\t\t\tlimit?: number,\n\t\t\t) => Promise<SurfaceMap[S] | null>\n\t\t\tif (surface === 'youtube/videos') {\n\t\t\t\treturn fn(toYouTubeRange(range), limit)\n\t\t\t}\n\t\t\treturn fn(toYouTubeRange(range))\n\t\t}\n\t\tcase 'traffic':\n\t\tcase 'traffic/daily':\n\t\tcase 'traffic/pages':\n\t\tcase 'traffic/sources': {\n\t\t\tconst fn = provider[entry.fn] as (\n\t\t\t\trange: '24h' | '7d' | '30d' | '90d',\n\t\t\t\tlimit?: number,\n\t\t\t) => Promise<SurfaceMap[S]>\n\t\t\tif (surface === 'traffic/pages' || surface === 'traffic/sources') {\n\t\t\t\treturn fn(toGA4Range(range), limit)\n\t\t\t}\n\t\t\treturn fn(toGA4Range(range))\n\t\t}\n\t\tcase 'surveys':\n\t\tcase 'surveys/list':\n\t\tcase 'surveys/daily': {\n\t\t\tconst fn = provider[entry.fn] as (\n\t\t\t\trange: AnalyticsRange,\n\t\t\t) => Promise<SurfaceMap[S]>\n\t\t\treturn fn(range)\n\t\t}\n\t\tcase 'attribution/email-campaigns': {\n\t\t\tconst fn = provider[entry.fn] as (\n\t\t\t\trange: AnalyticsRange,\n\t\t\t\tlimit?: number,\n\t\t\t) => Promise<SurfaceMap[S]>\n\t\t\treturn fn(range, limit)\n\t\t}\n\t\tcase 'surveys/questions':\n\t\tcase 'surveys/responses': {\n\t\t\tconst fn = provider[entry.fn] as (\n\t\t\t\trange: AnalyticsRange,\n\t\t\t\tlimit: number,\n\t\t\t) => Promise<SurfaceMap[S]>\n\t\t\treturn fn(range, limit)\n\t\t}\n\t\tdefault: {\n\t\t\tconst fn = provider[entry.fn] as (\n\t\t\t\trange: AnalyticsRange,\n\t\t\t\tlimit?: number,\n\t\t\t) => Promise<SurfaceMap[S] | null>\n\t\t\tif (\n\t\t\t\tsurface === 'attribution/content' ||\n\t\t\t\tsurface === 'correlation/survey-revenue'\n\t\t\t) {\n\t\t\t\treturn fn(range, limit)\n\t\t\t}\n\t\t\treturn fn(range)\n\t\t}\n\t}\n}\n\n/**\n * Creates an analytics engine bound to the given provider map.\n *\n * @param providers - A map of provider name → provider implementation. Keys\n * must match the `provider` field values used in the catalog\n * ('database', 'ga4', 'youtube', 'derived', 'newsletter', 'survey').\n * @returns An object with `query`, `queryMany`, and `getCatalog` methods.\n */\nexport function createAnalyticsEngine(providers: ProviderMap) {\n\tasync function query<S extends SurfaceName>(\n\t\tsurface: S,\n\t\toptions?: QueryOptions,\n\t): Promise<QueryResult<S>> {\n\t\tconst range = options?.range ?? '30d'\n\t\tconst limit = options?.limit ?? 20\n\t\tconst entry = catalog[surface]\n\t\tconst startMs = Date.now()\n\n\t\ttry {\n\t\t\tconst data = await invokeSurface(providers, surface, range, limit)\n\n\t\t\tif (data === null) {\n\t\t\t\treturn {\n\t\t\t\t\tok: false,\n\t\t\t\t\tsurface,\n\t\t\t\t\terror: {\n\t\t\t\t\t\tmessage: `${entry.provider} is not available`,\n\t\t\t\t\t\tcode: `${entry.provider.toUpperCase()}_UNAVAILABLE`,\n\t\t\t\t\t},\n\t\t\t\t\tfix: entry.unavailableFix ?? 'Check provider configuration',\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tok: true,\n\t\t\t\tsurface,\n\t\t\t\trange,\n\t\t\t\tdata,\n\t\t\t\tmeta: {\n\t\t\t\t\tqueryTimeMs: Date.now() - startMs,\n\t\t\t\t\ttruncated: Array.isArray(data) && data.length >= limit,\n\t\t\t\t},\n\t\t\t}\n\t\t} catch (error) {\n\t\t\treturn {\n\t\t\t\tok: false,\n\t\t\t\tsurface,\n\t\t\t\terror: {\n\t\t\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\t\t\tcode: 'QUERY_FAILED',\n\t\t\t\t},\n\t\t\t\tfix: `The ${surface} query failed. Try a different range or check server logs.`,\n\t\t\t}\n\t\t}\n\t}\n\n\tasync function queryMany<S extends SurfaceName>(\n\t\tsurfaces: S[],\n\t\toptions?: QueryOptions,\n\t): Promise<{ [K in S]: QueryResult<K> }> {\n\t\tconst results = await Promise.all(\n\t\t\tsurfaces.map(\n\t\t\t\tasync (surface) => [surface, await query(surface, options)] as const,\n\t\t\t),\n\t\t)\n\n\t\treturn Object.fromEntries(results) as { [K in S]: QueryResult<K> }\n\t}\n\n\t/**\n\t * Returns catalog entries filtered to only surfaces that this engine\n\t * can actually serve (provider present and function exported).\n\t */\n\tfunction getCatalog(): SurfaceEntry[] {\n\t\treturn Object.values(catalog).filter((entry) => {\n\t\t\tconst provider = providers[entry.provider]\n\t\t\treturn provider && typeof provider[entry.fn] === 'function'\n\t\t})\n\t}\n\n\treturn { query, queryMany, getCatalog }\n}\n"],"mappings":";;;;AAiBO,IAAMA,UAA6C;EACzDC,SAAS;IACRC,MAAM;IACNC,aAAa;IACbC,UAAU;IAC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ACrC,YAAME,WAAWL,UAAUG,MAAME,QAAQ;AACzC,aAAOA,YAAY,OAAOA,SAASF,MAAMG,EAAE,MAAM;IAClD,CAAA;EACD;AALS+B;AAOT,SAAO;IAAE7B;IAAOqB;IAAWQ;EAAW;AACvC;AAzEgB9B;","names":["catalog","summary","name","description","category","provider","fn","attribution","traffic","youtube","unavailableFix","surveys","toGA4Range","range","toYouTubeRange","invokeSurface","providers","surface","limit","entry","catalog","provider","fn","createAnalyticsEngine","query","options","startMs","Date","now","data","ok","error","message","code","toUpperCase","fix","unavailableFix","meta","queryTimeMs","truncated","Array","isArray","length","Error","String","queryMany","surfaces","results","Promise","all","map","Object","fromEntries","getCatalog","values","filter"]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { AnalyticsRange, AttributionCount, AttributionCoverage, ContentCorrelation, ConversionFunnel, Count, EmailCampaignFunnel, EmailCampaignKitLink, EmailCampaignShortlink, EmailRevenueOverview, Minutes, Percentage, QueryOptions, QueryResult, RecentPurchase, RevenueByCountry, RevenueByProduct, RevenueBySource, RevenueDaily, RevenueSummary, Seconds, ShortlinkPerformance, SurfaceMap, SurfaceName, SurveyConversionByQuestion, SurveyListItem, SurveyQuestionBreakdown, SurveyResponseRow, SurveyResponsesDaily, SurveyRevenueCorrelation, SurveySummary, TopPage, TrafficDaily, TrafficOverview, TrafficRevenueCorrelation, TrafficSource, USD, YouTubeChannelOverview, YouTubeDaily, YouTubeRevenueCorrelation, YouTubeTrafficSource, YouTubeVideoPerformance } from './types.js';
|
|
2
|
+
export { ANALYTICS_CATALOG, SurfaceEntry, catalog } from './catalog.js';
|
|
3
|
+
export { AnalyticsProvider, ProviderMap, createAnalyticsEngine } from './engine.js';
|