function recordFunnelSteps($notification) { $info = $notification->getNotificationInfo(); $idSite = $info['idSite']; printDebug('Looking for funnel steps'); $websiteAttributes = Piwik_Common::getCacheWebsiteAttributes($idSite); if (isset($websiteAttributes['funnels'])) { $funnels = $websiteAttributes['funnels']; printDebug('got funnel steps'); } else { $funnels = array(); } if (count($funnels) > 0) { $idVisit = $info['idVisit']; $idLinkVisitAction = $info['idLinkVisitAction']; $idRefererAction = $info['idRefererAction']; $action = $notification->getNotificationObject(); $actionName = $action->getActionName(); $sanitizedUrl = $action->getActionUrl(); $actionUrl = htmlspecialchars_decode($sanitizedUrl); $idActionUrl = $action->getIdActionUrl(); $url = Piwik_Common::getRequestVar('url', '', 'string', $action->getRequest()); printDebug("idActionUrl" . $idActionUrl . " idSite " . $idSite . " idVisit " . $idVisit . " idRefererAction " . $idRefererAction); # Is this the next action for a recorded funnel step? $previous_step_action = Piwik_Query("UPDATE " . Piwik_Common::prefixTable('log_funnel_step') . "\n SET idaction_url_next = ?\n WHERE idsite = ? \n AND idvisit = ? \n AND idaction_url = ?\n AND idaction_url_next is null", array($idActionUrl, $idSite, $idVisit, $idRefererAction)); } foreach ($funnels as &$funnel) { $steps = $funnel['steps']; foreach ($steps as &$step) { if ($step['url'] == $actionUrl or $step['name'] == $actionName) { printDebug("Matched Goal Funnel " . $funnel['idfunnel'] . " Step " . $step['idstep'] . "(name: " . $step['name'] . ", url: " . $step['url'] . "). "); $serverTime = time(); $datetimeServer = Piwik_Tracker::getDatetimeFromTimestamp($serverTime); // Look to see if this step has already been recorded for this visit $exists = Piwik_FetchOne("SELECT idlink_va\n FROM " . Piwik_Common::prefixTable('log_funnel_step') . " \n WHERE idsite = ? \n AND idfunnel = ?\n AND idstep = ?\n AND idvisit = ?", array($idSite, $funnel['idfunnel'], $step['idstep'], $idVisit)); // Record it if not if (!$exists) { printDebug("Recording..."); Piwik_Query("INSERT INTO " . Piwik_Common::prefixTable('log_funnel_step') . "\n (idvisit, idsite, idaction_url, url, \n idgoal, idfunnel, idstep, idlink_va, \n idaction_url_ref, server_time)\n VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", array($idVisit, $idSite, $idActionUrl, $url, $funnel['idgoal'], $step['idfunnel'], $step['idstep'], $idLinkVisitAction, $idRefererAction, $datetimeServer)); } } } } }
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(); } }
/** * Records one or several goals matched in this request. * @param int $idSite * @param array $visitorInformation * @param array $visitCustomVariables * @param string $action * @param $referrerTimestamp * @param string $referrerUrl * @param string $referrerCampaignName * @param string $referrerCampaignKeyword */ public function recordGoals($idSite, $visitorInformation, $visitCustomVariables, $action, $referrerTimestamp, $referrerUrl, $referrerCampaignName, $referrerCampaignKeyword, $browserLanguage) { $location_country = isset($visitorInformation['location_country']) ? $visitorInformation['location_country'] : Piwik_Common::getCountry($browserLanguage, $enableLanguageToCountryGuess = Piwik_Config::getInstance()->Tracker['enable_language_to_country_guess'], $visitorInformation['location_ip']); $goal = array('idvisit' => $visitorInformation['idvisit'], 'idsite' => $idSite, 'idvisitor' => $visitorInformation['idvisitor'], 'server_time' => Piwik_Tracker::getDatetimeFromTimestamp($visitorInformation['visit_last_action_time']), 'location_country' => $location_country, 'visitor_returning' => $visitorInformation['visitor_returning'], 'visitor_days_since_first' => $visitorInformation['visitor_days_since_first'], 'visitor_days_since_order' => $visitorInformation['visitor_days_since_order'], 'visitor_count_visits' => $visitorInformation['visitor_count_visits']); $extraLocationCols = array('location_region', 'location_city', 'location_latitude', 'location_longitude'); foreach ($extraLocationCols as $col) { if (isset($visitorInformation[$col])) { $goal[$col] = $visitorInformation[$col]; } } // Copy Custom Variables from Visit row to the Goal conversion for ($i = 1; $i <= Piwik_Tracker::MAX_CUSTOM_VARIABLES; $i++) { if (isset($visitorInformation['custom_var_k' . $i]) && strlen($visitorInformation['custom_var_k' . $i])) { $goal['custom_var_k' . $i] = $visitorInformation['custom_var_k' . $i]; } if (isset($visitorInformation['custom_var_v' . $i]) && strlen($visitorInformation['custom_var_v' . $i])) { $goal['custom_var_v' . $i] = $visitorInformation['custom_var_v' . $i]; } } // Otherwise, set the Custom Variables found in the cookie sent with this request $goal += $visitCustomVariables; // Attributing the correct Referrer to this conversion. // Priority order is as follows: // 0) In some cases, the campaign is not passed from the JS so we look it up from the current visit // 1) Campaign name/kwd parsed in the JS // 2) Referrer URL stored in the _ref cookie // 3) If no info from the cookie, attribute to the current visit referrer // 3) Default values: current referrer $type = $visitorInformation['referer_type']; $name = $visitorInformation['referer_name']; $keyword = $visitorInformation['referer_keyword']; $time = $visitorInformation['visit_first_action_time']; // 0) In some (unknown!?) cases the campaign is not found in the attribution cookie, but the URL ref was found. // In this case we look up if the current visit is credited to a campaign and will credit this campaign rather than the URL ref (since campaigns have higher priority) if (empty($refererCampaignName) && $type == Piwik_Common::REFERER_TYPE_CAMPAIGN && !empty($name)) { // Use default values per above } elseif (!empty($referrerCampaignName)) { $type = Piwik_Common::REFERER_TYPE_CAMPAIGN; $name = $referrerCampaignName; $keyword = $referrerCampaignKeyword; $time = $referrerTimestamp; } elseif (!empty($referrerUrl)) { $referrer = new Piwik_Tracker_Visit_Referer(); $referrer = $referrer->getRefererInformation($referrerUrl, $currentUrl = '', $idSite); // if the parsed referer is interesting enough, ie. website or search engine if (in_array($referrer['referer_type'], array(Piwik_Common::REFERER_TYPE_SEARCH_ENGINE, Piwik_Common::REFERER_TYPE_WEBSITE))) { $type = $referrer['referer_type']; $name = $referrer['referer_name']; $keyword = $referrer['referer_keyword']; $time = $referrerTimestamp; } } $goal += array('referer_type' => $type, 'referer_name' => $name, 'referer_keyword' => $keyword, 'referer_visit_server_date' => date("Y-m-d", $time)); // some goals are converted, so must be ecommerce Order or Cart Update if ($this->requestIsEcommerce) { $this->recordEcommerceGoal($goal, $visitorInformation); } else { $this->recordStandardGoals($goal, $action, $visitorInformation); } }
/** * In the case of a new visit, we have to do the following actions: * * 1) Insert the new action * * 2) Insert the visit information */ protected function handleNewVisit($actionId, $someGoalsConverted) { printDebug("New Visit."); $localTime = Piwik_Common::getRequestVar('h', $this->getCurrentDate("H"), 'numeric', $this->request) . ':' . Piwik_Common::getRequestVar('m', $this->getCurrentDate("i"), 'numeric', $this->request) . ':' . Piwik_Common::getRequestVar('s', $this->getCurrentDate("s"), 'numeric', $this->request); $serverTime = $this->getCurrentTimestamp(); $serverDate = $this->getCurrentDate(); $idcookie = $this->getVisitorIdcookie(); $returningVisitor = $this->isVisitorKnown() ? 1 : 0; $defaultTimeOnePageVisit = Piwik_Tracker_Config::getInstance()->Tracker['default_time_one_page_visit']; $userInfo = $this->getUserSettingsInformation(); $country = Piwik_Common::getCountry($userInfo['location_browser_lang'], $enableLanguageToCountryGuess = Piwik_Tracker_Config::getInstance()->Tracker['enable_language_to_country_guess']); $refererInfo = $this->getRefererInformation(); // if the referer is Live! or Bing we check if the IP comes from microsoft // we don't count their cloak checks requests (which really is "Live referer spam") see #686 if (in_array($refererInfo['referer_name'], array("Live", "Bing")) && preg_match('/^65\\.55/', long2ip($userInfo['location_ip']))) { throw new Piwik_Tracker_Visit_Excluded("Spam Live bot, go away, you're making me cry"); } /** * Save the visitor */ $this->visitorInfo = array('idsite' => $this->idsite, 'visitor_localtime' => $localTime, 'visitor_idcookie' => $idcookie, 'visitor_returning' => $returningVisitor, 'visit_first_action_time' => Piwik_Tracker::getDatetimeFromTimestamp($serverTime), 'visit_last_action_time' => Piwik_Tracker::getDatetimeFromTimestamp($serverTime), 'visit_server_date' => $serverDate, 'visit_entry_idaction' => $actionId, 'visit_exit_idaction' => $actionId, 'visit_total_actions' => 1, 'visit_total_time' => $defaultTimeOnePageVisit, 'visit_goal_converted' => $someGoalsConverted ? 1 : 0, 'referer_type' => $refererInfo['referer_type'], 'referer_name' => $refererInfo['referer_name'], 'referer_url' => $refererInfo['referer_url'], 'referer_keyword' => $refererInfo['referer_keyword'], 'config_md5config' => $userInfo['config_md5config'], 'config_os' => $userInfo['config_os'], 'config_browser_name' => $userInfo['config_browser_name'], 'config_browser_version' => $userInfo['config_browser_version'], 'config_resolution' => $userInfo['config_resolution'], 'config_pdf' => $userInfo['config_pdf'], 'config_flash' => $userInfo['config_flash'], 'config_java' => $userInfo['config_java'], 'config_director' => $userInfo['config_director'], 'config_quicktime' => $userInfo['config_quicktime'], 'config_realplayer' => $userInfo['config_realplayer'], 'config_windowsmedia' => $userInfo['config_windowsmedia'], 'config_gears' => $userInfo['config_gears'], 'config_silverlight' => $userInfo['config_silverlight'], 'config_cookie' => $userInfo['config_cookie'], 'location_ip' => $userInfo['location_ip'], 'location_browser_lang' => $userInfo['location_browser_lang'], 'location_country' => $country); Piwik_PostEvent('Tracker.newVisitorInformation', $this->visitorInfo); $this->saveVisitorInformation(); }
/** * Records in the DB the association between the visit and this action. * * @param int idVisit is the ID of the current visit in the DB table log_visit * @param int idRefererActionUrl is the ID of the last action done by the current visit. * @param int timeSpentRefererAction is the number of seconds since the last action was done. * It is directly related to idRefererActionUrl. */ public function record( $idVisit, $visitorIdCookie, $idRefererActionUrl, $idRefererActionName, $timeSpentRefererAction) { $this->loadIdActionNameAndUrl(); $idActionName = in_array($this->getActionType(), array(Piwik_Tracker_Action::TYPE_ACTION_NAME, Piwik_Tracker_Action::TYPE_ACTION_URL)) ? (int)$this->getIdActionName() : null; $insert = array( 'idvisit' => $idVisit, 'idsite' => $this->idSite, 'idvisitor' => $visitorIdCookie, 'server_time' => Piwik_Tracker::getDatetimeFromTimestamp($this->timestamp), 'idaction_url' => (int)$this->getIdActionUrl(), 'idaction_name' => $idActionName, 'idaction_url_ref' => $idRefererActionUrl, 'idaction_name_ref' => $idRefererActionName, 'time_spent_ref_action' => $timeSpentRefererAction ); $customVariables = Piwik_Tracker_Visit::getCustomVariables($scope = 'page', $this->request); $insert = array_merge($insert, $customVariables); // Mysqli apparently does not like NULL inserts? $insertWithoutNulls = array(); foreach($insert as $column => $value) { if(!is_null($value)) { $insertWithoutNulls[$column] = $value; } } $fields = implode(", ", array_keys($insertWithoutNulls)); $bind = array_values($insertWithoutNulls); $values = Piwik_Common::getSqlStringFieldsArray($insertWithoutNulls); $sql = "INSERT INTO ".Piwik_Common::prefixTable('log_link_visit_action'). " ($fields) VALUES ($values)"; Piwik_Tracker::getDatabase()->query( $sql, $bind ); $this->idLinkVisitAction = Piwik_Tracker::getDatabase()->lastInsertId(); $info = array( 'idSite' => $this->idSite, 'idLinkVisitAction' => $this->idLinkVisitAction, 'idVisit' => $idVisit, 'idRefererActionUrl' => $idRefererActionUrl, 'idRefererActionName' => $idRefererActionName, 'timeSpentRefererAction' => $timeSpentRefererAction, ); printDebug($insertWithoutNulls); /* * send the Action object ($this) and the list of ids ($info) as arguments to the event */ Piwik_PostEvent('Tracker.Action.record', $this, $info); }
/** * In the case of a new visit, we have to do the following actions: * * 1) Insert the new action * * 2) Insert the visit information */ protected function handleNewVisit($actionId, $someGoalsConverted) { printDebug("New Visit."); $localTime = Piwik_Common::getRequestVar('h', $this->getCurrentDate("H"), 'int', $this->request) . ':' . Piwik_Common::getRequestVar('m', $this->getCurrentDate("i"), 'int', $this->request) . ':' . Piwik_Common::getRequestVar('s', $this->getCurrentDate("s"), 'int', $this->request); $serverTime = $this->getCurrentTimestamp(); $serverDate = $this->getCurrentDate(); $idcookie = $this->getVisitorIdcookie(); $returningVisitor = $this->isVisitorKnown() ? 1 : 0; $defaultTimeOnePageVisit = Piwik_Tracker_Config::getInstance()->Tracker['default_time_one_page_visit']; $userInfo = $this->getUserSettingsInformation(); $country = Piwik_Common::getCountry($userInfo['location_browser_lang'], $enableLanguageToCountryGuess = Piwik_Tracker_Config::getInstance()->Tracker['enable_language_to_country_guess']); $refererInfo = $this->getRefererInformation(); /** * Save the visitor */ $this->visitorInfo = array('idsite' => $this->idsite, 'visitor_localtime' => $localTime, 'visitor_idcookie' => $idcookie, 'visitor_returning' => $returningVisitor, 'visit_first_action_time' => Piwik_Tracker::getDatetimeFromTimestamp($serverTime), 'visit_last_action_time' => Piwik_Tracker::getDatetimeFromTimestamp($serverTime), 'visit_server_date' => $serverDate, 'visit_entry_idaction' => $actionId, 'visit_exit_idaction' => $actionId, 'visit_total_actions' => 1, 'visit_total_time' => $defaultTimeOnePageVisit, 'visit_goal_converted' => $someGoalsConverted ? 1 : 0, 'referer_type' => $refererInfo['referer_type'], 'referer_name' => $refererInfo['referer_name'], 'referer_url' => $refererInfo['referer_url'], 'referer_keyword' => $refererInfo['referer_keyword'], 'config_md5config' => $userInfo['config_md5config'], 'config_os' => $userInfo['config_os'], 'config_browser_name' => $userInfo['config_browser_name'], 'config_browser_version' => $userInfo['config_browser_version'], 'config_resolution' => $userInfo['config_resolution'], 'config_pdf' => $userInfo['config_pdf'], 'config_flash' => $userInfo['config_flash'], 'config_java' => $userInfo['config_java'], 'config_director' => $userInfo['config_director'], 'config_quicktime' => $userInfo['config_quicktime'], 'config_realplayer' => $userInfo['config_realplayer'], 'config_windowsmedia' => $userInfo['config_windowsmedia'], 'config_gears' => $userInfo['config_gears'], 'config_silverlight' => $userInfo['config_silverlight'], 'config_cookie' => $userInfo['config_cookie'], 'location_ip' => $userInfo['location_ip'], 'location_browser_lang' => $userInfo['location_browser_lang'], 'location_country' => $country); Piwik_PostEvent('Tracker.newVisitorInformation', $this->visitorInfo); $this->saveVisitorInformation(); }
/** * Records in the DB the association between the visit and this action. * * @param int idVisit is the ID of the current visit in the DB table log_visit * @param int idRefererActionUrl is the ID of the last action done by the current visit. * @param int timeSpentRefererAction is the number of seconds since the last action was done. * It is directly related to idRefererActionUrl. */ public function record($idVisit, $visitorIdCookie, $idRefererActionUrl, $idRefererActionName, $timeSpentRefererAction) { $this->loadIdActionNameAndUrl(); $idActionName = $this->getIdActionName(); if (is_null($idActionName)) { $idActionName = 0; } Piwik_Tracker::getDatabase()->query("INSERT INTO " . Piwik_Common::prefixTable('log_link_visit_action') . " (idvisit, idsite, idvisitor, server_time, idaction_url, idaction_name, idaction_url_ref, idaction_name_ref, time_spent_ref_action) \n\t\t\t\t\t\t\tVALUES (?,?,?,?,?,?,?,?,?)", array($idVisit, $this->idSite, $visitorIdCookie, Piwik_Tracker::getDatetimeFromTimestamp($this->timestamp), $this->getIdActionUrl(), $idActionName, $idRefererActionUrl, $idRefererActionName, $timeSpentRefererAction)); $this->idLinkVisitAction = Piwik_Tracker::getDatabase()->lastInsertId(); $info = array('idSite' => $this->idSite, 'idLinkVisitAction' => $this->idLinkVisitAction, 'idVisit' => $idVisit, 'idRefererActionUrl' => $idRefererActionUrl, 'idRefererActionName' => $idRefererActionName, 'timeSpentRefererAction' => $timeSpentRefererAction); printDebug($info); /* * send the Action object ($this) and the list of ids ($info) as arguments to the event */ Piwik_PostEvent('Tracker.Action.record', $this, $info); }
function recordGoals($idSite, $visitorInformation, $visitCustomVariables, $action, $referrerTimestamp, $referrerUrl, $referrerCampaignName, $referrerCampaignKeyword) { $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'], $visitorInformation['location_ip']); $location_continent = isset($visitorInformation['location_continent']) ? $visitorInformation['location_continent'] : Piwik_Common::getContinent($location_country); $goal = array('idvisit' => $visitorInformation['idvisit'], 'idsite' => $idSite, 'idvisitor' => $visitorInformation['idvisitor'], 'server_time' => Piwik_Tracker::getDatetimeFromTimestamp($visitorInformation['visit_last_action_time']), 'location_country' => $location_country, 'location_continent' => $location_continent, 'visitor_returning' => $visitorInformation['visitor_returning'], 'visitor_days_since_first' => $visitorInformation['visitor_days_since_first'], 'visitor_count_visits' => $visitorInformation['visitor_count_visits']); // Attributing the correct Referrer to this conversion. // Priority order is as follows: // 1) Campaign name/kwd parsed in the JS // 2) Referrer URL stored in the _ref cookie // 3) If no info from the cookie, attribute to the current visit referrer // 3) Default values: current referrer $type = $visitorInformation['referer_type']; $name = $visitorInformation['referer_name']; $keyword = $visitorInformation['referer_keyword']; $time = $visitorInformation['visit_first_action_time']; // 1) Campaigns from 1st party cookie if (!empty($referrerCampaignName)) { $type = Piwik_Common::REFERER_TYPE_CAMPAIGN; $name = $referrerCampaignName; $keyword = $referrerCampaignKeyword; $time = $referrerTimestamp; } elseif (!empty($referrerUrl)) { $referrer = new Piwik_Tracker_Visit_Referer(); $referrer = $referrer->getRefererInformation($referrerUrl, $currentUrl = '', $idSite); // if the parsed referer is interesting enough, ie. website or search engine if (in_array($referrer['referer_type'], array(Piwik_Common::REFERER_TYPE_SEARCH_ENGINE, Piwik_Common::REFERER_TYPE_WEBSITE))) { $type = $referrer['referer_type']; $name = $referrer['referer_name']; $keyword = $referrer['referer_keyword']; $time = $referrerTimestamp; } } $goal += array('referer_type' => $type, 'referer_name' => $name, 'referer_keyword' => $keyword, 'referer_visit_server_date' => date("Y-m-d", $time)); $goal += $visitCustomVariables; 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_url'] = $action->getIdActionUrl(); $newGoal['idlink_va'] = $action->getIdLinkVisitAction(); } // If multiple Goal conversions per visit, set a cache buster $newGoal['buster'] = $convertedGoal['allow_multiple'] == 0 ? '0' : $visitorInformation['visit_last_action_time']; $newGoalDebug = $newGoal; $newGoalDebug['idvisitor'] = bin2hex($newGoalDebug['idvisitor']); printDebug($newGoalDebug); $fields = implode(", ", array_keys($newGoal)); $bindFields = substr(str_repeat("?,", count($newGoal)), 0, -1); $sql = "INSERT IGNORE INTO " . Piwik_Common::prefixTable('log_conversion') . "\t\n\t\t\t\t\t({$fields}) VALUES ({$bindFields}) "; $bind = array_values($newGoal); Piwik_Tracker::getDatabase()->query($sql, $bind); } }
/** * In the case of a new visit, we have to do the following actions: * * 1) Insert the new action * * 2) Insert the visit information * @param $idActionUrl * @param $idActionName * @param $actionType * @param $visitIsConverted */ protected function handleNewVisit($idActionUrl, $idActionName, $actionType, $visitIsConverted) { printDebug("New Visit (IP = " . Piwik_IP::N2P($this->getVisitorIp()) . ")"); $localTimes = array('h' => (string) Piwik_Common::getRequestVar('h', $this->getCurrentDate("H"), 'int', $this->request), 'i' => (string) Piwik_Common::getRequestVar('m', $this->getCurrentDate("i"), 'int', $this->request), 's' => (string) Piwik_Common::getRequestVar('s', $this->getCurrentDate("s"), 'int', $this->request)); foreach ($localTimes as $k => $time) { if (strlen($time) == 1) { $localTimes[$k] = '0' . $time; } } $localTime = $localTimes['h'] . ':' . $localTimes['i'] . ':' . $localTimes['s']; $idcookie = $this->getVisitorIdcookie(); $defaultTimeOnePageVisit = Piwik_Config::getInstance()->Tracker['default_time_one_page_visit']; // Days since first visit $cookieFirstVisitTimestamp = Piwik_Common::getRequestVar('_idts', 0, 'int', $this->request); if (!$this->isTimestampValid($cookieFirstVisitTimestamp)) { $cookieFirstVisitTimestamp = $this->getCurrentTimestamp(); } $daysSinceFirstVisit = round(($this->getCurrentTimestamp() - $cookieFirstVisitTimestamp) / 86400, $precision = 0); if ($daysSinceFirstVisit < 0) { $daysSinceFirstVisit = 0; } // Number of Visits $visitCount = Piwik_Common::getRequestVar('_idvc', 1, 'int', $this->request); if ($visitCount < 1) { $visitCount = 1; } // Days since last visit $daysSinceLastVisit = 0; $lastVisitTimestamp = Piwik_Common::getRequestVar('_viewts', 0, 'int', $this->request); if ($this->isTimestampValid($lastVisitTimestamp)) { $daysSinceLastVisit = round(($this->getCurrentTimestamp() - $lastVisitTimestamp) / 86400, $precision = 0); if ($daysSinceLastVisit < 0) { $daysSinceLastVisit = 0; } } $daysSinceLastOrder = 0; $isReturningCustomer = false; $lastOrderTimestamp = Piwik_Common::getRequestVar('_ects', 0, 'int', $this->request); if ($this->isTimestampValid($lastOrderTimestamp)) { $daysSinceLastOrder = round(($this->getCurrentTimestamp() - $lastOrderTimestamp) / 86400, $precision = 0); if ($daysSinceLastOrder < 0) { $daysSinceLastOrder = 0; } $isReturningCustomer = true; } // User settings $userInfo = $this->getUserSettingsInformation(); $country = Piwik_Common::getCountry($userInfo['location_browser_lang'], $enableLanguageToCountryGuess = Piwik_Config::getInstance()->Tracker['enable_language_to_country_guess'], $this->getVisitorIp()); // Referrer data $referrer = new Piwik_Tracker_Visit_Referer(); $refererUrl = Piwik_Common::getRequestVar('urlref', '', 'string', $this->request); $currentUrl = Piwik_Common::getRequestVar('url', '', 'string', $this->request); $refererInfo = $referrer->getRefererInformation($refererUrl, $currentUrl, $this->idsite); /** * Save the visitor */ $this->visitorInfo = array('idsite' => $this->idsite, 'visitor_localtime' => $localTime, 'idvisitor' => $idcookie, 'visitor_returning' => $isReturningCustomer ? 2 : ($visitCount > 1 || $this->isVisitorKnown() ? 1 : 0), 'visitor_count_visits' => $visitCount, 'visitor_days_since_last' => $daysSinceLastVisit, 'visitor_days_since_order' => $daysSinceLastOrder, 'visitor_days_since_first' => $daysSinceFirstVisit, 'visit_first_action_time' => Piwik_Tracker::getDatetimeFromTimestamp($this->getCurrentTimestamp()), 'visit_last_action_time' => Piwik_Tracker::getDatetimeFromTimestamp($this->getCurrentTimestamp()), 'visit_entry_idaction_url' => (int) $idActionUrl, 'visit_entry_idaction_name' => (int) $idActionName, 'visit_exit_idaction_url' => (int) $idActionUrl, 'visit_exit_idaction_name' => (int) $idActionName, 'visit_total_actions' => in_array($actionType, array(Piwik_Tracker_Action::TYPE_ACTION_URL, Piwik_Tracker_Action::TYPE_DOWNLOAD, Piwik_Tracker_Action::TYPE_OUTLINK)) ? 1 : 0, 'visit_total_time' => $defaultTimeOnePageVisit, 'visit_goal_converted' => $visitIsConverted ? 1 : 0, 'visit_goal_buyer' => $this->goalManager->getBuyerType(), 'referer_type' => $refererInfo['referer_type'], 'referer_name' => $refererInfo['referer_name'], 'referer_url' => $refererInfo['referer_url'], 'referer_keyword' => $refererInfo['referer_keyword'], 'config_id' => $userInfo['config_id'], 'config_os' => $userInfo['config_os'], 'config_browser_name' => $userInfo['config_browser_name'], 'config_browser_version' => $userInfo['config_browser_version'], 'config_resolution' => $userInfo['config_resolution'], 'config_pdf' => $userInfo['config_pdf'], 'config_flash' => $userInfo['config_flash'], 'config_java' => $userInfo['config_java'], 'config_director' => $userInfo['config_director'], 'config_quicktime' => $userInfo['config_quicktime'], 'config_realplayer' => $userInfo['config_realplayer'], 'config_windowsmedia' => $userInfo['config_windowsmedia'], 'config_gears' => $userInfo['config_gears'], 'config_silverlight' => $userInfo['config_silverlight'], 'config_cookie' => $userInfo['config_cookie'], 'location_ip' => $this->getVisitorIp(), 'location_browser_lang' => $userInfo['location_browser_lang'], 'location_country' => $country); // Add Custom variable key,value to the visitor array $this->visitorInfo = array_merge($this->visitorInfo, $this->visitorCustomVariables); Piwik_PostEvent('Tracker.newVisitorInformation', $this->visitorInfo); $debugVisitInfo = $this->visitorInfo; $debugVisitInfo['idvisitor'] = bin2hex($debugVisitInfo['idvisitor']); $debugVisitInfo['config_id'] = bin2hex($debugVisitInfo['config_id']); printDebug($debugVisitInfo); $this->saveVisitorInformation(); }
function doStepMatchAndSave($idSite, $idVisit, $idRefererAction, $actionName = "", $actionUrl = "", $idActionUrl = Piwik_Funnels::INDEX_MANUAL_CONVERSION) { printDebug('Looking for funnel steps'); $websiteAttributes = Piwik_Tracker_Cache::getCacheWebsiteAttributes($idSite); if (isset($websiteAttributes['funnels'])) { $funnels = $websiteAttributes['funnels']; printDebug('got funnel steps'); } else { $funnels = array(); } if (count($funnels) <= 0) { return; } printDebug("idActionUrl " . $idActionUrl . " idSite " . $idSite . " idVisit " . $idVisit . " idRefererAction " . $idRefererAction); // Is this the next action for a recorded funnel step? $previous_step_action = Piwik_Query("UPDATE " . Piwik_Common::prefixTable('log_funnel_step') . "\n SET idaction_url_next = ?\n WHERE idsite = ? \n AND idvisit = ? \n AND idaction_url = ?\n AND idaction_url_next is null", array($idActionUrl, $idSite, $idVisit, $idRefererAction)); // early out for special case of manual conversion // Since this is a manual conversion for a goal, there is no URL to // match with, so the following loop is simply a waste of resources if ($idActionUrl == Piwik_Funnels::INDEX_MANUAL_CONVERSION) { return; } foreach ($funnels as &$funnel) { $steps = $funnel['steps']; foreach ($steps as &$step) { $url = $actionUrl; // Matching on Page Title if ($step['match_attribute'] == 'title') { $url = $actionName; } if (self::isMatch($url, $step['pattern_type'], $step['url'], $step['case_sensitive'])) { printDebug("Matched Goal Funnel " . $funnel['idfunnel'] . " Step " . $step['idstep'] . "(name: " . $step['name'] . ", url: " . $step['url'] . "). "); $serverTime = time(); $datetimeServer = Piwik_Tracker::getDatetimeFromTimestamp($serverTime); // Look to see if this step has already been recorded for this visit $exists = Piwik_FetchOne("SELECT *\n FROM " . Piwik_Common::prefixTable('log_funnel_step') . " \n WHERE idsite = ? \n AND idfunnel = ?\n AND idstep = ?\n AND idvisit = ?", array($idSite, $funnel['idfunnel'], $step['idstep'], $idVisit)); // Record it if not if (!$exists) { printDebug("Recording..."); Piwik_Query("INSERT INTO " . Piwik_Common::prefixTable('log_funnel_step') . "\n (idvisit, idsite, idaction_url, url, \n idgoal, idfunnel, idstep, \n idaction_url_ref, server_time)\n VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", array($idVisit, $idSite, $idActionUrl, $url, $funnel['idgoal'], $step['idfunnel'], $step['idstep'], $idRefererAction, $datetimeServer)); } } } } }
/** * Records one or several goals matched in this request. */ public function recordGoals($idSite, $visitorInformation, $visitCustomVariables, $action, $referrerTimestamp, $referrerUrl, $referrerCampaignName, $referrerCampaignKeyword) { $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'], $visitorInformation['location_ip'] ); $location_continent = isset($visitorInformation['location_continent']) ? $visitorInformation['location_continent'] : Piwik_Common::getContinent($location_country); $goal = array( 'idvisit' => $visitorInformation['idvisit'], 'idsite' => $idSite, 'idvisitor' => $visitorInformation['idvisitor'], 'server_time' => Piwik_Tracker::getDatetimeFromTimestamp($visitorInformation['visit_last_action_time']), 'location_country' => $location_country, 'location_continent'=> $location_continent, 'visitor_returning' => $visitorInformation['visitor_returning'], 'visitor_days_since_first' => $visitorInformation['visitor_days_since_first'], 'visitor_days_since_order' => $visitorInformation['visitor_days_since_order'], 'visitor_count_visits' => $visitorInformation['visitor_count_visits'], ); // Attributing the correct Referrer to this conversion. // Priority order is as follows: // 1) Campaign name/kwd parsed in the JS // 2) Referrer URL stored in the _ref cookie // 3) If no info from the cookie, attribute to the current visit referrer // 3) Default values: current referrer $type = $visitorInformation['referer_type']; $name = $visitorInformation['referer_name']; $keyword = $visitorInformation['referer_keyword']; $time = $visitorInformation['visit_first_action_time']; // 1) Campaigns from 1st party cookie if(!empty($referrerCampaignName)) { $type = Piwik_Common::REFERER_TYPE_CAMPAIGN; $name = $referrerCampaignName; $keyword = $referrerCampaignKeyword; $time = $referrerTimestamp; } // 2) Referrer URL parsing elseif(!empty($referrerUrl)) { $referrer = new Piwik_Tracker_Visit_Referer(); $referrer = $referrer->getRefererInformation($referrerUrl, $currentUrl = '', $idSite); // if the parsed referer is interesting enough, ie. website or search engine if(in_array($referrer['referer_type'], array(Piwik_Common::REFERER_TYPE_SEARCH_ENGINE, Piwik_Common::REFERER_TYPE_WEBSITE))) { $type = $referrer['referer_type']; $name = $referrer['referer_name']; $keyword = $referrer['referer_keyword']; $time = $referrerTimestamp; } } $goal += array( 'referer_type' => $type, 'referer_name' => $name, 'referer_keyword' => $keyword, // this field is currently unused 'referer_visit_server_date' => date("Y-m-d", $time), ); $goal += $visitCustomVariables; // some goals are converted, so must be ecommerce Order or Cart Update if($this->requestIsEcommerce) { $this->recordEcommerceGoal($goal, $visitorInformation); } else { $this->recordStandardGoals($goal, $action, $visitorInformation); } }