Reputation: 2293
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.
Upvotes: 0
Views: 98
Reputation: 24815
You are recommended by Google to put the script in <head>
: https://developers.google.com/analytics/devguides/collection/gajs/
The reason is you can add more custom trackers in the page. If you put it in bottom, you lost that ability.
There is no concern on page loading performance as Google GA code is async.
At first, there is no external script to load in page loading. In page you load full html with the fixed script from Rails server. Then it's the script's job to load Google's gs file, but that is about JS execution, not page loading.
So there is no difference in page loading and external script loading on where you put the script. But in <head>
you get more benefits, so go with that.
For judging production env, yes, that is a must.
Upvotes: 1
Reputation: 3400
add <%= analytics_init if Rails.env.production? %>
in the <head>
tag of your app/views/layout/application.html.erb
the GA script is handled by the gem the only other thing you need to do is to add your GA tracker code in config/environments/production.rb
like this:
# replace this with your tracker code
GA.tracker = "UA-xxxxxx-x"
Upvotes: 1