Skip to content

Instantly share code, notes, and snippets.

@alewolf
Last active May 5, 2020 20:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alewolf/e6fcecfba548c765a4ad52d46abcd81e to your computer and use it in GitHub Desktop.
Save alewolf/e6fcecfba548c765a4ad52d46abcd81e to your computer and use it in GitHub Desktop.
AdWords Script: Switch all broad match keywords to modified broad match
// Title: Modified Broad Match
// Descritpion: Switch all broad match keywords to modified broad match
// Author: Wolf+Bär Agency, Aleksandar Vucenovic
// License: GNU GPLv3
// Version: 0.3
// URL: https://gist.github.com/alewolf/e6fcecfba548c765a4ad52d46abcd81e
// URL:
// START Settings
var PLUS_BROAD_MATCH_LABEL = 'plus_broad_match_done';
// END Settings
function main() {
// Check if the label for this script already exists in the account.
// If not create it.
checkLabel(PLUS_BROAD_MATCH_LABEL);
// Run the Plus Broad Match function
transformPlusBroadMatch();
}
// Transform all common broad match keywords into plus broad match
function transformPlusBroadMatch(){
// Select all broad match keywords that have not been processed in an earlier run
var keywordSelector = AdWordsApp
.keywords()
.withCondition("CampaignStatus = ENABLED")
.withCondition("AdvertisingChannelType = SEARCH")
.withCondition("AdGroupStatus = ENABLED")
.withCondition("Status = ENABLED")
.withCondition("KeywordMatchType = BROAD")
.withCondition("LabelNames CONTAINS_NONE [ " + PLUS_BROAD_MATCH_LABEL + " ]")
.forDateRange("ALL_TIME");
// Get the KeywordIterater for the selection
var keywordIterator = keywordSelector.get();
// Create a list for of applyLabel operations to be executed after the new keywords have been build
// (for performance reasons)
var operations = [];
// Create a list of all keywords that are being created
var newKeywordArray = [];
// Iterate through the keyword list
while (keywordIterator.hasNext()) {
// Get the next keyword
var keyword = keywordIterator.next();
//Get the keyword text
var originalKeywordText = keyword.getText();
// Apply the Plus Broad Match fix to the keyword
var newKeywordText = applyMBMfix(originalKeywordText);
// Logger.log("Original Keyword = " + originalKeywordText);
// Check if the old and the new keyword are the same
// If they are equal, continue with next keyword, if not, fix
if( (originalKeywordText != newKeywordText) ){
// Logger.log("New Keyword = " + newKeywordText);
// Pause the old keyword
keyword.pause();
// Logger.log('building new keyword');
// Logger.log('old keyword text = ' + originalKeywordText);
// Logger.log('new keyword text = ' + newKeywordText);
// Logger.log('ad group name = ' + keyword.getAdGroup().getName());
// Logger.log('campaign name = ' + keyword.getCampaign().getName());
// Logger.log(' ');
// We want to check if the new keyword maybe already exists in the ad group
// and has been processed in an earlier run.
var newKeywordSelector = AdWordsApp
.keywords()
.withCondition('CampaignName="' + keyword.getCampaign().getName() + '"')
.withCondition('AdGroupName="' + keyword.getAdGroup().getName() + '"')
.withCondition('Text="' + newKeywordText + '"');
// Get the iterator for this new selection
var newKeywordIterator = newKeywordSelector.get();
// Only continue building the new keyword if we don't find the new keyword in the iterator (keyword exists in that ad group)
// and only continue if the keyword has not been created earlier in the while loop.
// (That can happen if we process '++example' and '+++example'.
if (! newKeywordIterator.hasNext() && (newKeywordArray.indexOf(newKeywordText) == -1)){
// we now know that this keyword doesn't exists and act accordingly
// Logger.log('creating keyword: ' + newKeywordText);
// create new keyword in same ad group with newKeywordText
var keywordOperation = keyword.getAdGroup()
.newKeywordBuilder()
.withText(newKeywordText)
.build();
operations.push( keywordOperation );
// Push the new keyword into an array,
// so that we can check later in the loop if we already created this keyword.
newKeywordArray.push(newKeywordText);
} else {
// Logger.log('omitting keyword as it already exists');
}
} else {
// If the keyword does not need to be fixed apply a label in order to omit it in the next run
keyword.applyLabel(PLUS_BROAD_MATCH_LABEL);
} // end if
} // end while
//Logger.log( 'operations length = ' + operations.length);
// Check in the keyword operations list if the keywords have been created and apply the label
for (var i = 0; i < operations.length; i++) {
var newKeyword = operations[i].getResult();
newKeyword.applyLabel(PLUS_BROAD_MATCH_LABEL);
}
}
// Take the input keyword and transform it into a modified broad match keyword
function applyMBMfix(TextToFix){
//Logger.log("TextToFix = " + TextToFix);
// Create variable for the new keyword
var fixedText;
// replace all pluses with spaces
fixedText = TextToFix.replace(/\+/g, " ");
// replace all multiple spaces with a single space
fixedText = fixedText.replace(/\s\s+/g, ' ');;
// remove all spaces in front of the keyword
fixedText = fixedText.replace(/^ /g, "");
// add a plus at the beginning of the keyword
fixedText = "+" + fixedText;
// change all spaces into space_pluses
fixedText = fixedText.replace(/ /g, " +");
// Logger.log("fixedText = " + fixedText);
// Return the new keyword
return fixedText;
}
// Check if the label exists in the account.
// If not, create it
function checkLabel(label_to_check) {
// Logger.log("check if label exists");
// Get a list of all labels with the set label name
var labelIterator = AdWordsApp.labels()
.withCondition('Name = ' + label_to_check)
.get();
// Logger.log( "labelIterator.int = " + labelIterator.totalNumEntities());
// Check if the label name is in the list
// If not create it
if ( labelIterator.totalNumEntities() == 0 ) {
// Logger.log("label doesn't exist -> create");
// Create the label
createLabel(label_to_check);
}
}
// Create the label
function createLabel(label_to_create){
// Logger.log("creating label: " + label_to_create);
AdWordsApp.createLabel(label_to_create);
// Logger.log("label created");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment