Optimizing Matomo (Piwik) for Best Performance

Introduction

Are you looking to optimize Matomo (formerly known as Piwik) for the best performance? This article will guide you through various settings and configurations to ensure your Matomo installation runs smoothly and efficiently. Whether you’re handling large volumes of data or just want to make sure your analytics platform is as responsive as possible, these tips will help you achieve optimal performance.

Key Settings and Configurations

Server Environment

First, let’s ensure your server environment is properly configured. Matomo relies heavily on your server’s resources, so you should start by optimizing these:

  1. PHP Configuration: Increase memory limit and execution time.
  2. Database Optimization: Fine-tune your MySQL or MariaDB settings.
  3. Web Server Configuration: Optimize Apache or Nginx for better performance.

PHP Configuration

Matomo is a PHP-based application, so optimizing PHP settings is crucial.

memory_limit = 512M
max_execution_time = 300
post_max_size = 100M
upload_max_filesize = 100M

By increasing the memory limit, you can handle more extensive datasets. Extending the maximum execution time ensures that longer scripts have enough time to run without timing out.

Database Optimization

Your database is the backbone of your Matomo installation. Proper configuration can significantly impact performance.

[mysqld]
innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
query_cache_size = 64M
max_connections = 200

Increasing the InnoDB buffer pool size and log file size helps manage large amounts of data. Adjusting the query cache size can improve query performance.

Web Server Configuration

Depending on whether you use Apache or Nginx, specific optimizations can enhance performance.

Apache

<IfModule mpm_prefork_module>
    StartServers             5
    MinSpareServers          5
    MaxSpareServers         10
    MaxRequestWorkers       150
    MaxConnectionsPerChild   0
</IfModule>

Nginx

worker_processes auto;
worker_connections 1024;
keepalive_timeout 65;
client_max_body_size 100M;

Matomo-Specific Settings

Within Matomo, several settings can be adjusted to improve performance.

Archiving Reports

Matomo generates reports periodically. Configuring the archiving process can significantly impact performance.

# crontab -e

# Add the following line to archive reports every hour
0 * * * * /path/to/matomo/console core:archive --url=https://your-matomo-url.example

By setting up a cron job to archive reports, you offload the processing from real-time requests, which helps in reducing server load during peak times.

Enabling Cache

Caching can reduce the load on your server by storing frequently accessed data in memory.

[General]
enable_browser_archiving_triggering = 0
enable_sql_optimize_queries = 1
enable_caching = 1

Disabling browser-triggered archiving and enabling SQL optimization queries can lead to significant performance improvements. Enabling caching can help reduce database load and improve response times by storing frequently accessed data in memory.

Caching on the Web Server

In addition to Matomo-specific caching, configuring your web server for caching can further enhance performance. Here’s how you can set up caching for both Apache and Nginx.

Apache Caching

Apache supports several caching modules, such as mod_cache and mod_expires.

# Enable caching modules
LoadModule cache_module modules/mod_cache.so
LoadModule cache_disk_module modules/mod_cache_disk.so
LoadModule expires_module modules/mod_expires.so

# Configure caching
<IfModule mod_cache.c>
    CacheRoot "/var/cache/apache2/mod_cache_disk"
    CacheEnable disk "/"
    CacheDirLevels 2
    CacheDirLength 1
</IfModule>

# Set expiration headers
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/html "access plus 1 hour"
    ExpiresByType text/css "access plus 1 week"
    ExpiresByType application/javascript "access plus 1 week"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
</IfModule>

Nginx Caching

Nginx uses the proxy_cache and fastcgi_cache modules for caching.

# Enable caching
http {
    include       mime.types;
    default_type  application/octet-stream;

    # Proxy cache settings
    proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=cache_zone:10m max_size=1g inactive=60m use_temp_path=off;

    server {
        location / {
            proxy_pass http://your_matomo_backend;
            proxy_cache cache_zone;
            proxy_cache_valid 200 302 1h;
            proxy_cache_valid 404 1m;
            add_header X-Proxy-Cache $upstream_cache_status;
        }

        # Set expiration headers
        location ~* \.(css|js|jpg|jpeg|png|gif|ico)$ {
            expires 1M;
            access_log off;
            add_header Cache-Control "public";
        }
    }
}

Database Maintenance

Regular database maintenance is crucial to keep Matomo running efficiently. Here’s how you can perform routine maintenance:

  1. Optimize Tables: Regularly optimize your database tables to reclaim unused space and improve performance. OPTIMIZE TABLE piwik_log_visit, piwik_log_link_visit_action, piwik_log_conversion;
  2. Remove Old Data: Configure Matomo to automatically delete old raw data that is no longer needed. [General] delete_logs_older_than_days = 180 delete_reports_older_than_days = 365
  3. Run Database Repairs: Regularly check and repair your database tables to prevent corruption. CHECK TABLE piwik_log_visit, piwik_log_link_visit_action, piwik_log_conversion; REPAIR TABLE piwik_log_visit, piwik_log_link_visit_action, piwik_log_conversion;

Practical Usage

Implementing these settings can lead to faster page load times and more efficient data processing. This is especially beneficial for websites with high traffic, as it ensures that your analytics data is updated and accessible without overloading your server.

Questions and Answers

Q: How can I monitor Matomo’s performance?

A: You can use the Matomo System Check tool under the Diagnostics menu. It provides insights into your current setup and suggests optimizations.

Q: What are the benefits of setting up a cron job for archiving?

A: Setting up a cron job ensures that reports are generated during off-peak hours, reducing the load on your server during high traffic periods.

Q: How do I know if I need to increase my PHP memory limit?

A: If you notice frequent out-of-memory errors or slow performance during high traffic, increasing the PHP memory limit can help.

Q: Is it necessary to optimize both the web server and database?

A: Yes, both the web server and database play critical roles in performance. Optimizing both ensures a balanced load distribution and efficient data handling.

Q: Can I use Matomo on shared hosting?

A: While Matomo can run on shared hosting, for best performance, a dedicated server or VPS is recommended, especially for high-traffic websites.

Matomo Plugins

  • Plugins can enhance functionality but may impact performance. Learn how to manage and optimize them. Matomo Plugin Guide

Data Privacy with Matomo

Custom Reporting in Matomo

Scaling Matomo for Large Websites

  • Techniques and strategies for scaling Matomo to handle large volumes of data. Scaling Matomo Guide

Conclusion

Optimizing Matomo for best performance involves a combination of server, database, and application-level tweaks. By following the guidelines provided, you can ensure that your Matomo installation runs smoothly and efficiently. Feel free to try out these settings and let me know how they work for you. If you have any questions, please ask in the comments below!