Cannot set userId via PHP

Hello All

I am trying to set UserId on the backend and saw the docs at https://developer.matomo.org/api-reference/PHP-Matomo-Tracker

        $matomoSiteId = 3;                  // Site ID
        $matomoUrl = "https://www.mydomain.com"; // Your matomo URL
        $matomoToken = "131zzxt98474776xt63t639268z202at";                  // Your authentication token , created on personal->security

        // Load object
        //included in autoload in prepended file require_once("MatomoTracker.php");

        // Matomo object
        $matomoTracker = new MatomoTracker((int)$matomoSiteId, $matomoUrl);
        // Set authentication token
        $matomoTracker->setTokenAuth($matomoToken);  
        //https://forum.matomo.org/t/track-same-user-by-js-and-php/9525
        //$matomoTracker->setVisitorId( $_COOKIE[$cookie_name] );

        //let us get the userId to see if one exists:
          $matomoUserId = $matomoTracker->getUserId();
          if ($matomoUserId === false) {
            $matomoTracker->setUserId($cookie_val); //setting it to the cookie value for the main cookie
            echo "\r\n <h1>matomoUserId has been set to  $cookie_val\r\n</h1>";
          } else {
            echo "\r\n <h1>matomoUserId = $matomoUserId\r\n</h1>";
          }

Of course… myDomain has the actual domain name

The $cookie_val is not a null string. I have a long string in there (56 alphanumeric characters)

The visit gets logged via javascript tags. Am able to set the userId in javascript. However, with the above code, the userId is not set. On a new user, with the above code, when i visit the dashboard, under “Vistors” → “User IDs”, the userId doesn’t show up! :frowning:

Edit: : Error reporting is ON in php. Also, nothing is getting logged from php tracker at all. No errors, no exceptions!

What am I doing wrong, please?

Thanks

Hi @vmds7777
Can you check the error and access log files on the Matomo server and see the HTTP request? (unfortunately, you won’t be able to see the cookie value, but maybe a clue can be hidden there…)

I figured it out

I had to do a “$matomoTracker->doTrackPageView(‘hardcodedTitleForTest’);”

Thanks!

1 Like