Add Google Analytics to Jekyll
-
Sign up for new Google Analytics account using this link.
-
Paste the Google Analytics tracking code into a new file named
_includes/analytics.htmlfile under your Jekyll project. This step enables the tracking code template for use for all available Jekyll pages.
_includes/
├── analytics.html
- Insert
{% include analytics.html %}after{% include head.html %}line in the_layouts/default.htmlfile. This step ensures that the tracking code template will be included in all generated Jekyll pages.
{% include head.html %}
+ {% include analytics.html %}-
Run
jekyll buildto generate the Jekyll site using the Google analytics template. You will notice that Google Analytics tracking code is included in the generated pages. For example,_site/index.html. -
Otherwise, run
jekyll serveorbundle exec jekyll servecommand to serve and test your site locally atlocalhost:4000.
Credits
Read Michael Lee’s post or Jekyll documentation on further customizing Google analytics for Jekyll for use only in production environment.