Ah yes, then it’s “normal” that you get this issue.
When using an “external” JS file, the file get’s served as-is.
It won’t go through CakePHP’s engine and therefore, you’ll get this error.
Like I’ve already said, I’m not aware of a solution for this :\
I got around it myself by adding this into the head tag and then using that variable in my script (since it’ll be scoped to the entire window) but that might not work nicely for what you’re doing:
var matomoHost = '<?= h($this->matomoUrl); ?>';
var matomoId = '<?= h($this->matomoId); ?>';