1
votes

I want to inject a matomo tag manager script into the head section of a page by javascript code. However when I try the code in matomo preview mode, the code doesn't fire. What am I misssing?

var script = document.createElement('script');
script.type = 'text/javascript';
script.innerHTML = `
var _mtm = window._mtm = window._mtm || [];
_mtm.push({'mtm.startTime': (new Date().getTime()), 'event': 'mtm.Start'});
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src='http://url.com/matomo/js/container_xyz.js'; s.parentNode.insertBefore(g,s);
`;
document.head.appendChild(script);
Why can't you just run the code that is inside ``?Alon Eitan