Google analytics is easy to set up from the instructions which are provided by Google themselves. However, there are some quirks if you set it up with default settings. Mainly around if people are viewing the site via services such as Google translate.
The following should allow you to set up analytics without getting strange results when looking at the traffic in Google Analytics.
Once you have the Google analytics setup, Google says
paste your tracking code snippet (unaltered, in its entirety) before the closing </head> tag on every web page on your site you wish to track.
This would be error prone if we had to put it into every page we create so we are going to create a new file called
analytics.html in the
_includes folder in the Jekyll project. We don’t want to be posting the actual snippet from Google analytics because this will cause us problems later on, instead we want to use the following:
Don’t forget to add your tracking code to
Now we need to have the
analytics.html included on all our pages, this can be done by adding it to
head.html, if you are using the default layout, otherwise put the following snippet in the appropriate location.
You may have noticed that I’ve wrapped mine in an if statement, this is so that when testing on localhost I don’t get analytics sent from my testing.
The final step is to build the site with the production environment variable, the default for Jekyll is development. Since I am build my blog on Windows 10 and use powershell, the following command will get it to build with the analytics included
$env:JEKYLL_ENV="production"; jekyll build