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

Adwords Script to pause ad groups when out of stock

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hi;

 

I found this great script from Russel to pause/activate ads based on content found on the page (detects stock availibility if a phrase such as "out of stock" is in the page), but it fails after 10k entities.

Can someone help please - how to overcome this limit?

 

var URL_LEVEL = 'Ad'; // or Keyword
var ONLY_ACTIVE = true; // set to false to check keywords or ads in all campaigns (paused and active)
var CAMPAIGN_LABEL = 'CheckDispo'; // set this if you want to only check campaigns with this label
var STRIP_QUERY_STRING = true; // set this to false if the stuff that comes after the question mark is important
var WRAPPED_URLS = false; // set this to true if you use a 3rd party like Marin or Kenshoo for managing you account
// This is the specific text (or texts) to search for
// on the page that indicates the item
// is out of stock. If ANY of these match the html
// on the page, the item is considered "out of stock"
var OUT_OF_STOCK_TEXTS = [
'aucune annonce ne correspond à vos critères de recherches',
'Another string might go here but does not need to'
];

function main() {
var alreadyCheckedUrls = {};
var iter = buildSelector().get();
while(iter.hasNext()) {
var entity = iter.next();
var urls = [];
if(entity.urls().getFinalUrl()) {
urls.push(entity.urls().getFinalUrl());
}
if(entity.urls().getMobileFinalUrl()) {
urls.push(entity.urls().getMobileFinalUrl());
}
for(var i in urls) {
var url = cleanUrl(urls[i]);
if(alreadyCheckedUrls[url]) {
if(alreadyCheckedUrls[url] === 'out of stock') {
entity.pause();
} else {
entity.enable();
}
} else {
var htmlCode;
try {
htmlCode = UrlFetchApp.fetch(url).getContentText();
} catch(e) {
Logger.log('There was an issue checking:'+url+', Skipping.');
continue;
}
var did_pause = false;
for(var x in OUT_OF_STOCK_TEXTS) {
if(htmlCode.indexOf(OUT_OF_STOCK_TEXTS[x]) >= 0) {
alreadyCheckedUrls[url] = 'out of stock';
entity.pause();
did_pause = true;
break;
}
}
if(!did_pause) {
alreadyCheckedUrls[url] = 'in stock';
entity.enable();
}
}
Logger.log('Url: '+url+' is '+alreadyCheckedUrls[url]);
}
}
}

function cleanUrl(url) {
if(WRAPPED_URLS) {
url = url.substr(url.lastIndexOf('http'));
if(decodeURIComponent(url) !== url) {
url = decodeURIComponent(url);
}
}
if(STRIP_QUERY_STRING) {
if(url.indexOf('?')>=0) {
url = url.split('?')[0];
}
}
if(url.indexOf('{') >= 0) {
//Let's remove the value track parameters
url = url.replace(/\{[0-9a-zA-Z]+\}/g,'');
}
return url;
}

function buildSelector() {
var selector = (URL_LEVEL === 'Ad') ? AdWordsApp.ads() : AdWordsApp.keywords();
selector = selector.withCondition('CampaignStatus != DELETED').withCondition('AdGroupStatus != DELETED');
if(ONLY_ACTIVE) {
selector = selector.withCondition('CampaignStatus = ENABLED');
if(URL_LEVEL !== 'Ad') {
selector = selector.withCondition('AdGroupStatus = ENABLED');
}
}
if(CAMPAIGN_LABEL) {
if(AdWordsApp.labels().withCondition("Name = '"+CAMPAIGN_LABEL+"'").get().hasNext()) {
var label = AdWordsApp.labels().withCondition("Name = '"+CAMPAIGN_LABEL+"'").get().next();
var campIter = label.campaigns().get();
var campaignNames = [];
while(campIter.hasNext()) {
campaignNames.push(campIter.next().getName());
}
selector = selector.withCondition("CampaignName IN ['"+campaignNames.join("','")+"']");
} else {
Logger.log('WARNING: Campaign label does not exist: '+CAMPAIGN_LABEL);
}
}
return selector;
}

 

 

 

2 Expert replyverified_user
1 ACCEPTED SOLUTION

Accepted Solutions
Marked as Best Answer.
Solution
Accepted by topic author Amandine R
August 2016

Re: Adwords Script to pause ad groups when out of stock

Visitor ✭ ✭ ✭
# 5
Visitor ✭ ✭ ✭
A solution could be by means of Labels. Your script once gets a keyword, it adds the Label "OUT_STOCK_CHECKED". This label is included in the selector in order that the keywords with this label will not be reviewed again. This script that runs each hour, it is adding this Label to all the keywords. The script should included an extra condition in order to remove all these Labels "OUT_STOCK_CHECKED" once all the keywords have this Label.

Wrt the first reason you can write the log into a spreadsheet, the problem is that it is time consuming
https://developers.google.com/adwords/scripts/docs/examples/spreadsheetapp

Related to the second reason, my only solution is to use executionParallel, you have an example here
https://developers.google.com/adwords/scripts/docs/features/mcc

Best.
David.

View solution in original post

Re: Adwords Script to pause ad groups when out of stock

Visitor ✭ ✭ ✭
# 2
Visitor ✭ ✭ ✭
Good afternoon Amandine,

Probably the reason is: https://developers.google.com/adwords/scripts/docs/limits

A single selector can handle at most 10,000 IDs in selector.withIds(). If 10,000 or more IDs are specified, selector.get() will throw a runtime error. Similarly, specifying an Id IN [LIST] condition with a list of IDs > 10,000 will result in a runtime error.

A solution could be work with Google API in instead of Adwords Scripts.
Another more easy solution could be to replicate the script N-times and in the selector to filter by country,products,...

Re: Adwords Script to pause ad groups when out of stock

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭
Thanks David, the script stops for 2 reasons:
1- Logs truncated: length exceeded limit of 100000.--> though I have only 1,500 URLs to check
2 - Exceeded maximum execution time

Filtering by product etc would not be an ption if it can not even check 2,000 URLs.

How can we change the code to make it running more times, taking it up to the last check it has done? I can not wirk with the API at that moment...

Best
Amandine

Re: Adwords Script to pause ad groups when out of stock

Top Contributor
# 4
Top Contributor
Marked as Best Answer.
Solution
Accepted by topic author Amandine R
August 2016

Re: Adwords Script to pause ad groups when out of stock

Visitor ✭ ✭ ✭
# 5
Visitor ✭ ✭ ✭
A solution could be by means of Labels. Your script once gets a keyword, it adds the Label "OUT_STOCK_CHECKED". This label is included in the selector in order that the keywords with this label will not be reviewed again. This script that runs each hour, it is adding this Label to all the keywords. The script should included an extra condition in order to remove all these Labels "OUT_STOCK_CHECKED" once all the keywords have this Label.

Wrt the first reason you can write the log into a spreadsheet, the problem is that it is time consuming
https://developers.google.com/adwords/scripts/docs/examples/spreadsheetapp

Related to the second reason, my only solution is to use executionParallel, you have an example here
https://developers.google.com/adwords/scripts/docs/features/mcc

Best.
David.

Re: Adwords Script to pause ad groups when out of stock

Top Contributor
# 6
Top Contributor

Hi @Amandine R @David C's suggestions are good ones, I'll just expand a little.


Firstly, the truncated logs are not a problem, this "error" only affects the actual logging, not the running of the script itself.  So a script with truncated logs has still run correctly.  If you're not using the logs, or not all the lines being logged, it's worth removing these from the script as they can slow execution time.

 

As @David C said, Labels are a good way to deal with script timeout errors; I'd label each URL successfully checked, then, at midnight, remove all these Labels (this will be a lot quicker than checking whether all entries have been labelled).

 

What system are you using to manage your products?  Most back ends will provide the option to produce a merchant feed for Shopping - or at least some kind of output - and rather than checking the site directly (which is slow, as you've found) it might be more efficient to check this output from the back end in a file somewhere...

 

Jon

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

Re: Adwords Script to pause ad groups when out of stock

Visitor ✭ ✭ ✭
# 7
Visitor ✭ ✭ ✭
Hi

many thanks @Jon_Gritton and @David C'.

The label option coud be a good one - I need to dig into this further since I am quite new to scripts (if you have a reference, this is welcome Smiley Happy.

Regarding the merchant center: I do not run shopphing camapigns but adwords for product aggregator (we do nto sell). however I could use our internal feed to know the product availability and pause/un pause ad groups accordingly. i am trying out the Adwords script "Inventory Management" to trying to achieving this.

best
Amandine


Adwords Script to pause ad groups when out of stock

Visitor ✭ ✭ ✭
# 8
Visitor ✭ ✭ ✭

Hi Amandine,

 

Could you send me your new scripts, please? 

 

I have read all the replies above but I'm still stuck with "Log". I don't know about "Log" and have no ability to edit the scripts.

 

Kindly thanks in advance,

Alex