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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9047323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 08:29:27  来源:igfitidea点击:

Google Analytics - Can I put script in footer?

phpjavascriptgoogle-analytics

提问by Kevin

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!!

谢谢!!

回答by MMM

You can.

你可以。

The only difference is that if you put in the "footer" (i.e. just before the </body>tag), the browser will first load the DOM (i.e. everything up to the scripttag) and then start to load the 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).

唯一的区别是,如果您放入“页脚”(即就在</body>标签之前),浏览器将首先加载 DOM(即标签之前的所有内容script),然后开始加载脚本。如果您将它放在标题中,它将尝试在加载 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>将其放在

回答by Natalie K

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. 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.

我建议将分析代码保存在文件中并将其包含在页脚或页眉中 - 例如:. 我最近在 Firefox 和 IE 中遇到了一些关于页面加载和分析的问题。它将页面加载到存在分析代码的位置,然后页面无限期挂起。我可以刷新页面并且它可以正常加载,但是由于某种原因它正在发生并且关于解决方案的信息很少。因此,就我而言,我已将代码包含在页脚或底部页面的页脚中。这样,对于您的网站访问者,他们甚至可能不会注意到,因为您页面的其余部分将加载,一旦他们点击链接,它就不应该再次挂起。

回答by fozylet

It's possible and is in fact the way many plugins do it (especially most WordPress plugins I've seen). 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.

这是可能的,事实上许多插件都是这样做的(尤其是我见过的大多数 WordPress 插件)。实际上,< /body>如果在页面加载期间不需要这些JS,建议将所有 JS 放在 html 的末尾(就在 之前)。

回答by pagid

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.

我会说你可以 - 但比这更容易。只需将它放在页脚中,然后检查实时跟踪是否找到了访问者,如果您有答案的话。

回答by ba0708

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. The point is that putting scripts there still works. Therefore, I see no problem in putting it at the bottom of the page.

我见过人们把它放在页面上的几乎任何地方。你可以在页面的任何地方放置脚本标签;许多人喜欢将它们保留在<head>标签中,而其他人则将它们放在底部以避免使用document.ready. 关键是将脚本放在那里仍然有效。因此,我认为将它放在页面底部没有问题。

回答by Zar

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? Unless every page has unique footer/header page, of course. Then I'd recommend you to take a look at MVC.

如果您有页眉和页脚文件,为什么不将您的 Analytics 代码附加到其中?当然,除非每个页面都有唯一的页脚/页眉页。然后我建议你看看MVC