Google Analytics Only in Prodution with Jekyll
For Jekyll sites, here’s how I approach including the Google Analytics script in production, but not development.
Jekyll sets a variable for the environment, which is performed within the Jekyll module (/lib/jekyll.rb):
The code shows that by default Jekyll runs in the development environment, unless you set a JEKYLL_ENV environment variable in your shell prior to building the site. The environment value is then available within your liquid templates as ‘jekyll.environment’.
So, to only include the Google Analytics code in production:
In your layout file, or wherever you would normally place your Google Analytics script, add a conditional include:
- Then specify your environment when building for production:
I use the above command within as part of my deploy script so that I can’t forget to include the Google Analytics script.
That’s it, nice and easy!
Nothing is different when building for development, just use the default build command.