Resolved: Twig UTF-8 escaping error in Live Widget on IIS (Japanese characters in URLs)

Environment:

  • OS/Server: IIS (Windows Server) / Shared Hosting

  • Matomo Version: Latest

  • Database: MySQL 8.0 (utf8mb4)

Issue: I encountered the following fatal error when opening the Live Widget (Visitor Log) for a specific date range containing requests with Japanese characters in URLs/page titles:

ERROR Piwik\FrontController [...] Uncaught exception: \plugins\Live\templates_actionCommon.twig(25): The string to escape is not a valid UTF-8 string.

Root Cause & Solution:

  1. Malformed UTF-8 in database: IIS/FastCGI passed incoming Japanese URL strings in a non-UTF-8 encoding (e.g. Windows codepage / Shift-JIS), storing invalid byte sequences in piwik_log_action.

  2. Fixing the database: Repaired corrupted binary strings in piwik_log_action using SQL re-encoding:

UPDATE IGNORE piwik_log_action 
SET name = CONVERT(CAST(CONVERT(name USING latin1) AS BINARY) USING utf8mb4)
WHERE name LIKE '%\%' OR name REGEXP '[^\x00-\x7F]';
  1. Prevention: Added clean_up_invalid_utf8 = 1 under [General] in config.ini.php and set UTF-8 globalization rules in web.config for IIS.

Posting this here in case anyone running Matomo on IIS encounters a similar UTF-8 exception with non-ASCII / CJK characters in tracking requests!

Finaly, I must add encoding in Javascript:

var _paq = _paq || [];
_paq.push(["setCookieDomain", "*.domain.ch"]);
_paq.push(['trackPageView']);
_paq.push(['setCustomUrl', encodeURI(window.location.href)]);_paq.push(['setDocumentTitle', encodeURIComponent(document.title)]);_paq.push(['enableLinkTracking']);
(function() {
var u="https://statistic.domain.ch/data/"
_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);
})();