Drafts and Experiments
Stay organized with collections
Save and categorize content based on your preferences.
Create a draft Search or Display 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 2025-08-20 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 2025-08-20 UTC."],[[["\u003cp\u003eThis script provides functions for managing Google Ads drafts and experiments, including creating, retrieving, and interacting with them.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003ecreateDraft\u003c/code\u003e function enables the creation of a new draft campaign from an existing campaign using their respective names.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003egetDrafts\u003c/code\u003e function retrieves and displays either all existing drafts or a specific draft based on its name.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003ecreateExperiment\u003c/code\u003e function initiates a new experiment based on a selected draft, assigning it a name and traffic split percentage.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003egetExperiments\u003c/code\u003e function lists all available experiments or a specific one using its name, aiding in experiment monitoring and management.\u003c/p\u003e\n"]]],[],null,["# Drafts and Experiments\n\nCreate a draft Search or Display campaign\n-----------------------------------------\n\n```gdscript\nfunction createDraft(campaignName, newDraftName) {\n const campaign = AdsApp.campaigns()\n .withCondition(`campaign.name = '${campaignName}'`)\n .get()\n .next();\n\n var draftBuilder = campaign.newDraftBuilder()\n .withName(newDraftName)\n .build();\n\n var draft = draftBuilder.getResult();\n}\n```\n\nGet draft campaigns\n-------------------\n\n```gdscript\nfunction getDrafts() {\n // Get all drafts.\n const drafts = AdsApp.drafts().get();\n\n console.log(drafts.totalNumEntities());\n\n for (const draft of drafts) {\n console.log(\"Draft: \" + draft.getName());\n }\n\n // Get a specific draft.\n const campaignIterator = AdsApp.drafts()\n .withCondition(\"campaign_draft.name = 'INSERT_DRAFT_NAME'\")\n .get();\n\n for (const campaign of campaignIterator) {\n console.log(campaign.getName());\n }\n}\n```\n\nCreate an experiment\n--------------------\n\n```gdscript\nfunction createExperiment(draftName, newExperimentName) {\n const draft = AdsApp.drafts()\n .withCondition(`campaign_draft.name = '${draftName}'`)\n .get()\n .next();\n\n var experimentBuilder = draft.newExperimentBuilder();\n\n experimentBuilder.withName(newExperimentName)\n .withTrafficSplitPercent(50)\n .startBuilding();\n}\n```\n\nGet experiments\n---------------\n\n```gdscript\nfunction getExperiments() {\n // Get all experiments.\n var exps = AdsApp.experiments().get();\n\n console.log(exps.totalNumEntities());\n\n while (exps.hasNext()) {\n var exp = exps.next();\n console.log(\"Experiment: \" + exp.getName());\n }\n\n // Get specific experiment.\n var campaignIterator = AdsApp.experiments()\n .withCondition(\"Name = 'INSERT_EXPERIMENT_NAME'\")\n .get();\n\n while (campaignIterator.hasNext()) {\n console.log(campaignIterator.next().getName());\n }\n}\n```"]]