New Plugin Brings Google Analytics Data to Jekyll Sites


3 min read

For a content-heavy site like the new, surfacing popular content gets the most interesting information to readers quickly. To make this possible with static sites generated with Jekyll, we’re releasing the new jekyll-ga plugin that gets the latest Google Analytics data when Jekyll builds the site, making it available to use for sorting and in templates.

By default Jekyll sorts content posts chronologically and alphabetically. This works well for blogs, but now with jekyll-ga, we have many more options. Any metric you can track in Google Analytics — including custom variables — can be used to sort content or as a variable in our Liquid templates.

Using the plugin

Jekyll has a plugin system thats allows for dropping in custom code written in Ruby to generate or modify content. Any .rb files in the /_plugins directory get run at site build time.

Full instructions for setting up jekyll-ga are in the readme file, but here’s the basic idea. By adding some configuration information into the _config.yml file for a Jekyll site, you can define a custom report to query from Google Analytics. You can specify any metric you want, and the date range for the report using absolute dates or several relative date formats such as now, yesterday, last week, and three months ago. Optionally, you can apply filters or segments to your reports based on settings you configure in Google Analytics. Here’s how it all looks:

  service_account_email:    # service account email address
  key_file: privatekey.p12  # service account private key file
  key_secret: notasecret    # service account private key's password
  profileID: ga:####        # profile ID
  start: last month         # Beginning of report
  end: now                  # End of report
  metric: ga:pageviews      # Metric code
  segment:                  # optional
  filters:                  # optional
  sort: true                # Sort posts by this metric`

In this case, we’re querying pageviews, but you could use any metric available in Google analytics, which opens up several possibilities for organizing your content. For instance, with custom variables, you could have javascript trigger events that Google Analytics records, such as clicking a “like” button, that you then use for sorting content when your site is rebuilt.

Advanced sorting

Pairing jekyll-ga with jekyll-sort let’s you have content sorted in multiple lists, such as preserving the default chronological sorting of site.posts content lists, while also adding in a site.popular_posts list.

For more on this and other uses, see the readme file for jekyll-ga, and get the plugin on GitHub.

Next Up

The redesign of will make it easy to discover and consume content by ensuring accessibility, supporting Spanish translations, and now exposing popular content across the site.

We’re developing as a CMS-free Jekyll website in tandem with a new release of Prose, the online editor for Jekyll and GitHub. Stay tuned for more updates on both in the next few days, including a simple web server that lets you run your own on-premises GitHub Pages-like hosting service.

What we're doing.