Out of Stock Script - Change Campaign Label to Keyword Label

79 views
Skip to first unread message

Galina Georgieva

unread,
May 25, 2021, 11:45:53 AM5/25/21
to Google Ads Scripts Forum
Hi, 

I have this script for pausing/enabling keywords for items that are out of stock. Currently the script is based on campaign label. What I want is to use Keyword Label instead as I have a lot of campaigns that contain both category/product URLs and currently the script also pauses keywords with category landing pages when only one of the items is out of stock?
Shouldn't be too complicated to change? 

Thank you.

Galina


/************************************

* Item Out Of Stock Checker

* Version 1.2

* ChangeLog v1.2

* - ONLY_ACTIVE is used to filter campaigns and adgroups only. All Keywords and Ads in the AdGroups will

* be checked which solves the "once disabled, always disabled" issue.

* - Updated call to get the Final Urls. Now calls getFinalUrl and getMobileFinalUrl instead of getDestinationUrl

* - OUT_OF_STOCK_TEXTS can now contain multiple things to check for.

* - If the CAMPAIGN_LABEL does not exist, it is ignored with a warning.

* 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 to check keywords or ads in all campaigns (paused and active)

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 (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 = [

'The Text That Identifies An Out Of Stock Item Goes Here',

'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;

}  

Google Ads Scripts Forum Advisor

unread,
May 25, 2021, 11:19:25 PM5/25/21
to adwords...@googlegroups.com
Hi Galina,

Thanks for reaching out. I am Harry from the Google Ads Scripts Teams.

Changing this line to var URL_LEVEL = 'Ad'; to var URL_LEVEL = 'Keyword'; should let you have the script run with Keyword URLs and Labels.

Let me know if there's anything else I can assist you with.

Thanks,
Google Logo
Harry Cliford Rivera
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q2HDYcc:ref

Galina Georgieva

unread,
May 26, 2021, 2:24:12 AM5/26/21
to Google Ads Scripts Forum
Hi Harry,

If I change the URL_Level to keyword instead of Ad, what happens is that it checks the URLs set on keyword level (not on ad level), however it still checks URLS for keywords within Campaigns with the specified label in var CAMPAIGN_LABEL.  What I want is to change the var CAMPAIGN_LABEL to keyword_label so that it only checks the URLS for keywords with this label. 

Thanks

Galina

Google Ads Scripts Forum Advisor

unread,
May 26, 2021, 4:50:04 AM5/26/21
to adwords...@googlegroups.com
Hi Galina,

Thanks for clarifying further on your requirement. I would suggest that you have the URL_LEVEL as Keyword, the CAMPAIGN_LABEL as empty and declare a new variable named KEYWORD_LABEL with the desired keyword label, then add a withCondition method in either the lines below:
  • var iter = buildSelector().get();
  • selector = selector.withCondition("CampaignName IN ['"+campaignNames.join("','")+"']");
    You can add the method like this: .withCondition("LabelNames CONTAINS_ALL ['" + KEYWORD_LABEL + "']") . This should let you filter the KeywordSelector by Labels at Keyword level.

    Kindly try and let me know how it goes.

    Galina Georgieva

    unread,
    May 26, 2021, 8:45:00 AM5/26/21
    to Google Ads Scripts Forum
    Hi Harry,

    Are you able to modify this in the script for me and just paste the whole thing so I can copy it? I have never written scripts before and this is really complicated for me, I don't know what exactly I need to modify or add... I keep on getting errors.. 

    Thanks in advance.

    Kind regards,

    Galina

    Google Ads Scripts Forum Advisor

    unread,
    May 26, 2021, 11:20:30 PM5/26/21
    to adwords...@googlegroups.com
    Hi Galina,

    Please excuse me if I did not provided simpler guidance on how you can update the script. Since this is a third party script, it would still be best that you reach out to the author of the script. Nevertheless, I have attached here an edited version of the script for your reference to check for URLs at the Keyword level filtered with a Keyword Label. Please set the keyword label on line 18, then kindly try this at your end and let me know it goes.
    script.txt

    Galina Georgieva

    unread,
    May 27, 2021, 3:46:57 AM5/27/21
    to Google Ads Scripts Forum
    Hey Harry,

    Thanks a lot for your help. I had to adjust a few things but it finally worked!! :) Script is now doing exactly what I wanted it to do.

    Thanks.

    Kind regards,
    Galina
    Reply all
    Reply to author
    Forward
    0 new messages