Keywords
Stay organized with collections
Save and categorize content based on your preferences.
Create a keyword in an existing ad group
function
createHatsKeyword
()
{
//
This
example
snippet
creates
a
broad
match
keyword
for
"hats"
.
Keywords
//
can
be
created
with
many
optional
settings
,
such
as
a
max
CPC
bid
,
tracking
//
URL
templates
,
and
more
.
Please
customize
this
example
for
your
specific
//
use
case
.
For
more
details
about
keyword
builder
options
,
see
//
https
:
//
developers
.
google
.
com
/
google
-
ads
/
scripts
/
docs
/
reference
/
adsapp
/
adsapp_keywordbuilder
.
const
adGroupName
=
'Ad group 1'
;
const
adGroupIterator
=
AdsApp
.
adGroups
()
.
withCondition
(
`
ad_group
.
name
=
"${adGroupName}"
`
)
.
get
();
if
(
!
adGroupIterator
.
hasNext
())
{
throw
new
Error
(
`
No
ad
group
found
with
name
"${adGroupName}"
`
);
}
const
adGroup
=
adGroupIterator
.
next
();
if
(
adGroupIterator
.
totalNumEntities
()
>
1
)
{
console
.
warn
(
`
Multiple
ad
groups
named
"${adGroupName}"
found
.
Using
the
one
from
campaign
"${adGroup.getCampaign().getName()}"
.
`
);
}
const
keywordOperation
=
adGroup
.
newKeywordBuilder
()
.
withText
(
'hats'
)
.
withCpc
(
1.25
)
.
withFinalUrl
(
'https://www.example.com'
)
.
build
();
return
keywordOperation
;
}
Pause an existing keyword in an ad group
function
pauseKeywordInAdGroup
(
keywordText
,
adGroupName
)
{
const
adGroupIterator
=
AdsApp
.
adGroups
()
.
withCondition
(
`
ad_group
.
name
=
"${adGroupName}"
`
)
.
get
();
if
(
!
adGroupIterator
.
hasNext
())
{
throw
new
Error
(
`
No
ad
group
found
with
name
"${adGroupName}"
`
);
}
const
adGroup
=
adGroupIterator
.
next
();
if
(
adGroupIterator
.
totalNumEntities
()
>
1
)
{
console
.
warn
(
`
Multiple
ad
groups
named
"${adGroupName}"
found
.
Using
the
one
from
campaign
"${adGroup.getCampaign().getName()}"
.
`
);
}
for
(
const
keyword
of
adGroup
.
keywords
()
.
withCondition
(
`
ad_group_criterion
.
keyword
.
text
=
"${keywordText}"
`
))
{
keyword
.
pause
();
}
}
Get all keywords in an ad group
function
getKeywordsInAdGroup
( adGroupName
) { const
keywordIterator
= AdsApp
. keywords
()
. withCondition
(` ad_group
. name
= "${adGroupName}"
`)
. get
(); console
. log
(` Ad
Group
"${adGroupName}"
has
${ keywordIterator
. totalNumEntities
()} keywords
`); return
keywordIterator
;
}
Log stats for all keywords in an ad group
function
logKeywordStatsForAdGroup
()
{
//
This
example
snippet
prints
click
and
impression
statistics
to
the
script
//
execution
log
.
Please
customize
this
example
for
your
specific
use
case
.
//
For
all
the
kinds
of
statistics
that
can
be
logged
,
see
//
https
:
//
developers
.
google
.
com
/
google
-
ads
/
scripts
/
docs
/
reference
/
adsapp
/
adsapp_stats
.
const
adGroupName
=
'Ad group 1'
;
const
adGroupIterator
=
AdsApp
.
adGroups
()
.
withCondition
(
`
ad_group
.
name
=
"${adGroupName}"
`
)
.
get
();
if
(
!
adGroupIterator
.
hasNext
())
{
throw
new
Error
(
`
No
ad
group
found
with
name
"${adGroupName}"
`
);
}
const
adGroup
=
adGroupIterator
.
next
();
if
(
adGroupIterator
.
totalNumEntities
()
>
1
)
{
console
.
warn
(
`
Multiple
ad
groups
named
"${adGroupName}"
found
.
Using
the
one
from
campaign
"${adGroup.getCampaign().getName()}"
.
`
);
}
for
(
const
keyword
of
adGroup
.
keywords
())
{
let
stats
=
keyword
.
getStatsFor
(
'LAST_MONTH'
);
console
.
log
(
`
Ad
Group
:
"${adGroup.getName()}"
`
);
console
.
log
(
`
Keyword
:
"${keyword.getText()}"
`
);
console
.
log
(
`
Clicks
:
$
{
stats
.
getClicks
()}
`
);
console
.
log
(
`
Impressions
:
$
{
stats
.
getImpressions
()}
`
);
console
.
log
(
'--------------------'
);
}
}
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 webpage provides Google Ads Scripts examples for managing keywords within an ad group.\u003c/p\u003e\n"],["\u003cp\u003eYou can use these scripts to create new keywords, specifying attributes like match type, bid, and final URL.\u003c/p\u003e\n"],["\u003cp\u003eThe scripts also demonstrate how to pause existing keywords based on their text and ad group.\u003c/p\u003e\n"],["\u003cp\u003eYou can retrieve and iterate through all keywords within a specific ad group using the provided functions.\u003c/p\u003e\n"],["\u003cp\u003eExamples for logging key performance statistics, such as clicks and impressions, for keywords in an ad group are included.\u003c/p\u003e\n"]]],[],null,["# Keywords\n\nCreate a keyword in an existing ad group\n----------------------------------------\n\n```gdscript\nfunction createHatsKeyword() {\n // This example snippet creates a broad match keyword for \"hats\". Keywords\n // can be created with many optional settings, such as a max CPC bid, tracking\n // URL templates, and more. Please customize this example for your specific\n // use case. For more details about keyword builder options, see\n // https://developers.google.com/google-ads/scripts/docs/reference/adsapp/adsapp_keywordbuilder.\n const adGroupName = 'Ad group 1';\n\n const adGroupIterator = AdsApp.adGroups()\n .withCondition(`ad_group.name = \"${adGroupName}\"`)\n .get();\n\n if (!adGroupIterator.hasNext()) {\n throw new Error(`No ad group found with name \"${adGroupName}\"`);\n }\n\n const adGroup = adGroupIterator.next();\n\n if (adGroupIterator.totalNumEntities() \u003e 1) {\n console.warn(`Multiple ad groups named \"${adGroupName}\" found.\nUsing the one from campaign \"${adGroup.getCampaign().getName()}\".`);\n }\n\n const keywordOperation = adGroup.newKeywordBuilder()\n .withText('hats')\n .withCpc(1.25)\n .withFinalUrl('https://www.example.com')\n .build();\n\n return keywordOperation;\n}\n```\n\nPause an existing keyword in an ad group\n----------------------------------------\n\n```gdscript\nfunction pauseKeywordInAdGroup(keywordText, adGroupName) {\n const adGroupIterator = AdsApp.adGroups()\n .withCondition(`ad_group.name = \"${adGroupName}\"`)\n .get();\n\n if (!adGroupIterator.hasNext()) {\n throw new Error(`No ad group found with name \"${adGroupName}\"`);\n }\n\n const adGroup = adGroupIterator.next();\n\n if (adGroupIterator.totalNumEntities() \u003e 1) {\n console.warn(`Multiple ad groups named \"${adGroupName}\" found.\nUsing the one from campaign \"${adGroup.getCampaign().getName()}\".`);\n }\n\n for (const keyword of adGroup.keywords().withCondition(\n `ad_group_criterion.keyword.text = \"${keywordText}\"`)) {\n keyword.pause();\n }\n}\n```\n\nGet all keywords in an ad group\n-------------------------------\n\n```perl6\nfunction getKeywordsInAdGroup(adGroupName) {\n const keywordIterator = AdsApp.keywords()\n .withCondition(`ad_group.name = \"${adGroupName}\"`)\n .get();\n console.log(`Ad Group \"${adGroupName}\" has ${\n keywordIterator.totalNumEntities()} keywords`);\n return keywordIterator;\n}\n```\n\nLog stats for all keywords in an ad group\n-----------------------------------------\n\n```gdscript\nfunction logKeywordStatsForAdGroup() {\n // This example snippet prints click and impression statistics to the script\n // execution log. Please customize this example for your specific use case.\n // For all the kinds of statistics that can be logged, see\n // https://developers.google.com/google-ads/scripts/docs/reference/adsapp/adsapp_stats.\n const adGroupName = 'Ad group 1';\n\n const adGroupIterator = AdsApp.adGroups()\n .withCondition(`ad_group.name = \"${adGroupName}\"`)\n .get();\n\n if (!adGroupIterator.hasNext()) {\n throw new Error(`No ad group found with name \"${adGroupName}\"`);\n }\n\n const adGroup = adGroupIterator.next();\n\n if (adGroupIterator.totalNumEntities() \u003e 1) {\n console.warn(`Multiple ad groups named \"${adGroupName}\" found.\nUsing the one from campaign \"${adGroup.getCampaign().getName()}\".`);\n }\n\n for (const keyword of adGroup.keywords()) {\n let stats = keyword.getStatsFor('LAST_MONTH');\n console.log(`Ad Group: \"${adGroup.getName()}\"`);\n console.log(`Keyword: \"${keyword.getText()}\"`);\n console.log(`Clicks: ${stats.getClicks()}`);\n console.log(`Impressions: ${stats.getImpressions()}`);\n console.log('--------------------');\n }\n}\n```"]]