Displaying Google Calendar events on the web, with this simple diary banner for your site.
Introduction
You have a website, you have a Google calendar which you have shared publically. Now you would like to have some of the events from your calendar display as a banner on your website, highlighting the most important events for the world to see.
Instructions
The easiest way to do this is to make a client-side request for your calendar events and render them directly on your web-page. This approach will work on any site and is also easily embeddable if you are using a templated site (like Wordpress).
An API Key
To ensure your website can make a valid request to get your calendar events, you will need an API key from Google. This is a quick process and will result in a long string of letters and numbers that looks like a randomly generated password (which is pretty much what it is). To get a key, follow the instructions in the article linked above, or log onto the Google Developers Console, create a project and enable the Calendar API.
Once you have created your project, and the API key, you can further secure the usage of that key (to make sure that it can only be used from your website) by setting ‘Application Restrictions’ from the ‘Key Restrictions’ section of the API key page (click on the key name, or edit icon). Here you can list your website address/es (e.g. www.example.com/*) in the ‘HTTP referrers (websites)’ section. This will restrict the key, so it only works from your site.
Calendar ID
Before you make a request for calendar data, you need to know which calendar you wish to access. This is done by supplying your calendar ID, which can be found in the settings page of the calendar, under ‘Integrate calendar’ and ‘Calendar ID’. It will look like an email address, and much be quite long in the case of a shared or secondary public calendar. Your calendar uses your email address as it’s ID.
Your Web Page
If you are looking to embed this calendar banner on an existing page, you can skip over this section. The banner embed code assumes you already have JQuery linked into your page, and assumes we are using the Roboto font family.
If you want to embed the banner in a new HTML page, you can use the following as a good starting point for that page. It links the relevant JQuery library and the Roboto font family, as well as providing a full width body element (without any padding or margins) so that the banner stretches right across the page.
Embedding a banner
Using the excellent Swiper library to provide touch control and animated transitions, the code below can be dropped into your page. Insert your API Key and Calendar ID in into the relevant spots, and you are good to go! If you don’t see anything on the screen when you test the page, remember to check the developer console in your browser for any errors. Pay particular attention to checking you have enabled the calendar API in your developer console project, as this often gets forgotten!
Filtering your Events
The code above will display the next five events, starting from a couple of days before now. You can alter this behaviour by adjusting the LIMIT and START variables. You can also choose to filter the events using extended properties. These data fields are baked into the Google Calendar system, but there is no standard Google UI available for editing them. Instead, you can choose to write your code to add these properties to your events (e.g. using Apps Script) or install a third-party tool to do it for you!
If you use the Tag-A-Doc chrome extension and associated web app, add the Highlight tag to your events. Then change the value of the FILTERED variable in the code above to true. This will request only those events that have the Highlight tag applied, using the filter: “Highlight=TRUE”. You need not stop there; you can add your own tags and filters to display just the events you would like to show!