ID
大多數 Google Ads 實體都會公開 getId()
方法,用於傳回其 ID。雖然在大多數情況下並非必要,但在以下情況下,ID 可能會很實用:
- 使用報表
- ID 是連結報表資料列與實際 Google Ads 實體的好方法。
- 維護與外部資料儲存的對應
- 您可能已將 ID 資訊儲存在自己的資料庫中。
- 想要稍微提升成效
按編號擷取通常比其他方式來得快,擷取單一實體的程式碼也比較簡單:
let campaigns = AdsApp.campaigns()
.withIds([678678])
.get();
// vs.
let campaigns = AdsApp.campaigns()
.withCondition("Name='My Campaign'")
.get();
獨特性
廣告活動 ID 和廣告群組 ID 皆為唯一值:兩個廣告活動或廣告群組不會共用相同的 ID。不過,廣告和關鍵字有複合 ID:關鍵字的專屬 ID 是廣告群組 ID 和關鍵字 ID 的組合。同樣地,廣告的專屬 ID 是廣告群組 ID 和廣告 ID 的組合。這會影響 selector.withIds()
的呼叫方式。
針對廣告活動和廣告群組,selector.withIds()
會預期數字陣列:
let ids = [123123, 234234, 345345];
let campaignSelector = AdsApp.campaigns().withIds(ids);
不過,如果是廣告和關鍵字,selector.withIds()
需要兩個元素陣列的陣列,其中第一個元素為廣告群組 ID。以下程式碼片段會從廣告群組擷取三個關鍵字:
let adGroupId = 123123;
let keywordSelector = AdsApp.keywords().withIds([
[adGroupId, 234234],
[adGroupId, 345345],
[adGroupId, 456456]
]);
擷取廣告時也適用相同的結構。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-03-04 (世界標準時間)。
[null,null,["上次更新時間:2025-03-04 (世界標準時間)。"],[[["Most Google Ads entities have a `getId()` method, which returns a unique identifier that can be useful for linking data, improving performance, and referencing external databases."],["When working with reports, IDs can connect report rows to specific Google Ads entities."],["Fetching entities by ID is often faster than using other methods like filtering by name."],["Campaign and ad group IDs are unique, while ad and keyword IDs are composite, requiring both the ad group ID and their individual ID for unique identification."],["The `selector.withIds()` method is used to fetch entities by ID, taking an array of numbers for campaigns and ad groups and an array of two-element arrays (ad group ID and entity ID) for ads and keywords."]]],[]]