Add Privatus Analytics to Ruby on Rails
Add cookieless analytics to a Rails app with our privatus-analytics-rails gem and one layout helper, or with a plain script tag in production only.
With the gem#
Our gem is published on RubyGems.
- Add it to your
Gemfile:
ruby
gem "privatus-analytics-rails"
- Run
bundle install. - Set your site id, either as an environment variable (
PRIVATUS_SITE_ID=pa_YOURSITEID) or inconfig/environments/production.rb:
ruby
config.privatus_analytics.site_id = "pa_YOURSITEID"
- Put the helper in the
<head>ofapp/views/layouts/application.html.erb:
erb
<%= privatus_analytics_tag %>
The helper prints nothing without a site id and nothing outside production.
To see it while you build, set config.privatus_analytics.enabled = true.
If you would rather not touch your layouts, config.privatus_analytics.auto_inject = true
adds the tag with a Rack middleware. Every other setting maps to a tracker
attribute.
Without the gem#
Put the tag in app/views/layouts/application.html.erb inside <head>:
erb
<% if Rails.env.production? %>
<script defer src="https://privatusstats.com/js/pa.js" data-site="pa_YOURSITEID" data-modules="engage,auto,vitals,clicks,ask"></script>
<% end %>
Turbo and Turbolinks#
Rails apps with Turbo change pages with
history.pushState. The tracker follows that by default, so keep the tag in
the layout's <head> and nothing else is needed.