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 { resolveMetaAccessToken } from '@/lib/ads-discovery';
import { serviceHeaders, supabaseUrl } from '@/lib/agency';
import { leadFromMetaActions } from '@/lib/meta-leads';
const META_GRAPH = 'https://graph.facebook.com/v21.0';
@@ -434,18 +435,9 @@ async function fetchMetaAdLevelInsights(
for (const row of raw.data || []) {
const adId = str(row.ad_id);
if (!adId) continue;
let conversions = 0;
for (const a of row.actions || []) {
const t = (a.action_type || '').toLowerCase();
if (
t === 'offsite_conversion.fb_pixel_complete_registration' ||
t === 'lead' ||
t === 'onsite_conversion.lead_grouped' ||
t.includes('complete_registration')
) {
conversions += n(a.value);
}
}
// Same metric as ad_daily / Ads Manager «Clientes potenciales»
// (do not sum lead + lead_grouped + registration — they overlap ~3×).
const conversions = leadFromMetaActions(row.actions);
out.push({
adId,
adName: str(row.ad_name),

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() ||

20
src/lib/meta-leads.ts Normal file
View File

@@ -0,0 +1,20 @@
/** 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;
}