Currently the script below helps to find duplicate keywords, but if i'm not wrong it is doing it according to case sensitive which means that if the keyword business to business is available in campaign 1 and campaign 7 it would be listed. However, i tested it by changing campaign 1's keyword to Business to business and campaign 7 business to business, it doesn't show up. May i know what steps to take ensure that i eliminate case sensitiveness so even if one is Business and one is business i still want it displayed in my spreadsheet. Thank you for your time. The script which i mentioned is below.
function main() {
var SHEET_NAME = 'Dup'; //variable for sheet name // Change according to your Sheet Name
var ss = SpreadsheetApp.openByUrl(SPREADSHEET_URL); //opens the spreadsheet by URL
var sheet = ss.getSheetByName(SHEET_NAME); //gets the sheet using the SHEET_NAME variable
sheet.appendRow(['Duplicate Keyword','No. of Campaigns ' ,'Campaign Name', 'Bid-Price' ,'Campaign Labels' ,'Keyword match type']); //populates row with string keyword
// helps to select which campaign type to look for duplicate keywords
var campaignIds = [];
var campaigns = AdWordsApp.campaigns().withCondition("AdvertisingChannelType = SEARCH").get(); // **change this portion according to which channel type you would like to search duplicate keywords for
while(campaigns.hasNext()){
campaignIds.push(campaigns.next().getId());
}
var keywordIterator = AdWordsApp.keywords().withCondition('CampaignId IN [' + campaignIds + ']').get(); //calls the keywords() object and fetches keywords
var keywordArr = []; //array initialization
while (keywordIterator.hasNext()) { //iterates through the keywords list selected
var keyword = keywordIterator.next(); //returns the next keyword iterator
if (keyword.getCampaign().isEnabled()){ //checks if campaign of the keyword is enabled,
keywordArr.push(keyword); //if enabled, add the keyword for further checking of duplicates
}
}
var duplicates = []; //array initialization
var keywordName =[]; //array initialization
for (var i = 0; i < keywordArr.length; i++) { //iterates until array length > 0
var count = 1; //variable initialization(to help count the number of campaigns)
var deletedDuplicates = ''; //variable initialization
for (var j = i + 1; j < keywordArr.length; ) { //iterates until array length > 0
if (keywordArr[i].getText() == keywordArr[j].getText() && keywordArr[i].getMatchType() == keywordArr[j].getMatchType()) { //compares keywords to check for duplicate keywordText and same match type
count++; //adds 1 to var count
var campaignLabels = getCampaignLabels(keywordArr[j]); //stores the list of labelNames associated in the campaign
//stores the campaign name and bid price in deletedDuplicates variable
deletedDuplicates += keywordArr[j].getCampaign().getName() + ' | bid-Price = ' + keywordArr[j].bidding().getCpc() + ' | Campaign Labels = ' + campaignLabels + ' | Keyword match type = ' + keywordArr[j].getMatchType();;
//splice method wherein j is the integer that specifies at what position to add/remove items and 1 is the number of items to be removed
keywordArr.splice(j, 1); //deletes 1 array at position j
}
else { //if keywords are not the same as well as the keyword match type,
j++; //adds 1 to var j
}
}
//If there are duplicates
if (count > 1) { //if 1 is less than count
keywordName.push(" " +keywordArr[i].getText()); //pushes keyword text, keyword campaign name, bid, and deletedDuplicates variable in duplicates array
var campaignLabels = getCampaignLabels(keywordArr[i]); //creates variable campaignLabels and assings the return value of function getCampaignLabels passing the value of keywordArr[i]
//pushes keyword text, keyword campaign name, bid, and deletedDuplicates variable in duplicates array
duplicates.push(" No. of Campaigns " + count + ' | ' + keywordArr[i].getCampaign().getName() + ' | bid-Price = ' + keywordArr[i].bidding().getCpc() + ' | Campaign Labels = ' + campaignLabels + ' | Keyword match type = ' + keywordArr[i].getMatchType() + " | " + deletedDuplicates);
}
}
for (var i = 0; i < duplicates.length; i++) { //iterates until array length > 0
sheet.appendRow([keywordName[i],duplicates[i]]); //populates spreadsheet with keyword text, campaign names, and bid prices
Logger.log("duplicate keyword = %s, %s ", keywordName[i] ,duplicates[i]); //logs the duplicates array
}
}
//stores the list of labelNames associated in the campaign
function getCampaignLabels(keywordSelected){ //function getCampaignLabels; keywordSelected here is keywordArr[i] called in the main function
var campaignLabelIterator = keywordSelected.getCampaign().labels().get(); //calls the label() object of the campaign where the value of keywordSelected belongs and fetches labels
var campaignLabels = "";//initialize variable campaignLabels and stores label names as String
while (campaignLabelIterator.hasNext()) { //iterates through the labels list selected
var campaignLabel = campaignLabelIterator.next(); //returns the next label iterator
campaignLabels += campaignLabel.getName() + " , "; //concatenates in variable campaignLabels the campaign label names
}
return campaignLabels; //returns the value of variable campaignLabels to the main function
}