mirror of
https://github.com/christianvidalwolf-prog/CrazeAnalytix.git
synced 2026-08-03 17:15:22 +02:00
101 lines
3.9 KiB
TypeScript
101 lines
3.9 KiB
TypeScript
import { createClient } from '@supabase/supabase-js';
|
|
import { readFileSync } from 'fs';
|
|
import { processAdsExcel, mergeSalesAndAdsData } from './services/dataProcessor';
|
|
import { computeDiD } from './services/experimentAnalysis';
|
|
import { CombinedKPIs } from './types';
|
|
|
|
// Load .env.local if it exists
|
|
import { config } from 'dotenv';
|
|
config({ path: '.env.local' });
|
|
|
|
async function run() {
|
|
const supabase = createClient(
|
|
process.env.SUPABASE_URL || '',
|
|
process.env.SUPABASE_SERVICE_KEY || ''
|
|
);
|
|
|
|
// Get the INKEE DE experiment
|
|
const { data: experiments, error } = await supabase
|
|
.from('experiments')
|
|
.select('*')
|
|
.ilike('name', '%INKEE%')
|
|
.eq('marketplace', 'DE');
|
|
|
|
if (error) {
|
|
console.error('Supabase error:', error);
|
|
return;
|
|
}
|
|
|
|
if (!experiments || experiments.length === 0) {
|
|
console.log('No INKEE DE experiments found. Listing all experiments...');
|
|
const { data: all } = await supabase.from('experiments').select('name, marketplace, start_date, end_date, asins').limit(20);
|
|
console.log(JSON.stringify(all, null, 2));
|
|
return;
|
|
}
|
|
|
|
for (const exp of experiments) {
|
|
console.log(`\n=== Experiment: "${exp.name}" ===`);
|
|
console.log(` Marketplace: ${exp.marketplace}`);
|
|
console.log(` Start: ${exp.start_date} → End: ${exp.end_date}`);
|
|
console.log(` Baseline: ${exp.baseline_start_date} → ${exp.baseline_end_date}`);
|
|
console.log(` ASINs (${exp.asins?.length || 0}):`, exp.asins);
|
|
console.log(` Control ASINs (${exp.control_asins?.length || 0}):`, exp.control_asins);
|
|
}
|
|
|
|
// Now load the Ads data and check all ASINs
|
|
console.log('\n=== Loading Ads data ===');
|
|
const buf = readFileSync('/tmp/Ads-Weekly.xlsx').buffer;
|
|
const adsData = await processAdsExcel(buf);
|
|
const merged = mergeSalesAndAdsData([], adsData) as CombinedKPIs[];
|
|
|
|
const inkeeExp = experiments[0];
|
|
const allAsins = inkeeExp.asins?.map((a: string) => a.trim().toUpperCase()) || [];
|
|
|
|
console.log(`\n=== Checking ${allAsins.length} ASINs in Ads data (weeks 7-8 of 2026) ===`);
|
|
let totalCostW7W8 = 0;
|
|
let totalAdRevW7W8 = 0;
|
|
let missingFromAds: string[] = [];
|
|
|
|
for (const asin of allAsins) {
|
|
const adRecords = merged.filter(r =>
|
|
r.asin === asin &&
|
|
r.year === 2026 &&
|
|
[7, 8].includes(r.week) &&
|
|
(r.marketplace || r.customer || '').toLowerCase().includes('de')
|
|
);
|
|
|
|
if (adRecords.length === 0) {
|
|
missingFromAds.push(asin);
|
|
} else {
|
|
const cost = adRecords.reduce((s, r) => s + (r.cost || 0), 0);
|
|
const adRev = adRecords.reduce((s, r) => s + (r.salesAds || 0), 0);
|
|
const clicks = adRecords.reduce((s, r) => s + (r.clicks || 0), 0);
|
|
if (cost > 0 || adRev > 0 || clicks > 0) {
|
|
console.log(` ${asin}: W7+W8 cost=${cost.toFixed(2)} adRev=${adRev.toFixed(2)} clicks=${clicks}`);
|
|
totalCostW7W8 += cost;
|
|
totalAdRevW7W8 += adRev;
|
|
} else {
|
|
console.log(` ${asin}: W7+W8 cost=0 adRev=0 clicks=0 (zero activity)`);
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log(`\nMissing from Ads file entirely (${missingFromAds.length}):`, missingFromAds);
|
|
console.log(`\nTotal W7+W8 2026 cost: €${totalCostW7W8.toFixed(2)}`);
|
|
console.log(`Total W7+W8 2026 adRev: €${totalAdRevW7W8.toFixed(2)}`);
|
|
|
|
if (totalCostW7W8 > 0) {
|
|
console.log(`Implied ACOS: ${(totalCostW7W8 / (totalAdRevW7W8 || 1) * 100).toFixed(1)}%`);
|
|
}
|
|
|
|
// Run actual DiD
|
|
const did = computeDiD(inkeeExp, merged);
|
|
console.log('\n=== DiD Result ===');
|
|
console.log('ACOS:', did.metrics['acos']);
|
|
console.log('ROAS:', did.metrics['roas']);
|
|
console.log('Revenue:', did.metrics['revenue']);
|
|
console.log('Units:', did.metrics['units']);
|
|
}
|
|
|
|
run().catch(console.error);
|