Google Ads API报表如何通过geo_target_constant获取地域名称
Google Ads API 地域ID转名称实现方法
不需要拆分两次请求单独查询维度表,Google Ads API 原生支持在业务查询中直接关联返回公共维度字段,按以下方式调整即可:
方案1:单查询直接返回地域名称(推荐)
直接把需要的geo_target_constant字段加入原有GAQL查询的SELECT子句,API会自动根据segments.geo_target_state的资源ID关联匹配对应的名称信息,无需额外做ID解析和二次请求。
修改后的完整查询语句:
SELECT campaign.name, segments.geo_target_state, geo_target_constant.name, geo_target_constant.canonical_name, metrics.cost_micros FROM geographic_view WHERE geographic_view.location_type = LOCATION_OF_PRESENCE AND segments.date BETWEEN 20220101 AND 20220430
对应调整后的curl请求示例:
curl "https://googleads.googleapis.com/v10/customers/${CUSTOMER_ID}/googleAds:searchStream" \ --header "Content-Type: application/json" \ --header "developer-token: ${DEVELOPER_TOKEN}" \ --header "login-customer-id: ${MANAGER_CUSTOMER_ID}" \ --header "Authorization: Bearer ${OAUTH2_ACCESS_TOKEN}" \ --data '{ "query": " SELECT campaign.name, segments.geo_target_state, geo_target_constant.name, geo_target_constant.canonical_name, metrics.cost_micros FROM geographic_view WHERE geographic_view.location_type = LOCATION_OF_PRESENCE AND segments.date BETWEEN 20220101 AND 20220430 " }'
调用后返回的结果会直接携带地域名称字段,你提到的ID为21136的新泽西州返回片段如下:
"results": [ { "campaign": { "resourceName": "customers/1234/campaigns/1234", "name": "Display - macines - Leads Display Campaign Test - AA" }, "metrics": { "costMicros": "66664821" }, "segments": { "geoTargetState": "geoTargetConstants/21136" }, "geoTargetConstant": { "resourceName": "geoTargetConstants/21136", "name": "New Jersey", "canonicalName": "New Jersey,United States" }, "geographicView": { "resourceName": "customers/6383148790/geographicViews/2840~LOCATION_OF_PRESENCE" } } ]
方案2:批量查询已获取的ID映射
如果你已经拿到了一批geoTargetConstants/xxx格式的资源名,需要单独做ID映射,按以下步骤处理:
- 从资源名字符串中提取数字ID:截取最后一个
/后的数字部分即可,比如geoTargetConstants/21136提取得到ID21136 - 用
IN条件批量查询geo_target_constant表,不要单条循环查询,避免触发API限流,查询语句示例:
SELECT geo_target_constant.id, geo_target_constant.name, geo_target_constant.canonical_name FROM geo_target_constant WHERE geo_target_constant.id IN (21136, 21137, 21138) -- 替换为你提取到的所有地域ID
注意:
geo_target_constant是全局公共维度表,不绑定特定客户账户,只要持有有效的Ads API调用凭证即可查询。其中name返回的是对应API请求头中设置语言的本地化名称,canonical_name是带国家/上级地域的标准全称,适合做跨区域去重和地域层级识别。
内容的提问来源于stack exchange,提问作者Code Guy
相关产品推荐
相关产品推荐

