If you’ve set up a Jekyll site via GitHub Pages it’s pretty easy to add Google Analytics to your site. Analytics is a “free” service from Google and can help you improve the usefulness of your site with insight into user behavior.

Google Analytics

Prerequisites

Implementation

Open terminal and change to your site’s root directory, e.g.

$ cd ~/path/to/your/site/username.github.io/

Open _layouts/default.html in your favorite text editor, e.g.

$ nano _layouts/default.html

Under {% include head.html %} add the following line:

{% include analytics.html %}

`default.html`

Visit https://analytics.google.com and copy your Google Analytics tracking code

Create a new file in _includes/, paste your tracking code in it and save it as analytics.html, e.g.

$ nano _includes/analytics.html

`analytics.html`

Test

Verify that your site is working locally

$ bundle exec jekyll serve

Copy the Server Address, e.g.

JekyllAdmin mode: production
  Server address: http://127.0.0.1:4000/    # open in your browser to view your site locally
Server running... press ctrl-c to stop.

While you’re viewing your site, open your web inspector to view the page as source and check to see if the tracking code is visible

You should see the contents of analytics.html after the closing </head> tag, e.g.

View your site pages' source to verify that your ga-analytics script is included

Check that you see this code on all (or at least some of) your site’s pages.

Deploy

If everything looks good, commit and push your changes to github so you can view them on your site

$ git add .
$ git commit -m "added ga-tracking code to enable Google-Analytics"
$ git push

Finally, confirm analytics is running on the live site and enjoy the beautiful data!