Live Demo: Try this and other plugins live β no installation, no risk: demo.markus-michalski.net
Flow: ViewConfigExtension reads the settings through MatomoConfigFactory as an immutable MatomoConfig, asks the ConsentChecker, and renders the JavaScript via TrackingCodeRenderer and EcommerceTracker. When an order is saved, the OrderSubscriber builds a MatomoEcommerceData object and stores it in the session. On the confirmation page, the ThankYouControllerExtension reads it from the session and generates the JavaScript with the EcommerceTracker.
βββ metadata.php
βββ services.yaml
βββ composer.json
βββ src/
β βββ Config/
β β βββ MatomoConfig.php
β β βββ MatomoConfigFactory.php
β βββ Controller/
β β βββ MatomoOptOutController.php
β βββ DataTransfer/
β β βββ MatomoEcommerceData.php
β βββ EventSubscriber/
β β βββ OrderSubscriber.php
β βββ Extension/
β β βββ Application/Controller/ThankYouControllerExtension.php
β β βββ Core/ViewConfigExtension.php
β βββ Service/
β βββ ConsentChecker.php
β βββ EcommerceTracker.php
β βββ TrackingCodeRenderer.php
βββ views/
βββ admin_twig/{de,en}/mmd_matomo_analytics_lang.php
βββ twig/
βββ {de,en}/mmd_matomo_analytics_lang.php
βββ page/matomo_optout.html.twig
βββ widget/matomo_optout.html.twig
βββ extensions/themes/apex/
βββ layout/base.html.twig
βββ page/checkout/thankyou.html.twig
| Entry | Value |
|---|---|
| Module ID | mmd_matomo_analytics |
| Extended classes | ViewConfig (ViewConfigExtension), ThankYouController (ThankYouControllerExtension) |
| Controller | matomo_optout (MatomoOptOutController) |
| Templates | @mmd_matomo_analytics/page/matomo_optout.html.twig, @mmd_matomo_analytics/widget/matomo_optout.html.twig |
| Setting groups | mmd_matomo_basic, mmd_matomo_privacy, mmd_matomo_ecommerce, mmd_matomo_advanced |
| PHP namespace | Mmd\MatomoAnalytics\ |
| Class | Responsibility |
|---|---|
MatomoConfigFactory |
Reads the settings of the current shop and creates MatomoConfig |
MatomoConfig |
Immutable value object, checks isValid() and the HTTPS URL |
ConsentChecker |
Server-side check of the DNT header and consent cookies |
TrackingCodeRenderer |
Generates the tracking code |
EcommerceTracker |
Generates the e-commerce calls for product view, cart and order |
MatomoEcommerceData |
Data object for product view, cart update and order |
OrderSubscriber |
Reacts to AfterModelUpdateEvent; only processes Order models with an order number (oxordernr) and stores the order data in the session |
ViewConfigExtension |
Extends ViewConfig with the template methods (see Storefront and Templates) |
ThankYouControllerExtension |
Extends the ThankYouController with getMatomoOrderTracking() |
MatomoOptOutController |
Opt-out page, controller ID matomo_optout |
With the defaults (tracking on, Matomo URL and site ID set), this code is generated. The order is fixed: privacy calls, heartbeat, trackPageView, link tracking, tracker initialization.
var _paq = window._paq = window._paq || [];
_paq.push(["disableCookies"]);
_paq.push(["setDoNotTrack", true]);
_paq.push(["trackPageView"]);
_paq.push(["enableLinkTracking"]);
(function() {
var u="https://analytics.example.com/";
_paq.push(["setTrackerUrl", u+"matomo.php"]);
_paq.push(["setSiteId", 1]);
var d=document, g=d.createElement("script"), s=d.getElementsByTagName("script")[0];
g.async=true; g.src=u+"matomo.js"; s.parentNode.insertBefore(g,s);
})();
Depending on the settings, requireConsent (after the other privacy calls) and enableHeartBeatTimer (before trackPageView) are added.
| Type | Calls |
|---|---|
| Product view | setEcommerceView(sku, name, category, price) |
| Cart | addEcommerceItem(sku, name, "", price, quantity) per line, then trackEcommerceCartUpdate(total) |
| Order | addEcommerceItem(...) per line, then trackEcommerceOrder(orderId, revenue, subTotal, tax, shipping) |
Prices are output with two decimal places and a dot as decimal separator. The category path of the product view is built from the category titles joined with >. The lines of cart and order pass an empty string as category.
| Variable | Content |
|---|---|
matomoOrderTracking |
Order data until it is output on the confirmation page, deleted afterwards |
matomoTrackedOrders |
List of already registered order numbers, prevents double registration within a session |
The settings are read per shop. The site ID and the heartbeat interval are stored as text and converted to numbers. An empty Klaro service name becomes matomo, a heartbeat interval of 0 becomes 15.