public function onNewVisit(VisitProperties $visitProperties, Request $request) { $visitCustomVariables = $request->getMetadata('CustomVariables', 'visitCustomVariables'); if (!empty($visitCustomVariables)) { $visitProperties->setProperties(array_merge($visitProperties->getProperties(), $visitCustomVariables)); } }
public function afterRequestProcessed(VisitProperties $visitProperties, Request $request) { $goalsConverted = $request->getMetadata('Goals', 'goalsConverted'); if (!empty($goalsConverted)) { $isThereExistingCartInVisit = $this->goalManager->detectIsThereExistingCartInVisit($visitProperties->getProperties()); $request->setMetadata('Goals', 'isThereExistingCartInVisit', $isThereExistingCartInVisit); } }
public function onExistingVisit(&$valuesToUpdate, VisitProperties $visitProperties, Request $request) { $dimensionsToSet = $this->getCustomDimensionsInScope(CustomDimensions::SCOPE_VISIT, $request); foreach ($dimensionsToSet as $field => $value) { $valuesToUpdate[$field] = $value; $visitProperties->setProperty($field, $value); } }
private function makeVisitorAndAction($lastActionTimestamp, $currentActionTime, $isVisitorKnown = false) { $idsite = API::getInstance()->addSite("name", "http://piwik.net/"); /** @var Request $request */ list($visit, $request) = $this->prepareVisitWithRequest(array('idsite' => $idsite), $currentActionTime); $visitProperties = new VisitProperties(); $visitProperties->setProperty('visit_last_action_time', Date::factory($lastActionTimestamp)->getTimestamp()); $request->setMetadata('CoreHome', 'isVisitorKnown', $isVisitorKnown); return array($visit, $visitProperties, $request); }
public function processRequestParams(VisitProperties $visitProperties, Request $request) { // normal page view, potentially triggering a URL matching goal $action = Action::factory($request); $action->writeDebugInfo(); $request->setMetadata('Actions', 'action', $action); // save the exit actions of the last action in this visit as the referrer actions for the action being tracked. // when the visit is updated, these columns will be changed, so we have to do this before recordLogs $request->setMetadata('Actions', 'idReferrerActionUrl', $visitProperties->getProperty('visit_exit_idaction_url')); $request->setMetadata('Actions', 'idReferrerActionName', $visitProperties->getProperty('visit_exit_idaction_name')); }
public function findKnownVisitor($configId, VisitProperties $visitProperties, Request $request) { $idSite = $request->getIdSite(); $idVisitor = $request->getVisitorId(); $isVisitorIdToLookup = !empty($idVisitor); if ($isVisitorIdToLookup) { $visitProperties->setProperty('idvisitor', $idVisitor); Common::printDebug("Matching visitors with: visitorId=" . bin2hex($idVisitor) . " OR configId=" . bin2hex($configId)); } else { Common::printDebug("Visitor doesn't have the piwik cookie..."); } $persistedVisitAttributes = $this->getVisitFieldsPersist(); $shouldMatchOneFieldOnly = $this->shouldLookupOneVisitorFieldOnly($isVisitorIdToLookup, $request); list($timeLookBack, $timeLookAhead) = $this->getWindowLookupThisVisit($request); $visitRow = $this->model->findVisitor($idSite, $configId, $idVisitor, $persistedVisitAttributes, $shouldMatchOneFieldOnly, $isVisitorIdToLookup, $timeLookBack, $timeLookAhead); $isNewVisitForced = $request->getParam('new_visit'); $isNewVisitForced = !empty($isNewVisitForced); $enforceNewVisit = $isNewVisitForced || $this->trackerAlwaysNewVisitor; if (!$enforceNewVisit && $visitRow && count($visitRow) > 0) { // These values will be used throughout the request foreach ($persistedVisitAttributes as $field) { $visitProperties->setProperty($field, $visitRow[$field]); } $visitProperties->setProperty('visit_last_action_time', strtotime($visitRow['visit_last_action_time'])); $visitProperties->setProperty('visit_first_action_time', strtotime($visitRow['visit_first_action_time'])); // Custom Variables copied from Visit in potential later conversion if (!empty($numCustomVarsToRead)) { for ($i = 1; $i <= $numCustomVarsToRead; $i++) { if (isset($visitRow['custom_var_k' . $i]) && strlen($visitRow['custom_var_k' . $i])) { $visitProperties->setProperty('custom_var_k' . $i, $visitRow['custom_var_k' . $i]); } if (isset($visitRow['custom_var_v' . $i]) && strlen($visitRow['custom_var_v' . $i])) { $visitProperties->setProperty('custom_var_v' . $i, $visitRow['custom_var_v' . $i]); } } } Common::printDebug("The visitor is known (idvisitor = " . bin2hex($visitProperties->getProperty('idvisitor')) . ",\n config_id = " . bin2hex($configId) . ",\n idvisit = {$visitProperties->getProperty('idvisit')},\n last action = " . date("r", $visitProperties->getProperty('visit_last_action_time')) . ",\n first action = " . date("r", $visitProperties->getProperty('visit_first_action_time')) . ",\n visit_goal_buyer' = " . $visitProperties->getProperty('visit_goal_buyer') . ")"); return true; } else { Common::printDebug("The visitor was not matched with an existing visitor..."); return false; } }
/** * @param $visitor * @param $valuesToUpdate * @return mixed */ private function setIdVisitorForExistingVisit($valuesToUpdate) { // Might update the idvisitor when it was forced or overwritten for this visit if (strlen($this->visitProperties->getProperty('idvisitor')) == Tracker::LENGTH_BINARY_ID) { $binIdVisitor = $this->visitProperties->getProperty('idvisitor'); $valuesToUpdate['idvisitor'] = $binIdVisitor; } // User ID takes precedence and overwrites idvisitor value $userId = $this->request->getForcedUserId(); if ($userId) { $userIdHash = $this->request->getUserIdHashed($userId); $binIdVisitor = Common::hex2bin($userIdHash); $this->visitProperties->setProperty('idvisitor', $binIdVisitor); $valuesToUpdate['idvisitor'] = $binIdVisitor; } return $valuesToUpdate; }
private function getGoalFromVisitor(VisitProperties $visitProperties, Request $request, $action) { $goal = array('idvisit' => $visitProperties->getProperty('idvisit'), 'idvisitor' => $visitProperties->getProperty('idvisitor'), 'server_time' => Date::getDatetimeFromTimestamp($visitProperties->getProperty('visit_last_action_time'))); $visitDimensions = VisitDimension::getAllDimensions(); $visit = Visitor::makeFromVisitProperties($visitProperties, $request); foreach ($visitDimensions as $dimension) { $value = $dimension->onAnyGoalConversion($request, $visit, $action); if (false !== $value) { $goal[$dimension->getColumnName()] = $value; } } return $goal; }
/** * Returns true if the last action was not today. * @param VisitProperties $visitor * @return bool */ private function wasLastActionNotToday(VisitProperties $visitProperties, Request $request) { $lastActionTime = $visitProperties->getProperty('visit_last_action_time'); if (empty($lastActionTime)) { return false; } $idSite = $request->getIdSite(); $timezone = $this->getTimezoneForSite($idSite); if (empty($timezone)) { throw new UnexpectedWebsiteFoundException('An unexpected website was found, check idSite in the request'); } $date = Date::factory((int) $lastActionTime, $timezone); $now = $request->getCurrentTimestamp(); $now = Date::factory((int) $now, $timezone); return $date->toString() !== $now->toString(); }