# 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

- **Host System:** Ubuntu Server with CasaOS or Docker installed.
- **Storage Path:** /DATA/AppData/matomo
- **Port Availability:** Port 8484

### 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:

<table id="bkmrk-setting-value-databa" style="border-collapse: collapse; width: 100%; border: 1px solid #ddd;"><thead><tr style="background-color: #f2f2f2;"><th style="padding: 10px; border: 1px solid #ddd; text-align: left;">Setting</th><th style="padding: 10px; border: 1px solid #ddd; text-align: left;">Value</th></tr></thead><tbody><tr><td style="padding: 10px; border: 1px solid #ddd;">Database Server</td><td style="padding: 10px; border: 1px solid #ddd;">matomo\_db</td></tr><tr><td style="padding: 10px; border: 1px solid #ddd;">Login</td><td style="padding: 10px; border: 1px solid #ddd;">matomo</td></tr><tr><td style="padding: 10px; border: 1px solid #ddd;">Password</td><td style="padding: 10px; border: 1px solid #ddd;">matomo\_secure</td></tr><tr><td style="padding: 10px; border: 1px solid #ddd;">Database Name</td><td style="padding: 10px; border: 1px solid #ddd;">matomo</td></tr></tbody></table>

### 3. Implementation on BookStack

1. Log in to Matomo and navigate to **Administration &gt; Websites &gt; Manage**.
2. Copy the **JavaScript Tracking Code**.
3. In BookStack, navigate to **Settings &gt; 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 &gt; 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

- Open your BookStack homepage in a new tab.
- Navigate to the **Dashboard** in Matomo.
- Check the **Visits in Real-time** widget; your activity should appear within seconds.

*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

- **Matomo Site ID:** Ensure you have created a "Measurable" for Nextcloud. For this setup, the **Site ID is 3**.
- **Nextcloud Version:** Hub 28 Winter (v33.0.0).
- **App Version:** Matomo Tracking (v1.0.0).

---

## 2. Matomo Configuration

1. Log in to your Matomo instance at `https://matomo.goonersnas.com/`.
2. Navigate to **Administration (Cog Icon) &gt; Websites &gt; 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 &gt; Additional settings**.
2. Locate the **Matomo Tracking** section.
3. Enter the following details:

<div id="bkmrk-matomo-server-url%3A-h" style="background-color: #f4f4f4; color: #000000; padding: 15px; border: 1px solid #ddd; border-radius: 4px; font-family: 'Courier New', Courier, monospace;">Matomo Server Url: https://matomo.goonersnas.com/ Site ID: 3 [x] Track file browsing [x] Track user id</div>*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 &gt; Real-time &gt; Visitor Log**.
3. Look for active visits originating from your Nextcloud domain.

---

## 6. Troubleshooting &amp; Security

### Content Security Policy (CSP)

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

<div id="bkmrk-%27custom_csp_policy%27-" style="background-color: #f4f4f4; color: #000000; padding: 15px; border: 1px solid #ddd; border-radius: 4px; font-family: 'Courier New', Courier, monospace;">'custom_csp_policy' =&gt; "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;",</div>### 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.