by
back

How to use Google Analytics in production environment only

Basically there are to effective ways to control the distribution of your Google Analytics Tracking Code in your Ruby on Rails application.

The first approach is more simple but doesn't fit every need. We use Rails ActionDispatch to check for the current domain, especially the subdomain "www" and only in cases were both are set, we provide the sharing code.

<% if request.subdomain.present? && request.subdomain == "www" %>

        // add your google analytics code snippet here

<% end %>

The second approach is more likely the "rails way". Using Rails.env to get the current environment the server is setup to, we can check for the production environment and simple add the needed code as seen below. You can use this way to get statistics for other environments as well. Just add a second condition to check for the required environment like "staging" or "development".

<% if Rails.env == "production" %>

        // add your google analytics code snippet here

<% end %>

Cheers,
Flo

comments powered by Disqus