Learn more about migrating to the Google Ads API by watching our latest webinar

Resource Names

The unique identifier for an entity in the Google Ads API is called a resource name, and is represented as a string with a predictable format. If you know the constituent components of a resource name, you can generate resource names using helper methods present on many Service objects.

Service path methods

All Services that are designed to handle reading or mutating specific types of objects in the API have helper methods to make it easy to construct resource_names. For example creating a resource name for a Campaign object:

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)

Starting in version 10.0.0 of the client library, each service also has an accompanying parse_*_path method that deconstructs a resource_name into its individual segments, for example:

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"]

Composite resource names

Starting with version 9.0.0 of the client library, path helpers on services construct composite segments of a resource name. The different segments of the composite ID are accepted as individual parameters in the method:

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
)

Versions prior to 9.0.0

Prior to version 9.0.0, the path helpers on services don't construct the composite segment of a resource name. Those segments need to be provided by the caller. The client library has a small utility to help with this:

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
)