cdp-edge 2.0.3 → 2.0.5

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.
Files changed (32) hide show
  1. package/contracts/agent-versions.json +364 -0
  2. package/dist/commands/install.js +1 -1
  3. package/dist/commands/setup.js +326 -111
  4. package/extracted-skill/tracking-events-generator/INTEGRACAO-COMPLETA.md +89 -0
  5. package/extracted-skill/tracking-events-generator/MELHORIAS-IMPLEMENTADAS.md +101 -0
  6. package/extracted-skill/tracking-events-generator/agents/devops-agent.md +11 -0
  7. package/extracted-skill/tracking-events-generator/agents/intelligence-agent.md +27 -0
  8. package/extracted-skill/tracking-events-generator/agents/master-orchestrator.md +1 -1
  9. package/extracted-skill/tracking-events-generator/agents/server-tracking.md +5 -5
  10. package/extracted-skill/tracking-events-generator/knowledge-base.md +172 -0
  11. package/package.json +7 -2
  12. package/server-edge-tracker/INSTALAR.md +27 -3
  13. package/server-edge-tracker/SEGMENTATION-DOCS.md +69 -0
  14. package/server-edge-tracker/index.js +791 -0
  15. package/server-edge-tracker/migrate-v7.sql +64 -0
  16. package/server-edge-tracker/modules/db.js +531 -0
  17. package/server-edge-tracker/modules/dispatch/ga4.js +65 -0
  18. package/server-edge-tracker/modules/dispatch/meta.js +119 -0
  19. package/server-edge-tracker/modules/dispatch/platforms.js +237 -0
  20. package/server-edge-tracker/modules/dispatch/tiktok.js +100 -0
  21. package/server-edge-tracker/modules/dispatch/whatsapp.js +233 -0
  22. package/server-edge-tracker/modules/intelligence.js +321 -0
  23. package/server-edge-tracker/modules/ml/bidding.js +245 -0
  24. package/server-edge-tracker/modules/ml/fraud.js +301 -0
  25. package/server-edge-tracker/modules/ml/logistic.js +195 -0
  26. package/server-edge-tracker/modules/ml/ltv.js +420 -0
  27. package/server-edge-tracker/modules/ml/matchquality.js +176 -0
  28. package/server-edge-tracker/modules/ml/segmentation.js +316 -0
  29. package/server-edge-tracker/modules/utils.js +89 -0
  30. package/server-edge-tracker/schema-indexes.sql +67 -0
  31. package/server-edge-tracker/worker.js +395 -4
  32. package/server-edge-tracker/wrangler.toml +15 -0
@@ -0,0 +1,64 @@
1
+ -- CDP Edge — Migration v7
2
+ -- Tabelas para: LTV Model (regressão logística) + Match Quality Log
3
+ -- Idempotente: todos os CREATE usam IF NOT EXISTS
4
+
5
+ -- ── LTV Model Weights ─────────────────────────────────────────────────────────
6
+ -- Armazena os pesos treinados pelo cron semanal de regressão logística.
7
+ -- O Worker carrega via KV (TTL 7 dias) — query D1 apenas se KV miss.
8
+ CREATE TABLE IF NOT EXISTS ltv_model_weights (
9
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
10
+ trained_at TEXT NOT NULL DEFAULT (datetime('now')),
11
+ version INTEGER NOT NULL DEFAULT 1,
12
+ is_active INTEGER NOT NULL DEFAULT 0,
13
+ sample_size INTEGER NOT NULL DEFAULT 0,
14
+ positive_rate REAL, -- % de samples com label=1 (compra)
15
+ accuracy REAL, -- acurácia no conjunto de treino
16
+ weights_json TEXT NOT NULL -- JSON: {"bias": n, "features": {"utm_fb": w, ...}}
17
+ );
18
+
19
+ CREATE INDEX IF NOT EXISTS idx_ltv_weights_active ON ltv_model_weights(is_active);
20
+ CREATE INDEX IF NOT EXISTS idx_ltv_weights_trained ON ltv_model_weights(trained_at DESC);
21
+
22
+ -- ── Match Quality Log ─────────────────────────────────────────────────────────
23
+ -- Registra a qualidade dos dados enviados ao Meta CAPI por evento.
24
+ -- Sem PII — apenas flags booleanos.
25
+ CREATE TABLE IF NOT EXISTS match_quality_log (
26
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
27
+ logged_at TEXT NOT NULL DEFAULT (datetime('now')),
28
+ event_name TEXT NOT NULL,
29
+ has_email INTEGER NOT NULL DEFAULT 0,
30
+ has_phone INTEGER NOT NULL DEFAULT 0,
31
+ has_fbp INTEGER NOT NULL DEFAULT 0,
32
+ has_fbc INTEGER NOT NULL DEFAULT 0,
33
+ has_external_id INTEGER NOT NULL DEFAULT 0,
34
+ was_email_recovered INTEGER NOT NULL DEFAULT 0, -- recuperado do Identity Graph
35
+ was_utm_restored INTEGER NOT NULL DEFAULT 0 -- via UTM Resurrection
36
+ );
37
+
38
+ CREATE INDEX IF NOT EXISTS idx_mq_logged ON match_quality_log(logged_at DESC);
39
+ CREATE INDEX IF NOT EXISTS idx_mq_event ON match_quality_log(event_name, logged_at DESC);
40
+
41
+ -- ── View: Match Quality Dashboard (últimas 24h) ───────────────────────────────
42
+ CREATE VIEW IF NOT EXISTS v_match_quality_24h AS
43
+ SELECT
44
+ COUNT(*) AS total_events,
45
+ ROUND(AVG(has_email) * 100, 1) AS email_rate_pct,
46
+ ROUND(AVG(has_phone) * 100, 1) AS phone_rate_pct,
47
+ ROUND(AVG(has_fbp) * 100, 1) AS fbp_rate_pct,
48
+ ROUND(AVG(has_fbc) * 100, 1) AS fbc_rate_pct,
49
+ ROUND(AVG(has_external_id) * 100, 1) AS ext_id_rate_pct,
50
+ ROUND(AVG(was_email_recovered) * 100, 1) AS email_recovered_pct,
51
+ ROUND(AVG(was_utm_restored) * 100, 1) AS utm_restored_pct,
52
+ -- Score composto: email(40%) + fbp(30%) + phone(20%) + fbc(10%)
53
+ ROUND((AVG(has_email)*0.4 + AVG(has_fbp)*0.3 + AVG(has_phone)*0.2 + AVG(has_fbc)*0.1) * 100, 1) AS composite_score_pct
54
+ FROM match_quality_log
55
+ WHERE logged_at >= datetime('now', '-24 hours');
56
+
57
+ -- ── ALTER: colunas de auditoria em ltv_ab_tests ───────────────────────────────
58
+ -- Adiciona auto_decided_at se ainda não existir (SQLite não tem IF NOT EXISTS em ALTER)
59
+ -- Protegido por trigger-style: INSERT OR IGNORE não se aplica a ALTER, então usamos
60
+ -- um approach de criação de tabela auxiliar que faz nada se a coluna já existir.
61
+ -- A abordagem mais segura no Cloudflare D1 é tentar e ignorar o erro no app.
62
+ -- Aqui deixamos comentado — o worker.js já lida com campos opcionais:
63
+ -- ALTER TABLE ltv_ab_tests ADD COLUMN auto_decided_at TEXT;
64
+ -- ALTER TABLE ltv_ab_tests ADD COLUMN auto_decided_reason TEXT;
@@ -0,0 +1,531 @@
1
+ /**
2
+ * CDP Edge — Camada D1 (Database)
3
+ * Todas as operações de escrita/leitura no banco D1.
4
+ * Bindings: env.DB, env.GEO_CACHE, env.AUDIT_LOGS
5
+ */
6
+
7
+ import { sha256, normalizePhone, normalizeCity } from './utils.js';
8
+
9
+ // ── saveLead — inserir evento de conversão ────────────────────────────────────
10
+ export async function saveLead(env, eventName, payload, request, platform = 'website') {
11
+ if (!env.DB) return;
12
+ try {
13
+ const {
14
+ email, phone, firstName, lastName,
15
+ city, state, country,
16
+ fbp, fbc, userId,
17
+ utmSource, utmMedium, utmCampaign, utmContent, utmTerm,
18
+ pageUrl, value, currency, eventId, botScore,
19
+ engagementScore, intentionLevel, utmRestored,
20
+ } = payload;
21
+
22
+ await env.DB.prepare(`
23
+ INSERT INTO leads (
24
+ event_name, event_id, email, phone, first_name, last_name,
25
+ city, state, country, fbp, fbc, user_id,
26
+ utm_source, utm_medium, utm_campaign, utm_content, utm_term,
27
+ page_url, value, currency, ip_address, platform, bot_score,
28
+ engagement_score, intention_level, utm_restored, created_at
29
+ ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,datetime('now'))
30
+ `).bind(
31
+ eventName,
32
+ eventId || null,
33
+ email || null,
34
+ normalizePhone(phone) || null,
35
+ firstName || null,
36
+ lastName || null,
37
+ city || null,
38
+ state || null,
39
+ (country || request.cf?.country || null),
40
+ fbp || null,
41
+ fbc || null,
42
+ userId || null,
43
+ utmSource || null,
44
+ utmMedium || null,
45
+ utmCampaign || null,
46
+ utmContent || null,
47
+ utmTerm || null,
48
+ pageUrl || null,
49
+ value !== undefined ? parseFloat(value) : null,
50
+ currency || 'BRL',
51
+ request.headers.get('CF-Connecting-IP') || null,
52
+ platform,
53
+ botScore || 0,
54
+ engagementScore !== undefined ? parseFloat(engagementScore) : null,
55
+ intentionLevel || null,
56
+ utmRestored ? 1 : 0,
57
+ ).run();
58
+ } catch (err) {
59
+ console.error('D1 saveLead error:', err.message);
60
+ }
61
+ }
62
+
63
+ // ── calculateCohortLabel ──────────────────────────────────────────────────────
64
+ export function calculateCohortLabel(score, eventName) {
65
+ if (eventName === 'Purchase') return 'buyer_lookalike';
66
+ if (score >= 80) return 'high_intent';
67
+ if (score >= 30) return 'nurture';
68
+ return 'lost';
69
+ }
70
+
71
+ // ── upsertProfile — acumula cookies/scores entre visitas ─────────────────────
72
+ export async function upsertProfile(env, eventName, payload, request) {
73
+ if (!env.DB || !payload.userId) return;
74
+ try {
75
+ const {
76
+ userId, email, phone,
77
+ fbp, fbc, ttp, gclid, ttclid, gaClientId,
78
+ city, state, country,
79
+ engagementScore, userScore,
80
+ } = payload;
81
+
82
+ const scoreMap = { PageView: 5, ViewContent: 10, ScrollDepth: 3, TimeOnPage: 5, Lead: 30, InitiateCheckout: 50, Purchase: 100 };
83
+ const eventScore = scoreMap[eventName] || 2;
84
+
85
+ const behaviorBonus = userScore
86
+ ? Math.round((Math.min(userScore, 100) / 100) * 20)
87
+ : (engagementScore ? Math.round((Math.min(engagementScore, 5) / 5) * 10) : 0);
88
+
89
+ const totalDelta = eventScore + behaviorBonus;
90
+
91
+ await env.DB.prepare(`
92
+ INSERT INTO user_profiles
93
+ (user_id, email, phone, fbp, fbc, ttp, gclid, ttclid, ga_client_id,
94
+ city, state, country, score, cohort_label, created_at, updated_at)
95
+ VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,datetime('now'),datetime('now'))
96
+ ON CONFLICT(user_id) DO UPDATE SET
97
+ email = COALESCE(excluded.email, user_profiles.email),
98
+ phone = COALESCE(excluded.phone, user_profiles.phone),
99
+ fbp = COALESCE(excluded.fbp, user_profiles.fbp),
100
+ fbc = COALESCE(excluded.fbc, user_profiles.fbc),
101
+ ttp = COALESCE(excluded.ttp, user_profiles.ttp),
102
+ gclid = COALESCE(excluded.gclid, user_profiles.gclid),
103
+ ttclid = COALESCE(excluded.ttclid, user_profiles.ttclid),
104
+ ga_client_id = COALESCE(excluded.ga_client_id, user_profiles.ga_client_id),
105
+ city = COALESCE(excluded.city, user_profiles.city),
106
+ state = COALESCE(excluded.state, user_profiles.state),
107
+ country = COALESCE(excluded.country, user_profiles.country),
108
+ score = user_profiles.score + excluded.score,
109
+ cohort_label = excluded.cohort_label,
110
+ updated_at = datetime('now')
111
+ `).bind(
112
+ userId,
113
+ email || null,
114
+ normalizePhone(phone) || null,
115
+ fbp || null,
116
+ fbc || null,
117
+ ttp || null,
118
+ gclid || null,
119
+ ttclid || null,
120
+ gaClientId || null,
121
+ city || null,
122
+ state || null,
123
+ (country || request.cf?.country || null),
124
+ totalDelta,
125
+ calculateCohortLabel(totalDelta, eventName),
126
+ ).run();
127
+ } catch (err) {
128
+ console.error('D1 upsertProfile error:', err.message);
129
+ }
130
+ }
131
+
132
+ // ── resolveDeviceGraph — Cross-Device Identity ────────────────────────────────
133
+ export async function resolveDeviceGraph(DB, currentUserId, email, phone) {
134
+ if (!DB || !currentUserId) return;
135
+ if (!email && !phone) return;
136
+
137
+ try {
138
+ const conditions = [];
139
+ const bindings = [];
140
+
141
+ if (email) {
142
+ conditions.push('email = ?');
143
+ bindings.push(email.toLowerCase().trim());
144
+ }
145
+ if (phone) {
146
+ const digits = String(phone).replace(/\D/g, '');
147
+ if (digits.length >= 10) {
148
+ conditions.push('phone LIKE ?');
149
+ bindings.push(`%${digits.slice(-10)}`);
150
+ }
151
+ }
152
+
153
+ if (conditions.length === 0) return;
154
+
155
+ bindings.push(currentUserId);
156
+ const rows = await DB.prepare(`
157
+ SELECT user_id, email, phone, created_at
158
+ FROM user_profiles
159
+ WHERE (${conditions.join(' OR ')})
160
+ AND user_id != ?
161
+ ORDER BY created_at ASC
162
+ LIMIT 5
163
+ `).bind(...bindings).all();
164
+
165
+ if (!rows.results || rows.results.length === 0) return;
166
+
167
+ for (const match of rows.results) {
168
+ const emailMatch = email && match.email &&
169
+ email.toLowerCase().trim() === match.email.toLowerCase().trim();
170
+ const phoneMatch = phone && match.phone && (() => {
171
+ const a = String(phone).replace(/\D/g, '');
172
+ const b = String(match.phone).replace(/\D/g, '');
173
+ return a.slice(-10) === b.slice(-10) && a.length >= 10;
174
+ })();
175
+
176
+ if (!emailMatch && !phoneMatch) continue;
177
+
178
+ const matchType = emailMatch && phoneMatch ? 'email+phone' : (emailMatch ? 'email' : 'phone');
179
+ const matchConfidence = emailMatch && phoneMatch ? 0.99 : (emailMatch ? 0.95 : 0.85);
180
+ const primary = match.user_id;
181
+ const secondary = currentUserId;
182
+
183
+ await DB.prepare(`
184
+ INSERT OR IGNORE INTO device_graph
185
+ (primary_user_id, secondary_user_id, match_type, match_confidence)
186
+ VALUES (?, ?, ?, ?)
187
+ `).bind(primary, secondary, matchType, matchConfidence).run();
188
+
189
+ console.log(`[DeviceGraph] Linked ${secondary} → ${primary} via ${matchType} (confidence: ${matchConfidence})`);
190
+ }
191
+ } catch (err) {
192
+ console.error('resolveDeviceGraph error:', err.message);
193
+ }
194
+ }
195
+
196
+ // ── fireAutomation — dispara regras de automação (WA/Email) ──────────────────
197
+ export async function fireAutomation(env, eventName, leadId, payload) {
198
+ if (!env.DB) return;
199
+
200
+ try {
201
+ const { results: rules } = await env.DB
202
+ .prepare(
203
+ `SELECT id, channel, subject_template, message_template
204
+ FROM automation_rules
205
+ WHERE trigger_event = ?1 AND is_active = 1`
206
+ )
207
+ .bind(eventName)
208
+ .all();
209
+
210
+ if (!rules || rules.length === 0) return;
211
+
212
+ const vars = {
213
+ name: String(payload.firstName || payload.name || ''),
214
+ email: String(payload.email || ''),
215
+ phone: String(payload.phone || ''),
216
+ campaign: String(payload.utm_campaign || payload.utmCampaign || ''),
217
+ intention: String(payload.intentionLevel || payload.intention_level || ''),
218
+ };
219
+
220
+ const interpolate = (tpl) =>
221
+ tpl.replace(/\{\{(\w+)\}\}/g, (_, k) => vars[k] ?? '');
222
+
223
+ for (const rule of rules) {
224
+ const message = interpolate(rule.message_template);
225
+ const subject = rule.subject_template ? interpolate(rule.subject_template) : null;
226
+
227
+ try {
228
+ if (rule.channel === 'whatsapp' && payload.phone && env.WHATSAPP_ACCESS_TOKEN && env.WHATSAPP_PHONE_NUMBER_ID) {
229
+ const digits = String(payload.phone).replace(/\D/g, '');
230
+ const e164 = digits.startsWith('55') ? `+${digits}` : `+55${digits}`;
231
+ const waRes = await fetch(
232
+ `https://graph.facebook.com/v22.0/${env.WHATSAPP_PHONE_NUMBER_ID}/messages`,
233
+ {
234
+ method: 'POST',
235
+ headers: { 'Authorization': `Bearer ${env.WHATSAPP_ACCESS_TOKEN}`, 'Content-Type': 'application/json' },
236
+ body: JSON.stringify({ messaging_product: 'whatsapp', recipient_type: 'individual', to: e164, type: 'text', text: { body: message } }),
237
+ }
238
+ );
239
+ const waData = await waRes.json();
240
+ const status = waRes.ok ? 'sent' : 'failed';
241
+ const meta = waRes.ok ? (waData.messages?.[0]?.id ?? null) : JSON.stringify(waData);
242
+ await env.DB.prepare(
243
+ `INSERT INTO messaging_history (lead_id, channel, recipient, subject, content, status, meta) VALUES (?1,?2,?3,?4,?5,?6,?7)`
244
+ ).bind(leadId, 'whatsapp', e164, null, message, status, meta).run();
245
+
246
+ } else if (rule.channel === 'email' && payload.email && env.RESEND_API_KEY) {
247
+ const resendRes = await fetch('https://api.resend.com/emails', {
248
+ method: 'POST',
249
+ headers: { 'Authorization': `Bearer ${env.RESEND_API_KEY}`, 'Content-Type': 'application/json' },
250
+ body: JSON.stringify({
251
+ from: env.RESEND_FROM_EMAIL || 'noreply@cdp-edge.app',
252
+ to: [payload.email],
253
+ subject: subject || `Olá, ${vars.name || 'você'}!`,
254
+ html: `<p>${message.replace(/\n/g, '<br>')}</p>`,
255
+ }),
256
+ });
257
+ const resendData = await resendRes.json();
258
+ const status = resendRes.ok ? 'sent' : 'failed';
259
+ const meta = resendRes.ok ? (resendData.id ?? null) : JSON.stringify(resendData);
260
+ await env.DB.prepare(
261
+ `INSERT INTO messaging_history (lead_id, channel, recipient, subject, content, status, meta) VALUES (?1,?2,?3,?4,?5,?6,?7)`
262
+ ).bind(leadId, 'email', payload.email, subject, message, status, meta).run();
263
+ }
264
+ } catch (err) {
265
+ console.error(`[Automation] rule ${rule.id} error:`, err.message);
266
+ }
267
+ }
268
+ } catch (err) {
269
+ console.error('[Automation] fireAutomation error:', err.message);
270
+ }
271
+ }
272
+
273
+ // ── getProfileByEmail ─────────────────────────────────────────────────────────
274
+ export async function getProfileByEmail(env, email) {
275
+ if (!env.DB || !email) return null;
276
+ try {
277
+ return await env.DB.prepare(
278
+ 'SELECT * FROM user_profiles WHERE email = ? ORDER BY updated_at DESC LIMIT 1'
279
+ ).bind(email.toLowerCase().trim()).first();
280
+ } catch {
281
+ return null;
282
+ }
283
+ }
284
+
285
+ // ── enrichGeoFromEdge — enriquece payload com dados de geolocalização ─────────
286
+ export async function enrichGeoFromEdge(request, env, payload) {
287
+ const cf = request.cf || {};
288
+ const ip = request.headers.get('CF-Connecting-IP') || '';
289
+
290
+ let geoData = null;
291
+ if (env.GEO_CACHE && ip) {
292
+ try {
293
+ const cached = await env.GEO_CACHE.get(`geo:${ip}`, 'json');
294
+ if (cached) geoData = cached;
295
+ } catch {}
296
+ }
297
+
298
+ if (!geoData) {
299
+ geoData = {
300
+ country: cf.country || null,
301
+ continent: cf.continent || null,
302
+ asn: cf.asn || null,
303
+ asOrg: cf.asOrganization || null,
304
+ colo: cf.colo || null,
305
+ city: cf.city || null,
306
+ region: cf.region || null,
307
+ regionCode: cf.regionCode || null,
308
+ postalCode: cf.postalCode || null,
309
+ latitude: cf.latitude || null,
310
+ longitude: cf.longitude || null,
311
+ timezone: cf.timezone || null,
312
+ metroCode: cf.metroCode || null,
313
+ };
314
+
315
+ if (env.GEO_CACHE && ip && geoData.country) {
316
+ try {
317
+ await env.GEO_CACHE.put(`geo:${ip}`, JSON.stringify(geoData), { expirationTtl: 3600 });
318
+ } catch {}
319
+ }
320
+ }
321
+
322
+ payload.country = payload.country || geoData.country;
323
+ payload.city = payload.city || geoData.city;
324
+ payload.state = payload.state || geoData.regionCode;
325
+ payload.zip = payload.zip || geoData.postalCode;
326
+ payload.geo = geoData;
327
+
328
+ return geoData;
329
+ }
330
+
331
+ // ── writeAuditLog — grava evento no R2 ───────────────────────────────────────
332
+ export async function writeAuditLog(env, eventName, payload, geoData) {
333
+ if (!env.AUDIT_LOGS) return;
334
+ try {
335
+ const now = new Date();
336
+ const y = now.getUTCFullYear();
337
+ const m = String(now.getUTCMonth() + 1).padStart(2, '0');
338
+ const d = String(now.getUTCDate()).padStart(2, '0');
339
+ const key = `logs/${y}/${m}/${d}/${now.getTime()}_${eventName}.json`;
340
+
341
+ const log = {
342
+ timestamp: now.toISOString(),
343
+ event: eventName,
344
+ userId: payload.userId || null,
345
+ eventId: payload.eventId || null,
346
+ value: payload.value || null,
347
+ currency: payload.currency || null,
348
+ ltvClass: payload.ltvClass || null,
349
+ utm: {
350
+ source: payload.utmSource || null,
351
+ medium: payload.utmMedium || null,
352
+ campaign: payload.utmCampaign || null,
353
+ content: payload.utmContent || null,
354
+ term: payload.utmTerm || null,
355
+ restored: payload.utmRestored || false,
356
+ },
357
+ geo: geoData || null,
358
+ };
359
+
360
+ await env.AUDIT_LOGS.put(key, JSON.stringify(log), {
361
+ httpMetadata: { contentType: 'application/json' },
362
+ });
363
+ } catch (err) {
364
+ console.error('[R2 Audit] Error:', err.message);
365
+ }
366
+ }
367
+
368
+ // ── generateEdgeFingerprint ───────────────────────────────────────────────────
369
+ export async function generateEdgeFingerprint(request) {
370
+ const asn = String(request.cf?.asn || '0');
371
+ const lang = (request.headers.get('Accept-Language') || 'unknown').split(',')[0].trim();
372
+ const ua = request.headers.get('User-Agent') || '';
373
+
374
+ const uaBase = ua
375
+ .toLowerCase()
376
+ .replace(/[\d.]+/g, '')
377
+ .replace(/[^a-z\s]/g, ' ')
378
+ .split(' ')
379
+ .filter(w => w.length > 3)
380
+ .slice(0, 4)
381
+ .join(' ')
382
+ .trim();
383
+
384
+ const raw = `${asn}|${lang}|${uaBase}`;
385
+ return sha256(raw);
386
+ }
387
+
388
+ // ── saveEdgeFingerprint ───────────────────────────────────────────────────────
389
+ export async function saveEdgeFingerprint(DB, fingerprint, userId, payload) {
390
+ if (!DB || !fingerprint) return;
391
+ const { utmSource, utmMedium, utmCampaign, utmContent, utmTerm } = payload;
392
+ if (!utmSource) return;
393
+
394
+ try {
395
+ await DB.prepare(`
396
+ INSERT INTO edge_fingerprints (fingerprint, user_id, utm_source, utm_medium, utm_campaign, utm_content, utm_term)
397
+ VALUES (?, ?, ?, ?, ?, ?, ?)
398
+ `).bind(
399
+ fingerprint,
400
+ userId || null,
401
+ utmSource || null,
402
+ utmMedium || null,
403
+ utmCampaign || null,
404
+ utmContent || null,
405
+ utmTerm || null,
406
+ ).run();
407
+ } catch (err) {
408
+ console.error('saveEdgeFingerprint error:', err.message);
409
+ }
410
+ }
411
+
412
+ // ── resurrectUTM ──────────────────────────────────────────────────────────────
413
+ export async function resurrectUTM(DB, fingerprint) {
414
+ if (!DB || !fingerprint) return null;
415
+ try {
416
+ return await DB.prepare(`
417
+ SELECT utm_source, utm_medium, utm_campaign, utm_content, utm_term
418
+ FROM edge_fingerprints
419
+ WHERE fingerprint = ?
420
+ AND utm_source IS NOT NULL
421
+ AND created_at > datetime('now', '-48 hours')
422
+ ORDER BY created_at DESC
423
+ LIMIT 1
424
+ `).bind(fingerprint).first();
425
+ } catch {
426
+ return null;
427
+ }
428
+ }
429
+
430
+ // ── upsertLtvProfile — persiste LTV no perfil ────────────────────────────────
431
+ export async function upsertLtvProfile(env, userId, ltv) {
432
+ if (!env.DB || !userId) return;
433
+ try {
434
+ await env.DB.prepare(`
435
+ UPDATE user_profiles
436
+ SET predicted_ltv_class = ?,
437
+ predicted_ltv_value = ?,
438
+ updated_at = datetime('now')
439
+ WHERE user_id = ?
440
+ `).bind(ltv.class, ltv.value, userId).run();
441
+ } catch (err) {
442
+ console.error('upsertLtvProfile error:', err.message);
443
+ }
444
+ }
445
+
446
+ // ── Feedback Loop — Log de falhas e métricas de saúde ────────────────────────
447
+
448
+ export async function logApiFailure(DB, platform, eventName, errorCode, errorMessage, eventId, rawPayload) {
449
+ try {
450
+ await DB.prepare(`
451
+ INSERT INTO api_failures (platform, event_name, error_code, error_message, event_id, raw_payload, retry_count, final_status)
452
+ VALUES (?, ?, ?, ?, ?, ?, 0, 'failed')
453
+ `).bind(platform, eventName, String(errorCode), errorMessage, eventId, rawPayload).run();
454
+ } catch (err) {
455
+ console.error('Failed to log API failure:', err.message);
456
+ }
457
+ }
458
+
459
+ export async function getHealthMetrics(DB, platform, hours = 24) {
460
+ try {
461
+ const failures = await DB.prepare(`
462
+ SELECT COUNT(*) as count, error_code
463
+ FROM api_failures
464
+ WHERE platform = ? AND created_at > datetime('now', '-${hours} hours')
465
+ GROUP BY error_code
466
+ `).bind(platform).all();
467
+
468
+ const totalSent = await DB.prepare(`
469
+ SELECT COUNT(*) as count
470
+ FROM leads
471
+ WHERE platform = ? AND created_at > datetime('now', '-${hours} hours')
472
+ `).bind(platform).first();
473
+
474
+ const totalFailed = failures.results?.reduce((sum, f) => sum + f.count, 0) || 0;
475
+ const successRate = totalSent?.count > 0
476
+ ? ((totalSent.count - totalFailed) / totalSent.count) * 100
477
+ : 100;
478
+
479
+ return {
480
+ platform,
481
+ hours,
482
+ events_sent: totalSent?.count || 0,
483
+ events_failed: totalFailed,
484
+ success_rate: successRate,
485
+ errors_detected: (failures.results || []).map(f => ({ code: f.error_code, count: f.count })),
486
+ issues: totalFailed > (totalSent?.count || 0) * 0.1 ? ['high_error_rate'] : [],
487
+ };
488
+ } catch (err) {
489
+ console.error('Failed to get health metrics:', err.message);
490
+ return { platform, hours, events_sent: 0, events_failed: 0, success_rate: 0, errors_detected: [], issues: ['metrics_unavailable'] };
491
+ }
492
+ }
493
+
494
+ export async function generateDailyReport(DB) {
495
+ const platforms = ['meta', 'ga4', 'tiktok', 'pinterest', 'reddit'];
496
+ const today = new Date().toISOString().split('T')[0];
497
+ const reports = [];
498
+
499
+ for (const platform of platforms) {
500
+ const metrics = await getHealthMetrics(DB, platform, 24);
501
+
502
+ try {
503
+ await DB.prepare(`
504
+ INSERT INTO health_reports (report_date, platform, events_sent, events_failed, success_rate, errors_detected, issues_detected)
505
+ VALUES (?, ?, ?, ?, ?, ?, ?)
506
+ `).bind(
507
+ today, platform,
508
+ metrics.events_sent, metrics.events_failed, metrics.success_rate,
509
+ JSON.stringify(metrics.errors_detected), JSON.stringify(metrics.issues)
510
+ ).run();
511
+ reports.push({ platform, status: 'ok' });
512
+ } catch (err) {
513
+ console.error(`Failed to generate report for ${platform}:`, err.message);
514
+ reports.push({ platform, status: 'failed' });
515
+ }
516
+ }
517
+
518
+ return reports;
519
+ }
520
+
521
+ export async function logIntelligence(DB, runType, platform, checkType, status, currentValue, expectedValue, message, alertSent = false) {
522
+ if (!DB) return;
523
+ try {
524
+ await DB.prepare(`
525
+ INSERT INTO intelligence_logs (run_type, platform, check_type, status, current_value, expected_value, message, alert_sent)
526
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)
527
+ `).bind(runType, platform, checkType, status, String(currentValue ?? ''), String(expectedValue ?? ''), message, alertSent ? 1 : 0).run();
528
+ } catch (err) {
529
+ console.error('logIntelligence error:', err.message);
530
+ }
531
+ }
@@ -0,0 +1,65 @@
1
+ /**
2
+ * CDP Edge — GA4 Measurement Protocol
3
+ * Envia eventos server-side para o GA4 via Measurement Protocol.
4
+ */
5
+
6
+ import { normalizePhone } from '../utils.js';
7
+ import { logApiFailure } from '../db.js';
8
+
9
+ export async function sendGA4Mp(env, ga4EventName, payload, ctx) {
10
+ if (!env.GA4_API_SECRET) return { skipped: 'GA4_API_SECRET not set' };
11
+
12
+ const {
13
+ clientId, sessionId,
14
+ value, currency, contentName,
15
+ email, phone, firstName,
16
+ orderId,
17
+ } = payload;
18
+
19
+ if (!clientId) return { skipped: 'no clientId' };
20
+
21
+ const eventParams = {
22
+ ...(value !== undefined && { value: parseFloat(value) }),
23
+ ...(currency && { currency: String(currency).toUpperCase() }),
24
+ ...(contentName && { content_name: contentName }),
25
+ ...(orderId && { transaction_id: orderId }),
26
+ ...(email && { user_data_email_address: email.toLowerCase().trim() }),
27
+ ...(phone && { user_data_phone_number: normalizePhone(phone) }),
28
+ ...(firstName && { user_data_first_name: firstName.toLowerCase().trim() }),
29
+ ...(sessionId && { session_id: sessionId }),
30
+ engagement_time_msec: 100,
31
+ };
32
+
33
+ const body = {
34
+ client_id: clientId,
35
+ events: [{ name: ga4EventName, params: eventParams }],
36
+ };
37
+
38
+ const url = `https://www.google-analytics.com/mp/collect`
39
+ + `?measurement_id=${env.GA4_MEASUREMENT_ID}`
40
+ + `&api_secret=${env.GA4_API_SECRET}`;
41
+
42
+ try {
43
+ const res = await fetch(url, {
44
+ method: 'POST',
45
+ headers: { 'Content-Type': 'application/json' },
46
+ body: JSON.stringify(body),
47
+ });
48
+
49
+ if (res.status !== 204) {
50
+ if (env.DB && ctx) {
51
+ ctx.waitUntil(logApiFailure(env.DB, 'ga4', ga4EventName, String(res.status), 'GA4 returned non-204 status', null, JSON.stringify(body)));
52
+ }
53
+ }
54
+
55
+ return res.status === 204 ? { ok: true } : { status: res.status };
56
+ } catch (err) {
57
+ console.error('GA4 MP fetch failed:', err.message);
58
+
59
+ if (env.DB && ctx) {
60
+ ctx.waitUntil(logApiFailure(env.DB, 'ga4', ga4EventName, 'FETCH_ERROR', err.message, null, JSON.stringify(body)));
61
+ }
62
+
63
+ return { error: err.message };
64
+ }
65
+ }