ID
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
大多数 Google Ads 实体都会公开一个 getId()
方法,用于返回其标识符。虽然在大多数情况下并非绝对必要,但在以下情况下,ID 可能会派上用场:
- 使用报告
- ID 提供了一种将报告行与实际 Google Ads 实体相关联的好方法。
- 维护与外部数据存储之间的映射
- 您可能已在自己的数据库中存储了基于 ID 的信息。
- 寻求性能方面的提升
通过 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 的组合。这会影响 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]
]);
在提取广告时,也适用相同的结构。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-27。
[null,null,["最后更新时间 (UTC):2025-08-27。"],[[["\u003cp\u003eMost Google Ads entities have a \u003ccode\u003egetId()\u003c/code\u003e method, which returns a unique identifier that can be useful for linking data, improving performance, and referencing external databases.\u003c/p\u003e\n"],["\u003cp\u003eWhen working with reports, IDs can connect report rows to specific Google Ads entities.\u003c/p\u003e\n"],["\u003cp\u003eFetching entities by ID is often faster than using other methods like filtering by name.\u003c/p\u003e\n"],["\u003cp\u003eCampaign 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.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eselector.withIds()\u003c/code\u003e 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.\u003c/p\u003e\n"]]],[],null,["# IDs\n\nMost Google Ads entities expose a `getId()` method that returns their\nidentifier. While not strictly necessary in most cases, IDs may come in handy\nwhen\n\nWorking with [reports](/google-ads/scripts/docs/features/reports)\n: IDs provide a good way to link a report row to the actual Google Ads entity.\n\nMaintaining a mapping with an external data store\n: You may already have ID-based information stored in your own database.\n\nLooking for a bit of a performance boost\n\n: Fetching by IDs is often quicker than alternatives. The code for fetching a\n single entity is a bit easier too:\n\n let campaigns = AdsApp.campaigns()\n .withIds([678678])\n .get();\n // vs.\n let campaigns = AdsApp.campaigns()\n .withCondition(\"Name='My Campaign'\")\n .get();\n\nUniqueness\n----------\n\nCampaign IDs and ad group IDs are unique: no two campaigns or ad groups will\never share the same ID. Ads and keywords, however, have composite IDs: a unique\nidentifier of a keyword is a combination of its ad group ID and keyword ID.\nLikewise, a unique identifier of an ad is a combination of its ad group ID and\nad ID. This has implications for the way `selector.withIds()` is called.\n\nFor campaigns and ad groups, `selector.withIds()` expects an array of numbers: \n\n let ids = [123123, 234234, 345345];\n let campaignSelector = AdsApp.campaigns().withIds(ids);\n\nFor ads and keywords, however, `selector.withIds()` needs an array of\ntwo-element arrays, the first element being the ad group ID. The following\nsnippet retrieves three keywords from an ad group: \n\n let adGroupId = 123123;\n let keywordSelector = AdsApp.keywords().withIds([\n [adGroupId, 234234],\n [adGroupId, 345345],\n [adGroupId, 456456]\n ]);\n\nThe same construct applies when fetching ads."]]