Google Ads API 中實體的專屬 ID 稱為資源名稱,以可預測的格式表示為字串。如果您知道資源名稱的組成元件,就可以使用許多 Service 物件上提供的輔助方法產生資源名稱。
服務路徑方法
所有旨在處理 API 中特定類型物件讀取或變異的服務,都會提供輔助方法,方便建構 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
)