Matomo

Setting Up Matomo Analytics with a Dedicated MariaDB Instance

Overview

This article details the process of deploying Matomo Analytics using a dedicated, isolated MariaDB database. This method ensures that your analytics data remains separate from other services (like Nextcloud), preventing database conflicts and simplifying maintenance.

Prerequisites

1. Docker Compose Configuration

Use the following configuration for a Custom Install in CasaOS:

services:
  matomo_db:
    image: mariadb:10.11
    container_name: matomo_db
    command: --max-allowed-packet=64MB
    environment:
      - MARIADB_ROOT_PASSWORD=matomo_root_password
      - MARIADB_DATABASE=matomo
      - MARIADB_USER=matomo
      - MARIADB_PASSWORD=matomo_secure
    volumes:
      - /DATA/AppData/matomo/db:/var/lib/mysql
    restart: unless-stopped

  matomo_app:
    image: matomo:latest
    container_name: matomo_app
    restart: unless-stopped
    ports:
      - "8484:80"
    environment:
      - MATOMO_DATABASE_HOST=matomo_db
      - MATOMO_DATABASE_DBNAME=matomo
      - MATOMO_DATABASE_USERNAME=matomo
      - MATOMO_DATABASE_PASSWORD=matomo_secure
    volumes:
      - /DATA/AppData/matomo/html:/var/www/html
    depends_on:
      - matomo_db

2. Database Connection Credentials

During the Matomo web-based setup wizard, use these internal credentials:

Setting Value
Database Server matomo_db
Login matomo
Password matomo_secure
Database Name matomo

3. Implementation on BookStack

  1. Log in to Matomo and navigate to Administration > Websites > Manage.
  2. Copy the JavaScript Tracking Code.
  3. In BookStack, navigate to Settings > Customization.
  4. Paste the script into the Custom HTML Head Content area and save.

Integrating Matomo Analytics with BookStack

Overview

Once Matomo is installed, you must link it to BookStack to begin capturing visitor data, page views, and search trends. This is done by injecting a small JavaScript snippet into the BookStack header.

1. Retrieve Tracking Code from Matomo

  1. Log in to your Matomo dashboard (e.g., http://192.168.0.152:8484).
  2. Click the Gear Icon (Administration) in the top right.
  3. On the left sidebar, navigate to Websites > Tracking Code.
  4. Ensure the correct website (e.g., "BookStack") is selected in the dropdown.
  5. In the JavaScript Tracking Code section, copy the entire block of code.

2. Apply Code to BookStack

  1. Open your BookStack instance and log in as an Administrator.
  2. Go to Settings (Gear icon in top navigation).
  3. Select Customization from the left-hand menu.
  4. Find the section labeled Custom HTML Head Content.
  5. Paste your copied Matomo script directly into the text area.
  6. Scroll to the bottom and click Save Settings.
<!-- Matomo Tracking Script Example -->
<script>
  var _paq = window._paq = window._paq || [];
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="//YOUR_SERVER_IP:8484/";
    _paq.push(['setTrackerUrl', u+'matomo.php']);
    _paq.push(['setSiteId', '1']);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
  })();
</script>

3. Verification

Note: If you use an ad-blocker, you may need to disable it for your local domain to see your own tracking data.

Integrating Nextcloud Hub 28 with Matomo Analytics

This article outlines the steps to connect your self-hosted Nextcloud instance (v33.0.0+) to Matomo for tracking user activity and file browsing.


1. Prerequisites


2. Matomo Configuration

  1. Log in to your Matomo instance at https://matomo.goonersnas.com/.
  2. Navigate to Administration (Cog Icon) > Websites > Manage.
  3. Verify that the URL for Site ID 3 is set to https://nc.goonersnas.com/.

3. Nextcloud App Installation

  1. Log in to Nextcloud as an Administrator.
  2. Go to Apps and select the Integration category.
  3. Search for "Matomo Tracking".
    • Note: Avoid the legacy "Piwik" version (v0.13.0) as it is incompatible with v33.
  4. Click Download and enable (you may need to click "Allow untested app").

4. Connection Settings

  1. Navigate to Administration settings > Additional settings.
  2. Locate the Matomo Tracking section.
  3. Enter the following details:
Matomo Server Url: https://matomo.goonersnas.com/ Site ID: 3 [x] Track file browsing [x] Track user id

Note: Ensure the URL includes the trailing slash. Settings save automatically upon losing focus from the text fields.


5. Verification

  1. Open Nextcloud in an Incognito/Private browser window and navigate through a few folders.
  2. Return to Matomo and go to Dashboard > Real-time > Visitor Log.
  3. Look for active visits originating from your Nextcloud domain.

6. Troubleshooting & Security

Content Security Policy (CSP)

If tracking fails to report, you may need to whitelist your Matomo domain in the Nextcloud config.php file:

'custom_csp_policy' => "default-src 'self'; script-src 'self' https://matomo.goonersnas.com; img-src 'self' data: https://matomo.goonersnas.com; connect-src 'self' https://matomo.goonersnas.com;",

Nginx Proxy Manager

Ensure that your Matomo proxy host does not have "Block Common Exploits" enabled if it interferes with the tracking .js delivery to external subdomains.