Last active
December 28, 2020 11:13
-
-
Save siliconvallaeys/fd6affc8d3ddedd016956fdc5481d294 to your computer and use it in GitHub Desktop.
Automatically add negative placements for any new automatic GDN placements that include a particular piece of text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**************************** | |
* 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!