转变策略
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本指南将介绍如何构建与现有效果最大化广告系列指南完全类似的指南,这些指南假定您将在单个原子请求中创建整个广告系列,而不是在单独的请求中一次创建一个实体。这意味着,您需要使用临时 ID 将资源彼此关联,因为在获得 API 响应之前,您无法知道完整的资源名称。
为此,您必须编写一些代码,以确保不会创建任何重复的临时 ID:
let nextId = -1;
function getNextTempId() {
const ret = nextId;
nextId -= 1;
return ret;
}
每次连续调用 getNextTempId
都会返回比上一次少 1 的数字。由于所有临时 ID 都必须为负数,因此从 -1 开始。
完成此设置后,您现在可以创建一个数组来保存所有操作:
const operations = [];
您经常需要您要创建广告系列的客户的客户 ID,因为每个资源名称中都需要此 ID。
const customerId = AdsApp.currentAccount().getCustomerId();
每次要创建新操作时,您都将在资源名称中使用下一个临时 ID,以便稍后引用此对象,并将创建的对象插入到数组中:
const newOperation = {
[OPERATION_TYPE_VARIES]: {
create: {
resourceName: `customers/${customerId}/[EXACT_PATH_VARIES]/${getNextTempId()}`
// Other fields, relevant to the resource being created.
}
}
}
operations.push(newOperation);
您可以参阅 Google Ads API REST mutate 文档,详细了解相关信息并查看示例操作。
构建完所有操作后,以单个批次执行这些操作:
AdsApp.mutateAll(operations);
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-27。
[null,null,["最后更新时间 (UTC):2025-08-27。"],[[["\u003cp\u003eThis guide provides instructions on creating Google Ads Performance Max campaigns using the Google Ads API with a single atomic request, as opposed to creating each entity individually.\u003c/p\u003e\n"],["\u003cp\u003eTo link resources within the single request, temporary IDs are utilized and assigned with a function ensuring unique negative values for each.\u003c/p\u003e\n"],["\u003cp\u003eThe guide involves constructing an array of operations, where each operation represents the creation of a specific campaign component.\u003c/p\u003e\n"],["\u003cp\u003eAfter defining all campaign elements and their relationships through the operations array, the entire campaign structure is created by executing a single batch mutation request via \u003ccode\u003eAdsApp.mutateAll(operations)\u003c/code\u003e.\u003c/p\u003e\n"]]],[],null,["# Mutate Strategy\n\nThis guide will be presented to construct an exact analog to the existing\nPerformance Max guides, which assume that you will be creating the entire\ncampaign in a single atomic request, rather than creating each entity one at a\ntime in separate requests. This means that you'll need to use\n[temporary IDs](/google-ads/api/docs/batch-processing/temporary-ids) to link\nresources to each other, since you won't know the full resource names until you\nget the API response.\n\nTo do this, you'll have to write some code to ensure that you don't create any\nduplicate temp IDs: \n\n let nextId = -1;\n\n function getNextTempId() {\n const ret = nextId;\n nextId -= 1;\n return ret;\n }\n\nEach successive call to `getNextTempId` will return a number one less than the\nprevious. Since all temp IDs must be negative, start at -1.\n\nWith this in place, you can now create an array to hold all the operations: \n\n const operations = [];\n\nYou will frequently need the customer ID for the customer you're making the\ncampaign in, since it's required in every resource name. \n\n const customerId = AdsApp.currentAccount().getCustomerId();\n\nEach time you want to create a new operation, you will use the next temp ID in\nthe resource name, so that you can reference this object later, and insert the\nobject created into the array: \n\n const newOperation = {\n [OPERATION_TYPE_VARIES]: {\n create: {\n resourceName: `customers/${customerId}/[EXACT_PATH_VARIES]/${getNextTempId()}`\n // Other fields, relevant to the resource being created.\n }\n }\n }\n operations.push(newOperation);\n\nYou can read more and see an example operation on the\n[Google Ads API REST mutate documentation](/google-ads/api/rest/common/mutate).\n\nOnce you have constructed all of our operations, execute them in a single\nbatch: \n\n AdsApp.mutateAll(operations);"]]