要按位置将国家/地区报告分割的谷歌广告API,您可以使用Python编写代码来实现。以下是一个示例代码,其使用了Google Ads API的客户端库,用于获取按位置分割的国家/地区报告。
from google.ads.google_ads.client import GoogleAdsClient
from google.ads.google_ads.errors import GoogleAdsException
def main():
# 使用您的Google Ads API凭据初始化客户端
google_ads_client = GoogleAdsClient.load_from_storage()
# 设置查询语句以获取按位置分割的国家/地区报告
query = (
f'SELECT '
f' location.geo_target_country_criterion_id, '
f' metrics.impressions, '
f' metrics.clicks, '
f' metrics.average_cpc '
f'FROM '
f' geo_target_report '
f'WHERE '
f' segments.date DURING LAST_7_DAYS '
f' AND segments.device = DESKTOP '
f'ORDER BY '
f' metrics.impressions DESC'
)
try:
# 创建Google Ads服务客户端
ga_service = google_ads_client.service.google_ads
# 发送查询请求
response = ga_service.search(
customer_id='your-customer-id',
query=query
)
# 处理响应并打印结果
for row in response:
location_id = row.location.geo_target_country_criterion_id.value
impressions = row.metrics.impressions.value
clicks = row.metrics.clicks.value
average_cpc = row.metrics.average_cpc.value
print(
f'Location ID: {location_id}, '
f'Impressions: {impressions}, '
f'Clicks: {clicks}, '
f'Average CPC: {average_cpc}'
)
except GoogleAdsException as ex:
# 处理Google Ads API异常
print(f'Request ID: {ex.request_id}\n'
f'Google Ads API错误发生: {ex.message}\n'
f'错误代码: {ex.error.code}\n'
f'位置: {ex.error.location}\n'
f'错误详细信息: {ex.error.details}')
if __name__ == '__main__':
main()
您需要将customer_id
替换为您的Google Ads客户ID,并根据您的需求调整查询语句。此代码将返回最近7天按位置分割的国家/地区报告,并按印象次数从高到低排序。
请确保已安装google-ads
库,您可以使用以下命令安装:
pip install google-ads
然后,您可以运行此Python脚本以获取按位置分割的国家/地区报告。