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


  1. Hi Russel,
    first of all : Great Blog , I love it !

    Now the big question :D - there is away to drill down conversions segmented by location including the new bidding adjustment in a enhanced campaing ?

    Thanks, Fabio

    1. Good question Fabio. Right now, i don't think the adwords conversion reporting provides that level of drill down. Your best bet might be to create some hyper targeted campaigns with the locations you are interested in. Assuming you have conversion tracking enabled, you can put together a script that iterates through your location targeted campaigns and reports on the conversions from them.

      Enhanced campaign support in adwords scripts is almost non-existent. It has been asked for on the forums so I would expect it to come around the time of the forced cut-off date this summer.

      Thanks for reading my blog. If you have any suggestions for scripts you'd like to see, drop me a line through my google profile or twitter.


  2. with the help of free coin master you will get a free spin and in spin you can win many coins and you can use those coins in your game..

  3. It is important for medical science research paper service students to seek Medical Science Assignment Help from a reputable custom medical science writing company so as to be assured of good grades in their medical science essay writing services.

  4. Thanks for sharing this post. Quickbooks is one of the advanced accounting software that offers multiple accounting features to manage your business data. You can install quickbooks on Mac also.