Most Google Ads entities expose a
getId() method that returns
their identifier. While not strictly necessary in most cases, IDs may come in
- Working with Reports
- IDs provide for a good way to link a report row to the actual Google Ads entity.
- Maintaining a mapping with an external data store
- You may already have ID-based information stored in your own database.
- Looking for a bit of a performance boost
- Fetching by IDs is often quicker than alternatives. The code for fetching a single
entity is a bit easier too:
var campaigns = AdsApp.campaigns() .withIds() .get(); // vs. var campaigns = AdsApp.campaigns() .withCondition("Name='My Campaign'") .get();
Campaign IDs and ad group IDs are unique: no two campaigns or ad groups will ever
share the same ID. Ads and keywords, however, have composite IDs: a unique
identifier of a keyword is a combination of its ad group ID and keyword ID.
Likewise, a unique identifier of an ad is a combination of its ad group ID
and ad ID. This has implications for the way
selector.withIds() is called.
For campaigns and ad groups,
selector.withIds() expects an array
var ids = [123123, 234234, 345345]; var campaignSelector = AdsApp.campaigns().withIds(ids);
For ads and keywords, however,
selector.withIds() needs an array
of two-element arrays, the first element being the ad group ID. The following
snippet retrieves three keywords from an ad group:
var adGroupId = 123123; var keywordSelector = AdsApp.keywords().withIds([ [adGroupId, 234234], [adGroupId, 345345], [adGroupId, 456456] ]);
Same construct applies when fetching ads.