Search code examples
google-analyticsjekyllgithub-pages

Google Analytics tag not working on Jekyll with GH Pages


I'm trying to add a Google Analytics tag to this website, built with Jekyll and hosted on Github Pages (repo). I can't get the tag to work.

I've followed this tutorial exactly for Google Analytics with Jekyll. I added an _includes/analytics.html file with the script, and edited my config.yml and _layouts/default.html files accordingly. I've tried it both with a Universal Analytics property ("UA-" ID) and the new Google Analytics 4 property ("G-" ID), but the tag is not working.

I've waiting for over 24 hours multiple times, followed all the troubleshooting guidelines, but the tag is clearly not installed properly. There is no tracking code that is added to the head of any files in the _site folder, Google Tag assistant is not identifying any tags, I don't see any Analytics hits in the Network tab, and no data is showing up on my Analytics page.

What could I be doing wrong here? Maybe the {% include analytics.html %} line is supposed to go somewhere other than my default.html file?


Solution

  • You can see your default templates in the _config.yml file. Notice the layout for pages is page. In layouts/page.html you have not included {% include analytics.html %}. You have a couple of options:

    1. Add your conditional to the page layout.
    2. Don't duplicate the content in the page and default layouts. You can make whatever changes need making to the page layout (meaning the differences between that layout and the post layout, for example) and then pass the page layout to the default layout. You can see an example in the minima theme.
    3. Change the defaults in the site config file so that your pages go to the default layout.