Skip to content

3rd party cookie visitor ID value should not change over time #12755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 66 additions & 15 deletions core/Tracker/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,18 @@ protected function shouldUseThirdPartyCookie()
return (bool)Config::getInstance()->Tracker['use_third_party_id_cookie'];
}

public function getThirdPartyCookieVisitorId()
{
$cookie = $this->makeThirdPartyCookieUID();
$idVisitor = $cookie->get(0);
if ($idVisitor !== false
&& strlen($idVisitor) == Tracker::LENGTH_HEX_ID_STRING
) {
return $idVisitor;
}
return null;
}

/**
* Update the cookie information.
*/
Expand All @@ -615,12 +627,12 @@ public function setThirdPartyCookie($idVisitor)
return;
}

Common::printDebug("We manage the cookie...");

$cookie = $this->makeThirdPartyCookieUID();
// idcookie has been generated in handleNewVisit or we simply propagate the old value
$cookie->set(0, bin2hex($idVisitor));
$idVisitor = bin2hex($idVisitor);
$cookie->set(0, $idVisitor);
$cookie->save();

Common::printDebug(sprintf("We set the visitor ID to %s in the 3rd party cookie...", $idVisitor));
}

protected function makeThirdPartyCookieUID()
Expand Down Expand Up @@ -672,7 +684,7 @@ protected function getCookieDomain()
public function getVisitorId()
{
$found = false;

// If User ID is set it takes precedence
$userId = $this->getForcedUserId();
if ($userId) {
Expand All @@ -696,14 +708,10 @@ public function getVisitorId()

// - If set to use 3rd party cookies for Visit ID, read the cookie
if (!$found) {
// - By default, reads the first party cookie ID
$useThirdPartyCookie = $this->shouldUseThirdPartyCookie();
if ($useThirdPartyCookie) {
$cookie = $this->makeThirdPartyCookieUID();
$idVisitor = $cookie->get(0);
if ($idVisitor !== false
&& strlen($idVisitor) == Tracker::LENGTH_HEX_ID_STRING
) {
$idVisitor = $this->getThirdPartyCookieVisitorId();
if(!empty($idVisitor)) {
$found = true;
}
}
Expand All @@ -716,16 +724,45 @@ public function getVisitorId()
}

if ($found) {
$truncated = $this->truncateIdAsVisitorId($idVisitor);
$binVisitorId = @Common::hex2bin($truncated);
if (!empty($binVisitorId)) {
return $binVisitorId;
return $this->getVisitorIdAsBinary($idVisitor);
}

return false;
}

/**
* When creating a third party cookie, we want to ensure that the original value set in this 3rd party cookie
* sticks and is not overwritten later.
*/
public function getVisitorIdForThirdPartyCookie()
{
$found = false;

// For 3rd party cookies, priority is on re-using the existing 3rd party cookie value
if (!$found) {
$useThirdPartyCookie = $this->shouldUseThirdPartyCookie();
if ($useThirdPartyCookie) {
$idVisitor = $this->getThirdPartyCookieVisitorId();
if(!empty($idVisitor)) {
$found = true;
}
}
}

// If a third party cookie was not found, we default to the first party cookie
if (!$found) {
$idVisitor = Common::getRequestVar('_id', '', 'string', $this->params);
$found = strlen($idVisitor) >= Tracker::LENGTH_HEX_ID_STRING;
}

if ($found) {
return $this->getVisitorIdAsBinary($idVisitor);
}

return false;
}


public function getIp()
{
return IPUtils::stringToBinaryIP($this->getIpString());
Expand Down Expand Up @@ -838,4 +875,18 @@ public function getMetadata($pluginName, $key)
{
return isset($this->requestMetadata[$pluginName][$key]) ? $this->requestMetadata[$pluginName][$key] : null;
}

/**
* @param $idVisitor
* @return bool|string
*/
private function getVisitorIdAsBinary($idVisitor)
{
$truncated = $this->truncateIdAsVisitorId($idVisitor);
$binVisitorId = @Common::hex2bin($truncated);
if (!empty($binVisitorId)) {
return $binVisitorId;
}
return false;
}
}
2 changes: 1 addition & 1 deletion core/Tracker/Visit.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function handle()
}

// update the cookie with the new visit information
$this->request->setThirdPartyCookie($this->visitProperties->getProperty('idvisitor'));
$this->request->setThirdPartyCookie($this->request->getVisitorIdForThirdPartyCookie());

foreach ($this->requestProcessors as $processor) {
Common::printDebug("Executing " . get_class($processor) . "::recordLogs()...");
Expand Down