当主题不包含head.html时,在Jekyll中设置Google Analytics [英] Setting up Google Analytics in Jekyll when theme includes no head.html

查看:352
本文介绍了当主题不包含head.html时,在Jekyll中设置Google Analytics的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Jekyll和GitHub Pages搭建了这个静态页面( repo ),我想用Google Analytics(分析)来跟踪它。



我在关注本教程



但是我已经达到了说明的步骤:


最后,打开_includes / head.html并在结束标记之前添加以下代码。

实际上我的主题没有_include / head.html文件!

所以我的问题是,如果我创建一个名为_include / head.html的文件应该会自动包含在Jekyll建立的每个页面中? (我试图创建这样一个文件,并添加一个占位符图像,看看是否有效,但它没有)



该引号后面的代码应该包含在每个html中Jekyll建立的页面,对吧?就像,那是我想要它工作的,不是吗?所以如果我把它放在footer.html中,它应该可以工作吗?

解决方案

那么你可以在任何地方使用它(例如在你的布局中),所以每当你包含它时,它被呈现。



使用分析内容创建文件 _includes / head.html



然后在你的布局中包含你希望它出现的地方,如:

  {%包括head.html%} 

然后你可以把你头脑里所有的代码放在那里,所以您的布局更清晰



旁注



我更喜欢在Google建议之后立即使用分析代码打开< body> 标记。因此,我的默认布局如下所示:

 <!DOCTYPE html> 
< html>

{%include head.html%}

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

{{content}}

{%include footer。 html%}

< / body>

< / html>

_includes / ganalytics.html 分析代码。

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?

解决方案

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.

这篇关于当主题不包含head.html时,在Jekyll中设置Google Analytics的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆