How do I fix an « Allowed memory size exhausted » error in Media Analytics?
In some very rare cases you might see a memory error caused by Media Analytics if you are tracking hundreds of thousands of different media URLs per day. This can typically be the case if every media resource URL includes a random URL parameter such as key
in the below example:
https://example.com/media.mp4?key=123
https://example.com/media.mp4?key=456
In this case you could avoid this memory issue by excluding the URL parameter that changes every time. In above example the URL parameter to exclude would be key
. To exclude this parameter locate your local config file in $matomoDir/config/config.ini.php
and tweak the media_analytics_exclude_query_parameters
setting which by default only excludes the two URL parameters enablejsapi
and player_id
:
[MediaAnalytics]
media_analytics_exclude_query_parameters=enablejsapi,player_id,key
Simply append any randomly changing URL parameter to the media_analytics_exclude_query_parameters
setting separated by a comma. This will not only fix the memory issue but also improve your reports since the number of views and plays will no longer be always 1
but can be accurately calculated based on the number of visits that viewed and played a particular media.
If you want to fix the issue retrospectively, you can execute the following SQL query in your database. Make sure to adjust the matomo_
database table prefix if needed, the idSite, and the URL parameter key
update matomo_log_media set resource = substring_index(resource, '?key', 1) where idsite = 1 and server_time > date_sub(NOW(), INTERVAL 1 week) and resource like '%?key%'
If the above does not fix your issue
If you are still getting a memory error from Media Analytics you may want to limit the number of rows per subtable by changing the default value of 1000
to 500
or an even lower value in your config/config.ini.php
:
[MediaAnalytics]
datatable_archiving_maximum_rows_subtable_media=500
Starting with version 4.2.0 of the plugin, you have the option of limiting the results that are used to generate the archived table. By default, the primary results will be limited to 10,000 rows, ranking them so that the resources with the most plays will be in those first 10,000 rows. Any remaining resources will be grouped together under the Other
row. When you expand a resource in the report, it shows secondary information specific to that resource. By default, those results are limited to 75,000 rows to be divided amongst the 10,000 resources. If you find that the secondary information seems to be incomplete, you can increase the secondary limit or decrease the primary limit. Decreasing the primary limit will also decrease the likelihood of running out of memory while MediaAnalytics reports are being archived. Setting the new configs in your config/config.ini.php
file would look something like the following:
[MediaAnalytics]
archiving_ranking_query_row_limit_primary = 5000
archiving_ranking_query_row_limit_secondary = 70000
For Matomo cloud customers, please contact support and they should be able to adjust those configs for you.