• Search Engine Land
  • Sections
    • News
    • SEO
    • SEM
    • Local
    • Retail
    • Search Engines
    • Social
    • Home
  • Follow Us
    • Subscribe to Our Feed! See us on Instagram See us on Pinterest Check out our Tumblr! Join our LinkedIn Group
    • Follow
  • Subscribe
  • Search Engine Land
  • News
  • SEO
  • SEM
  • Local
  • Social
  • Retail
  • Search Engines
  • More
  • Follow Us
    • Subscribe to Our Feed! See us on Instagram See us on Pinterest Check out our Tumblr! Join our LinkedIn Group
    • Follow
  • Subscribe

Search Engine Land

Search Engine Land

Early Returns

Are These The Winners & Losers Of Google Mobilegeddon?

Bing See, Bing Do

Bing Shows Detailed Answers In Search Results Like Google

Subscribe to SearchCap

  • News
  • SEO
  • SEM
  • Local
  • Retail
  • Search Engines
  • Social
  • Home
SEM

Connecting AdWords Scripts & The Advanced Calendar API

In this helpful how-to, columnist Russell Savage explains how to integrate Google Calendar with AdWords to monitor your campaign start and end dates.

Russell Savage on December 18, 2014 at 9:41 am
  • More

events-calendar-schedule-ss-1920

AdWords scripts just got a lot more useful thanks to the recent rollout of Advanced APIs.

These additional APIs allow AdWords scripts to access other Google services like Google Analytics and Google Calendar. Anyone familiar with other Google Scripts will recognize these objects as the same ones available there.

Today we are going to focus on the Calendar API and build an integration that allows you to monitor campaign start and end dates via your Google calendar.

Many times, for meetings or reporting, it helps to see things laid out in a week or month view so that overlapping items stand out. This script will create entries on a separate calendar representing the duration of your campaigns.

The first thing I recommend doing is creating a new calendar in your Google account to hold the campaign start and end dates. This way, you can easily share the calendar with other team members or hide it to reduce clutter. I named mine “AdWords,” but you can name it whatever you like.

Create A New Google Calendar

Now that you have a place to put all your events, let’s start writing some code. When you open a new scripting window, you will notice a new button next to the Preview button titled “Advanced APIs.” This is where you will need to enable the use of the Calendar API before you continue.

You will also need to enable the API in the Developers Console. Just click the link at the bottom of the dialog box and follow the prompts.

Once everything is enabled, go back to your AdWords script window and complete the authorization process as normal. You will now have access to a new Calendar class in your scripts. You will need to follow this initial set up process for each new script you create.

Calendar API Enabled via Google Developers Console

The first thing we are going to need is a function that allows us to find the calendar id from the nice name (or summary) you entered earlier. This was adapted from some of the sample code on the AdWords Scripts developer site and does the job nicely.

// Lookup the calendar id given the summary
// If no calendar id is found, an error is raised
function getCalendarId(summary) {
  var calendarList = Calendar.CalendarList.list();
  for(var i in calendarList.items) {
    var cal = calendarList.items[i];
    if(cal.summary === summary) {
      return cal.id;
    }
  }
  throw "No calendar with the summary: '"+summary+"' found.";
}

 

Now that we have the id of the calendar, we can start updating it. This script is going to take the simple approach and clear all the entries from the calendar each time it is run.

Yeah, it is a little wasteful, but it shouldn’t matter as long as you have less than 2500 campaigns in your MCC. You can always call the delete function multiple times if you need to since it is pretty fast.

// Removes all the events from a given calendar
function blowAwayTheCalendar(calendarSummary) {
  var calendarId = getCalendarId(calendarSummary);
  var calendarEvents = Calendar.Events.list(calendarId, { maxResults: 2500 } );
  if (calendarEvents.items && calendarEvents.items.length > 0) {
    for(var i = 0; i < calendarEvents.items.length; i++) {
      var calendarEvent = calendarEvents.items[i];
      Calendar.Events.remove(calendarId, calendarEvent.id);
    }
  }
}

 

The script is going to run through each account we have, look at all the campaigns, and create calendar events reflecting the start and end dates of the campaign.

We will need some code to actually create the event. The most important things in a calendar event are the summary and the dates, so that’s all I’m going to care about here.

// Creates an event on the given calendar
function createEvent(calendarId,eventSummary,startDate,endDate) {
  var eventData = {
    'summary': eventSummary,
    'description': '', // You can add something here if you wish
    'start': { 'date': formatDate(startDate) },
    'end': { 'date': formatDate(endDate) }
  };
  var maxRetries = 3;
  while(maxRetries > 0) {
    try {
      var calendarEvent = Calendar.Events.insert(eventData, calendarId);
      Logger.log('New event with ID = %s was created.',calendarEvent.getId());
      return calendarEvent.getId();
    } catch(e) {
      maxRetries--;
      Utilities.sleep(1000);
    }
  }
  throw "Could not create calendar event: "+eventSummary;
}

// Helper function for formatting the date
function formatDate(theDate) {
  return Utilities.formatDate(theDate, 
                              AdWordsApp.currentAccount().getTimeZone(), 
                              'yyyy-MM-dd');
}

 

With only a few lines of code, we can now create events from our AdWords campaigns. This is going to create a new all-day event starting on the start date and ending on the end date. There are many other customizable parameters you can add if you like, but this will work for now.

You might notice the retry logic here when trying to create the event. If you are running this in parallel with other accounts, you will most likely run into the limits of five events per second. The retry logic captures that and retries after waiting a second.

This should help with any quota issues. Now we’re ready to put together the code to iterate through our accounts and add the events to our calendar. We can use a simple executeInParallel command to accomplish this.

// This is the nice name you used when
// creating your calendar.
var CALENDAR_SUMMARY = 'AdWords';

function main() {
  blowAwayTheCalendar(CALENDAR_SUMMARY); // remove all existing entries
  MccApp.accounts().executeInParallel('campaignsToCalender');
}

// Iterates through the campaigns in an account
// and adds a calendar event using the start and 
// optional end date of the campaign.
function campaignsToCalender() {
  var calendarId = getCalendarId(CALENDAR_SUMMARY);
  var accountName = AdWordsApp.currentAccount().getName();
  var campIter = AdWordsApp.campaigns().get();
  while(campIter.hasNext()) {
    var camp = campIter.next();
    var start = camp.getStartDate() ? awDateToDate(camp.getStartDate()) : null;
    if(!start) { continue; } // this shouldn't happen
    var end;
    if(camp.getEndDate()) {
      end = awDateToDate(camp.getEndDate());
    } else {
      // Default campaign length is a year
      // Maybe a few months is better?
      end = new Date(start);
      end.setFullYear(end.getYear()+1);
    }
    // Event name will be Account > Campaign
    var summary = [accountName,camp.getName()].join(' > ');
    var eventId = createEvent(calendarId,summary,start,end);
  }
}

// Helper function to transform AdWordsDate
// objects to normal Date objects.
function awDateToDate(awDate) {
  return new Date(awDate.year, 
                  awDate.month, 
                  awDate.day, 0, 0, 0);
}

 

Final Thoughts

Our main function becomes extremely simple because we have created functions for all the complicated stuff. One thing to notice is that while all campaigns will have a start date, not all of them require an end date.

In cases like these, you will have to decide what you want to do. In this code, I am setting the campaigns for a length of a year just so I can keep track of them, but you can do whatever makes sense for your account.

This is just a simple example of course; but hopefully, it sparks an idea for you to continue exploring these new APIs. For example, you could extend this code to allow you to change events on your calendar and have your campaigns automatically update.

There are plenty of other Advanced APIs that have recently been enabled, and I encourage you to check them out. It’s getting harder and harder to keep up with the momentum of AdWords Scripts, and I can’t wait to see what they have in store for us next.


Some opinions expressed in this article may be those of a guest author and not necessarily Search Engine Land. Staff authors are listed here.


Attend SMX Advanced for expert-only tactics from leading authorities on SEO, paid search, mobile, social media and more. They will tackle master-level topics you won't hear discussed anywhere else. Don't miss this once a year opportunity to meet them and learn what they know! Early bird rates expire this Saturday­ register today!

About The Author

Russell Savage
Russell Savage is a Business Systems Architect for Elasticsearch and loves saving people time through automation. He writes the blog FreeAdWordsScripts.com where he posts AdWords scripts that he has built for all to enjoy.

Related Articles

Keep Your AdWords Scripts Maintainable With These 3 Tips
Create Charts Using AdWords Scripts For Quick Insights
AdWords Scripts Grows Up With MCC Level Scripts
This AdWords MCC Script Will Save You An Hour Every Day!
Channel: SEM Google Google: AdWords How To: PPC Paid Search Column
(Some images used under license from Shutterstock.com.)

Sponsored

No fluff - just the best news in paid search marketing every week.

Submit A Tip

Got a tip? We respect your anonymity.

Submit it here!

Attend Our Conferences

The only conference designed for advanced search marketers returns to Seattle June 2-3. Learn more about SMX Advanced

Or attend an SMX near you. See all the dates and locations.

Learn More About Our SMX Events

MarTech: The Marketing Tech Conference is for marketers responsible for selecting marketing technologies and developing marketing technologists. Visit MarTechConf.com for details.

Learn More About Our MarTech Events

White Papers

  • 30 Seconds or Less: How to Engage the Distracted Consumer
  • How To Be Successful in Mobile Advertising
  • Mobile Advertising Around the Globe: 2015 Annual Report
See All

Webcasts

  • What’s That Smell? Your R.O.T. Content
  • Writing Ad Copy That Converts
  • Click-to-Call Deep Dive: Best Practices from Search Experts
See All

Research Reports

  • Enterprise Paid Media Campaign Management Platforms 2015: A Marketer’s Report
  • B2B Marketing Automation Platforms 2015: A Marketer’s Guide
  • Content Marketing Tools 2015: A Marketer’s Guide
  • Enterprise Social Media Management Software 2014: A Marketer’s Guide
See All

The Periodic Table of Seo Success

Search Engine Land Periodic Table of SEO Success Factors
Sign up for our daily newsletter.
Search Engine Land Marketing Land

Channels

  • News
  • SEO
  • SEM
  • Local
  • Retail
  • Search Marketing
  • Search Engines
  • Social

About

  • About Us
  • Contact
  • Privacy
  • Advertise
  • Staff
  • Connect With Us

Follow Us

  • Facebook
  • Twitter
  • Google +
  • Tumblr
  • LinkedIn
  • Pinterest
  • Youtube
  • Instagram
  • Newsletters
  • RSS

© 2015 Third Door Media, Inc. All rights reserved.