AdWords is now Google Ads. Our new name reflects the full range of advertising options we offer across Search, Display, YouTube, and more. Learn more

Ads
3K members online now
3K members online now
Dive into advanced features like Remarketing, Flexible Bid Strategies, AdWords Editor, and AdWords Scripts
Guide Me
star_border
Reply

Adwords Scripts Question

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hi guys,

 

I am trying unsuccessfully making work the following script that limits the amount of money to spend when you can’t afford to go over on your budget from Sean Dolan from Pushfire. Any help will be appreciated.

 

 

Source: www.evernote.com/pub/narch2002002/adwordsscripts#st=p&n=5c812f1e-9107-4970-b1ac-b53580feb2ec

 

The script code is:

------------------------------------------------------

var CUTOFF_COST = 10000;
var CUTOFF_LABEL = "Total Spend cutoff";

function main() {
var label = AdWordsApp.labels().withCondition("Name='" + CUTOFF_LABEL + "'").get().next();

if (AdWordsApp.currentAccount().getStatsFor("THIS_MONTH").getCost() > CUTOFF_COST) {
var campaignIterator = label.campaigns().get();

while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
campaign.pause();
}
}
}

------------------------------------------------------

And my error:

Cannot retrieve the next object: iterator has reached the end. (line 5)

 

Thanks 

1 Expert replyverified_user
1 ACCEPTED SOLUTION

Accepted Solutions
Marked as Best Answer.
Solution
Accepted by topic author Enrique David G
March 2016

Re: Adwords Scripts Question

Top Contributor
# 2
Top Contributor

Hi @Enrique David G it's hard to know what's causing that error without seeing the Account it's running against, but if I explain what the error means, that might help you track the problem down.

 

The error "Cannot retrieve the next object: iterator has reached the end." is being generated by the line that beings "var label=" and what it's saying is that you've asked for the "next" object in a set of objects when there isn't one to fetch, it's like having a box of six eggs and asking for a seventh egg.  While this situation can exist when the set of objects is not zero (as in the egg example) in scripting the most common cause of this message is a failure to retrieve any objects (you're asking for an egg when the box started off empty).  When using any selector, it's usually a good idea not to ask for the next object unless you're sure there is one, so rather than this line:

 

var label = AdWordsApp.labels().withCondition("Name='" + CUTOFF_LABEL + "'").get().next();

you'd use coding like this:

var label = AdWordsApp.labels().withCondition("Name='" + CUTOFF_LABEL + "'").get();
while(label.hasNext()) {
var thisLabel = label.next();

//insert your code to work on "thisLabel"

}

This will prevent the error occurring, as the while loop will only run while a next object exists in the selector but it doesn't solve the original problem - why wasn't there another object?

 

Looking at the code, I'd say it's likely your problem is the label name itself.  You're asking for a label with the name "Total Spend cutoff" and there's lots of opportunities for typos there.  I'd make a small wager that the Label in the Account itself either has different capitalisation, or spaces, or is possibly mispelled in some way.  Check the Label in the Account and compare it to the name you've got for CUTOFF_LABEL.

 

As a matter of interest, is this really how you want the script to work?  I would have thought it more useful to allow Campaigns to run all month, controlling daily budgets so the total in a month doesn't exceed your limit.  Pausing Campaigns in the way you have here will mean your Ads won't run at all for some days towards the end of every month, which is not an ideal way to capture customers...

 

Jon

AdWords Top Contributor Google+ Profile | Partner Profile | AdWords Audits

View solution in original post

Marked as Best Answer.
Solution
Accepted by topic author Enrique David G
March 2016

Re: Adwords Scripts Question

Top Contributor
# 2
Top Contributor

Hi @Enrique David G it's hard to know what's causing that error without seeing the Account it's running against, but if I explain what the error means, that might help you track the problem down.

 

The error "Cannot retrieve the next object: iterator has reached the end." is being generated by the line that beings "var label=" and what it's saying is that you've asked for the "next" object in a set of objects when there isn't one to fetch, it's like having a box of six eggs and asking for a seventh egg.  While this situation can exist when the set of objects is not zero (as in the egg example) in scripting the most common cause of this message is a failure to retrieve any objects (you're asking for an egg when the box started off empty).  When using any selector, it's usually a good idea not to ask for the next object unless you're sure there is one, so rather than this line:

 

var label = AdWordsApp.labels().withCondition("Name='" + CUTOFF_LABEL + "'").get().next();

you'd use coding like this:

var label = AdWordsApp.labels().withCondition("Name='" + CUTOFF_LABEL + "'").get();
while(label.hasNext()) {
var thisLabel = label.next();

//insert your code to work on "thisLabel"

}

This will prevent the error occurring, as the while loop will only run while a next object exists in the selector but it doesn't solve the original problem - why wasn't there another object?

 

Looking at the code, I'd say it's likely your problem is the label name itself.  You're asking for a label with the name "Total Spend cutoff" and there's lots of opportunities for typos there.  I'd make a small wager that the Label in the Account itself either has different capitalisation, or spaces, or is possibly mispelled in some way.  Check the Label in the Account and compare it to the name you've got for CUTOFF_LABEL.

 

As a matter of interest, is this really how you want the script to work?  I would have thought it more useful to allow Campaigns to run all month, controlling daily budgets so the total in a month doesn't exceed your limit.  Pausing Campaigns in the way you have here will mean your Ads won't run at all for some days towards the end of every month, which is not an ideal way to capture customers...

 

Jon

AdWords Top Contributor Google+ Profile | Partner Profile | AdWords Audits

Re: Adwords Scripts Question

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭
Hi Jon,

Thank you for your feedback. My intention was testing different scripts and this one in particular to see if in certain situations could work. Thanks

Adwords Scripts Question

Visitor ✭ ✭ ✭
# 4
Visitor ✭ ✭ ✭

I had a similar challenge, the issue is WITH THE LABEL NAME. The "Total Spend cutoff" is a sample name, you ought to change it.

Use this code to check all the labels in the account.

 

function getAllLabels() {
 
var labelIterator = AdWordsApp.labels().get();

 
while (labelIterator.hasNext()) {
   
var label = labelIterator.next();
   
Logger.log(label.getName());
 
}
}

...To make sure a label with the name "Total Spend cutoff" exists, or create it or use the appropriate one from your list.