- Add get_daily_conversions_for_month() to GoogleAdsClient - Add backfill_leads_google_mayo.py: populates 'leads' (Google conv) for all May days - 147 day-entries updated across 49 GACampaignMes records Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
63 lines
2.2 KiB
Python
63 lines
2.2 KiB
Python
"""
|
|
Script one-off: rellena el campo 'leads' (conversiones Google Ads) en MetricasDiarias
|
|
para cada dia de mayo ya registrado en GACampaignMes.
|
|
|
|
Ejecutar una sola vez:
|
|
python backfill_leads_google_mayo.py
|
|
"""
|
|
import json
|
|
from airtable_client import AirtableClient
|
|
from google_ads_client import GoogleAdsClient
|
|
at = AirtableClient()
|
|
gads = GoogleAdsClient()
|
|
|
|
print("Obteniendo conversiones diarias de Google Ads para mayo 2026...")
|
|
daily_conv = gads.get_daily_conversions_for_month(2026, 5)
|
|
print(f" > {len(daily_conv)} dias con datos en Google Ads")
|
|
|
|
print("Cargando registros GACampaignMes de mayo...")
|
|
records = at.gacampaignmes.all(
|
|
formula="AND({Mes}='5',{Año}='2026')",
|
|
fields=["CampaignID", "MetricasDiarias", "Campaign Name (from CampaignID)"],
|
|
)
|
|
|
|
campaigns_records = at.campaigns.all(fields=["CampaignID"])
|
|
at_id_to_gid = {r["id"]: str(r["fields"].get("CampaignID", "")).strip() for r in campaigns_records}
|
|
|
|
gid_to_gcm: dict[str, tuple[str, dict]] = {}
|
|
for r in records:
|
|
at_cids = r["fields"].get("CampaignID", [])
|
|
if not at_cids:
|
|
continue
|
|
gid = at_id_to_gid.get(at_cids[0], "")
|
|
if not gid:
|
|
continue
|
|
try:
|
|
md = json.loads(r["fields"].get("MetricasDiarias") or "{}")
|
|
except (json.JSONDecodeError, TypeError):
|
|
md = {}
|
|
gid_to_gcm[gid] = (r["id"], md)
|
|
|
|
total_updated = 0
|
|
for date_str, conv_by_cid in sorted(daily_conv.items()):
|
|
day_str = date_str[8:10] # "YYYY-MM-DD" -> "DD"
|
|
for gid, (gcm_id, md) in gid_to_gcm.items():
|
|
if day_str in md and "leads" not in md[day_str]:
|
|
md[day_str]["leads"] = int(conv_by_cid.get(gid, 0))
|
|
total_updated += 1
|
|
|
|
print(f"Entradas actualizadas en memoria: {total_updated}")
|
|
|
|
batch = [
|
|
{"id": gcm_id, "fields": {"MetricasDiarias": json.dumps(md, ensure_ascii=False)}}
|
|
for _, (gcm_id, md) in gid_to_gcm.items()
|
|
if any("leads" in v for v in md.values())
|
|
]
|
|
|
|
print(f"Guardando {len(batch)} registros en Airtable...")
|
|
for i in range(0, len(batch), 10):
|
|
at.gacampaignmes.batch_update(batch[i:i+10])
|
|
print(f" > {min(i+10, len(batch))}/{len(batch)} guardados")
|
|
|
|
print(f"OK Backfill completado: {len(batch)} registros, {total_updated} entradas de dia actualizadas.")
|