Multiple issues with VisitorID when combining PHP and Javascript code

Consider the following codes:

The PHP is executed 1st at the top of the page before the HTML is rendered for the page and the javascript kicks in

<?php 



try {
      $matomoSiteId = 3;                  // Site ID

      $matomoTracker = new MatomoTracker((int)$matomoSiteId, $matomoUrl);
      $matomoTracker->setTokenAuth($matomoToken);  
      $theMatomoVisitorId = $matomoTracker->getVisitorId() ;
      $matomoUserId = $matomoTracker->getUserId();

        if (!$matomoUserId) {
          $matomoTracker->setUserId($cookieSetVal); //setting it to the cookie value for the main cookie
          echo "<script> console.log('<h1>matomoUserId has been set to  $cookieSetVal for  VisitorId:  $theMatomoVisitorId </h1>'); </script>";

        } else {
          echo "<script> console.log('matomoUserId = $matomoUserId for visitorID =  $theMatomoVisitorId '); </script>";
          //echo "\r\n //<h1>matomoUserId = $matomoUserId for $theMatomoVisitorId  and unhexed is $theMatomoVisitorIdUnHexed \r\n</h1> <br>  VisitorId:  $theMatomoVisitorId </h1>";
        }

           /* setCustomDimension()
            Sets a specific custom dimension

            Signature
            It accepts the following parameter(s):
            $id (int) — id of custom dimension
            $value (string) — value for custom dimension
            It returns a $this value.

            */

        $matomoTracker->doTrackPageView('hardcodedTitleForTest' . $_GET['version']);
        $matomoUserId = $matomoTracker->getUserId();
        echo "<script> console.log('at line " . __LINE__ . " matomoUserId is $matomoUserId '); </script>"  ;
        echo "<script> console.log('at line " . __LINE__ . " VisitorId is $theMatomoVisitorId '); </script>"  ;

} catch (\Throwable $th) {
  //throw $th;
  echo "<h1>Caught exception .... " . $th->getMessage() . "</h1>";
}

?>

And here’s the javascript:

<!-- Matomo -->
<script>
    var currentMatoUserID = null;
    var currentUpdatedUserId = 'null';
    var currentVisitorID = null;
    var currentVisitorOID = null;

  var _paq = window._paq = window._paq || [];
        console.log('setting setVisitorId to <?php echo $theMatomoVisitorId; ?> at <?php echo __LINE__; ?>') ;
        _paq.push(['setVisitorId', '<?php echo $theMatomoVisitorId; ?>']);
        _paq.push(['setUserId', '<?php echo $cookieSetVal; ?>']);    
        _paq.push(['trackVisibleContentImpressions']); 


//example: _paq.push(['setCustomDimension', customDimensionId = 1, customDimensionValue = 'Member']);
//getCustomDimension(customDimensionId)


  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {

        var u="//www.myActualDomain.com/matomo/";
    _paq.push(['setTrackerUrl', u+'matomo.php']);
    _paq.push(['setSiteId', '3']);

    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);
  })();




</script>
<!-- End Matomo Code -->


Te userId works like a charm

Issues:

The visitorId, set in the Javascript doesn’t take effect

  1. The visitorId returned by PHP $theMatomoVisitorId , is not visible on the dashboard. I assume this ought to be visible in “Visits Log” → “view visitor profile” (for any visitor) & the ID should be the visitorId. On the dashboard, it doesn’t match the visitorId i had retreived in PHP. None of the visits do! I even searched with a segment to match the visitorId i got in PHP but no data exists!
  2. The cookies starting with “pk” are simply not set with the above code. If I just execute the php tracking and not include the JS code, then these cookies are set. I don’t know if this has anything to do with the above 2 issues.

Edited: I kept the console open on Chrome and was watching the Application->cookies in chrome console…and saw the “pk” appearing for a split second and disappearing! Hmmmmmm…

I would like to set the visitorId myself each time, so that I can synchronize the PHP tracking as well as the JavascriptTracking. The reason I would like to track certain things separately is because:

  1. I want to track certain backend stuff only in PHP so that it is not visible on the client side
  2. My main issue with the JS tracking is that they are accessible in javascript (HttpOnly is false), ergo, it is prone to injection and hence can corrupt some data here and there for a given visitor

Any ideas/ help will be very much appreciated

Thanks

Backlink to the GitHub issue: