How to improve the Matomo API response time for segments
Slow Matomo API responses can be caused by real-time segment calculation rather than server resource limits or cron configuration. Matomo calculates ad-hoc segments on the fly, which can take longer to process, such as using segment=dimension2
in the API method=VisitsSummary.get&segment=dimension2==ABC&format=JSON
.
You can improve performance by saving the segment in the user interface and configuring it to be archived in the background. This ensures it is pre-processed, resulting in faster API responses for recurring requests, for example using a defined idSegment=1
in the API method=VisitsSummary.get&idSegment=1&format=JSON
.
- In the Matomo Dashboard, expand on the Segment Selector.
- Click Add new segment and create the segment you are using in your API call.
- In the segment editor, choose the configuration option, Segmented reports are pre-processed (faster, requires cron).
- Ensure your cron job is running regularly (e.g., hourly), so the segment data is processed during archiving.
- Update your API request to use the saved segment.
After saving the segment and running cron, monitor your API response time to confirm that the optimisation is working. You can also learn more about real-time and archived segments in the Matomo Developer Documentation.