Count Google Lead Form leads in Airtable lead attribution
Leads submitted via Google Lead Form arrive with UserAgent 'Google-Ads-Notifications' and campaign name in attr_referer. Added this as a fourth attribution clause in get_leads_this_month_gads() to reduce discrepancy between Airtable and Google Ads conversion counts. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
a85bc90b80
commit
26eeff794c
@ -334,20 +334,27 @@ class AirtableClient:
|
||||
for i in range(0, len(cat_batch), 10):
|
||||
self.campaigns.batch_update(cat_batch[i:i+10])
|
||||
|
||||
def get_leads_this_month_gads(self, campaign_id: str) -> tuple[int, list[str]]:
|
||||
def get_leads_this_month_gads(self, campaign_id: str, campaign_name: str = "") -> tuple[int, list[str]]:
|
||||
"""
|
||||
Leads del mes actual para una campaña de Google Ads.
|
||||
Filtra buscando gad_campaignid=ID directamente en attr_referer,
|
||||
más attr_utm_medium paid_search y mes actual.
|
||||
Cubre tres vías de atribución:
|
||||
1. GACampaignID / GoogleCampaignID (leads web normales)
|
||||
2. gad_campaignid en attr_referer (UTM web)
|
||||
3. attr_referer = campaign_name con UserAgent Google-Ads-Notifications (Lead Form)
|
||||
"""
|
||||
now = datetime.now()
|
||||
mes_inicio = f"{now.year}-{now.month:02d}-01"
|
||||
leadform_clause = (
|
||||
f"AND({{attr_referer}}='{campaign_name}',{{UserAgent del visitante}}='Google-Ads-Notifications')"
|
||||
if campaign_name else "FALSE()"
|
||||
)
|
||||
formula = (
|
||||
f"AND("
|
||||
f"OR("
|
||||
f"{{GACampaignID}}='{campaign_id}',"
|
||||
f"FIND(',{campaign_id},',',' & {{GoogleCampaignID}} & ','),"
|
||||
f"FIND('gad_campaignid={campaign_id}',{{attr_referer}})"
|
||||
f"FIND('gad_campaignid={campaign_id}',{{attr_referer}}),"
|
||||
f"{leadform_clause}"
|
||||
f"),"
|
||||
f"{{creado}}>='{mes_inicio}'"
|
||||
f")"
|
||||
|
||||
2
run.py
2
run.py
@ -162,7 +162,7 @@ def run():
|
||||
for campaign in campaigns:
|
||||
cid = campaign["google_campaign_id"]
|
||||
|
||||
leads, lead_ids = at.get_leads_this_month_gads(cid)
|
||||
leads, lead_ids = at.get_leads_this_month_gads(cid, campaign["curso"])
|
||||
at.update_gacampaignmes_leads_lake(campaign["airtable_id"], lead_ids)
|
||||
|
||||
metrics = gads.get_campaign_metrics(cid)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user