资源名称

Google Ads API 中实体的唯一标识符称为资源 名称,并以可预测格式的字符串表示。如果您知道 资源名称的组成部分,您可以生成资源名称 使用许多 Service 对象上都存在的辅助方法。

服务路径方法

所有用于处理特定类型的读取或转换的服务, 对象都提供了辅助方法,以便您轻松构建 resource_names。例如,为 Campaign 对象创建资源名称:

from google.ads.googleads.client import GoogleAdsClient

client = GoogleAdsClient.load_from_storage()
customer_id = "7892134783"
campaign_id = "1234567890"
campaign_service = client.get_service("CampaignService")
resource_name = campaign_service.campaign_path(customer_id, campaign_id)

从客户端库 10.0.0 版开始,每项服务还有一个 附带的 parse_*_path 方法,该方法可将 resource_name 解构为 例如:

from google.ads.googleads.client import GoogleAdsClient

client = GoogleAdsClient.load_from_storage()
resource_name = "customers/7892134783/campaigns/1234567890"
campaign_service = client.get_service('CampaignService')
segments = campaign_service.parse_campaign_path(resource_name)
customer_id = segments["customer_id"]
campaign_id = segments["campaign_id"]

复合资源名称

从客户端库 9.0.0 版开始,服务上的路径帮助程序 构建资源名称的复合细分。不同细分受众群 在 方法中接受复合 ID 作为单个参数:

from google.ads.google_ads.client import GoogleAdsClient

customer_id = "0987654321"
ad_group_id = "1234567890"
criterion_id = "74932"

client = GoogleAdsClient.load_from_storage()
ad_group_criterion_service = client.get_service("AdGroupCriterionService")

# An AdGroupCriterion resource name that uses the above IDs looks like this:
# "customers/0987654321/adGroupCriteria/1234567890~74932"
resource_name = ad_group_criterion_service.ad_group_criterion_path(
    customer_id, ad_group_id, criterion_id
)

9.0.0 之前的版本

对于 9.0.0 版之前的版本,服务上的路径帮助程序不会构造 资源名称的复合段。这些细分受众群需要由 调用方。客户端库具有一个小型实用程序来帮助您执行此操作:

from google.ads.google_ads.client import GoogleAdsClient
from google.ads.google_ads.util import ResourceName

customer_id = "0987654321"
ad_group_id = "1234567890"
criterion_id = "74932"

client = GoogleAdsClient.load_from_storage()
ad_group_criterion_service = client.get_service("AdGroupCriterionService")

# An AdGroupCriterion resource name that uses the above IDs looks like this:
# "customers/0987654321/adGroupCriteria/1234567890~74932"
composite_id = ResourceName.format_composite(ad_group_id, criterion_id)
resource_name = ad_group_criterion_service.ad_group_criterion_path(
    customer_id, composite_id
)