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
4.4K members online now
4.4K members online now
Dive into advanced features like Remarketing, Flexible Bid Strategies, AdWords Editor, and AdWords Scripts
Guide Me
star_border
Reply

adword script help

[ Edited ]
Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

So I have this script that will pause the ads that are landing on an out of stock page or 404. It works by scanning a phrase and pausing any ads that land on the page with that certain phrase. The phrase can be out of stock or not in inventory. I'm trying to reverse this to instead of pausing the ad when it find a certain phrase it will activate it. I tried changing the pause to activate but doesn't work.

 

 

 

/************************************
* Item Out Of Stock Checker
* Version 1.1
* ChangeLog v1.1 - Filtered out deleted Campaigns and AdGroups
* Created By: Russ Savage
* FreeAdWordsScripts.com
***********************************/
var URL_LEVEL = 'Ad'; // or Keyword
var ONLY_ACTIVE = true; // set to false for all ads or keywords
var CAMPAIGN_LABEL = ''; // 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 = true; // set this to true if you use a 3rd party like Marin or Kenshoo for managing you account
// This is the specific text to search for 
// on the page that indicates the item 
// is out of stock.
var OUT_OF_STOCK_TEXT = 'The Text That Identifies An Out Of Stock Item Goes Here';
 
function main() {
  var alreadyCheckedUrls = {};
  var iter = buildSelector().get();
  while(iter.hasNext()) {
    var entity = iter.next();
   var url = cleanUrl(entity.urls().getFinalUrl());
    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;
      }
      if(htmlCode.indexOf(OUT_OF_STOCK_TEXT) >= 0) {
        alreadyCheckedUrls[url] = 'out of stock';
        entity.pause();
      } else {
        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').withCondition('Status = ENABLED');
    if(URL_LEVEL !== 'Ad') {
      selector = selector.withCondition('AdGroupStatus = ENABLED');
    }
  }
  if(CAMPAIGN_LABEL) {
    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("','")+"']");
  }
  return selector;
}

 

1 Expert replyverified_user
1 ACCEPTED SOLUTION

Accepted Solutions
Marked as Best Answer.
Solution
Accepted by CassieH (Community Manager)
January 2016

Re: adword script help

Visitor ✭ ✭ ✭
# 2
Visitor ✭ ✭ ✭
never mind I found it

you change var ONLY_ACTIVE = true; // set to false for all ads or keywords to false and then change entity pause in line 26 & 40 to enable

View solution in original post

Marked as Best Answer.
Solution
Accepted by CassieH (Community Manager)
January 2016

Re: adword script help

Visitor ✭ ✭ ✭
# 2
Visitor ✭ ✭ ✭
never mind I found it

you change var ONLY_ACTIVE = true; // set to false for all ads or keywords to false and then change entity pause in line 26 & 40 to enable

Re: adword script help

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

I get the following notification: "One of the conditions in the query is invalid. (line 20)". The line 20 looks as follow: "var iter = buildSelector().get();". Do you have any idea what the problem is?

Best,
Adam

Re: adword script help

Top Contributor
# 4
Top Contributor

Hi @Adam G I'll take a bet that it's the condition "DELETED".  AdWords changed its terminology a while back and that condition should now be "REMOVED".  Take a look through your code and replace every occurrence of DELETED with REMOVED and you should be good to go...

 

Jon

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

Re: adword script help

Visitor ✭ ✭ ✭
# 5
Visitor ✭ ✭ ✭
Hi @Jon_Gritton thank you for your response! Unfortunately it did not help...
Best,
Adam

Re: adword script help

Top Contributor
# 6
Top Contributor

Hmm.  I can't see anything obviously wrong (the change to REMOVED is correct, by the way, you should keep that).  The only other thing that might be a problem is if no Campaigns are being selected.  The line near the end:

selector = selector.withCondition("CampaignName IN ['"+campaignNames.join("','")+"']");

would then fail.  Try putting in a Logger line to check that there are actually some values in campaignNames

while(campIter.hasNext()) {
      campaignNames.push(campIter.next().getName());
    }
Logger.log("Any Campaigns?: " + campaignNames);
    selector = selector.withCondition("CampaignName IN ['"+campaignNames.join("','")+"']");

In the Logs tab you should see a line containing the selected Campaign names.  If there's nothing after the "Any Campaigns?:" there's your problem.  Check you have the Label applied (if you're using it).

 

To help you debug yourself, the error generated is saying there's a problem with one of the selection criteria in that function at the end of the script.  This would usually be either a syntax error (a problem with the code itself) or a problem with how the selectors are built (i.e., not having any selected Campaign names).  Since the script apparently worked for the original poster, I don't think it's a syntax error.

 

Jon

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

Re: adword script help

Visitor ✭ ✭ ✭
# 7
Visitor ✭ ✭ ✭
Hi Jon,

thank you very much for your answer! I will take a look later.

Best,
Adam

Re: adword script help

Visitor ✭ ✭ ✭
# 8
Visitor ✭ ✭ ✭
This is my code and it does not work:

/************************************
* Item Out Of Stock Checker
* Version 1.1
* ChangeLog v1.1 - Filtered out deleted Campaigns and AdGroups
* Created By: Russ Savage
* FreeAdWordsScripts.com
***********************************/
var URL_LEVEL = 'Ad'; // or Keyword
var ONLY_ACTIVE = true; // set to false for all ads or keywords
var CAMPAIGN_LABEL = 'Marken 1'; // 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 = true; // set this to true if you use a 3rd party like Marin or Kenshoo for managing you account
// This is the specific text to search for
// on the page that indicates the item
// is out of stock.
var OUT_OF_STOCK_TEXT = 'out_of_stock';

function main() {
var alreadyCheckedUrls = {};
var iter = buildSelector().get();
while(iter.hasNext()) {
var entity = iter.next();
var url = cleanUrl(entity.urls().getFinalUrl());
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;
}
if(htmlCode.indexOf(OUT_OF_STOCK_TEXT) >= 0) {
alreadyCheckedUrls[url] = 'out of stock';
entity.pause();
} else {
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 != REMOVED').withCondition('AdGroupStatus != REMOVED');
if(ONLY_ACTIVE) {
selector = selector.withCondition('CampaignStatus = ENABLED').withCondition('Status = ENABLED');
if(URL_LEVEL !== 'Ad') {
selector = selector.withCondition('AdGroupStatus = ENABLED');
}
}
if(CAMPAIGN_LABEL) {
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("','")+"']");
}
return selector;
}

Do you maybe see where I did a mistake?

Best,
Adam

Re: adword script help

Visitor ✭ ✭ ✭
# 9
Visitor ✭ ✭ ✭
Hello,

I get the following response by testing the script:

"Value REMOVED is invalid. Please check spelling and casing and verify the value's type matches the field's type. (line 21)"

Has the spelling changed again?

Best,
Adam

Re: adword script help

Top Contributor
# 10
Top Contributor

Hi @Adam G well, according to the API documentation, it should be REMOVED (and that was a change made some time ago), see here:

 

https://developers.google.com/adwords/scripts/docs/reference/adwordsapp/adwordsapp_adselector#withCo...

 

(scroll down to the Columns section, and see the enumeration values for "AdGroupStatus" and "CampaignStatus").  However, I've been testing the script (and my own tests) and it seems that AdGroupStatus has a problem with "REMOVED" and is still using "DELETED".  (CampaignStatus works fine with REMOVED).  I've had a (very) quick look and I can't see any other reports of this problem, so it may be either something new (in the latest release of the API) or something no one's spotted before.

 

I'll look into it.  In the meantime, if you change the line:

selector = selector.withCondition('CampaignStatus != REMOVED').withCondition('AdGroupStatus != REMOVED');

to read:

selector = selector.withCondition('CampaignStatus != REMOVED').withCondition('AdGroupStatus != DELETED');

it should work fine.

 

Jon

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