JS error when calling MTM container

Hi,

Probably since a wordpress update, I’ve got an error when calling my MTM container:

Uncaught SyntaxError: Identifier 's' has already been declared (at data:text/javascript;base64,.......................)
    at data:text/javascript;base64,......................................

Browser console also gives me:

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 = !0;
g.src = 'https://MY-MATOMO-SERVER.org/js/container_WC3rwORo.js';
s.parentNode.insertBefore(g, s)

Does anyone have any ideas for solving this sudden problem ?

Thanks a lot !
Best regards

Hi @bmail
Are you sure your code is not implemented twice in your page?
A solution to solve this is:

(function () {
  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 = !0;
  g.src = 'https://MY-MATOMO-SERVER.org/js/container_WC3rwORo.js';
  s.parentNode.insertBefore(g, s)
})()

But if the problem comes from my first intuition (and if you don’t fix it), then woud would track twice instead once… :face_with_diagonal_mouth:

Hi Philippe,

Thank you for your answer.
I did not change anything since a long time… Just an update of wordpress few days ago.

My functions.php (into wordpress) (always) contains:

function Matomo_Tag_Manager() {
	echo "<script>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='https://MY-MATOMO-SERVER.org/js/container_WC3rwORo.js'; s.parentNode.insertBefore(g,s);</script>";
}
add_action('wp_head', 'Matomo_Tag_Manager');
/****************Insertion code MATOMO dans header.php**************************/

My header.php or footer.php do not call another time the MTM javascript.

And no, I don’t have twice tracking… Since wordpress core update: no visits recorded (while the web server log show me visits).

Do you think I could safely edit this code to change the ‘s’ variable to ‘my_personal_s’ ?

Thank again !
Best regards

Well…

I tried to rename the ‘s’ variable, and it seems to work.
Container js is loading without error, and I can see visits.

I don’t know if it’s very … academic… and if it will cause some others issues, but for the moment it works.

I’m absolutely sure that it started with the last update of Wordpress core.

Thanks Philippe for your help.
Best regards

Hi @bmail
Maybe the s variable has been declared in one of the WP update files…
The advantage of the solution I provided (the use of an anonymous auto-called function), is that you are sure there will never be any variable collision.

Hi Philippe,

You’re absolutely right !
I understood that we were making this function completely autonomous (including the variables used) thanks to the code you provided, but I haven’t managed to adapt it to use your method in my case …

My function ‘echoes’ the script in a tag and then integrates it into header.php, and I can’t see how to do this via an anonymous function.

That’s why I tagged my last post as “the solution”… Rather, it’s “my” solution, not having the skills to implement yours, which is indeed the best ! Anyway, I didn’t want to offend you (if that’s the case).
Thank you for your suggestion, which is the right one. I’ll just have to figure out how to put it into practice.

Best regards and have a good day !