Matomo js script insertion is too opinionated

Created on 27 May 2021, almost 4 years ago
Updated 20 January 2025, about 1 month ago

Problem/Motivation

In hook_page_attachments(), the Matomo module is using a javascript snippet to force the insertion of the matomo.js script at the top of head. This is making it difficult for other modules to ensure that their script appears before it. This is needed in the case of cookie compliance banner scripts which need to run before other scripts like matomo.

Proposed resolution

Rather than including javascript to create a script element and insert it into the head, the module should instead use hook_page_attachments() to add the external js file and set #weight to -100 or similar to ensure it is loaded early on. Other modules, like cookie compliance ones or custom ones, can either set their own script weight to be lower again, or use hook_page_attachments_alter() to modify the matomo one if needed for their site.

The attached patch does this. It removes the js which does the manual insertion and adds two page attachments - one for the external js file, and one for the _paq.push() calls to come after it.

โœจ Feature request
Status

Needs work

Version

1.0

Component

Code

Created by

๐Ÿ‡ฎ๐Ÿ‡ชIreland stella

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024