matomo-org / matomo

Empowering People Ethically with the leading open source alternative to Google Analytics that gives you full control over your data. Matomo lets you easily collect data from websites & apps and visualise this data and extract insights. Privacy is built-in. Liberating Web Analytics. Star us on Github? +1. And we love Pull Requests!
https://matomo.org/
GNU General Public License v3.0
19.7k stars 2.62k forks source link

PHP tracking: userId is messed up #20887

Closed itscvenk closed 9 months ago

itscvenk commented 1 year ago

Hi

latest version of Matomo 4.x plus PHP 7.4

Please consider the following code:

<?php 

try {
      $matomoSiteId = 3;                  // Site ID

      $matomoTracker = new MatomoTracker((int)$matomoSiteId, $matomoUrl);
      $matomoTracker->setTokenAuth($matomoToken);  
      $setVisitorIdResult = $matomoTracker->setVisitorId('a0aa6368fc475d97'); 
      echo "<hr>at line " . __LINE__ . " setVisitorIdResult is <pre>";  var_dump($setVisitorIdResult) ; echo  "</pre>";

        $matomoUserId = $matomoTracker->getUserId();
        echo "<hr>at line " . __LINE__ . " matomoUserId is <pre>";  var_dump($matomoUserId) ; echo  "</pre>";

        if (!$matomoUserId) {
          $matomoTracker->setUserId($cookieSetVal); //setting it to the cookie value for the main cookie
          echo "\r\n <h1>matomoUserId has been set to  $cookieSetVal for $theMatomoVisitorId  
          <br>  VisitorId:  $theMatomoVisitorId </h1>";

        } else {
          echo "\r\n <h1>matomoUserId = $matomoUserId for $theMatomoVisitorId  and unhexed is $theMatomoVisitorIdUnHexed \r\n</h1>
          <br>  VisitorId:  $theMatomoVisitorId </h1>";
        }

        $matomoTracker->doTrackPageView('hardcodedTitleForTest' . $_GET['version']);
        $matomoUserId = $matomoTracker->getUserId();
        echo "<hr>at line " . __LINE__ . " matomoUserId is <pre>";  var_dump($matomoUserId) ; echo  "</pre>";

        $theMatomoVisitorId = $matomoTracker->getVisitorId() ;
        echo "at line " . __LINE__ . " theMatomoVisitorId is $theMatomoVisitorId<hr>";

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

The userId towards the bottom is reported as 4dee7e4ab4824b25, for example.

Now look at these 2 screenshots from the dashboard. The userId magically changes and there is no mention of 4dee7e4ab4824b25 in the 2nd screenshot below (1st screenshot is from "visitor log" and the 2nd one is after clicking on the "view visitor profile")

How reliable is the php tracker? Will this mismatch impact reporting in any other way? I will be setting the userId manually like this using the php tracking

Thanks

image

image

michalkleiner commented 1 year ago

Hi @itscvenk. We appreciate your time opening the issues you come across while testing out Matomo and its tracking features. It would help us understand more about your use case and what you're trying to achieve if you could provide a step by step replication steps following the issue template. Also if you provide a script like above it would help providing the full output it produces, the config of your instance that can be important as to how your local instance behaves in some of the tracking aspects etc. Can you clarify if you're raising a bug or is it more of a general question?

itscvenk commented 9 months ago

Using http api now so that i can do what i want with full flexibility

No time to retest above things, sorry. So closing it for now