fix: Meta Ads muestra clics todos y clics enlace (y ambos CPC)
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
import { resolveMetaAccessToken } from '@/lib/ads-discovery';
|
||||
import { serviceHeaders, supabaseUrl } from '@/lib/agency';
|
||||
import { leadFromMetaActions } from '@/lib/meta-leads';
|
||||
import { leadFromMetaActions, linkClicksFromMetaActions } from '@/lib/meta-leads';
|
||||
|
||||
const META_GRAPH = 'https://graph.facebook.com/v21.0';
|
||||
|
||||
@@ -761,3 +761,50 @@ export function buildGoogleCardsFromDaily(
|
||||
status: undefined,
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Account-level Meta clicks for the period:
|
||||
* - clicks = todos (Ads Manager)
|
||||
* - linkClicks = clics en el enlace
|
||||
*/
|
||||
export async function fetchMetaAccountClickSplit(opts: {
|
||||
tenantId: string;
|
||||
since: string;
|
||||
until: string;
|
||||
}): Promise<{ clicks: number; linkClicks: number; spend: number } | null> {
|
||||
const token = await resolveMetaAccessToken();
|
||||
if (!token) return null;
|
||||
const account = await resolveMetaAccountId(opts.tenantId);
|
||||
if (!account) return null;
|
||||
const act = account.startsWith('act_') ? account : `act_${account}`;
|
||||
const timeRange = JSON.stringify({ since: opts.since, until: opts.until });
|
||||
try {
|
||||
const raw = await graphGet<{
|
||||
data?: {
|
||||
spend?: string;
|
||||
clicks?: string;
|
||||
actions?: { action_type?: string; value?: string }[];
|
||||
}[];
|
||||
}>(
|
||||
`${act}/insights`,
|
||||
{
|
||||
fields: 'spend,clicks,actions',
|
||||
level: 'account',
|
||||
time_increment: 'all_days',
|
||||
time_range: timeRange,
|
||||
limit: '1',
|
||||
},
|
||||
token
|
||||
);
|
||||
const row = raw.data?.[0];
|
||||
if (!row) return null;
|
||||
const clicks = n(row.clicks);
|
||||
return {
|
||||
clicks,
|
||||
linkClicks: linkClicksFromMetaActions(row.actions, clicks),
|
||||
spend: Math.round(n(row.spend) * 100) / 100,
|
||||
};
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user