如何使用jade文件中的谷歌分析

时间:2023-01-27 15:18:33

I want to track the users of my website. Since I do not have an old fashioned HTML file, should I adapt the given code to the jade syntax or can i leave the script untouched and include it somehow?

我想跟踪我网站的用户。由于我没有旧的HTML文件,我应该将给定的代码调整为jade语法,还是可以保持脚本不变并以某种方式包含它?

In case I need to convert it to jade syntax, can this be auto generated by some tool.

如果我需要将其转换为jade语法,可以通过某些工具自动生成。

<script>
   (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
   (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
   m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-XXXXXXXX-X', 'domain.com');
  ga('send', 'pageview');
</script>

3 个解决方案

#1


49  

Instead of having it look for another code file and load it. Inline (like Trevor suggested is better).

而不是让它寻找另一个代码文件并加载它。内联(像Trevor建议的更好)。

In order to accomplish this you have to make use of the script. tag.... not just script

为了实现这一点,您必须使用该脚本。标签......不仅仅是脚本

See below:

见下文:

script.
            (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
            m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
            })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

            ga('create', 'UA-11111111-1', 'yourdomain.com');
            ga('send', 'pageview');

Make sure the spacing and such is actually indented one tab from your stuff

确保间距等实际上是从您的东西缩进一个标签

#2


12  

The solution was easy.

解决方案很简单。

Step 1: i created a file called 'analytics.js' and placed the code between the script tags in it. No conversion to jade syntax needed.

第1步:我创建了一个名为“analytics.js”的文件,并将代码放在脚本标记之间。不需要转换为jade语法。

Step 2: i referenced the script from the jade file

第2步:我从jade文件中引用了脚本

script(src='/js/analytics.js')

#3


3  

As said here before - putting the GA snippet inline in your files is recommended, as the GA snippet will actually call another file (www.google-analytics.com/analytics.js), so it's a bit of a waste to GET another js file which will then GET the Google one.

如前所述 - 建议将GA片段内联到您的文件中,因为GA片段实际上会调用另一个文件(www.google-analytics.com/analytics.js),因此获取另一个js有点浪费然后获取谷歌的文件。

However - if you do want to keep your layout.jade clean (and you should), you can put the code snippet BRogers wrote (the one that starts with "script.") , in a separate file, call it googleAnalytics.jade, and in your layout.jade just include :

但是 - 如果你想保持你的layout.jade干净(你应该),你可以把BRogers写的代码片段(以“script。”开头的那个)放在一个单独的文件中,称之为googleAnalytics.jade,在你的layout.jade中只包括:

include partials/googleAnalytics

(of course partials is the way I folder my views, that's up to you)

(当然,部分是我记录我的观点的方式,这取决于你)

This will mean the GA snippet will be included in your pages on the server, saving you the additional GET, but still keeping your layout.jade nice and tidy.

这意味着GA代码段将包含在服务器上的页面中,为您节省额外的GET,但仍然保持layout.jade的整洁。

#1


49  

Instead of having it look for another code file and load it. Inline (like Trevor suggested is better).

而不是让它寻找另一个代码文件并加载它。内联(像Trevor建议的更好)。

In order to accomplish this you have to make use of the script. tag.... not just script

为了实现这一点,您必须使用该脚本。标签......不仅仅是脚本

See below:

见下文:

script.
            (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
            m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
            })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

            ga('create', 'UA-11111111-1', 'yourdomain.com');
            ga('send', 'pageview');

Make sure the spacing and such is actually indented one tab from your stuff

确保间距等实际上是从您的东西缩进一个标签

#2


12  

The solution was easy.

解决方案很简单。

Step 1: i created a file called 'analytics.js' and placed the code between the script tags in it. No conversion to jade syntax needed.

第1步:我创建了一个名为“analytics.js”的文件,并将代码放在脚本标记之间。不需要转换为jade语法。

Step 2: i referenced the script from the jade file

第2步:我从jade文件中引用了脚本

script(src='/js/analytics.js')

#3


3  

As said here before - putting the GA snippet inline in your files is recommended, as the GA snippet will actually call another file (www.google-analytics.com/analytics.js), so it's a bit of a waste to GET another js file which will then GET the Google one.

如前所述 - 建议将GA片段内联到您的文件中,因为GA片段实际上会调用另一个文件(www.google-analytics.com/analytics.js),因此获取另一个js有点浪费然后获取谷歌的文件。

However - if you do want to keep your layout.jade clean (and you should), you can put the code snippet BRogers wrote (the one that starts with "script.") , in a separate file, call it googleAnalytics.jade, and in your layout.jade just include :

但是 - 如果你想保持你的layout.jade干净(你应该),你可以把BRogers写的代码片段(以“script。”开头的那个)放在一个单独的文件中,称之为googleAnalytics.jade,在你的layout.jade中只包括:

include partials/googleAnalytics

(of course partials is the way I folder my views, that's up to you)

(当然,部分是我记录我的观点的方式,这取决于你)

This will mean the GA snippet will be included in your pages on the server, saving you the additional GET, but still keeping your layout.jade nice and tidy.

这意味着GA代码段将包含在服务器上的页面中,为您节省额外的GET,但仍然保持layout.jade的整洁。