Setting up Google Analytics in Jekyll when theme i

2019-05-29 03:05发布

问题:

I have this static page built with Jekyll and host with GitHub Pages (repo), and I wanted to track it with Google Analytics.

I was following this tutorial.

But I've reached the step where it says:

Finally, open _includes/head.html and add the following code just before the end tag.

And actually my theme has no _include/head.html file!

So my question is, if I create a file called _include/head.html should it be automatically included in every page built by Jekyll? (I tried creating such a file and adding a placeholder image to see if that worked but it didn't)

The code that follows that quote should be included in every html page built by Jekyll, right? Like, that is what I want for it to work, no? So if I put it in footer.html instead it should work?

回答1:

If you create that file, as the tutorial suggests, then you can use it everywhere, (in your layout for example) so every time you include it, it gets rendered.

Create the file _includes/head.html with the analytics content.

Then in your layout include it where you want it to appear like:

{% include head.html %}

Then you can place all your code that goes in your head there, so you have a cleaner layout

side note

I prefer to have the analytics code following Google recommendation immediately after the opening <body> tag. So my default layout looks like:

<!DOCTYPE html>
<html>

  {% include head.html %}

  <body>
    {% include ganalytics.html %}
    {% include header.html %}

      {{ content }}

    {% include footer.html %}

  </body>

</html>

and _includes/ganalytics.html just contain the analytics code.



回答2:

Simple add analytics marker in _layouts/default.html head tag. No need to add a head include.