Ads Manager Scripts
Stay organized with collections
Save and categorize content based on your preferences.
Get all accounts
function
getAllAccounts
()
{
const
accountIterator
=
AdsManagerApp
.
accounts
()
.
get
();
for
(
const
account
of
accountIterator
)
{
const
accountName
=
account
.
getName
()
?
account
.
getName
()
:
'--'
;
console
.
log
(
'
%s
,
%s
,
%s
,
%s
'
,
account
.
getCustomerId
(),
accountName
,
account
.
getTimeZone
(),
account
.
getCurrencyCode
());
}
}
Get accounts from customer IDs
function
getAccountsFromCustomerIds
()
{
//
This
is
useful
when
you
are
reading
customer
IDs
from
an
external
data
//
source
,
such
as
a
Google
Spreadsheet
.
//
You
can
also
use
the
condition
"CustomerId in ['123-456-7890',
//
'345-678-9000'
,
'890-123-6000'
]
".
const
accountIterator
=
AdsManagerApp
.
accounts
()
.
withIds
([
'123-456-7890'
,
'345-678-9000'
,
'890-123-6000'
])
.
get
();
for
(
const
account
of
accountIterator
)
{
const
accountName
=
account
.
getName
()
?
account
.
getName
()
:
'--'
;
console
.
log
(
'
%s
,
%s
,
%s
,
%s
'
,
account
.
getCustomerId
(),
accountName
,
account
.
getTimeZone
(),
account
.
getCurrencyCode
());
}
}
Get accounts by label
function
getAccountsByLabel
()
{
//
Only
CONTAINS
and
DOES_NOT_CONTAIN
operators
are
supported
.
const
accountIterator
=
AdsManagerApp
.
accounts
()
.
withCondition
(
"LabelNames CONTAINS 'High spend accounts'"
)
.
get
();
for
(
const
account
of
accountIterator
)
{
const
accountName
=
account
.
getName
()
?
account
.
getName
()
:
'--'
;
console
.
log
(
'
%s
,
%s
,
%s
,
%s
'
,
account
.
getCustomerId
(),
accountName
,
account
.
getTimeZone
(),
account
.
getCurrencyCode
());
}
}
Update multiple accounts in series
function
updateAccountsInSeries
()
{
//
You
can
use
this
approach
when
you
have
only
minimal
processing
to
//
perform
in
each
of
your
client
accounts
.
//
Select
the
accounts
to
be
processed
.
const
accountIterator
=
AdsManagerApp
.
accounts
()
.
withCondition
(
"LabelNames CONTAINS 'Cars'"
)
.
get
();
for
(
const
account
of
accountIterator
)
{
//
Switch
to
the
account
you
want
to
process
.
AdsManagerApp
.
select
(
account
);
//
Retrieve
all
Search
and
Display
campaigns
to
be
paused
.
const
campaignIterator
=
AdsApp
.
campaigns
()
.
withCondition
(
"LabelNames = 'Christmas promotion'"
)
.
get
();
for
(
const
campaign
of
campaignIterator
)
{
console
.
log
(
`
Pausing
campaign
$
{
campaign
.
getName
()}
in
`
+
`
account
$
{
account
.
getCustomerId
()}
`
);
campaign
.
pause
();
}
}
}
Update multiple accounts in parallel
function
updateAccountsInParallel
()
{
//
You
can
use
this
approach
when
you
have
a
large
amount
of
processing
//
to
do
in
each
of
your
client
accounts
.
//
Select
the
accounts
to
be
processed
.
You
can
process
up
to
50
accounts
.
const
accountSelector
=
AdsManagerApp
.
accounts
()
.
withCondition
(
"LabelNames CONTAINS 'High spend accounts'"
)
.
withLimit
(
50
);
//
Process
the
account
in
parallel
.
The
'processAccount'
function
will
//
be
called
in
the
context
of
each
account
in
the
selector
.
The
'allFinished'
function
//
will
be
called
in
this
script
once
processing
is
complete
,
and
is
optional
.
accountSelector
.
executeInParallel
(
'processAccount'
,
'allFinished'
);
}
/**
*
Process
one
account
at
a
time
.
This
method
is
called
by
the
executeInParallel
*
method
call
in
updateAccountsInParallel
function
for
every
account
that
*
it
processes
.
*
*
@
return
{
Number
}
the
number
of
campaigns
paused
by
this
method
.
*/
function
processAccount
()
{
//
executeInParallel
will
automatically
switch
context
to
the
account
being
//
processed
,
so
all
calls
to
AdsApp
will
apply
to
the
selected
account
.
const
campaignIterator
=
AdsApp
.
campaigns
()
.
withCondition
(
"LabelNames = 'Christmas promotion'"
)
.
get
();
for
(
const
campaign
of
campaignIterator
)
{
console
.
log
(
`
Pausing
campaign
$
{
campaign
.
getName
()}
in
`
+
`
account
$
{
account
.
getCustomerId
()}
`
);
campaign
.
pause
();
}
//
Optional
:
return
a
string
value
.
If
you
have
a
more
complex
JavaScript
//
object
to
return
from
this
method
,
use
JSON
.
stringify
(
value
)
.
This
value
//
will
be
passed
on
to
the
callback
method
,
if
specified
,
in
the
//
executeInParallel
method
call
.
return
campaignIterator
.
totalNumEntities
()
.
toFixed
(
0
);
}
/**
*
Post
-
process
the
results
from
processAccount
.
This
method
will
be
called
*
once
all
the
accounts
have
been
processed
by
the
executeInParallel
method
*
call
.
*
*
@
param
{
Array
.
< ExecutionResult
> }
results
An
array
of
ExecutionResult
objects
,
*
one
for
each
account
that
was
processed
by
the
executeInParallel
method
.
*/
function
allFinished
(
results
)
{
for
(
const
result
of
results
)
{
console
.
log
(
`
Customer
ID
:
$
{
result
.
getCustomerId
};
`
+
`
status
=
$
{
result
.
getStatus
}
.
`
);
//
Check
the
execution
status
.
This
can
be
one
of
ERROR
,
OK
,
or
TIMEOUT
.
if
(
result
.
getStatus
()
==
'ERROR'
)
{
console
.
log
(
`
--
Failed
with
error
:
'${result.getError()}'
.
`
);
}
else
if
(
result
.
getStatus
()
==
'OK'
)
{
//
This
is
the
value
you
returned
from
processAccount
method
.
If
you
//
used
JSON
.
stringify
(
value
)
in
processAccount
,
you
can
use
//
JSON
.
parse
(
text
)
to
reconstruct
the
JavaScript
object
.
const
retval
=
result
.
getReturnValue
();
console
.
log
(
`
--
Processed
$
{
retval
}
campaigns
.
`
);
}
else
{
//
Handle
timeouts
here
.
}
}
}
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 showcases how to retrieve Google Ads accounts using various methods, including fetching all accounts, filtering by customer IDs, and selecting accounts based on label criteria.\u003c/p\u003e\n"],["\u003cp\u003eIt demonstrates methods for updating multiple accounts, either by processing them individually in series or handling them concurrently in parallel for improved efficiency.\u003c/p\u003e\n"],["\u003cp\u003eParallel processing is highlighted as a suitable approach for scenarios involving substantial workloads within each account, enabling quicker execution by handling up to 50 accounts simultaneously.\u003c/p\u003e\n"],["\u003cp\u003eAccount processing in parallel utilizes \u003ccode\u003eexecuteInParallel\u003c/code\u003e, allowing separate functions for individual account operations and a final callback for consolidating results and managing errors or timeouts.\u003c/p\u003e\n"]]],[],null,["# Ads Manager Scripts\n\nGet all accounts\n----------------\n\n```gdscript\nfunction getAllAccounts() {\n const accountIterator = AdsManagerApp.accounts().get();\n\n for (const account of accountIterator) {\n const accountName = account.getName() ? account.getName() : '--';\n console.log('%s,%s,%s,%s', account.getCustomerId(), accountName,\n account.getTimeZone(), account.getCurrencyCode());\n }\n}\n```\n\nGet accounts from customer IDs\n------------------------------\n\n```gdscript\nfunction getAccountsFromCustomerIds() {\n // This is useful when you are reading customer IDs from an external data\n // source, such as a Google Spreadsheet.\n\n // You can also use the condition \"CustomerId in ['123-456-7890',\n // '345-678-9000', '890-123-6000']\".\n const accountIterator = AdsManagerApp.accounts()\n .withIds(['123-456-7890', '345-678-9000', '890-123-6000'])\n .get();\n\n for (const account of accountIterator) {\n const accountName = account.getName() ? account.getName() : '--';\n console.log('%s,%s,%s,%s', account.getCustomerId(), accountName,\n account.getTimeZone(), account.getCurrencyCode());\n }\n}\n```\n\nGet accounts by label\n---------------------\n\n```gdscript\nfunction getAccountsByLabel() {\n // Only CONTAINS and DOES_NOT_CONTAIN operators are supported.\n const accountIterator = AdsManagerApp.accounts()\n .withCondition(\"LabelNames CONTAINS 'High spend accounts'\")\n .get();\n\n for (const account of accountIterator) {\n const accountName = account.getName() ? account.getName() : '--';\n console.log('%s,%s,%s,%s', account.getCustomerId(), accountName,\n account.getTimeZone(), account.getCurrencyCode());\n }\n}\n```\n\nUpdate multiple accounts in series\n----------------------------------\n\n```gdscript\nfunction updateAccountsInSeries() {\n // You can use this approach when you have only minimal processing to\n // perform in each of your client accounts.\n\n // Select the accounts to be processed.\n const accountIterator = AdsManagerApp.accounts()\n .withCondition(\"LabelNames CONTAINS 'Cars'\")\n .get();\n\n for (const account of accountIterator) {\n // Switch to the account you want to process.\n AdsManagerApp.select(account);\n\n // Retrieve all Search and Display campaigns to be paused.\n const campaignIterator = AdsApp.campaigns()\n .withCondition(\"LabelNames = 'Christmas promotion'\")\n .get();\n\n for (const campaign of campaignIterator) {\n console.log(`Pausing campaign ${campaign.getName()} in ` +\n `account ${account.getCustomerId()}`);\n campaign.pause();\n }\n }\n}\n```\n\nUpdate multiple accounts in parallel\n------------------------------------\n\n```gdscript\nfunction updateAccountsInParallel() {\n // You can use this approach when you have a large amount of processing\n // to do in each of your client accounts.\n\n // Select the accounts to be processed. You can process up to 50 accounts.\n const accountSelector = AdsManagerApp.accounts()\n .withCondition(\"LabelNames CONTAINS 'High spend accounts'\")\n .withLimit(50);\n\n // Process the account in parallel. The 'processAccount' function will\n // be called in the context of each account in the selector. The 'allFinished' function\n // will be called in this script once processing is complete, and is optional.\n accountSelector.executeInParallel('processAccount', 'allFinished');\n}\n\n/**\n * Process one account at a time. This method is called by the executeInParallel\n * method call in updateAccountsInParallel function for every account that\n * it processes.\n *\n * @return {Number} the number of campaigns paused by this method.\n */\nfunction processAccount() {\n // executeInParallel will automatically switch context to the account being\n // processed, so all calls to AdsApp will apply to the selected account.\n const campaignIterator = AdsApp.campaigns()\n .withCondition(\"LabelNames = 'Christmas promotion'\")\n .get();\n\n for (const campaign of campaignIterator) {\n console.log(`Pausing campaign ${campaign.getName()} in ` +\n `account ${account.getCustomerId()}`);\n campaign.pause();\n }\n // Optional: return a string value. If you have a more complex JavaScript\n // object to return from this method, use JSON.stringify(value). This value\n // will be passed on to the callback method, if specified, in the\n // executeInParallel method call.\n return campaignIterator.totalNumEntities().toFixed(0);\n}\n\n/**\n * Post-process the results from processAccount. This method will be called\n * once all the accounts have been processed by the executeInParallel method\n * call.\n *\n * @param {Array.\u003cExecutionResult\u003e} results An array of ExecutionResult objects,\n * one for each account that was processed by the executeInParallel method.\n */\nfunction allFinished(results) {\n for (const result of results) {\n console.log(`Customer ID: ${result.getCustomerId}; ` +\n `status = ${result.getStatus}.`);\n\n // Check the execution status. This can be one of ERROR, OK, or TIMEOUT.\n if (result.getStatus() == 'ERROR') {\n console.log(`-- Failed with error: '${result.getError()}'.`);\n } else if (result.getStatus() == 'OK') {\n // This is the value you returned from processAccount method. If you\n // used JSON.stringify(value) in processAccount, you can use\n // JSON.parse(text) to reconstruct the JavaScript object.\n const retval = result.getReturnValue();\n console.log(`--Processed ${retval} campaigns.`);\n } else {\n // Handle timeouts here.\n }\n }\n}\n```"]]