We use Matomo for web analytics on many client websites. Matomo keeps the data on a server that you control, and this is important for many of our clients. Often, a client also wants to see the analytics data in a different place. For example, in an internal dashboard or in a monthly report that the application makes.

The Matomo reporting API can give this data. But each request needs a module, a method, a site, a period, a date and an API key. We wanted a simpler way to write these requests, so we made a package: Laravel Matomo.

What the package does

  • It gives one resource for each Matomo API module, for example visits, referrers, goals and events.
  • It gives each API method a clear name. You do not need to remember the method names of Matomo.
  • It gives fluent methods for the site, the period, the date, the sort order and the number of rows.
  • It caches each response. By default, it keeps the response for 60 seconds.
  • It can connect to more than one Matomo instance.

Install the package

composer require bernskioldmedia/laravel-matomo

Add the URL of your Matomo instance and an API key to your .env file:

MATOMO_BASE_URL=https://analytics.example.com
MATOMO_API_KEY=your-api-key

Make the API key for a Matomo user that can only view the data. The package only reads data, so it does not need more access.

Get the data

Use the Matomo facade. Select a resource, then select the site, the period and the date. Then call the method that gets the data.

use BernskioldMedia\LaravelMatomo\Facades\Matomo;

$summary = Matomo::summary()
    ->site(1)
    ->month()
    ->lastMonth()
    ->all();

The summary() resource gives the core metrics: visits, unique visitors, actions, bounces and time on site.

For the other reports, use the other resources. For example, this request gets the ten websites that sent the most visits last week:

$websites = Matomo::referrers()
    ->site(1)
    ->week()
    ->lastWeek()
    ->sortByDesc('nb_visits')
    ->amount(10)
    ->websites();

Select the period and the date

Matomo uses a period and a date together. The period tells how to group the data. The date tells which data to get.

  • For the period, use day(), week(), month() or year().
  • For the date, use today(), yesterday(), lastWeek(), lastMonth() or lastYear().
  • For a range, use between($from, $to). You can give strings or date objects.
  • For the last number of periods, use lastX(30) or previousX(30).

The available resources

The package has resources for these Matomo modules:

  • Visits summary, visit frequency, visit time and visitor interest
  • Actions, such as page URLs, page titles, downloads, outlinks and site search
  • Referrers, such as search engines, websites, social networks and campaigns
  • Goals and ecommerce products
  • Events, with a secondary dimension for the action, name or category
  • Devices, screen resolutions, countries and languages
  • Content tracking, page performance and marketing campaigns
  • Live data, such as the visitor counters and the last visits

Control the cache

Each response stays in the Laravel cache for 60 seconds. For reports about past periods, the data does not change, so you can keep it longer:

Matomo::summary()->site(1)->month()->lastMonth()->cacheForMinutes(60)->all();

For live data, turn off the cache:

Matomo::live()->site(1)->dontCache()->counters(30);

Connect to more than one instance

Some clients have more than one Matomo instance. To use a different instance for one request, give the URL and the API key:

Matomo::summary()
    ->instance('https://stats.example.org', $apiKey)
    ->site(3)
    ->all();

A limit to know

The package does not cover all of the Matomo API. It covers the reports that we needed for dashboards and reports. A resource returns the response from Matomo without changes. If you need a different method, use get() on a resource with the name of the Matomo method.

Get the package

Laravel Matomo is open source, with the MIT license. The code is on GitHub. If you find a problem or have an idea, open an issue. We are happy to hear from you.