Add Google Analytics to Middleman static website

Installing Google Analytics into your Middleman static website is actually a breeze, thanks to the Google Analytics Middleman Gem by Daniel Bayerlein.

First, head over to Google Analytics and create a new account or add a new property. Then make sure to get the Tracking ID, starting with UA (Format should be UA-123456789-0).

Next, add the Google Analytics Gem for Middleman to your Gemfile:

gem 'middleman-google-analytics'

To install the gem, stop the server on localhost with ctrl-C and run bundle install to install the gem.

Next, add the following code to your config.rb:

# config.rb
activate :google_analytics do |ga|
ga.tracking_id = 'UA-XXXXXXX-X' # Use your Tracking ID from above

Lastly, you need to run the Google Analytics snippet on all the pages you want to track after your footer. I put it into layout.erb at the end, before the closing </body> tag:

<%= google_analytics_tag %>

That's it — you're all set and running GA now! More options for configuration on the gem's github page.

comments powered by Disqus