Mutate Strategy

This guide will be presented to construct an exact analog to the existing Performance Max guides, which assume that you will be creating the entire campaign in a single atomic request, rather than creating each entity one at a time in separate requests. This means that you'll need to use temporary IDs to link resources to each other, since you won't know the full resource names until you get the API response.

To do this, you'll have to write some code to ensure that you don't create any duplicate temp IDs:

let nextId = -1;

function getNextTempId() {
    const ret = nextId;
    nextId -= 1;
    return ret;
}

Each successive call to getNextTempId will return a number one less than the previous. Since all temp IDs must be negative, start at -1.

With this in place, you can now create an array to hold all the operations:

const operations = [];

You will frequently need the customer ID for the customer you're making the campaign in, since it's required in every resource name.

const customerId = AdsApp.currentAccount().getCustomerId();

Each time you want to create a new operation, you will use the next temp ID in the resource name, so that you can reference this object later, and insert the object created into the array:

const newOperation = {
    [OPERATION_TYPE_VARIES]: {
        create: {
            resourceName: `customers/${customerId}/[EXACT_PATH_VARIES]/${getNextTempId()}`
            // Other fields, relevant to the resource being created.
        }
    }
}
operations.push(newOperation);

You can read more and see an example operation on the Google Ads API REST mutate documentation.

Once you have constructed all of our operations, execute them in a single batch:

AdsApp.mutateAll(operations);