Modifying URL Split Function on Out Of Stock URL Script

34 views
Skip to first unread message

Natalie Musser

unread,
Jul 28, 2016, 5:03:16 PM7/28/16
to AdWords Scripts Forum

I have been using the following script to pause groups that have keywords leading to out of stock pages. The script had been working great up until we switched over to a different 3rd party provider that appended new tracking strings at the end of our urls.Now when the script runs, it just says "skipped" for all the urls.

The issue is that I need the the script to split the url at different text strings depending on the url because there are several different url structures existing in the account.

For example, all of our urls contain either "&camp" or "?camp" which is where the 3rd party tracking string begins and need the script to split the url at the point EITHER of these text strings begin/

 Ex: http://www.abc.com/xyz/index.jsp?categoryId=12345&camp=SEM:XTR_40798_ecom_nonbrand_402  

Additionally, some urls contain other tracking params, some of which come before the 3rd party tracking string begins and some of which come after it. 

 Ex: http://www.abc.com/xyz/index.jsp?categoryId=12345&002=239789&004=131735543&005={MatchType}&006={device}&camp=SEM:XTR_40798_ecom_nonbrand_402&googletargetexp={_googletargetexp}


In other words, we’d want it to first get rid of everything starting at “&camp” OR “?camp” and THEN get rid of anything after “={“ if they still remain.

I am not familiar with javascript coding, so as much specifics as possible on how to fix this would be wonderful.

The script currently being used is as follows:

/************************************
* 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 = 'Keyword'; // or Keyword
var ONLY_ACTIVE = true; // set to false for all ads or keywords
var CAMPAIGN_LABEL = 'Footwear'; // 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 = 'searchhandler';
var OUT_OF_STOCK_TEXT2 = 'searchNoResults';
 
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.getAdGroup().pause();
      } else {
        entity.getAdGroup().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) || (htmlCode.indexOf(OUT_OF_STOCK_TEXT2) >= 0))  {
        alreadyCheckedUrls[url] = 'out of stock';
        entity.getAdGroup().pause();
      } else {
        alreadyCheckedUrls[url] = 'in stock';
        entity.getAdGroup().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('&camp')>=0) {
      url = url.split('&camp')[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("','")+"']");
    selector = selector.withCondition('CampaignName IN ["'+campaignNames.join('","')+'"]');
  }
  return selector;
}

Jaren Callo (AdWords Scripts Team)

unread,
Jul 28, 2016, 11:14:35 PM7/28/16
to AdWords Scripts Forum
Hi Natalie,

Are the 3rd party parameters fixed? If yes, you can add the code below to get the base url you need:

if (finalUrl.indexOf('&camp') > -1){
 
var firstCleanUp = finaUrl.split('&camp');
 
//["http://abc.go.com/xyz/index.jsp?categoryId=12345&002=239789&004=131735543&005={MatchType}&006={device}", "=SEM:XTR_40798_ecom_nonbrand_402&googletargetexp={_googletargetexp}"]
  url
= firstCleanUp[0];
}


if (url.indexOf('={') > -1){
 
var secondCleanUp = firstCleanUp[0].split('={')
 
//["http://abc.go.com/xyz/index.jsp?categoryId=12345&002=239789&004=131735543&005", "MatchType}&006", "device}"]
  url
= secondCleanUp[0]; // "http://abc.go.com/xyz/index.jsp?categoryId=12345&002=239789&004=131735543&005"
}


Let me know if this works for you.

Thanks,
Jaren P. Callo
AdWords Scripts Team

Natalie Musser

unread,
Jul 29, 2016, 11:32:12 AM7/29/16
to AdWords Scripts Forum
Hi Jaren,

Unfortunately neither the 3rd party tracking strings or the tracking params are fixed. While the tracking strings all begin with "&camp" or "?camp" the numbers on the text string following are unique for each url. The same is true for the params.  

Tyler Sidell (AdWords Scripts Team)

unread,
Jul 29, 2016, 5:05:24 PM7/29/16
to AdWords Scripts Forum
Hi Natalie,

Below is a script that demonstrates what you are looking for.  It takes in the url, then splits the url where it finds the value specificed ('&');.  Run this script in AdWords Scripts and you'll see the expected results.
function main() {
var originalURL = "http://www.abc.com/xyz/index.jsp?categoryId=12345&camp=SEM:XTR_40798_ecom_nonbrand_402 ";
var appendedUrl = originalURL.split('&')[0];
Logger.log(appendedUrl);
}

Expected Output: http://www.abc.com/xyz/index.jsp?categoryId=12345

Thanks,
Tyler Sidell
AdWords Scripts Team
Reply all
Reply to author
Forward
0 new messages