Negative Keywords
Stay organized with collections
Save and categorize content based on your preferences.
Add negative keyword to a campaign
function
addNegativeKeywordToCampaign
(
keyword
,
campaignName
)
{
const
campaignIterator
=
AdsApp
.
campaigns
()
.
withCondition
(
`
campaign
.
name
=
"${campaignName}"
`
)
.
get
();
if
(
campaignIterator
.
hasNext
())
{
const
campaign
=
campaignIterator
.
next
();
campaign
.
createNegativeKeyword
(
keyword
);
}
else
{
throw
new
Error
(
`
Cannot
find
campaign
with
the
name
'${campaignName}'
`
);
}
}
Get negative keywords in a campaign
function
getNegativeKeywordsForCampaign
(
campaignName
)
{
const
campaignIterator
=
AdsApp
.
campaigns
()
.
withCondition
(
`
campaign
.
name
=
"${campaignName}"
`
)
.
get
();
if
(
campaignIterator
.
hasNext
())
{
const
campaign
=
campaignIterator
.
next
();
const
negativeKeywordIterator
=
campaign
.
negativeKeywords
()
.
get
();
console
.
log
(
`
Found
$
{
negativeKeywordIterator
.
totalNumEntities
()}
negative
keywords
.
`
);
return
negativeKeywordIterator
;
}
else
{
throw
new
Error
(
`
Cannot
find
campaign
with
the
name
'${campaignName}'
`
);
}
}
Add a negative keyword to an ad group
function
addNegativeKeywordToAdGroup
(
keyword
,
adGroupName
)
{
const
adGroupIterator
=
AdsApp
.
adGroups
()
.
withCondition
(
`
ad_group
.
name
=
"${adGroupName}"
`
)
.
get
();
if
(
!
adGroupIterator
.
hasNext
())
{
throw
new
Error
(
`
Cannot
find
ad
group
with
the
name
'${adGroupName}'
`
);
}
if
(
adGroupIterator
.
totalNumEntities
()
>
1
)
{
console
.
warn
(
`
Found
more
than
one
ad
group
named
'${adGroupName}'
,
using
the
first
one
.
`
);
}
const
adGroup
=
adGroupIterator
.
next
();
adGroup
.
createNegativeKeyword
(
keyword
);
}
Get negative keywords in an ad group
function
getNegativeKeywordsForAdGroup
(
adGroupName
)
{
const
adGroupIterator
=
AdsApp
.
adGroups
()
.
withCondition
(
`
ad_group
.
name
=
"${adGroupName}"
`
)
.
get
();
if
(
!
adGroupIterator
.
hasNext
())
{
throw
new
Error
(
`
Cannot
find
ad
group
with
the
name
'${adGroupName}'
`
);
}
if
(
adGroupIterator
.
totalNumEntities
()
>
1
)
{
console
.
warn
(
`
Found
more
than
one
ad
group
named
'${adGroupName}'
,
using
the
first
one
.
`
);
}
const
adGroup
=
adGroupIterator
.
next
();
const
negativeKeywordIterator
=
adGroup
.
negativeKeywords
()
.
get
();
if
(
negativeKeywordIterator
.
hasNext
())
{
const
negativeKeyword
=
negativeKeywordIterator
.
next
();
console
.
log
(
`
Found
$
{
negativeKeywordIterator
.
totalNumEntities
()}
negative
keywords
.
`
);
return
negativeKeywordIterator
;
}
}
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 documentation provides Google Ads scripts for managing negative keywords at both the campaign and ad group levels.\u003c/p\u003e\n"],["\u003cp\u003eIt includes functions to add negative keywords and retrieve existing negative keywords for campaigns or ad groups using their respective names.\u003c/p\u003e\n"],["\u003cp\u003eWhen searching for ad groups by name, if multiple ad groups share the same name, the script will use the first one it encounters and issue a warning.\u003c/p\u003e\n"],["\u003cp\u003eThese functions are built upon the Google Ads Scripts API, utilizing methods like \u003ccode\u003ecreateNegativeKeyword\u003c/code\u003e and iterators to interact with campaign and ad group entities.\u003c/p\u003e\n"],["\u003cp\u003eError handling is incorporated to notify users if the specified campaign or ad group is not found.\u003c/p\u003e\n"]]],[],null,["# Negative Keywords\n\nAdd negative keyword to a campaign\n----------------------------------\n\n```gdscript\nfunction addNegativeKeywordToCampaign(keyword, campaignName) {\n const campaignIterator = AdsApp.campaigns()\n .withCondition(`campaign.name = \"${campaignName}\"`)\n .get();\n if (campaignIterator.hasNext()) {\n const campaign = campaignIterator.next();\n campaign.createNegativeKeyword(keyword);\n } else {\n throw new Error(`Cannot find campaign with the name '${campaignName}'`);\n }\n}\n```\n\nGet negative keywords in a campaign\n-----------------------------------\n\n```gdscript\nfunction getNegativeKeywordsForCampaign(campaignName) {\n const campaignIterator = AdsApp.campaigns()\n .withCondition(`campaign.name = \"${campaignName}\"`)\n .get();\n if (campaignIterator.hasNext()) {\n const campaign = campaignIterator.next();\n const negativeKeywordIterator = campaign.negativeKeywords().get();\n console.log(`Found ${negativeKeywordIterator.totalNumEntities()} negative keywords.`);\n return negativeKeywordIterator;\n } else {\n throw new Error(`Cannot find campaign with the name '${campaignName}'`);\n }\n}\n```\n\nAdd a negative keyword to an ad group\n-------------------------------------\n\n```gdscript\nfunction addNegativeKeywordToAdGroup(keyword, adGroupName) {\n const adGroupIterator = AdsApp.adGroups()\n .withCondition(`ad_group.name = \"${adGroupName}\"`)\n .get();\n if (!adGroupIterator.hasNext()) {\n throw new Error(`Cannot find ad group with the name '${adGroupName}'`);\n }\n if (adGroupIterator.totalNumEntities() \u003e 1) {\n console.warn(`Found more than one ad group named '${adGroupName}', using the first one.`);\n }\n const adGroup = adGroupIterator.next();\n adGroup.createNegativeKeyword(keyword);\n}\n```\n\nGet negative keywords in an ad group\n------------------------------------\n\n```gdscript\nfunction getNegativeKeywordsForAdGroup(adGroupName) {\n const adGroupIterator = AdsApp.adGroups()\n .withCondition(`ad_group.name = \"${adGroupName}\"`)\n .get();\n if (!adGroupIterator.hasNext()) {\n throw new Error(`Cannot find ad group with the name '${adGroupName}'`);\n }\n if (adGroupIterator.totalNumEntities() \u003e 1) {\n console.warn(`Found more than one ad group named '${adGroupName}', using the first one.`);\n }\n const adGroup = adGroupIterator.next();\n const negativeKeywordIterator = adGroup.negativeKeywords().get();\n if (negativeKeywordIterator.hasNext()) {\n const negativeKeyword = negativeKeywordIterator.next();\n console.log(`Found ${negativeKeywordIterator.totalNumEntities()} negative keywords.`);\n return negativeKeywordIterator;\n }\n}\n```"]]