資源名稱

在 Google Ads API 中,實體的專屬 ID 稱為資源 名稱,並顯示為具有可預測格式的字串。如果您知道 資源名稱的組成元件 。

服務路徑方法

所有專為處理特定類型 API 中的物件都能提供輔助方法,讓您輕鬆建構 資源名稱。例如,為 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
)