fix: Ads Meta conv/CPL = Clientes potenciales (sin sumar acciones solapadas)

Las creatividades inflaban ~3× al sumar lead + lead_grouped + registration.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
4Ventures
2026-09-28 18:32:51 +02:00
parent 990915a051
commit 6d8dfaa79c
4 changed files with 36 additions and 30 deletions

View File

@@ -2,6 +2,7 @@
import { serviceHeaders, supabaseUrl } from '@/lib/agency';
import { resolveMetaAccessToken } from '@/lib/ads-discovery';
import { leadFromMetaActions, nMeta } from '@/lib/meta-leads';
const META_GRAPH = 'https://graph.facebook.com/v21.0';
const GADS_API = 'https://googleads.googleapis.com/v22';
@@ -55,10 +56,7 @@ type AdDailyInsert = {
platform_leads: number;
};
function n(v: unknown): number {
const x = Number(v);
return Number.isFinite(x) ? x : 0;
}
const n = nMeta;
function isoWeekMonday(d: Date): string {
const x = new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate()));
@@ -67,19 +65,6 @@ function isoWeekMonday(d: Date): string {
return x.toISOString().slice(0, 10);
}
function leadFromMetaActions(
actions?: { action_type?: string; value?: string }[]
): number {
if (!actions?.length) return 0;
// Exact Ads Manager «Clientes potenciales» for Meta Leads CRM campaigns.
// Do not fall back to lead / lead_grouped / pixel — those inflate past the
// Ads Manager period total when mixed with registration actions.
const primary = actions.find(
(a) => a.action_type === 'offsite_complete_registration_add_meta_leads'
);
return primary ? n(primary.value) : 0;
}
async function googleAccessToken(): Promise<string> {
const refresh =
process.env.GOOGLE_ADS_REFRESH_TOKEN?.trim() ||