Home > Blockchain >  Mutiply two column values in GoogleAds API V10
Mutiply two column values in GoogleAds API V10

Time:05-03

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):

CodePudding user response:

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.

  • Related