You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Ads API Java 如何获取所有已存在的Keyword Plan列表?

实现方案

Google Ads API 没有在 KeywordPlanService 中提供直接查询列表的接口,所有资源的列表查询都需要通过 GoogleAdsService 执行 GAQL(Google Ads Query Language)语句实现,完整实现代码如下:

1. 核心实现代码

import com.google.ads.googleads.v8.services.GoogleAdsServiceClient;
import com.google.ads.googleads.v8.services.SearchGoogleAdsRequest;
import com.google.ads.googleads.v8.resources.GoogleAdsRow;

// ... 你的其他业务代码
final String customerId = "XXXXXXXXXX"; // 替换为你的账户ID,移除所有横杠,无需转Long类型
GoogleAdsClient googleAdsClient = new ...; // 你的初始化逻辑保持不变

// 存储结果的集合,按需调整类型
List<Map<String, Object>> allExistingKeywordPlans = new ArrayList<>();

// 使用try-with-resources自动释放服务客户端资源
try (GoogleAdsServiceClient adsServiceClient = googleAdsClient.getVersion8().createGoogleAdsServiceClient()) {
    // GAQL查询语句,可按需补充需要返回的字段、WHERE筛选条件
    String gaql = "SELECT keyword_plan.id, keyword_plan.resource_name, keyword_plan.name FROM keyword_plan";

    // 构建查询请求
    SearchGoogleAdsRequest request = SearchGoogleAdsRequest.newBuilder()
            .setCustomerId(customerId)
            .setQuery(gaql)
            .build();

    // 遍历查询结果
    for (GoogleAdsRow row : adsServiceClient.search(request).iterateAll()) {
        Long planId = row.getKeywordPlan().getId();
        String resourceName = row.getKeywordPlan().getResourceName();
        String planName = row.getKeywordPlan().getName();
        
        // 存入结果集合
        Map<String, Object> planInfo = new HashMap<>();
        planInfo.put("planId", planId);
        planInfo.put("resourceName", resourceName);
        planInfo.put("planName", planName);
        allExistingKeywordPlans.add(planInfo);
    }
}

2. 补充说明

  • 你使用的16.0.0版本客户端对应Google Ads API v8,上述代码的包路径、接口完全适配你的依赖版本
  • 如果需要筛选特定状态的关键词计划,可以在GAQL语句中补充WHERE条件,比如筛选启用的计划添加 WHERE keyword_plan.status = 'ENABLED' 即可
  • KeywordPlanService 仅提供关键词计划的增删改、生成预测数据等操作,不包含列表查询能力,所有资源的批量查询统一走GoogleAdsService搜索接口

内容的提问来源于stack exchange,提问作者MJey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 19:45:09