typicallyl in sql, you can use * to mutiply columns, but I am getting an error when trying to do so in my query below within GoogleAds API - anyone face this challenge before.

Code:

import sys, json, io, gzip, sys, os from googleads import adwords import pandas as pd import numpy as np def google_ads_extract(client,customer_id, s3_path,fields,report_type,statuses,date_range, download_version, job_name):   ga_service = client.get_service("GoogleAdsService")   search_request=client.get_type("SearchGoogleAdsStreamRequest")   search_request.customer_id = customer_id     query = """         SELECT           segments.date,           ad_group.id,           ad_group.name,           campaign.id,           campaign.name,           metrics.impressions,           metrics.clicks,           metrics.clicks*metrics.average_cpc as cost,           metrics.conversions,           metrics.ctr,           metrics.average_cpc,           metrics.cost_per_conversion         FROM ad_group         where segments.date BETWEEN 20220427 AND 20220428                   limit 10         """                 print(query)   search_request.query = query      stream = ga_service.search_stream(search_request)      for batch in stream:     for row in batch.results:       print(row)         return 1 

Error:

Request made: , Host: googleads.googleapis.com, Method: /google.ads.googleads.v8.services.GoogleAdsService/SearchStream, RequestId: TevDX_z7WYF-AWUZBMVbnw, IsFault: True, FaultMessage: Error in query: unexpected input *. Traceback (most recent call last): 

Tag:google-ads-api, sql, python, api

5 comments.

  1. dorian

    Arithmetic operations are not supported in GAQL. You'll need to perform these calculations on the client side after you have downloaded the data.

    For your specific example, it might be possible to just use metrics.cost_micros as cost instead of metrics.clicks*metrics.average_cpc as cost.

    1. 0004

      is there a method to download the data? The old version did have one but I cannot find in the new?

    2. dorian

      I'm not sure what you mean—the example code in your question does download data, no?

    3. 0004

      as in download as a csv/json so I can push to s3

    4. dorian

      There's a helper function MessageToJson in google.protobuf.json_format that can convert the returned rows (which are protobuf messages) into JSON. Maybe it could be helpful for your use case.

Add a new comment.