I posted Draft Mode in Jekyll Templates on Monday, 20 July, 2015

Use site.show_drafts in your template to keep site development from cluttering analytics

Post jekyll

Draft Mode in Jekyll Templates

Yesterday I published a post about Jekyll collections. Today I checked Google Analytics to see if anybody looked at my site. 99 visits! Hey, nice. But I also noticed several localhost entries. Those times I was double-checking my page locally with jekyll serve -Dw counted as visits, because the browser saw the analytics code and dutifully notified Google’s servers. So - less than 99 visits to my site. Oh well.

At some point I may disconnect Analytics altogether and go back to analyzing server logs directly. That certainly solves the localhost entries. Today I only want to adjust Jekyll’s build process so that it skips analytics code when building drafts.

Turns out that the configuration documentation lists show_drafts: null down there in the default configuration. I only needed an unless block in my template.

{% unless site.show_drafts %}
  {% include analytics.html %}
{% endunless %}

I added a similar block for comments, since Disqus handles those - another external service I don’t need in draft mode.

You may need something more elaborate than this for your needs, such as ad code for a live site and a placeholder image when building drafts. No matter what the details, site.show_drafts provides the key that you need.


Got a comment? A question? More of a comment than a question? Talk to me about this post!

Indieweb Social

Did you mention this somewhere? I'd love it if you sent me the link!

disclaimer about timing

Mentions are sent to webmention.io. I fetch the latest mentions when building the site, so I may not see your feedback right away. Especially if my site's broken, which is often the case.

Public replies and mentions might be shared on the site, but I try to do a little quality check first.

Site Links