如何通过Google Ads API获取12个月以上的历史指标?
Google Ads API 获取超过12个月关键词历史指标的方案
一、能否通过指定日期范围获取24个月及更久的数据?
不行。Google Ads API的GenerateKeywordHistoricalMetrics接口默认仅返回最近12个月的历史数据,该接口未提供自定义查询日期范围的参数,无法直接获取超过12个月的指标。
二、可行的变通方案
1. 定期数据备份
从当前开始,每月调用一次API获取最新的关键词历史数据,将结果存储到自有数据库中。随着时间积累,即可逐步构建出超过12个月的完整历史数据集。这种方式适合需要长期追踪关键词趋势的场景,数据准确性最高。
2. 借助Google Ads关键词规划师导出数据
若需要回溯更早的数据,可手动使用Google Ads界面中的关键词规划师工具,它支持导出最长24个月的历史搜索数据,导出后可将数据导入自有系统使用。缺点是无法通过API自动化操作,适合一次性获取历史数据的场景。
示例代码(中文注释版)
def main(client, customer_id): """主方法,创建示例所需的所有实体。 参数: client: 初始化后的GoogleAdsClient实例。 customer_id: 客户ID。 """ generate_historical_metrics(client, customer_id) def generate_historical_metrics(client, customer_id): """生成关键词历史指标并打印结果。 参数: client: 初始化后的GoogleAdsClient实例。 customer_id: 客户ID。 """ googleads_service = client.get_service("GoogleAdsService") keyword_plan_idea_service = client.get_service("KeywordPlanIdeaService") request = client.get_type("GenerateKeywordHistoricalMetricsRequest") request.customer_id = customer_id # 指定要查询的关键词 request.keywords = ["火星邮轮", "低价邮轮", "木星邮轮"] # 地理目标常量2840代表美国 request.geo_target_constants.append( googleads_service.geo_target_constant_path("2840") ) # 设置查询的网络为谷歌搜索 request.keyword_plan_network = ( client.enums.KeywordPlanNetworkEnum.GOOGLE_SEARCH ) # 语言标准1000代表英语,语言标准ID列表可参考官方文档 request.language = googleads_service.language_constant_path("1000") # 调用接口获取历史指标 response = keyword_plan_idea_service.generate_keyword_historical_metrics( request=request ) # 遍历并打印结果 for result in response.results: metrics = result.keyword_metrics # 这些指标包含搜索词及其所有变体的数据 print( f"搜索词'{result.text}'(相关变体: '{result.close_variants if result.close_variants else '无'}')," "对应的历史指标如下:\n" ) # 过去12个月的平均月搜索量 print(f"\t平均月搜索量: {metrics.avg_monthly_searches}") # 搜索词的竞争水平 print(f"\t竞争水平: {metrics.competition}") # 竞争指数(范围0-100),指数越高代表广告位竞争越激烈,数据不足时返回undef print(f"\t竞争指数: {metrics.competition_index}") # 首页出价最低范围(20百分位),单位为微单位 print( f"\t首页出价最低范围: {metrics.low_top_of_page_bid_micros}" ) # 首页出价最高范围(80百分位),单位为微单位 print( "\t首页出价最高范围: " f"{metrics.high_top_of_page_bid_micros}" ) # 过去12个月的月度搜索量明细 for month in metrics.monthly_search_volumes: print( f"\t{month.year}年{month.month.name}月的搜索量约为{month.monthly_searches}" )
内容的提问来源于stack exchange,提问作者Abramo
相关产品推荐
相关产品推荐

