How to enable userid tracking in matomo?

I was trying to enable userid tracking on mediawiki using matomo, but as soon as i edit the “LocalSettings.php” with below changes the visit logs stop generating:-
Can anyone help me achieving this ?

==============================
# Enabled Matomo extension
wfLoadExtension( 'Matomo' );
$wgMatomoURL = ;
$wgMatomoIDSite = "2";
$wgMatomoUsePageTitle = "true";
$wgMatomoTrackUsernames = "true";

# Enable HeadScript extension
wfLoadExtension( 'HeadScript' );
$wgHeadScriptCode = <<<'START_END_MARKER'
<!-- Matomo -->
<script type="text/javascript">
  var _paq = window._paq = window._paq || [];
// If user is logged-in then call 'setUserId'
// $userId variable must be set by the server when the user has successfully authenticated to your app.
  if (isset($userId)) {
     echo sprintf("_paq.push(['setUserId', '%s']);", $userId);
  }
  /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="";
    _paq.push(['setTrackerUrl', u+'matomo.php']);
    _paq.push(['setSiteId', '2']);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.type='text/javascript'; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
  })();
</script>

Hi,

It is probably best to ask the Author of the Mediawiki plugin for help or invite them to answer here on the forum.

That said, you can’t just mix PHP syntax and Javascript syntax arbitrarily, but rather you need to create a text in PHP that is valid Javascript.

Take a look at this example and I’d also recommend you to read the PHP documentation on HEREDOC and NOWDOC:

if (isset($userId)) {
    $userIdLine = sprintf("_paq.push(['setUserId', '%s']);", $userId);
} else {
    $userIdLine = "";
}
$wgHeadScriptCode = <<<START_END_MARKER
<!-- Matomo -->
<script type="text/javascript">
  var _paq = window._paq = window._paq || [];

  $userIdLine
  /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="";
    _paq.push(['setTrackerUrl', u+'matomo.php']);
    _paq.push(['setSiteId', '2']);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.type='text/javascript'; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
  })();
</script>
START_END_MARKER;