function initAuthenticationObject($notification) { $auth = new Piwik_Login_Auth(); Zend_Registry::set('auth', $auth); $action = Piwik::getAction(); if(Piwik::getModule() === 'API' && (empty($action) || $action == 'index')) { return; } $authCookieName = Zend_Registry::get('config')->General->login_cookie_name; $authCookieExpiry = time() + Zend_Registry::get('config')->General->login_cookie_expire; $authCookie = new Piwik_Cookie($authCookieName, $authCookieExpiry); $defaultLogin = '******'; $defaultTokenAuth = 'anonymous'; if($authCookie->isCookieFound()) { $defaultLogin = $authCookie->get('login'); $defaultTokenAuth = $authCookie->get('token_auth'); } $auth->setLogin($defaultLogin); $auth->setTokenAuth($defaultTokenAuth); }
function recordGoals($visitorInformation, $action) { $location_country = isset($visitorInformation['location_country']) ? $visitorInformation['location_country'] : Piwik_Common::getCountry(Piwik_Common::getBrowserLanguage(), $enableLanguageToCountryGuess = Piwik_Tracker_Config::getInstance()->Tracker['enable_language_to_country_guess']); $location_continent = isset($visitorInformation['location_continent']) ? $visitorInformation['location_continent'] : Piwik_Common::getContinent($location_country); $goal = array('idvisit' => $visitorInformation['idvisit'], 'idsite' => $visitorInformation['idsite'], 'visitor_idcookie' => $visitorInformation['visitor_idcookie'], 'server_time' => Piwik_Tracker::getDatetimeFromTimestamp($visitorInformation['visit_last_action_time']), 'visit_server_date' => $visitorInformation['visit_server_date'], 'location_country' => $location_country, 'location_continent' => $location_continent, 'visitor_returning' => $this->cookie->get(Piwik_Tracker::COOKIE_INDEX_VISITOR_RETURNING)); $referer_idvisit = $this->cookie->get(Piwik_Tracker::COOKIE_INDEX_REFERER_ID_VISIT); if ($referer_idvisit !== false) { $goal += array('referer_idvisit' => $referer_idvisit, 'referer_visit_server_date' => date("Y-m-d", $this->cookie->get(Piwik_Tracker::COOKIE_INDEX_REFERER_TIMESTAMP)), 'referer_type' => htmlspecialchars_decode($this->cookie->get(Piwik_Tracker::COOKIE_INDEX_REFERER_TYPE)), 'referer_name' => htmlspecialchars_decode($this->cookie->get(Piwik_Tracker::COOKIE_INDEX_REFERER_NAME)), 'referer_keyword' => htmlspecialchars_decode($this->cookie->get(Piwik_Tracker::COOKIE_INDEX_REFERER_KEYWORD))); } foreach ($this->convertedGoals as $convertedGoal) { printDebug("- Goal " . $convertedGoal['idgoal'] . " matched. Recording..."); $newGoal = $goal; $newGoal['idgoal'] = $convertedGoal['idgoal']; $newGoal['url'] = $convertedGoal['url']; $newGoal['revenue'] = $convertedGoal['revenue']; if (!is_null($action)) { $newGoal['idaction'] = $action->getIdAction(); $newGoal['idlink_va'] = $action->getIdLinkVisitAction(); } printDebug($newGoal); $fields = implode(", ", array_keys($newGoal)); $bindFields = substr(str_repeat("?,", count($newGoal)), 0, -1); try { Piwik_Tracker::getDatabase()->query("INSERT INTO " . Piwik_Common::prefixTable('log_conversion') . "\t({$fields}) \n\t\t\t\t\tVALUES ({$bindFields}) ", array_values($newGoal)); } catch (Exception $e) { if (strpos($e->getMessage(), '1062') !== false) { // integrity violation when same visit converts to the same goal twice printDebug("--> Goal already recorded for this (idvisit, idgoal)"); } else { throw $e; } } //$idlog_goal = Piwik_Tracker::getDatabase()->lastInsertId(); } }
/** * Update the cookie information. */ protected function updateCookie() { printDebug("We manage the cookie..."); if (isset($this->visitorInfo['referer_type']) && $this->visitorInfo['referer_type'] != Piwik_Common::REFERER_TYPE_DIRECT_ENTRY) { // if the setting is set to use only the first referer, // we don't update the cookie referer values if they are already set if (!Piwik_Tracker_Config::getInstance()->Tracker['use_first_referer_to_determine_goal_referer'] || $this->cookie->get(Piwik_Tracker::COOKIE_INDEX_REFERER_TYPE) == false) { $this->cookie->set(Piwik_Tracker::COOKIE_INDEX_REFERER_TYPE, $this->visitorInfo['referer_type']); $this->cookie->set(Piwik_Tracker::COOKIE_INDEX_REFERER_NAME, $this->visitorInfo['referer_name']); $this->cookie->set(Piwik_Tracker::COOKIE_INDEX_REFERER_KEYWORD, $this->visitorInfo['referer_keyword']); $this->cookie->set(Piwik_Tracker::COOKIE_INDEX_REFERER_ID_VISIT, $this->visitorInfo['idvisit']); $this->cookie->set(Piwik_Tracker::COOKIE_INDEX_REFERER_TIMESTAMP, $this->getCurrentTimestamp()); } } // idcookie has been generated in handleNewVisit or we simply propagate the old value $this->cookie->set(Piwik_Tracker::COOKIE_INDEX_IDVISITOR, $this->visitorInfo['visitor_idcookie']); // the last action timestamp is the current timestamp $this->cookie->set(Piwik_Tracker::COOKIE_INDEX_TIMESTAMP_LAST_ACTION, $this->visitorInfo['visit_last_action_time']); // the first action timestamp is the timestamp of the first action of the current visit $this->cookie->set(Piwik_Tracker::COOKIE_INDEX_TIMESTAMP_FIRST_ACTION, $this->visitorInfo['visit_first_action_time']); // the idvisit has been generated by mysql in handleNewVisit or simply propagated here $this->cookie->set(Piwik_Tracker::COOKIE_INDEX_ID_VISIT, $this->visitorInfo['idvisit']); // the last action ID is the current exit idaction if (isset($this->visitorInfo['visit_exit_idaction'])) { $this->cookie->set(Piwik_Tracker::COOKIE_INDEX_ID_LAST_ACTION, $this->visitorInfo['visit_exit_idaction']); } // for a new visit, we flag the visit with visitor_returning if (isset($this->visitorInfo['visitor_returning'])) { $this->cookie->set(Piwik_Tracker::COOKIE_INDEX_VISITOR_RETURNING, $this->visitorInfo['visitor_returning']); } $this->cookie->save(); }
/** * Initializes the authentication object. * Listens to FrontController.initAuthenticationObject hook. * * @param Piwik_Event_Notification $notification notification object */ function initAuthenticationObject($notification) { $auth = new Piwik_Login_Auth(); Zend_Registry::set('auth', $auth); $allowCookieAuthentication = $notification->getNotificationInfo(); $action = Piwik::getAction(); if (Piwik::getModule() === 'API' && (empty($action) || $action == 'index') && $allowCookieAuthentication !== true) { return; } $authCookieName = Piwik_Config::getInstance()->General['login_cookie_name']; $authCookieExpiry = 0; $authCookiePath = Piwik_Config::getInstance()->General['login_cookie_path']; $authCookie = new Piwik_Cookie($authCookieName, $authCookieExpiry, $authCookiePath); $defaultLogin = '******'; $defaultTokenAuth = 'anonymous'; if ($authCookie->isCookieFound()) { $defaultLogin = $authCookie->get('login'); $defaultTokenAuth = $authCookie->get('token_auth'); } $auth->setLogin($defaultLogin); $auth->setTokenAuth($defaultTokenAuth); }
/** * Is the request for a known VisitorId, based on 1st party, 3rd party (optional) cookies or Tracking API forced Visitor ID * @throws Exception */ protected function assignVisitorIdFromRequest() { $found = false; // Was a Visitor ID "forced" (@see Tracking API setVisitorId()) for this request? $idVisitor = $this->forcedVisitorId; if (!empty($idVisitor)) { if (strlen($idVisitor) != Piwik_Tracker::LENGTH_HEX_ID_STRING) { throw new Exception("Visitor ID (cid) must be " . Piwik_Tracker::LENGTH_HEX_ID_STRING . " characters long"); } printDebug("Request will be recorded for this idvisitor = " . $idVisitor); $found = true; } // - 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) { $idVisitor = $this->cookie->get(0); if ($idVisitor !== false && strlen($idVisitor) == Piwik_Tracker::LENGTH_HEX_ID_STRING) { $found = true; } } } // If a third party cookie was not found, we default to the first party cookie if (!$found) { $idVisitor = Piwik_Common::getRequestVar('_id', '', 'string', $this->request); $found = strlen($idVisitor) >= Piwik_Tracker::LENGTH_HEX_ID_STRING; } if ($found) { $truncated = substr($idVisitor, 0, Piwik_Tracker::LENGTH_HEX_ID_STRING); $binVisitorId = @Piwik_Common::hex2bin($truncated); if (!empty($binVisitorId)) { $this->visitorInfo['idvisitor'] = $binVisitorId; } } }
/** * Returns the langage for the session * * @return string|null */ public static function getLanguageForSession() { $cookieName = Zend_Registry::get('config')->General->language_cookie_name; $cookie = new Piwik_Cookie($cookieName); if ($cookie->isCookieFound()) { return $cookie->get('language'); } return null; }
/** * Returns the langage for the session * * @return string|null */ public static function getLanguageForSession() { $cookieName = Piwik_Config::getInstance()->General['language_cookie_name']; $cookie = new Piwik_Cookie($cookieName); if ($cookie->isCookieFound()) { return $cookie->get('language'); } return null; }
/** * This methods tries to see if the visitor has visited the website before. * * We have to split the visitor into one of the category * - Known visitor * - New visitor * * A known visitor is a visitor that has already visited the website in the current month. * We define a known visitor using the algorithm: * * 1) Checking if a cookie contains * // a unique id for the visitor * - id_visitor * * // the timestamp of the last action in the most recent visit * - timestamp_last_action * * // the timestamp of the first action in the most recent visit * - timestamp_first_action * * // the ID of the most recent visit (which could be in the past or the current visit) * - id_visit * * // the ID of the most recent action * - id_last_action * * 2) If the visitor doesn't have a cookie, we try to look for a similar visitor configuration. * We search for a visitor with the same plugins/OS/Browser/Resolution for today for this website. */ protected function recognizeTheVisitor() { $this->visitorKnown = false; $this->setCookie(new Piwik_Cookie($this->getCookieName(), $this->getCookieExpire(), $this->getCookiePath())); $this->printCookie(); $found = $forcedVisitorId = false; // Was a Visitor ID "forced" (@see Tracking API setVisitorId()) for this request? $idVisitor = $this->forcedVisitorId; if (!empty($idVisitor)) { if (strlen($idVisitor) != Piwik_Tracker::LENGTH_HEX_ID_STRING) { throw new Exception("Visitor ID (cid) must be " . Piwik_Tracker::LENGTH_HEX_ID_STRING . " characters long"); } printDebug("Request will be forced to record for this idvisitor = " . $idVisitor); $forcedVisitorId = true; $found = true; } if (!$found) { // - If set to use 3rd party cookies for Visit ID, read the cookies // - By default, reads the first party cookie ID $useThirdPartyCookie = $this->shouldUseThirdPartyCookie(); if ($useThirdPartyCookie) { $idVisitor = $this->cookie->get(0); if ($idVisitor !== false && strlen($idVisitor) == Piwik_Tracker::LENGTH_HEX_ID_STRING) { $found = true; } } } // If a third party cookie was not found, we default to the first party cookie if (!$found) { $idVisitor = Piwik_Common::getRequestVar('_id', '', 'string', $this->request); $found = strlen($idVisitor) >= Piwik_Tracker::LENGTH_HEX_ID_STRING; } // Does the cookie contain a Visitor ID? if ($found) { $this->visitorInfo['idvisitor'] = Piwik_Common::hex2bin($idVisitor); printDebug("The visitor has the piwik cookie (idvisitor = " . $idVisitor . ") "); } else { printDebug("Visitor doesn't have the piwik cookie."); } $userInfo = $this->getUserSettingsInformation(); $configId = $userInfo['config_id']; $timeLookBack = date('Y-m-d H:i:s', $this->getCurrentTimestamp() - self::TIME_IN_PAST_TO_SEARCH_FOR_VISITOR); $where = "visit_last_action_time >= ?\n\t\t\t\t\tAND idsite = ?"; $bindSql = array($timeLookBack, $this->idsite); // we always match on the config_id, except if the current request forces the visitor id if (!$forcedVisitorId) { $where .= ' AND config_id = ? '; $bindSql[] = $configId; } // We force to match a visitor ID // 1) If the visitor cookies should be trusted (ie. intranet) - config file setting // 2) or if the Visitor ID was forced via the Tracking API setVisitorId() if (!empty($this->visitorInfo['idvisitor']) && (Piwik_Tracker_Config::getInstance()->Tracker['trust_visitors_cookies'] || $forcedVisitorId)) { printDebug("Matching the visitor based on his idcookie: " . bin2hex($this->visitorInfo['idvisitor']) . "..."); $where .= ' AND idvisitor = ?'; $bindSql[] = $this->visitorInfo['idvisitor']; } $sql = " SELECT \tidvisitor,\n\t\t\t\t\t\t\tvisit_last_action_time,\n\t\t\t\t\t\t\tvisit_first_action_time,\n\t\t\t\t\t\t\tidvisit,\n\t\t\t\t\t\t\tvisit_exit_idaction_url,\n\t\t\t\t\t\t\tvisit_exit_idaction_name,\n\t\t\t\t\t\t\tvisitor_returning,\n\t\t\t\t\t\t\tvisitor_days_since_first,\n\t\t\t\t\t\t\treferer_name,\n\t\t\t\t\t\t\treferer_keyword,\n\t\t\t\t\t\t\treferer_type,\n\t\t\t\t\t\t\tvisitor_count_visits\n\t\t\t\tFROM " . Piwik_Common::prefixTable('log_visit') . " WHERE " . $where . "\n\t\t\t\tORDER BY visit_last_action_time DESC\n\t\t\t\tLIMIT 1"; $visitRow = Piwik_Tracker::getDatabase()->fetch($sql, $bindSql); if (!Piwik_Tracker_Config::getInstance()->Debug['tracker_always_new_visitor'] && $visitRow && count($visitRow) > 0) { // These values will be used throughout the request $this->visitorInfo['visit_last_action_time'] = strtotime($visitRow['visit_last_action_time']); $this->visitorInfo['visit_first_action_time'] = strtotime($visitRow['visit_first_action_time']); $this->visitorInfo['idvisitor'] = $visitRow['idvisitor']; $this->visitorInfo['idvisit'] = $visitRow['idvisit']; $this->visitorInfo['visit_exit_idaction_url'] = $visitRow['visit_exit_idaction_url']; $this->visitorInfo['visit_exit_idaction_name'] = $visitRow['visit_exit_idaction_name']; $this->visitorInfo['visitor_returning'] = $visitRow['visitor_returning']; $this->visitorInfo['visitor_days_since_first'] = $visitRow['visitor_days_since_first']; $this->visitorInfo['visitor_count_visits'] = $visitRow['visitor_count_visits']; // Referer information will be potentially used for Goal Conversion attribution $this->visitorInfo['referer_name'] = $visitRow['referer_name']; $this->visitorInfo['referer_keyword'] = $visitRow['referer_keyword']; $this->visitorInfo['referer_type'] = $visitRow['referer_type']; $this->visitorKnown = true; printDebug("The visitor is known (idvisitor = " . bin2hex($this->visitorInfo['idvisitor']) . ",\n\t\t\t\t\t\tconfig_id = " . bin2hex($configId) . ", \n\t\t\t\t\t\tidvisit = {$this->visitorInfo['idvisit']}, \n\t\t\t\t\t\tlast action = " . date("r", $this->visitorInfo['visit_last_action_time']) . ", \n\t\t\t\t\t\tfirst action = " . date("r", $this->visitorInfo['visit_first_action_time']) . ")"); } else { printDebug("The visitor was not matched with an existing visitor..."); } }