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:
-
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. -
Fixing the database: Repaired corrupted binary strings in
piwik_log_actionusing 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]';
- Prevention: Added
clean_up_invalid_utf8 = 1under[General]inconfig.ini.phpand set UTF-8 globalization rules inweb.configfor 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);
})();