When I’m writing, I preview articles locally in Jekyll and reload them a lot. I don’t want to trigger my analytics code everytime and record hits that are just me checking my article in progress.
So I came up with a quick solution that will even work if you run Jekyll in safe mode, like GitHub Pages does.

In my layout, I check if Jekyll generates the site for local preview. If it’s the case, I have a custom flag called site.draft set and I can use that to avoid rendering the analytics code in preview.
The relevant lines are 18 to 20:

 1 <!DOCTYPE html>
 2 <html>
 4   {% include head.html %}
 6   <body>
 8     {% include header.html %}
10     <div class="page-content">
11       <div class="wrapper">
12         {{ content }}
13       </div>
14     </div>
16     {% include footer.html %}
18     {% unless site.draft %}
19       {% include google_analytics.html %}
20     {% endunless %}
22   </body>
24 </html>

Now you might ask how to set site.draft to the proper value? Good question.

Thankfully, Jekyll accepts multiple config files on the command line using the --config switch.
I take advantage of that by creating another config file used only for my preview mode. Let’s call it _config_draft.yml and create it in the root of the Jekyll site along the main config. It will only contain the following:

draft: true

Loading this config file in preview mode is only a matter of executing Jekyll with the proper options. As I don’t like typing long command lines, I set up Rake tasks to do it for me.
Here is an example that you can integrate into your Rakefile:

namespace :preview do
  task :draft do
     sh 'bundle exec jekyll serve --config _config.yml,_config_draft.yml --future --drafts --unpublished'

  desc 'Preview before release (exactly as it will be live)'
  task :release do
    sh 'bundle exec jekyll serve'

desc 'Preview for editing (site.draft flag set and draft, unpublished and future posts showing)'
task :preview => 'preview:draft'

Then when I run rake preview and have a look at the HTML source of my site, it doesn’t include the analytics code anymore! Win! Cookies!
Now next time I’ll need to include some stuff only on the live site, it’s just a conditionnal away.

Note that the preview task will also render draft, unpublished and future posts. The kind of things you might want to do when previewing your site.

I also included a preview:release task to check my site rendered exactly as it will be before I push it live.