Search code examples
ruby-on-railsrubygoogle-analyticsanalytics

Google Analytics Performance Effects?


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.


Solution

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