Integrate Matomo into your Rails Application

Contents

piwik_analytics is a Gem for Ruby on Rails 3.x. After installing Matomo (Piwik) on your server and creating a website, you get a JavaScript tracking tag that you have to include in your website. When working with Rails the tracking tag will most likely be embedded in the application.html.erb view. It is not very convenient to include the tracking tag in your views.

The piwik_analytics gem provides an easy way to include Matomo into your application without messing up your view templates.

Installation

Add the piwik_analytics Gem to your Gemfile:

gem 'piwik_analytics', '~> 1.0.1'

Do not forget to run

bundle install

! Then run the generator:

rails g piwik_analytics:install

This will install a piwik.yml configuration file into the config directory of your application.

Configuration

Open up config/piwik.yml and edit the settings. Each setting is described in the config file itself.

# Configuration:
#
# disabled
#   false if tracking tag should be shown
# use_async
#   Set to true if you want to use asynchronous tracking
# url
#   The url of your piwik instance (e.g. localhost/piwik/
# id_site
#   The id of your website inside Matomo
#
production:
  piwik:
    id_site: 1
    url: piwik-production.example.com
    use_async: false
    disabled: false

development:
  piwik:
    id_site: 1
    url: piwik-development.example.com
    disabled: true
    use_async: false

test:
  piwik:
    id_site: 1
    url: localhost
    disabled: true
    use_async: false

As you can see, by default Matomo is only enabled in production mode. You can of course enable Matomo in the development or test environment by setting disabled: false. You will then need to fetch the site ID of the website you want to track from Matomo. Login to your Matomo Installation, go to « Settings » and click the « Websites » tab.

Matomo Site ID
Matomo Site ID

In this case the site ID is « 1 ». As a last step you need to set the URL of your Matomo installation. If your Matomo is hosted under http://example.com/piwik/ you need to set the URL to example.com/piwik (without the trailing slash).

Matomo supports an asynchronous tracking script since version 1.1. In case you want to use asynchronous tracking in your application, simply set use_async: true.

Usage

The gem provides a simple helper that outputs the tracking tag. Inside your application.html.erb (or haml, slim) you can simply add the following snippet just before the closing body tag.

<%= piwik_tracking_tag %>

Make sure you have disabled: false when you test the gem.

Get Support for piwik_analytics

To get support or for further information:

Enjoyed this post?
Join the 160,000+ subscribers who receive the Matomo Newsletter straight to their inbox every month

Subscribe to our newsletter to receive regular information about Matomo. You can unsubscribe at any time from it. This service uses MadMimi. Learn more about it within our privacy Policy page.

Get started with Matomo

A powerful web analytics platform that gives you and your business 100% data ownership and user privacy protection.

No credit card required.

Free forever.

Get started with Matomo

A powerful web analytics platform that gives you and your business 100% data ownership and user privacy protection.

No credit card required.

Free forever.