/** Meta Ads «Clientes potenciales» ↔ platform_leads (same metric as Ads Manager). */ export function nMeta(v: unknown): number { const x = Number(v); return Number.isFinite(x) ? x : 0; } /** * Exact Ads Manager «Clientes potenciales» for Meta Leads CRM campaigns. * Do not sum lead / lead_grouped / pixel — those overlap and inflate totals. */ export function leadFromMetaActions( actions?: { action_type?: string; value?: string }[] ): number { if (!actions?.length) return 0; const primary = actions.find( (a) => a.action_type === 'offsite_complete_registration_add_meta_leads' ); 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; }