Drafts and Experiments
Stay organized with collections
Save and categorize content based on your preferences.
Create a draft campaign
function createDraft(campaignName, newDraftName) {
const campaign = AdsApp.campaigns()
.withCondition(`campaign.name = '${campaignName}'`)
.get()
.next();
var draftBuilder = campaign.newDraftBuilder()
.withName(newDraftName)
.build();
var draft = draftBuilder.getResult();
}
Get draft campaigns
function getDrafts() {
// Get all drafts.
const drafts = AdsApp.drafts().get();
console.log(drafts.totalNumEntities());
for (const draft of drafts) {
console.log("Draft: " + draft.getName());
}
// Get a specific draft.
const campaignIterator = AdsApp.drafts()
.withCondition("campaign_draft.name = 'INSERT_DRAFT_NAME'")
.get();
for (const campaign of campaignIterator) {
console.log(campaign.getName());
}
}
Create an experiment
function createExperiment(draftName, newExperimentName) {
const draft = AdsApp.drafts()
.withCondition(`campaign_draft.name = '${draftName}'`)
.get()
.next();
var experimentBuilder = draft.newExperimentBuilder();
experimentBuilder.withName(newExperimentName)
.withTrafficSplitPercent(50)
.startBuilding();
}
Get experiments
function getExperiments() {
// Get all experiments.
var exps = AdsApp.experiments().get();
console.log(exps.totalNumEntities());
while (exps.hasNext()) {
var exp = exps.next();
console.log("Experiment: " + exp.getName());
}
// Get specific experiment.
var campaignIterator = AdsApp.experiments()
.withCondition("Name = 'INSERT_EXPERIMENT_NAME'")
.get();
while (campaignIterator.hasNext()) {
console.log(campaignIterator.next().getName());
}
}
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2022-03-14 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2022-03-14 UTC."],[[["This script provides functions for managing Google Ads drafts and experiments, including creating, retrieving, and interacting with them."],["`createDraft` function enables the creation of a new draft campaign from an existing campaign using their respective names."],["`getDrafts` function retrieves and displays either all existing drafts or a specific draft based on its name."],["`createExperiment` function initiates a new experiment based on a selected draft, assigning it a name and traffic split percentage."],["`getExperiments` function lists all available experiments or a specific one using its name, aiding in experiment monitoring and management."]]],[]]