在 Google Ads API V10 中将两个列值相乘
Mutiply two column values in GoogleAds API V10
通常在 sql 中,您可以使用 * 来乘以列,但是当我在下面的 GoogleAds API 查询中尝试这样做时出现错误 - 以前任何人都遇到过这个挑战。
代码:
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
错误:
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):
GAQL 不支持算术运算。下载数据后,您需要在客户端执行这些计算。
对于您的具体示例,可能只使用 metrics.cost_micros as cost
而不是 metrics.clicks*metrics.average_cpc as cost
。
通常在 sql 中,您可以使用 * 来乘以列,但是当我在下面的 GoogleAds API 查询中尝试这样做时出现错误 - 以前任何人都遇到过这个挑战。
代码:
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
错误:
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):
GAQL 不支持算术运算。下载数据后,您需要在客户端执行这些计算。
对于您的具体示例,可能只使用 metrics.cost_micros as cost
而不是 metrics.clicks*metrics.average_cpc as cost
。