How to end a track and start a new one ?X(

i want to stop a track manully and start a new one ,how can i do ?

my idea is :

var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);

var uid="$_G['uid']";
var uname="$_G['username']";

//when enter the website for the first time
if(!Piwik.getCookie('ck_uid))
{
	Piwik.setCookie('ck_uid',uid);
}
else if(Piwik.getCookie('ck_uid)!=uid))  //when login or logout
{
	//end the former track
	piwikTracker.setDoNotTrack(true);
	
	//start a new track
	var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);
}

if(uid!=''&&uname!='')
{
	piwikTracker.setCustomVariable(1, "uid", uid,'visit');
	piwikTracker.setCustomVariable(2, "username", uname,'visit');
}
	
piwikTracker.trackPageView();
piwikTracker.enableLinkTracking();

Did you find a solution?

I’m looking for the same thing: 301 Moved Permanently

I have the same problem pls somebody help

There is a feature request ticket about being able to set the visitorId in the JS API which would solve this problem: Accurate User Detection cross devices: User ID (set in JS and all other clients) · Issue #3490 · matomo-org/matomo · GitHub