google-ads-apiadsensegoogle-ads-script

Google ads Measurement conversion


I'm trying to send a google ads measurement conversion purchase event from my python backend.

I have the correct conversion lable and id.

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

This request returns status code 200, but on google ads measurements ui the count is still 0.

Payload looks like this

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

Any idea why that could be happening?


Solution

  • The conversion you send to Google Ads does not show up because it can't be associated to a given ad impression/click, so the system doesn't know which campaign to attribute it to. This usually (i.e. in the browser) happens via a cookie that is sent with the conversion request.

    If you want to send those hits from your backend, I'd recommend to use GCLID-based offline conversion import.