Showing posts with label baseball. Show all posts
Showing posts with label baseball. Show all posts

Saturday, March 30, 2013

Automate Your Ads Based on Your Baseball Team's Schedule

I know everyone is focused on March Madness right now, but opening day for Baseball season is right around the corner. I wondered if it were possible to enable and disable specific ads based on the schedules of your local MLB team.

It turns out, the MLB has every team's full schedule in an easy to parse CSV format. All you need to do is search Google for " downloadable schedule" and look for the page that ends in "downloadable.jsp". It will look something like this:

The link you need is circled in the image above. You can see from the url what the team_id value should be in the code below. And if you right-click and download that .csv file, you will be able to see what value to use for the home_field as well. In the example below, I used my hometown teams of the Cubs and the White Sox, but you can use any teams you want.

The script itself will run through and look for a home game for your team. If the team is playing today and it is a home game, it will enable any ads that are tagged with the label you configured in the TEAM_INFO. If the team isn't playing a home game today, it will pause those same ads.

In the example below, I only used a portion of the data in the csv files. It would also be possible to enable ads only during the actual game, or enable ads for all game days, not just home games.

I encourage you to play around with the data and see what you can come up with. If you find anything useful, please leave a comment.


// Enable/Disable Ads Based on the MLB Schedule
// Created By: Russ Savage
function main() {
  var TEAM_INFO = [
    { team_id : 112, home_field : 'Wrigley Field', label : 'cubs' }, // Cubs
    { team_id : 145, home_field : 'U.S. Cellular Field', label : 'whitesox' } // White Sox
  //hopefully you've already created and tagged some ads with these labels
  //but just in case...
  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 = ""+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,
           end_date,end_date_et,end_time,end_time_et] = date_list[i].split(",");
      var today = new Date();
      var game_day = new Date();
      is_home = (location == team.home_field);
      is_game_day = (diffDays(game_day,today) == 0);
      if(is_home && is_game_day) {
    if(!(is_home && is_game_day)) {

function enableBaseballAds(label) {
  Logger.log("Enabling all ads with the "+label+" label.");
  var ads ="LabelNames CONTAINS_ALL ['"+label+"']").get();
  while(ads.hasNext()) {; 

function disableBaseballAds(label) {
  Logger.log("Disabling all ads with the "+label+" label.");
  var ads ="LabelNames CONTAINS_ALL ['"+label+"']").get();
  while(ads.hasNext()) {; 

function createLabelsIfNeeded(team_info) {
  var label_iter = AdWordsApp.labels().get();
  var label_list = [];
  while(label_iter.hasNext()) {
  for(var i in team_info) {
    if(label_list.indexOf(team_info[i].label) == -1) {

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