Google AdWords 脚本缺货

Google AdWords out of stock script

对于我们的 Google AdWords 广告系列,我们使用一个脚本来检查产品是否有货。如果产品缺货,它会暂停营销活动。在 运行 te 脚本之后,我们得到以下错误:

我收到以下错误:TypeError:无法调用 null 的 "indexOf" 方法。 (第 54 行)。

错误部分为:

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

完整的脚本是:

/************************************
Script aan/uit advertenties
***********************************/
var URL_LEVEL = 'Ad'; // or Keyword
var ONLY_ACTIVE = false; // set to false for all ads or keywords
var CAMPAIGN_LABEL = 'automatisch'; // 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 to search for 
// on the page that indicates the item 
// is out of stock.
var OUT_OF_STOCK_TEXT = 'adwords-niet-op-voorraad';
 
function main() {
  var alreadyCheckedUrls = {};
  var iter = buildSelector().get();
  while(iter.hasNext()) {
    var entity = iter.next();
    var url = cleanUrl(entity.getDestinationUrl());
    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('https'));
    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;
  }

您使用 getDestinationUrl() 检索广告's/keyword 的 URL,但目标 URL 在 Adwords 中已弃用大约一年,这就是为什么此方法 returns null 这意味着您使用 null 作为参数调用 cleanUrl() 导致您列出的错误。

为了使脚本工作,我猜你必须使用 .urls().getFinalUrl() 而不是 getDestinationUrl()