Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Automatically add negative placements for any new automatic GDN placements that include a particular piece of text
/****************************
* Add a Placement Exclusion When an Automatic Placement Contains the Text ...
* Version 1.0
*
* Created By: Frederick Vallaeys
* for FreeAdWordsScripts.com
* at the request of an Optmyzr.com subscriber
****************************/
function main() {
var currentSetting = new Object();
// The list of text strings to check in placement domains. If the placement domain contains ANY of these,
// we WILL attempt to exclude it from the campaign
currentSetting.stringsToExclude = new Array(
"pets",
".org"
)
// -- Most users should not have to edit anything after this
currentSetting.dateRange = "LAST_30_DAYS";
var DEBUG = 0;
currentSetting.matchedCampaigns = new Array();
currentSetting.reportType = "AUTOMATIC_PLACEMENTS_PERFORMANCE_REPORT";
currentSetting.metricsColumns = ['DisplayName',
'Domain',
'IsPathExcluded',
'CampaignName',
'CampaignId',
'AdGroupName',
'AdGroupId',
'Impressions',
'Clicks',
'Cost',
'CostPerConversion'
];
currentSetting.whereConditions = ['WHERE',
'Impressions', '>=', 1
];
var columnsUsedArray = currentSetting.metricsColumns;
var columnsStr = currentSetting.metricsColumns.join(',');
if(currentSetting.whereConditions.length > 0) {
var whereClause = currentSetting.whereConditions.join(" ");
} else {
var whereClause = '';
}
var query = 'SELECT ' + columnsStr + ' ' +
'FROM ' + currentSetting.reportType + ' ' +
whereClause + ' ' +
'DURING ' + currentSetting.dateRange;
//Logger.log("query: " + query);
var report = AdWordsApp.report(query);
var reportIterator = report.rows();
var numResults = 0;
while(reportIterator.hasNext()){
var row = reportIterator.next();
var displayName = row['DisplayName'];
var domain = row['Domain'];
var campaignId = row['CampaignId'];
var isPathExcluded = row['IsPathExcluded'];
// Only add negative placements that aren't already excluded
if(isPathExcluded == "false") {
var result = containsAny(displayName, currentSetting.stringsToExclude);
if(result) {
if(DEBUG) Logger.log(displayName + " - " + domain);
if(!currentSetting.matchedCampaigns[campaignId]) {
currentSetting.matchedCampaigns[campaignId] = new Array();
}
currentSetting.matchedCampaigns[campaignId].push(domain);
}
}
}
for(var campaignId in currentSetting.matchedCampaigns) {
var campaignExcludedPlacementCounter = 0;
if(DEBUG) Logger.log("Campaign ID: " + campaignId);
var campaignIdArray = new Array(campaignId);
var campaign = AdWordsApp.campaigns().withIds(campaignIdArray).get().next();
var campaignName = campaign.getName();
Logger.log("For Campaign: " + campaignName);
var matchedDomains = currentSetting.matchedCampaigns[campaignId];
for(var i=0; i<matchedDomains.length;i++) {
campaignExcludedPlacementCounter++;
var domain = matchedDomains[i];
Logger.log(" " + campaignExcludedPlacementCounter + ") excluding the domain: " + domain);
var excludedPlacement = campaign.display().newPlacementBuilder().withUrl(domain).exclude().getResult();
}
Logger.log(" -> " + campaignExcludedPlacementCounter + " negative placements added to campaign " + campaignName + ".");
Logger.log("");
}
function containsAny(str, substrings) {
for (var i = 0; i != substrings.length; i++) {
var substring = substrings[i];
if (str.indexOf(substring) != - 1) {
return substring;
}
}
return null;
}
}
@blaxc

This comment has been minimized.

Copy link

@blaxc blaxc commented Nov 12, 2018

Hey man, any idea how to include only selected campaigns?

I did this -

currentSetting.whereConditions = ['WHERE',
'Impressions', '>=', 1, 'AND', 'CampaignId', '=', 'XXXXXXXXX'
];

But I want to include more than one campaign. Please advise!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment