fix: Ads CPC con céntimos + clics enlace Meta; gráficas barras/línea con ejes

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
4Ventures
2026-09-29 01:28:59 +02:00
parent 5e8fd66e35
commit c83efa492d
4 changed files with 254 additions and 47 deletions

View File

@@ -2,7 +2,7 @@
import { serviceHeaders, supabaseUrl } from '@/lib/agency';
import { resolveMetaAccessToken } from '@/lib/ads-discovery';
import { leadFromMetaActions, nMeta } from '@/lib/meta-leads';
import { leadFromMetaActions, linkClicksFromMetaActions, nMeta } from '@/lib/meta-leads';
import { resolveGoogleAdsAccessToken } from '@/lib/google-ads-auth';
const META_GRAPH = 'https://graph.facebook.com/v21.0';
@@ -155,7 +155,8 @@ async function fetchMetaInsightsWindow(
ad_id: campaignId || '',
ad_name: '',
impressions: n(row.impressions),
clicks: n(row.clicks),
// Store link clicks so CPC = spend/clicks matches Ads Manager.
clicks: linkClicksFromMetaActions(row.actions, n(row.clicks)),
spend: Math.round(n(row.spend) * 100) / 100,
platform_leads: leadFromMetaActions(row.actions),
});

View File

@@ -18,3 +18,16 @@ export function leadFromMetaActions(
);
return primary ? nMeta(primary.value) : 0;
}
/**
* Ads Manager CPC uses «clics en el enlace» (link_click), not all clicks.
* Fall back to all clicks when link_click is absent.
*/
export function linkClicksFromMetaActions(
actions: { action_type?: string; value?: string }[] | undefined,
allClicks: number
): number {
if (!actions?.length) return allClicks;
const link = actions.find((a) => a.action_type === 'link_click');
return link ? nMeta(link.value) : allClicks;
}

View File

@@ -315,6 +315,14 @@ export function formatEur(v: number): string {
})} €`;
}
/** CPC / CPL — always show cents like Ads Manager. */
export function formatEurCents(v: number): string {
return `${v.toLocaleString('es-ES', {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
})} €`;
}
export function formatPct(v: number | null): string {
if (v === null || Number.isNaN(v)) return '—';
const sign = v > 0 ? '+' : '';