I would like to use this gem
Can I add the <%= analytics_init if Rails.env.production? %> at the very bottom of the page?
Where is it recommended to put the GA script?
I'm just trying to make sure the analytics script doesn't slow down the site.
I would like to use this gem
Can I add the <%= analytics_init if Rails.env.production? %> at the very bottom of the page?
Where is it recommended to put the GA script?
I'm just trying to make sure the analytics script doesn't slow down the site.
添加您的 GA 脚本<%= analytics_init if Rails.env.production? %>的<head>标签app/views/layout/application.html.erb由 gem 处理,您唯一需要做的另一件事是添加您的 GA 跟踪器代码,config/environments/production.rb如下所示:
# replace this with your tracker code
GA.tracker = "UA-xxxxxx-x"
Google 建议您将脚本放入<head>:https ://developers.google.com/analytics/devguides/collection/gajs/
原因是您可以在页面中添加更多自定义跟踪器。如果你把它放在底部,你就失去了那个能力。
由于 Google GA 代码是异步的,因此无需担心页面加载性能。
起初,页面加载中没有要加载的外部脚本。在页面中,您使用来自 Rails 服务器的固定脚本加载完整的 html。然后加载 Google 的 gs 文件是脚本的工作,但这是关于 JS 执行,而不是页面加载。
因此,在放置脚本的位置上,页面加载和外部脚本加载没有区别。但是在<head>您获得更多好处时,请继续这样做。
对于判断生产环境,是的,这是必须的。