Using Script for Adding Labels to Ads

42 views
Skip to first unread message

kalipa...@gmail.com

unread,
Feb 6, 2015, 12:38:52 AM2/6/15
to adwor...@googlegroups.com

Hi,

I want to add labels to my ads. I tried using this script I found from {http://white.net/blog/label-keywords-ads-bulk-adwords-script/}


function main() {
// Applies labels to specified ads, based on a Google Doc Spreadsheet.
// From http://white.net/blog/label-keywords-ads-bulk-adwords-script//Change this to your spreadsheet's URL!
var spreadsheetUrl = "https://docs.google.com/spreadsheets/d/1-eH8OS0vVxUrs0r9NtEDoUKeWG21JjX1vD-VfK7TVpo/edit#gid=0";
var inputSheet = SpreadsheetApp.openByUrl(spreadsheetUrl).getActiveSheet(); //The sheet in the Google Doc
var i = 2; //Integer for the loop
var numberOfLabels = inputSheet.getLastRow() //Number of rows in the sheet, which is the number of ad groups that want to be labeled
var lastLabelApplied = ""; //Records the label that was last applied, so if the next label is the same the Script knows it doesn't have to create it again
while (i <= numberOfLabels) //This loops through all the rows
{
if (inputSheet.getRange("I"+i).getValue() == "") //If there are no notes for the current row
{
var labelCampaignName = inputSheet.getRange("A" + i).getValue();
var labelAdGroupName = inputSheet.getRange("B" + i).getValue();
var labelHeadline = inputSheet.getRange("C" + i).getValue();
var labelDescriptionLine1 = inputSheet.getRange("D" + i).getValue();
var labelDescriptionLine2 = inputSheet.getRange("E" + i).getValue();
var labelDisplayURL = inputSheet.getRange("F" + i).getValue();
var labelDestinationURL = inputSheet.getRange("G" + i).getValue();
var labelText = inputSheet.getRange("H" + i).getValue();
var printToSpreadsheet = "-"
//This is a variable that records what will be noted in the spreadsheet for each ad group (whether the label has been added, or there was an error)
var adIterator = AdWordsApp.ads() //Finds the ads with the specified name and campaign name
.withCondition("CampaignName = '" + labelCampaignName + "'")
.withCondition("AdGroupName = '" + labelAdGroupName +"'")
.withCondition("Headline = '" + labelHeadline +"'")
.withCondition("Description1 = '" + labelDescriptionLine1 +"'")
.withCondition("Description2 = '" + labelDescriptionLine2 +"'")
.withCondition("DisplayUrl = '" + labelDisplayURL +"'")
.withCondition("DestinationUrl = '" + labelDestinationURL +"'")
.get();
if (adIterator.hasNext()) //If there is a keyword then the label will be applied
{
var ad = adIterator.next();
if (labelText != lastLabelApplied)
//If this label isn't the same as the last label to be applied, then the label is created.
//If the label was the same as the last label to be applied we know the label already exists, so we can skip this
{
AdWordsApp.createLabel(labelText); //Creates the label - if the label already exists then there may be an error, but the Script will continue
}
ad.applyLabel(labelText); //Applies the label to the keyword
printToSpreadsheet = "Done";
lastLabelApplied = labelText; //Records the last applied label
}
else
{
//This means the keyword iterator was empty, so there wasn't a keyword of the right name (or a campaign or a group of the right name).
printToSpreadsheet = "Ad not found"; //An error message will be recorded in the spreadsheet.
}
inputSheet.getRange("I" + i).setValue(printToSpreadsheet); //Writes into the Notes column in the spreadsheet
Logger.log("Label " + labelText + " applied to '" + labelHeadline + "' in '" + labelAdGroupName + "', '"+ labelCampaignName +"': " + printToSpreadsheet); //Also writes into the Log
}//End 'if there are notes'
i = i+1;
}//End while loop
}

And I got this result. 
https://lh3.googleusercontent.com/-d0yPFHLdRoI/VNRSEXn9MSI/AAAAAAAAAAk/gI5yhvSn0qY/s1600/ad%2Blabel%2Btest.JPG

Ads from 1-20 are successfully labeled but ads from 21 and beyond is returning a result " ad not found"

I tried checking my campaign, ad group, and ads, It is all good so I don't understand why it cannot find my ads.

Please help me, what do I need to do. 

Josh Radcliff (AdWords API Team)

unread,
Feb 9, 2015, 10:22:24 AM2/9/15
to adwor...@googlegroups.com
Hi,

This group is for the AdWords API. For AdWords Scripts questions, please post to the AdWords Scripts group.

Thanks,
Josh, AdWords API Team
Reply all
Reply to author
Forward
0 new messages