Skip to content

Commit

Permalink
Refs #4239 Fixing bug and adding custom variables cookie setter
Browse files Browse the repository at this point in the history
TODO: we need some tests to make sure this works as expected and will not be broken in the future
 *  Test new visit without a cookie: cookies 'ses' and 'id' should be created,
 *  Test new page view: the 'ses' cookie should be deleted and 'id' cookie updated
 *  Test new visit where the visitor has a cookie created with Piwik.JS: PiwikTracker should read the cookie as expected
  • Loading branch information
mattab committed Oct 24, 2013
1 parent 68c6818 commit 0f2af69
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions libs/PiwikTracker/PiwikTracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function __construct($idSite, $apiUrl = '')
$this->localSecond = false;
$this->hasCookies = false;
$this->plugins = false;
$this->visitorCustomVar = false;
$this->visitorCustomVar = $this->getCustomVariablesFromCookie();
$this->pageCustomVar = false;
$this->customData = false;
$this->forcedDatetime = false;
Expand Down Expand Up @@ -255,15 +255,10 @@ public function getCustomVariable($id, $scope = 'visit')
if (!empty($this->visitorCustomVar[$id])) {
return $this->visitorCustomVar[$id];
}
$customVariablesCookie = $this->getCookieName('cvar');
$cookie = $this->getCookieMatchingName($customVariablesCookie);
if (!$cookie) {
return false;
}
$cookieDecoded = $this->getCustomVariablesFromCookie();
if (!is_int($id)) {
throw new Exception("Parameter to getCustomVariable should be an integer");
}
$cookieDecoded = json_decode($cookie, $assoc = true);
if (!is_array($cookieDecoded)
|| !isset($cookieDecoded[$id])
|| !is_array($cookieDecoded[$id])
Expand Down Expand Up @@ -841,8 +836,7 @@ public function getVisitorId()
*/
protected function loadVisitorIdCookie()
{
$idCookieName = $this->getCookieName('id');
$idCookie = $this->getCookieMatchingName($idCookieName);
$idCookie = $this->getCookieMatchingName('id');
if ($idCookie === false) {
return false;
}
Expand Down Expand Up @@ -886,8 +880,7 @@ public function getAttributionInfo()
if(!empty($this->attributionInfo)) {
return $this->attributionInfo;
}
$attributionCookieName = $this->getCookieName('ref');
return $this->getCookieMatchingName($attributionCookieName);
return $this->getCookieMatchingName('ref');
}

/**
Expand Down Expand Up @@ -1196,6 +1189,8 @@ protected function getCookieMatchingName($name)
if($this->configCookiesDisabled) {
return false;
}
$name = $this->getCookieName($name);

// Piwik cookie names use dots separators in piwik.js,
// but PHP Replaces . with _ http://www.php.net/manual/en/language.variables.predefined.php#72571
$name = str_replace('.', '_', $name);
Expand Down Expand Up @@ -1315,25 +1310,25 @@ protected function setFirstPartyCookies()
}

// Set the 'ses' cookie
$sesname = $this->getCookieName('ses');
if (!$this->getCookieMatchingName($sesname)) {
if (!$this->getCookieMatchingName('ses')) {
// new session (new visit)
$this->visitCount++;
$this->lastVisitTs = $this->currentVisitTs;

// Set the 'ref' cookie
$attributionInfo = $this->getAttributionInfo();
if(!empty($attributionInfo)) {
$this->setCookie('ref', json_encode($attributionInfo), $this->configReferralCookieTimeout);
$this->setCookie('ref', $attributionInfo, $this->configReferralCookieTimeout);
}
}
$this->setCookie($sesname, '*', $this->configSessionCookieTimeout);
$this->setCookie('ses', '*', $this->configSessionCookieTimeout);

// Set the 'id' cookie
$cookieValue = $this->getVisitorId() . '.' . $this->createTs . '.' . $this->visitCount . '.' . $this->currentTs . '.' . $this->lastVisitTs . '.' . $this->lastEcommerceOrderTs;
$this->setCookie('id', $cookieValue, $this->configVisitorCookieTimeout);

// Set the 'cvar' cookie
$this->setCookie('cvar', json_encode($this->visitorCustomVar), $this->configSessionCookieTimeout);

}

Expand All @@ -1351,6 +1346,18 @@ protected function setCookie($cookieName, $cookieValue, $cookieTTL)
$cookieExpire = $this->createTs + $cookieTTL;
setrawcookie($this->getCookieName($cookieName), $cookieValue, $cookieExpire, $this->configCookiePath, $this->configCookieDomain);
}

/**
* @return bool|mixed
*/
protected function getCustomVariablesFromCookie()
{
$cookie = $this->getCookieMatchingName('cvar');
if (!$cookie) {
return false;
}
return json_decode($cookie, $assoc = true);
}
}

/**
Expand Down

0 comments on commit 0f2af69

Please sign in to comment.