Track Cloaked Links always new visitor


I try to switch from Google Analytics to Matomo because of the new privacy laws in Europe. But right now I have a problem to track my outgoing affiliate links correctly. Maybe somebody can help me please.

It works as follows: Visitor clicks on one of the affiliate links (www.[domain].de/out/[keyword]. Inside the /out directory is a simple php script, which makes a 302 redirect based on the keyword and the visitor is redirect to the shop.

I already read that you can either include the java script tracking code on the Cloaker site or use the HTTP API. I would not like to use the java script code there because it delays forwarding. So I tried the Tracking API

So this is the part in my script inside the /out directory:

require_once "MatomoTracker.php";
PiwikTracker::$URL = '[url]';
$piwikTracker = new PiwikTracker( $idSite = 1 );
$piwikTracker->setTokenAuth('[auth Code]');
header( "X-Robots-Tag: noindex, nofollow", true );
header( "Location: " .  [keyword-url], 302 );

Good news is, that it’s already partially working. The link is tracked as outgoing link! But the problem is, that inside the visitor log, the outgoing link is tracked as different user and is not connected to the session before.

I expect that in the example the outgoing link will be appended as the third action for the visitor. Instead, there is the new entry. Anybody know what’s causing it?

I do not allow cookies to be stored with regard to the eprivacy regulation and to not have to use a cookie banner.

Thank you!

I don’t want to get on your nerves, but is something unclear or does nobody have an idea?