Python后端发送Google Ads转化事件成功但统计无数据求助
你尝试从Python后端发送Google Ads Measurement购买转化事件,已确认conversion label和ID正确,请求返回200状态码,但Google Ads Measurement界面转化计数仍为0。相关代码如下:
发送事件代码
class GoogleAds(Analytic): def __init__(self, data, shop_unique_id, event): self.base_url = "https://www.googleadservices.com/pagead/conversion/{conversion_id}/" super().__init__(data, shop_unique_id, event) def send_google_ads_event(self, payload): try: label = "label" conversion_id = "id" # for market in self.profile.markets: # if market_domain == market.domain: # ga4_measurement_id = market.ga4_measurement_id # ga4_api_secret = market.ga4_api_secret if ( label != "" and label is not None and conversion_id != "" and conversion_id is not None ): payload['conversion_id'] = conversion_id payload['label'] = label response = requests.get(self.base_url.format(conversion_id=conversion_id), params=payload) # Check if the conversion was registered successfully if response.status_code == 200 and response.headers.get('Content-Type') == 'image/gif': log.info( "Finished sending data to Google ads", extra={ "url": response.request.url, "sending_params": payload, "response_content_type": response.headers.get('Content-Type'), "status_code": response.status_code, }, ) return True else: log.error("Failed to send data to Google ads", extra={"status_code": response.status_code}) return False except requests.RequestException as e: log.error(f"Error sending data to Google ads: {e}") return False
Payload构造代码
def purchase_data(webhook_data, shop_unique_id): transformed_items = [{ "id": item["product_id"], "price": item["price"], "quantity": item["quantity"] } for item in webhook_data["line_items"]] data = dict( label="label", value=float(webhook_data["total_price"]), currency_code=webhook_data["currency"], transaction_id=webhook_data["id"], conversion_id="conversion_id", items=transformed_items, feed_country="GB", merchant_id="merchant_id", feed_language="GB" ) return data
以下是可能的原因及对应解决方法:
1. 硬编码覆盖了真实的Label和Conversion ID
在send_google_ads_event函数中,你将label和conversion_id硬编码为字符串"label"和"id",之后又用这些占位符覆盖了payload中已有的值。即使你在purchase_data里设置了正确的标识,也会被这里的硬编码值替换,导致Google Ads收到无效的转化标识,自然不会计数。
解决方法:
- 取消硬编码,从配置(比如注释掉的
profile.markets逻辑)中读取真实的label和conversion_id,确保传递给Google的是正确的标识。 - 移除
payload['conversion_id'] = conversion_id和payload['label'] = label这两行,直接使用purchase_data构造的正确参数。
2. 请求方式与参数传递格式错误
你使用requests.get发送请求,并将items等复杂结构通过URL参数传递。Google Ads对于包含商品详情的转化事件,更推荐使用POST请求发送JSON格式的payload,GET请求的URL参数无法正确解析数组类型的items,导致商品信息无法被识别,进而影响转化计数。
解决方法:
- 将请求方式改为POST,把payload以JSON格式发送:
response = requests.post( self.base_url.format(conversion_id=conversion_id), json=payload )
3. 货币代码格式不符合要求
Google Ads要求currency_code必须是ISO 4217标准的三位字母代码(例如GBP代表英镑),但你的代码中直接使用webhook_data["currency"],如果返回的是GB而非GBP,会导致货币验证失败,转化不被计数。
解决方法:
- 确保
currency_code转换为三位标准代码,比如添加映射逻辑:currency_map = {"GB": "GBP", "US": "USD"} currency_code = currency_map.get(webhook_data["currency"], webhook_data["currency"])
4. Transaction ID重复或格式问题
Google Ads会对重复的transaction_id进行去重,如果测试时多次使用同一个订单ID发送转化,只会被计数一次。另外,如果transaction_id格式不符合要求(比如包含特殊字符),也可能导致无法被识别。
解决方法:
- 确保每个转化请求的
transaction_id唯一,测试时使用不同的订单ID。 - 清理
transaction_id中的特殊字符,保证格式合规。
5. 缺少归因相关参数
如果你的转化需要归因到广告点击,必须传递gclid(Google点击ID)或wbraid/gbraid参数。如果没有这些参数,Google无法将转化关联到广告活动,可能不会在界面上显示计数(除非你设置了无需归因的转化类型)。
解决方法:
- 从用户会话中获取并传递
gclid参数到payload中;如果是离线转化,确保在Google Ads后台将转化动作设置为“离线导入”类型。
6. Google Ads后台配置或数据延迟
- 确认转化动作的设置:检查Google Ads后台的转化动作是否允许服务器端发送,是否开启了正确的归因模型,是否存在IP过滤等规则阻止了测试转化。
- 数据延迟:Google Ads的转化数据通常有1-24小时的延迟,尤其是新创建的转化动作,建议等待一段时间后再查看计数。
内容的提问来源于stack exchange,提问作者Ritesh Chauhan

