Adding negative keywords while optimizing your campaigns is of course the most normal thing in the world. But it can sometimes happen that you have been too enthusiastic and that exclusion words have ended up in a list that will block keywords in other campaigns. Sin of course, because now there is no traffic on these keywords anymore. That's why Google has created a nice script that checks all keywords and exclusion words / lists to make sure that no keywords are blocked. Because blocked keywords also do not provide Google with money.
You can apply the script below in your MCC and thus check all your accounts in one go. The script uses the executeInParallel function, which must ensure that the analysis runs faster. Pay attention, however, that you therefore can not control more than 50 accounts at the same time. Use account labels to make a selection. There is also a script that you can run at the account level.
Settings
If you look through the script, you will automatically see 'var CONFIG = {'. Below are some things that you need to complete before you can run the script.
SPREADSHEET_URL: You must first make a copy of the Google Spreadsheet so that the data can end up there. The URL you copy must always contain '/ edit', but you can omit what comes after it.
RECIPIENTS_EMAILS: Enter the email addresses where you want to receive notifications about the conflicts found.
ACCOUNT_LABEL: Label your accounts to ensure that you do not check more than 50 accounts.
CAMPAIGN_LABEL: You can also label campaigns so that the script only checks what you want.
Scheduling: Daily will often be sufficient. If you make many changes to keywords / exclusion words, run the script every hour.
The script
// Copyright 2015, Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
var CONFIG = {
// URL of the spreadsheet template.
// This should be a copy of https://goo.gl/M4HjaH.
SPREADSHEET_URL: 'YOUR_SPREADSHEET_URL',
// Whether to output results to a copy of the above spreadsheet (true) or to
// the spreadsheet directly, overwriting previous results (false).
COPY_SPREADSHEET: false,
// Array of addresses to be alerted via email if conflicts are found.
RECIPIENT_EMAILS: [
'YOUR_EMAIL_HERE'
],
// Label on the accounts to be processed.
// Leave blank to include all accounts.
ACCOUNT_LABEL: '',
// Label on the campaigns to be processed.
// Leave blank to include all campaigns.
CAMPAIGN_LABEL: '',
// Limits on the number of keywords in an account the script can process.
MAX_POSITIVES: 250000,
MAX_NEGATIVES: 50000
};
var REPORTING_OPTIONS = {
// Comment out the following line to default to the latest reporting version.
apiVersion: 'v201809'
};
function main() {
validateConfig();
var accountSelector = AdsManagerApp.accounts();
if (CONFIG.ACCOUNT_LABEL) {
accountSelector = accountSelector
.withCondition('LabelNames CONTAINS "' + CONFIG.ACCOUNT_LABEL + '"');
}
accountSelector.executeInParallel('processAccount', 'processResults');
}
function processAccount() {
return JSON.stringify(findAllConflicts());
}
function processResults(results) {
var hasConflicts = false;
var spreadsheet = SpreadsheetApp.openByUrl(CONFIG.SPREADSHEET_URL);
if (CONFIG.COPY_SPREADSHEET) {
spreadsheet = spreadsheet.copy('Negative Keyword Conflicts');
}
initializeSpreadsheet(spreadsheet);
for (var i = 0; i < results.length; i++) {
if (!results[i].getError()) {
hasConflicts = outputConflicts(spreadsheet, results[i].getCustomerId(),
JSON.parse(results[i].getReturnValue())) || hasConflicts;
} else {
Logger.log('Processing for ' + results[i].getCustomerId() + ' failed.');
}
}
if (hasConflicts && CONFIG.RECIPIENT_EMAILS) {
sendEmail(spreadsheet);
}
}
function findAllConflicts() {
var campaignIds;
if (CONFIG.CAMPAIGN_LABEL) {
campaignIds = getCampaignIdsWithLabel(CONFIG.CAMPAIGN_LABEL);
} else {
campaignIds = getAllCampaignIds();
}
var campaignCondition = '';
if (campaignIds.length > 0) {
campaignCondition = 'AND CampaignId IN [' + campaignIds.join(',') + ']';
}
Logger.log('Downloading keywords performance report');
var query =
'SELECT CampaignId, CampaignName, AdGroupId, AdGroupName, ' +
' Criteria, KeywordMatchType, IsNegative ' +
'FROM KEYWORDS_PERFORMANCE_REPORT ' +
'WHERE CampaignStatus = "ENABLED" AND AdGroupStatus = "ENABLED" AND ' +
' Status = "ENABLED" AND IsNegative IN [true, false] ' +
' ' + campaignCondition + ' ' +
'DURING YESTERDAY';
var report = AdsApp.report(query, REPORTING_OPTIONS);
Logger.log('Building cache and populating with keywords');
var cache = {};
var numPositives = 0;
var numNegatives = 0;
var rows = report.rows();
while (rows.hasNext()) {
var row = rows.next();
var campaignId = row['CampaignId'];
var campaignName = row['CampaignName'];
var adGroupId = row['AdGroupId'];
var adGroupName = row['AdGroupName'];
var keywordText = row['Criteria'];
var keywordMatchType = row['KeywordMatchType'];
var isNegative = row['IsNegative'];
if (!cache[campaignId]) {
cache[campaignId] = {
campaignName: campaignName,
adGroups: {},
negatives: [],
negativesFromLists: [],
};
}
if (!cache[campaignId].adGroups[adGroupId]) {
cache[campaignId].adGroups[adGroupId] = {
adGroupName: adGroupName,
positives: [],
negatives: [],
};
}
if (isNegative == 'true') {
cache[campaignId].adGroups[adGroupId].negatives
.push(normalizeKeyword(keywordText, keywordMatchType));
numNegatives++;
} else {
cache[campaignId].adGroups[adGroupId].positives
.push(normalizeKeyword(keywordText, keywordMatchType));
numPositives++;
}
if (numPositives > CONFIG.MAX_POSITIVES ||
numNegatives > CONFIG.MAX_NEGATIVES) {
throw 'Trying to process too many keywords. Please restrict the ' +
'script to a smaller subset of campaigns.';
}
}
Logger.log('Downloading campaign negatives report');
var query =
'SELECT CampaignId, Criteria, KeywordMatchType ' +
'FROM CAMPAIGN_NEGATIVE_KEYWORDS_PERFORMANCE_REPORT ' +
'WHERE CampaignStatus = "ENABLED" ' +
' ' + campaignCondition;
var report = AdsApp.report(query, REPORTING_OPTIONS);
var rows = report.rows();
while (rows.hasNext()) {
var row = rows.next();
var campaignId = row['CampaignId'];
var keywordText = row['Criteria'];
var keywordMatchType = row['KeywordMatchType'];
if (cache[campaignId]) {
cache[campaignId].negatives
.push(normalizeKeyword(keywordText, keywordMatchType));
}
}
Logger.log('Populating cache with negative keyword lists');
var negativeKeywordLists =
AdsApp.negativeKeywordLists().withCondition('Status = ACTIVE').get();
while (negativeKeywordLists.hasNext()) {
var negativeKeywordList = negativeKeywordLists.next();
var negativeList = {name: negativeKeywordList.getName(), negatives: []};
var negativeKeywords = negativeKeywordList.negativeKeywords().get();
while (negativeKeywords.hasNext()) {
var negative = negativeKeywords.next();
negativeList.negatives.push(
normalizeKeyword(negative.getText(), negative.getMatchType()));
}
var campaigns = negativeKeywordList.campaigns()
.withCondition('Status = ENABLED').get();
while (campaigns.hasNext()) {
var campaign = campaigns.next();
var campaignId = campaign.getId();
if (cache[campaignId]) {
cache[campaignId].negativesFromLists =
cache[campaignId].negativesFromLists.concat(negativeList);
}
}
}
Logger.log('Finding negative conflicts');
var conflicts = [];
// Adds context about the conflict.
var enrichConflict = function(
conflict, campaignId, adGroupId, level, opt_listName) {
conflict.campaignId = campaignId;
conflict.adGroupId = adGroupId;
conflict.campaignName = cache[campaignId].campaignName;
conflict.adGroupName = cache[campaignId].adGroups[adGroupId].adGroupName;
conflict.level = level;
conflict.listName = opt_listName || '-';
};
for (var campaignId in cache) {
for (var adGroupId in cache[campaignId].adGroups) {
var positives = cache[campaignId].adGroups[adGroupId].positives;
var negativeLevels = {
'Campaign': cache[campaignId].negatives,
'Ad Group': cache[campaignId].adGroups[adGroupId].negatives
};
for (var level in negativeLevels) {
var newConflicts =
checkForConflicts(negativeLevels[level], positives);
for (var i = 0; i < newConflicts.length; i++) {
enrichConflict(newConflicts[i], campaignId, adGroupId, level);
}
conflicts = conflicts.concat(newConflicts);
}
var negativeLists = cache[campaignId].negativesFromLists;
var level = 'Negative list';
for (var k = 0; k < negativeLists.length; k++) {
var negativeList = negativeLists[k];
var newConflicts = checkForConflicts(negativeList.negatives, positives);
for (var j = 0; j < newConflicts.length; j++) {
enrichConflict(
newConflicts[j], campaignId, adGroupId, level, negativeList.name);
}
conflicts = conflicts.concat(newConflicts);
}
}
}
return conflicts;
}
function outputConflicts(spreadsheet, customerId, conflicts) {
if (conflicts.length > 0) {
saveConflictsToSpreadsheet(spreadsheet, customerId, conflicts);
Logger.log('Conflicts were found for ' + customerId +
'. See ' + spreadsheet.getUrl());
return true;
} else {
Logger.log('No conflicts were found for ' + customerId + '.');
return false;
}
}
function initializeSpreadsheet(spreadsheet) {
// Make sure the spreadsheet is using the account's timezone.
spreadsheet.setSpreadsheetTimeZone(AdsApp.currentAccount().getTimeZone());
// Clear the last run date on the spreadsheet.
spreadsheet.getRangeByName('RunDate').clearContent();
// Clear all rows in the spreadsheet below the header row.
var outputRange = spreadsheet.getRangeByName('Headers')
.offset(1, 0, spreadsheet.getSheetByName('Conflicts')
.getDataRange().getLastRow())
.clearContent();
}
function saveConflictsToSpreadsheet(spreadsheet, customerId, conflicts) {
// Find the first open row on the Report tab below the headers and create a
// range large enough to hold all of the failures, one per row.
var lastRow = spreadsheet.getSheetByName('Conflicts')
.getDataRange().getLastRow();
var headers = spreadsheet.getRangeByName('Headers');
var outputRange = headers
.offset(lastRow - headers.getRow() + 1, 0, conflicts.length);
// Build each row of output values in the order of the columns.
var outputValues = [];
for (var i = 0; i < conflicts.length; i++) {
var conflict = conflicts[i];
outputValues.push([
customerId,
conflict.negative,
conflict.level,
conflict.positives.join(', '),
conflict.campaignName,
conflict.adGroupName,
conflict.listName
]);
}
outputRange.setValues(outputValues);
spreadsheet.getRangeByName('RunDate').setValue(new Date());
for (var i = 0; i < CONFIG.RECIPIENT_EMAILS.length; i++) {
spreadsheet.addEditor(CONFIG.RECIPIENT_EMAILS[i]);
}
}
function sendEmail(spreadsheet) {
MailApp.sendEmail(CONFIG.RECIPIENT_EMAILS.join(','),
'Negative Keyword Conflicts Found',
'Negative keyword conflicts were found in your ' +
'Google Ads account(s). See ' +
spreadsheet.getUrl() + ' for details. You may wish ' +
'to delete the negative keywords causing the ' +
'the conflicts.');
}
function getCampaignIds(campaigns) {
var campaignIds = [];
while (campaigns.hasNext()) {
campaignIds.push(campaigns.next().getId());
}
return campaignIds;
}
function getAllCampaignIds() {
return getCampaignIds(AdsApp.campaigns().get());
}
function getCampaignIdsWithLabel(labelText) {
var labels = AdsApp.labels()
.withCondition('Name = "' + labelText + '"')
.get();
if (!labels.hasNext()) {
return null;
}
var label = labels.next();
return getCampaignIds(label.campaigns().get());
}
function checkForConflicts(negatives, positives) {
var conflicts = [];
for (var i = 0; i < negatives.length; i++) {
var negative = negatives[i];
var anyBlock = false;
var blockedPositives = [];
for (var j = 0; j < positives.length; j++) {
var positive = positives[j];
if (negativeBlocksPositive(negative, positive)) {
anyBlock = true;
blockedPositives.push(positive.display);
}
}
if (anyBlock) {
conflicts.push({
negative: negative.display,
positives: blockedPositives
});
}
}
return conflicts;
}
function trimKeyword(text, open, close) {
if (text.substring(0, 1) == open &&
text.substring(text.length - 1) == close) {
return text.substring(1, text.length - 1);
}
return text;
}
function normalizeKeyword(text, matchType) {
var display;
var raw = text;
matchType = matchType.toUpperCase();
// Replace leading and trailing "" for phrase match keywords and [] for
// exact match keywords, if it is there.
if (matchType == 'PHRASE') {
raw = trimKeyword(raw, '"', '"');
} else if (matchType == 'EXACT') {
raw = trimKeyword(raw, '[', ']');
}
// Collapse any runs of whitespace into single spaces.
raw = raw.replace(new RegExp('\\s+', 'g'), ' ');
// Keywords are not case sensitive.
raw = raw.toLowerCase();
// Set display version.
display = raw;
if (matchType == 'PHRASE') {
display = '"' + display + '"';
} else if (matchType == 'EXACT') {
display = '[' + display + ']';
}
// Remove broad match modifier '+' sign.
raw = raw.replace(new RegExp('\\s\\+', 'g'), ' ');
return {display: display, raw: raw, matchType: matchType};
}
function hasAllTokens(keywordText1, keywordText2) {
var keywordTokens1 = keywordText1.split(' ');
var keywordTokens2 = keywordText2.split(' ');
for (var i = 0; i < keywordTokens1.length; i++) {
if (keywordTokens2.indexOf(keywordTokens1[i]) == -1) {
return false;
}
}
return true;
}
function isSubsequence(keywordText1, keywordText2) {
return (' ' + keywordText2 + ' ').indexOf(' ' + keywordText1 + ' ') >= 0;
}
function negativeBlocksPositive(negative, positive) {
var isNegativeStricter;
switch (positive.matchType) {
case 'BROAD':
isNegativeStricter = negative.matchType != 'BROAD';
break;
case 'PHRASE':
isNegativeStricter = negative.matchType == 'EXACT';
break;
case 'EXACT':
isNegativeStricter = false;
break;
}
if (isNegativeStricter) {
return false;
}
switch (negative.matchType) {
case 'BROAD':
return hasAllTokens(negative.raw, positive.raw);
break;
case 'PHRASE':
return isSubsequence(negative.raw, positive.raw);
break;
case 'EXACT':
return positive.raw === negative.raw;
break;
}
}
function validateConfig() {
if (CONFIG.SPREADSHEET_URL == 'YOUR_SPREADSHEET_URL') {
throw new Error('Please specify a valid Spreadsheet URL. You can find' +
' a link to a template in the associated guide for this script.');
}
if (CONFIG.RECIPIENT_EMAILS &&
CONFIG.RECIPIENT_EMAILS[0] == 'YOUR_EMAIL_HERE') {
throw new Error('Please either specify a valid email address or clear' +
' the RECIPIENT_EMAILS field.');
}
}
Show whole script!
Loading Comments
The Experts
Tibbe van AstenTeam Lead Performance Marketing
Nils RooijmansWater Cooler Topics
Martijn KraanFreelance PPC Specialist
Bas BaudoinTeamlead SEA @ Happy Leads
Jermaya LeijenDigital Marketing Strategist
How about you?JOIN US!
Caring
Adsscripts.com is all about sharing knowledge. In the current market, PPC specialists like to keep their knowledge and experience to themselves. We're convinced that sharing knowledge can ensure that everyone gets better at their work. We want to change this by sharing our knowledge about scripts with everyone.
Do you also want to contribute? We are open to new ideas and feedback on everything you find on Adsscripts.com.