如何从 google ads api 报告中检索数据(geo_performance_report)
Posted
技术标签:
【中文标题】如何从 google ads api 报告中检索数据(geo_performance_report)【英文标题】:how to retrieve data from google ads api reports(geo_performance_report) 【发布时间】:2021-10-19 14:55:06 【问题描述】:我正在尝试获取诸如 geo_performance_report、keywords_perfromance_report 等报告,但我无法弄清楚如何使用新版本的 google-ads api 来实现这一点。 我尝试过这种方式尝试使用新的谷歌广告,但没有成功。 有没有其他方法可以使用 Python 自动化这个过程。
def googleads_report(client, client_id, report_type, columns, start_date, end_date):
client.SetClientCustomerId(client_id)
report_downloader = googleads_client.GetReportDownloader(version="v201809")
report =
'reportName': 'report-google-campaign-performance',
'dateRangeType': 'CUSTOM_DATE',
'reportType': report_type,
'downloadFormat': 'CSV',
'selector':
'fields': columns,
'dateRange': 'min': start_date, 'max': end_date
file = io.StringIO(report_downloader.DownloadReportAsString(
report,
skip_report_header=True,
skip_column_header=True,
skip_report_summary=True,
include_zero_impressions=False)
)
df = pd.read_csv(file, names=columns)
return df
def main(client, customer_id):
keyword_columns = [
'Date',
'AccountDescriptiveName',
'AdGroupId',
'AdGroupName',
'AdGroupStatus',
'CampaignId',
'CampaignName',
'CampaignStatus',
'CpcBid',
'Criteria',
'CriteriaDestinationUrl',
'ExternalCustomerId',
'FirstPageCpc',
'FirstPositionCpc',
'Id',
'KeywordMatchType',
'Labels',
'QualityScore',
'SearchImpressionShare',
'Status',
'TopOfPageCpc',
'Clicks',
'Conversions',
'Cost',
'ConversionValue',
'Impressions',
'ViewThroughConversions'
]
report_types = [
'KEYWORDS_PERFORMANCE_REPORT'
]
for report in report_types:
base_df = pd.DataFrame()
if report == 'CAMPAIGN_PERFORMANCE_REPORT':
table_suffix = 'campaigns'
#columns = campaign_columns
elif report == 'KEYWORDS_PERFORMANCE_REPORT':
table_suffix = 'keywords'
columns = keyword_columns
elif report == 'AD_PERFORMANCE_REPORT':
table_suffix = 'ads'
#columns = ad_columns
start_date = '2019-01-01'
df = googleads_report(client,customer_id, report, columns, start_date, yesterday)
df = df.applymap(str)
# Powershell output
print(df.head())
# csv output
df.to_csv('my_path' + table_suffix + '.csv')
if __name__ == "__main__":
# GoogleAdsClient will read the google-ads.yaml configuration file in the
# home directory if none is specified.
googleads_client = GoogleAdsClient.load_from_storage(path="mypath")
today = datetime.now().date()
yesterday = today - timedelta(days=1)
thirty_days_ago = today - timedelta(days=30)
try:
main( googleads_client, "#######")
except GoogleAdsException as ex:
print(
f'Request with ID "ex.request_id" failed with status '
f'"ex.error.code().name" and includes the following errors:'
)
for error in ex.failure.errors:
print(f'\tError with message "error.message".')
if error.location:
for field_path_element in error.location.field_path_elements:
print(f"\t\tOn field: field_path_element.field_name")
sys.exit(1)
【问题讨论】:
【参考方案1】:根据您的version='v201809'
,您没有使用最新版本的 google ads api。该版本的 API 计划在 spring 2022 中弃用。
最新版本的 google ads api 现在为他们的reporting examples 使用查询语言。
Google 广告提供了一个mapping,用于在查询语言所需的字段中进行常见报告。
一旦您的客户authenticated 使用更新版本的 API,您就可以将 Google Ads 查询发布给客户。
from google.ads.googleads.client import GoogleAdsClient
client = GoogleAdsClient.load_from_storage("creds")
service = client.get_service("GoogleAdsService", version="v9")
#query below pulls all accounts that your MCC has access to
query = """
SELECT
customer_client.client_customer,
customer_client.level,
customer_client.manager,
customer_client.id
FROM customer_client
WHERE customer_client.manager != True
"""
search_request = client.get_type("SearchGoogleAdsStreamRequest")
search_request.customer_id = "1234adswordscustomer_id"
search_request.query = query
response = service.search_stream(search_request)
【讨论】:
以上是关于如何从 google ads api 报告中检索数据(geo_performance_report)的主要内容,如果未能解决你的问题,请参考以下文章
google adwords API:如何检索所有字段(AWQL)
使用服务帐户时如何修复 google ads api 未经授权的错误