Docs
De docs doorbladeren

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.

Als Markdown bekijken

With the gem#

Our gem is published on RubyGems.

  1. Add it to your Gemfile:
ruby
   gem "privatus-analytics-rails"
  1. Run bundle install.
  2. Set your site id, either as an environment variable (PRIVATUS_SITE_ID=pa_YOURSITEID) or in config/environments/production.rb:
ruby
   config.privatus_analytics.site_id = "pa_YOURSITEID"
  1. Put the helper in the <head> of app/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 %>

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.