資源名稱

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)

每項服務也都有隨附的 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"]

複合式資源名稱

服務中的路徑輔助程式會建構資源名稱的複合區段。複合 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
)