Google Analytics with Jekyll

Hi,

I’ve created a new Google Analytics account and added an analytics.html file under the _includes dir with the code for my GA account like

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-????????-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-????-1');
</script>

And it works but I’m not sure if I should add anything else so I can track of every page the user visits (because it looks like I’m only tracking the home page).

In this page https://markomatic.me/blog/jekyll-google-analytics/ it talks about the default.html file located in the _layouts directory and right above the closing </body> tag add {% include tracking.html %}

which I do not have.

Thanks for your help!

To answer your question, yes, you need that script on every page.

To start you off on the right path:

  1. Visit Google Tag Manager Quick Start Guide
  2. Notice that there are 2 scripts to install on every page you want tracked
    a. the JavaScript excerpt like your current script you have above (although it looks like you’re missing a few items [see their example], this is to be located in the
    b. The iframe that’s supposed to be located in the
  3. Now, once you have the correct scripts (visit your GTag manager dashboard and copy and paste both script), you have a decision to make, do you want to manually add them to every page, or you want to create 2 partials (I call them layoutPartials, but Jekyll refers to them as includes)? Let’s be efficient, and choose the includes path.
  4. Create 2 files in includes:
    a. src/_includes/GoogleTagManager_head.liquid
    b. src/_includes/GoogleTagManager_body.liquid
  5. In GoogleTagManager_head.liquid add the JavaScript snippet from GTag manager dashboard
  6. In GoogleTagManager_body.liquid add the non-JavaScript code/iFrame snippet from GTag manager dashboard
  7. So, if you’re still with me (and I hope you still are =), it’s time for you to reference these in the layout file you are using for your blog (actually, if you have more than 1, add to all layout files).
    a. In the of your layout, {% include GoogleTagManager_head.liquid %}
    b. In the of your layout, {% include GoogleTagManager_body.liquid %}

Hoping this helps get all your pages tracking well! Give thumbs up if it helps :+1: so others can learn as well as mark as the answer..

All the best

1 Like

Thanks Vsiege

Now, 2 days later it looks like it works with the google code i pasted. I have the statistics of the pages and viewers, so I think it works.

:grinning:Glad to hear it!

Hope the above can helps another one of the community. Take care.