显示带有标签的帖子 启用广告. 显示所有帖子
显示带有标签的帖子 启用广告. 显示所有帖子

2013年3月30日,星期六

根据您的棒球队的日程安排自动化广告

我知道现在每个人都将注意力集中在疯狂三月,但是棒球赛季的开幕日指日可待。我想知道是否可以根据您当地的MLB团队的时间表启用和禁用特定广告。

事实证明,美国职业棒球大联盟以易于解析的CSV格式提供了每个团队的完整时间表。您需要做的就是在Google中搜索“ 可下载的时间表”,然后找到以“ downloadable.jsp”结尾的页面。看起来像这样:


您需要的链接在上图中被圈出。您可以从网址中看到以下代码中的team_id值。而且,如果右键单击并下载该.csv文件,您还将能够看到用于home_field的值。在下面的示例中,我使用了家乡的小熊队和白袜队,但是您可以使用任何想要的球队。

脚本本身将运行并为您的团队寻找主场比赛。如果团队今天在比赛并且是主场比赛,它将启用所有在TEAM_INFO中配置的带有标签的广告。如果团队今天不参加主场比赛,它将暂停这些相同的广告。

在下面的示例中,我仅使用了csv文件中的一部分数据。也可能仅在实际游戏中启用广告,或者在所有游戏日中启用广告,而不仅仅是家用游戏。

我鼓励您尝试使用数据,看看可以做什么。如果发现有用的东西,请发表评论。

谢谢,
拉斯


//-----------------------------------
// Enable/Disable Ads Based on the MLB Schedule
// Created By: 拉斯 Savage
// FreeAdWordsScripts.com
//-----------------------------------
function main() {
  var TEAM_INFO = [
    { team_id : 112, home_field : 'Wrigley Field', 标签 : 'cubs' }, // Cubs
    { team_id : 145, home_field : 'U.S. Cellular Field', 标签 : 'whitesox' } // White Sox
  ];
  //hopefully you've already 创造d and tagged some 广告 with these 标签
  //but just in case...
  创造LabelsIfNeeded(TEAM_INFO); 
  
  var SEASON = (new Date()).getFullYear();
  var is_home = false, is_game_day = false;
  for(var t in TEAM_INFO) {
    var team = TEAM_INFO[t];
    var url = "http://mlb.mlb.com/soa/ical/schedule.csv?team_id="+team.team_id+"&season="+SEASON;
    var html = UrlFetchApp.fetch(url).getContentText();
    var date_list = html.split("\r\n");
    for(var i in date_list) {
      if(i == 0) {continue;}
      var [start_date,start_time,start_time_et,
           subject,location,description,
           end_date,end_date_et,end_time,end_time_et] = date_list[i].split(",");
      
      var today = new Date();
      var game_day = new Date();
      game_day.setFullYear(SEASON,parseInt(start_date.split("/")[0])-1,parseInt(start_date.split("/")[1]));
      
      is_home = (location == team.home_field);
      is_game_day = (diffDays(game_day,today) == 0);
      
      if(is_home && is_game_day) {
        enableBaseballAds(team.label);
        break;
      }
    }
    if(!(is_home && is_game_day)) {
      disableBaseballAds(team.label); 
    }
  }
  
}

function enableBaseballAds(label) {
  Logger.log("Enabling all 广告 with the "+label+" 标签.");
  var 广告 = AdWordsApp.ads().withCondition("LabelNames CONTAINS_ALL ['"+label+"']").get();
  while(ads.hasNext()) {
    广告.next().enable(); 
  }
}

function disableBaseballAds(label) {
  Logger.log("Disabling all 广告 with the "+label+" 标签.");
  var 广告 = AdWordsApp.ads().withCondition("LabelNames CONTAINS_ALL ['"+label+"']").get();
  while(ads.hasNext()) {
    广告.next().pause(); 
  }
}

function 创造LabelsIfNeeded(team_info) {
  var 标签_iter = AdWordsApp.labels().get();
  var 标签_list = [];
  while(label_iter.hasNext()) {
    标签_list.push(label_iter.next().getName());
  }
  for(var i in team_info) {
    if(label_list.indexOf(team_info[i].label) == -1) {
      AdWordsApp.createLabel(team_info[i].label);
      标签_list.push(team_info[i].label);
    }
  }
}

//A helper function to compare 日期.
//Copied from: http://goo.gl/uW48a
function diffDays(firstDate,secondDate) {
  var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
  return Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay))); 
}

2013年3月10日,星期日

根据机场延误管理广告

如今,互联网上有大量的API。这些中的任何一个都可以合并到您的AdWords脚本中,以自动化一些帐户管理。

以下脚本使用 FAA.gov提供的机场状态服务 在主要机场出现延误时启用和暂停广告。这对于机场附近可能从航班延误中受益的任何企业(例如餐厅,酒吧或酒店)很有用。您的广告可能会说:



您可以从顶部的列表中添加或删除机场代码。您需要做的只是用[airport_code] _delay标签(“ ORD_delay”,“ ATL_delay”)标记广告。

您还可以使用其他哪些动态数据来优化广告?电影时间?体育游戏?在下面的评论中让我知道,然后我会提出我的建议。

谢谢,
拉斯

/*********************************************
* Pause/Enable Ads Based On Airport Delays
* Version 1.1
* ChangeLog v1.1 
*  - Added ability to completely 暂停 non-delay 广告
* Created By: 拉斯 Savage
* FreeAdWordsScripts.com
*********************************************/
// For this to work, you need to 广告 d a 标签 to all
// the 广告 for each 飞机场.  For example, PIT_normal
// or SFO_normal
var PAUSE_NORMAL_ADS_DURING_DELAY = false;
var DELAY_SUFFIX = '_delay'; //the suffix on the 标签 for delayed 广告
var NORMAL_SUFFIX = '_normal'; //the suffix on the 标签 for normal 广告

var AIR_CODES = ["ATL","ANC","AUS","BWI","BOS","CLT","MDW","ORD","CVG","CLE",
                 "CMH","DFW","DEN","DTW","FLL","RSW","BDL","HNL","IAH","HOU",
                 "IND","MCI","LAS","LAX","MEM","MIA","MSP","BNA","MSY","JFK",
                 "LGA","EWR","OAK","ONT","MCO","PHL","PHX","PIT","PDX","RDU",
                 "SMF","SLC","SAT","SAN","SFO","SJC","SNA","SEA","STL","TPA",
                 "IAD","DCA"];

function main() {
  var faaUrl = "http://services.faa.gov/airport/status/";
  var args = "?format=application/json";
  for(var i in AIR_CODES) {
    try{
      var resp = UrlFetchApp.fetch(faaUrl + AIR_CODES[i] + args);
      if( resp.getResponseCode() == 200 ) {
        var json = Utilities.jsonParse(resp.getContentText());
        if(json.delay == "false") {
          Logger.log("No 延误 在 "+json.IATA+". Pausing delay 广告 if any are running.");
          turnOffDelayAds(json.IATA);
          if(PAUSE_NORMAL_ADS_DURING_DELAY) {
            turnOnNonDelayAds(json.IATA);
          }
        } else {
          Logger.log("Delays in "+json.IATA+" Reason: "+json.status.reason);
          Logger.log("Turning on delay 广告 if there are any.");
          turnOnDelayAds(json.IATA);
          if(PAUSE_NORMAL_ADS_DURING_DELAY) {
            turnOffNonDelayAds(json.IATA);
          }
        }
      }
    }
    catch(e) {
      Logger.log("Error: " + e.message);
    }
  }
}

function turnOffDelayAds(airportCode) {
  var 标签Name = 飞机场Code + DELAY_SUFFIX;
  var 广告 Iter = AdWordsApp.ads()
    .withCondition("LabelNames CONTAINS_ANY ['"+labelName+"']")
    .withCondition("CampaignStatus = ENABLED")
    .withCondition("AdGroupStatus = ENABLED")
    .withCondition("Status = ENABLED")
    .get();
  while(adIter.hasNext()) {
    广告 Iter.next().pause();
  }
}

function turnOffNonDelayAds(airportCode) {
  var 标签Name = 飞机场Code + NORMAL_SUFFIX;
  var 广告 Iter = AdWordsApp.ads()
    .withCondition("LabelNames CONTAINS_ANY ['"+labelName+"']")
    .withCondition("CampaignStatus = ENABLED")
    .withCondition("AdGroupStatus = ENABLED")
    .withCondition("Status = ENABLED")
    .get();
  while(adIter.hasNext()) {
    广告 Iter.next().pause();
  }
}

function turnOnDelayAds(airportCode) {
  var 标签Name = 飞机场Code + DELAY_SUFFIX;
  var 广告 Iter = AdWordsApp.ads()
    .withCondition("LabelNames CONTAINS_ANY ['"+labelName+"']")
    .withCondition("CampaignStatus = ENABLED")
    .withCondition("AdGroupStatus = ENABLED")
    .withCondition("Status = PAUSED")
    .get();
  while(adIter.hasNext()) {
    广告 Iter.next().enable();
  }
}

function turnOnNonDelayAds(airportCode) {
  var 标签Name = 飞机场Code + NORMAL_SUFFIX;
  var 广告 Iter = AdWordsApp.ads()
    .withCondition("LabelNames CONTAINS_ANY ['"+labelName+"']")
    .withCondition("CampaignStatus = ENABLED")
    .withCondition("AdGroupStatus = ENABLED")
    .withCondition("Status = PAUSED")
    .get();
  while(adIter.hasNext()) {
    广告 Iter.next().enable();
  }
}