Knowledge Base

How to use Piwik with Drupal

This article describes how to integrate Drupal with the open-source Piwik analytics platform.

Using Piwik with Drupal

Piwik gathers detailed statistics about visitor traffic to your site. You can integrate Piwik with Drupal using either of the following methods:

  • Installing and configuring a Drupal module.
  • Editing the Drupal theme directly.

The following sections describe how to do both of these methods.

Before you can use Piwik with Drupal, you must first install the Piwik application files. There are two ways to do this:

  • If your hosting account includes Softaculous, you can use it to install Piwik.
  • If your hosting account does not include Softaculous, or if you want to install the newest version, you can install Piwik manually.
Method #1: Use a module

There are several modules available for Drupal that provide Piwik integration, but Piwik's own site recommends Piwik Web Analytics. This section describes how to install and configure this plugin.

Installing Piwik Web Analytics

To install the Piwik Web Analytics module on your Drupal site, follow these steps:

  1. Use your web browser to visit https://www.drupal.org/project/piwik.
  2. Download the .zip file for your Drupal version to your local computer.
  3. Log in to your Drupal site as the administrator.
  4. On the top menu bar, click Modules, and then click Install new module.
  5. Click Browse, select the .zip file on your local computer, and then click Install. Drupal installs the module.
  6. On the top menu bar, click Modules.
  7. Under STATISTICS, locate Piwik Web Analytics, select the ENABLED check box, and then click Save configuration. The module is now installed and activated.
Configuring Piwik Web Analytics

After you install the Piwik Web Analytics module, you must configure it to work with your Piwik installation. To do this, follow these steps:

  1. Log in to your Drupal site as the administrator.
  2. On the top menu bar, click Configuration.
  3. Under SYSTEM, click Piwik. The Piwik configuration page appears.
  4. Under GENERAL SETTINGS, in the Piwik site ID text box, type the Piwik ID number for your site.
    To obtain the Piwik ID number for your site, follow these steps:
    1. Log in to Piwik.
    2. On the top right of the page, click All Websites. The All Websites dashboard page appears.
    3. In the WEBSITE column, click the name of the website that you want to track.
    4. In the browser address bar, look for the idSite= string. The number following the idSite= string is the Piwik site ID number. For example, in the following URL the site ID number is 3:
      http://example.com/piwik/index.php?module=CoreHome&action=index&date=yesterday&period=day&idSite=3
      
  5. In the Piwik HTTP URL text box, type the full URL to your Piwik installation. For example, if your domain name is example.com, and you installed Piwik in the piwik subdirectory, the full URL is http://example.com/piwik.
  6. If you have an SSL certificate for your site, in the Piwik HTTPS URL text box, type the full secure URL to your Piwik installation. For example, if your domain name is example.com, and you installed Piwik in the piwik subdirectory, the full secure URL is https://example.com/piwik.
  7. In the Tracking scope section, you can configure additional settings to customize your site's tracking behavior.
  8. Click Save configuration. Piwik is now enabled for Drupal.
Method #2: Edit the theme

Modules are convenient, but they also carry processing overhead with them that may affect site performance. If you prefer not to use a module, you can edit the Drupal theme directly to enable Piwik.

To enable Piwik by editing the theme, follow these steps:

  1. Log in to your Piwik account as the administrator:
    • On the top right of the page, click your administrator username.
    • Under Manage, click Tracking Code.
    • Under JavaScript Tracking Code, copy the entire text in the tracking code box, including the starting <!—Piwik—> tag and the ending <!—End Piwik Code—> tag.
  2. Using SSH or the cPanel File Manager, navigate to the ~/public_html/themes/theme_name/templates directory, where theme_name represents the theme currently enabled for your site.

    If you installed Drupal in a subdirectory beneath public_html, navigate to that directory instead, and then navigate to the themes/theme_name/templates directory.
  3. Use your preferred text editor to open the page.tpl.php file.
  4. At the bottom of the page.tpl.php file, paste the tracking code snippet that you obtained in step 1.
  5. Save your changes to the page.tpl.php file. Piwik is now enabled for all pages on your Drupal site. To view site statistics, log in to your Piwik account.

More Information

For more information about Piwik, please visit https://piwik.org.