简体   繁体   English

Google Analytics - 我可以将脚本放在页脚中吗?

[英]Google Analytics - Can I put script in footer?

I want to add google analytics to our website and have read some conflicting info about where to put the script tag.我想将谷歌分析添加到我们的网站,并阅读了一些关于脚本标签放置位置的冲突信息。

Google says to put it before the closing </ head> tag: http://code.google.com/apis/analytics/docs/tracking/asyncTracking.html谷歌说把它放在结束</ head>标签之前: http : //code.google.com/apis/analytics/docs/tracking/asyncTracking.html

The way our site is designed, this would mean making an edit to every page.我们网站的设计方式意味着对每个页面进行编辑。 It's not that big of a deal if I need to do this, however, our site also uses header and footer include files.如果我需要这样做,这没什么大不了的,但是,我们的站点也使用页眉和页脚包含文件。

These header & footer files have html code in them and fall just inside the <body> and </body> tags once the page is loaded.这些页眉和页脚文件中包含 html 代码,并在页面加载后位于<body></body>标签内。

It would be so much easier to just add the script to the header or the footer file.将脚本添加到页眉或页脚文件会容易得多。 I'd just paste it in there once and it would be serving up the code on every page.我只需将它粘贴在那里一次,它就会在每个页面上提供代码。

My question is: Can I do this?我的问题是:我可以这样做吗? Can I move the script snippet inside footer include file even though it's not before the </head> tag as google suggests?我可以在页脚包含文件中移动脚本片段,即使它不在</head>标签之前,正如谷歌建议的那样?

Am I asking for problems if i do it this way?如果我这样做,我会问问题吗?

Thanks!!谢谢!!

You can.你可以。

The only difference is that if you put in the "footer" (ie just before the </body> tag), the browser will first load the DOM (ie everything up to the script tag) and then start to load the script.唯一的区别是,如果您放入“页脚”(即就在</body>标记之前),浏览器将首先加载 DOM(即直到script标记的所有内容),然后开始加载脚本。 If you put it in the header, it will try to load the script either before it loads the rest of the DOM or while it is loading it (depending on the browser).如果您将它放在标题中,它将尝试在加载 DOM 的其余部分之前或在加载它时(取决于浏览器)加载脚本。

You might feel a difference only if you have a huge page with a lot of elements, but the only difference really would be that the script would be loaded later, so if someone aborts a load of the page, the script might not get executed.只有当您拥有一个包含大量元素的大页面时,您才会感觉到不同,但唯一的区别实际上是脚本将在稍后加载,因此如果有人中止页面加载,脚本可能不会被执行。

Thus, Google recommends to place it in the <head> and if you can, you should, however you also can put it somewhere else if that makes life easier.因此,Google 建议将它放在<head> ,如果可以,您应该将其放在其他地方,如果这样可以使生活更轻松。

I would recommend saving the analytics code in a file and including it in your footer or header - Example: .我建议将分析代码保存在文件中并将其包含在页脚或页眉中 - 例如:. I've recently run into some issues with Firefox and IE regarding the page load and analytics.我最近在 Firefox 和 IE 中遇到了一些关于页面加载和分析的问题。 It loads the page to the point where the analytics code is present and then the page hangs indefinitely.它将页面加载到存在分析代码的位置,然后页面无限期挂起。 I can refresh the page and it loads fine, but for some reason it's happening and there's very little info out there regarding solutions.我可以刷新页面并且它可以正常加载,但是由于某种原因它正在发生并且关于解决方案的信息很少。 So, in my case I've included the code in the Footer or of the page at the bottom.因此,就我而言,我已将代码包含在页脚或底部页面的页脚中。 This way, in regards to your site visitors, they'll likely not even notice because the rest of your page will load and once they click on a link it shouldn't hang again.这样,对于您的网站访问者,他们甚至可能不会注意到,因为您页面的其余部分将加载,一旦他们点击链接,它就不应该再次挂起。

I'd say you can - but easier than that.我会说你可以 - 但比这更容易。 Just place it in the footer and check if the Live-tracking finds visitors if it does you have the answer.只需将其放在页脚中,然后检查实时跟踪是否找到了访问者,如果您有答案的话。

It's possible and is in fact the way many plugins do it (especially most WordPress plugins I've seen).这是可能的,事实上许多插件都是这样做的(尤其是我见过的大多数 WordPress 插件)。 In fact, putting all your JS at the end of the html (just before < /body> ) is recommended if those are not required during the load of the page.事实上,如果在页面加载期间不需要这些 JS,建议将所有 JS 放在 html 的末尾(就在< /body>之前)。

I have seen people put it pretty much anywhere on the page.我见过人们把它放在页面上的几乎任何地方。 You can put script tags anywhere on the page;你可以在页面的任何地方放置脚本标签; many prefer to keep them in the <head> tag, while others put them at the bottom to avoid using document.ready .许多人喜欢将它们保留在<head>标签中,而其他人则将它们放在底部以避免使用document.ready The point is that putting scripts there still works.关键是将脚本放在那里仍然有效。 Therefore, I see no problem in putting it at the bottom of the page.因此,我认为将它放在页面底部没有问题。

Yes, I believe that it would work - it's not recommended though.是的,我相信它会起作用——但不推荐这样做。

f you have a header and footer file, why not just append your Analytics code to it?如果您有页眉和页脚文件,为什么不将您的 Analytics 代码附加到其中? Unless every page has unique footer/header page, of course.当然,除非每个页面都有唯一的页脚/页眉页。 Then I'd recommend you to take a look at MVC .然后我建议你看看MVC

Observations have already been made here regarding the scripts placement which can be taken on a site by site basis.此处已经对可以逐个站点进行的脚本放置进行了观察。 We run Google Analytics using Google Tag Manager and use the header code placement in the footer and have no problems with that.我们使用 Google Tag Manager 运行 Google Analytics,并使用页脚中的页眉代码放置,并且没有任何问题。 Google is currently rolling out Core Web Vitals which places website performance as a must have, and so optimising code delivery is really important.谷歌目前正在推出 Core Web Vitals,它将网站性能作为必备条件,因此优化代码交付非常重要。 Just wanted to add this here as it was pertinant.只是想在这里添加这个,因为它是相关的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM