google-ads
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseQuery the Google Ads API via . Three credentials are needed:
curl + jq- — the user's OAuth bearer (
$GOOGLE_ADS_TOKENscope) →adwordsAuthorization: Bearer $GOOGLE_ADS_TOKEN - — the platform's developer token (injected server-side) → header
$GOOGLE_ADS_DEVELOPER_TOKENdeveloper-token: $GOOGLE_ADS_DEVELOPER_TOKEN - — the manager (MCC) id under which calls are made; use the target customer id, or
login-customer-idif set (digits only, no dashes).$GOOGLE_ADS_LOGIN_CUSTOMER_ID
API version: the base is. Google ships a newhttps://googleads.googleapis.com/<vNN>every ~4 months and retires old ones — setvNNto the current supported version (check developers.google.com/google-ads/api release notes); the example usesVER.v18
If is empty, the connector isn't fully provisioned —
say so rather than calling the API (it would 401/DEVELOPER_TOKEN_NOT_APPROVED).
$GOOGLE_ADS_DEVELOPER_TOKENbash
VER="v18"; BASE="https://googleads.googleapis.com/$VER"
AUTH="Authorization: Bearer $GOOGLE_ADS_TOKEN"; DEV="developer-token: $GOOGLE_ADS_DEVELOPER_TOKEN"通过调用Google Ads API。需要以下三种凭据:
curl + jq- — 用户的OAuth承载令牌(
$GOOGLE_ADS_TOKEN权限范围)→ 请求头adwordsAuthorization: Bearer $GOOGLE_ADS_TOKEN - — 平台的开发者令牌(由服务器端注入)→ 请求头
$GOOGLE_ADS_DEVELOPER_TOKENdeveloper-token: $GOOGLE_ADS_DEVELOPER_TOKEN - — 发起调用时所属的管理账号(MCC)ID;可使用目标客户ID,若已设置
login-customer-id则使用该值(仅含数字,无连字符)。$GOOGLE_ADS_LOGIN_CUSTOMER_ID
API版本说明:基础地址为。Google大约每4个月发布一个新的https://googleads.googleapis.com/<vNN>版本并停用旧版本,请将vNN设置为当前受支持的版本(可查看developers.google.com/google-ads/api的发布说明);示例中使用的是VER。v18
若为空,则连接器未完全配置完成——此时应告知用户这一情况,而非调用API(否则会返回401/DEVELOPER_TOKEN_NOT_APPROVED错误)。
$GOOGLE_ADS_DEVELOPER_TOKENbash
VER="v18"; BASE="https://googleads.googleapis.com/$VER"
AUTH="Authorization: Bearer $GOOGLE_ADS_TOKEN"; DEV="developer-token: $GOOGLE_ADS_DEVELOPER_TOKEN"Customers the OAuth user can access (ids are returned as customers/<id>)
OAuth用户可访问的客户(ID格式为customers/<id>)
curl -sS -H "$AUTH" -H "$DEV" "$BASE/customers:listAccessibleCustomers" | jq '.resourceNames'
undefinedcurl -sS -H "$AUTH" -H "$DEV" "$BASE/customers:listAccessibleCustomers" | jq '.resourceNames'
undefinedReport with GAQL (searchStream)
使用GAQL(searchStream)生成报告
bash
CID="1234567890" # target customer id, digits only
curl -sS -H "$AUTH" -H "$DEV" -H "login-customer-id: ${GOOGLE_ADS_LOGIN_CUSTOMER_ID:-$CID}" \
-H "Content-Type: application/json" -d '{
"query":"SELECT campaign.name, metrics.cost_micros, metrics.clicks, metrics.conversions FROM campaign WHERE segments.date DURING LAST_30_DAYS ORDER BY metrics.cost_micros DESC"
}' "$BASE/customers/$CID/googleAds:searchStream" \
| jq '.[].results[]? | {campaign: .campaign.name, cost_usd: (.metrics.costMicros|tonumber/1e6), clicks: .metrics.clicks, conv: .metrics.conversions}'GAQL resources: , , (keywords),
. Cost is (÷ 1,000,000 = account currency).
campaignad_groupad_group_criterioncustomermetrics.cost_microsbash
CID="1234567890" # 目标客户ID,仅含数字
curl -sS -H "$AUTH" -H "$DEV" -H "login-customer-id: ${GOOGLE_ADS_LOGIN_CUSTOMER_ID:-$CID}" \
-H "Content-Type: application/json" -d '{
"query":"SELECT campaign.name, metrics.cost_micros, metrics.clicks, metrics.conversions FROM campaign WHERE segments.date DURING LAST_30_DAYS ORDER BY metrics.cost_micros DESC"
}' "$BASE/customers/$CID/googleAds:searchStream" \
| jq '.[].results[]? | {campaign: .campaign.name, cost_usd: (.metrics.costMicros|tonumber/1e6), clicks: .metrics.clicks, conv: .metrics.conversions}'GAQL可查询的资源包括:(广告系列)、(广告组)、(关键词)、(客户)。成本字段为(除以1,000,000即可转换为账户货币单位)。
campaignad_groupad_group_criterioncustomermetrics.cost_microsGotchas
注意事项
- Three headers, not one. Missing or
developer-tokenis the #1 cause of 401/403 here.login-customer-id - Customer ids are digits only in URLs/headers (strip the dashes from
).
123-456-7890 - returns an array of chunks each with
searchStream— flatten with.results[]..[].results[]? - Cost is in micros of the account currency; divide by 1e6.
- 需包含三个请求头,而非一个。缺少或
developer-token是导致401/403错误的最常见原因。login-customer-id - 在URL和请求头中,客户ID必须仅含数字(需移除中的连字符)。
123-456-7890 - 返回的是包含多个数据块的数组,每个数据块包含
searchStream——可使用.results[]来扁平化数据。.[].results[]? - 成本单位为账户货币的微单位;需除以1e6进行转换。