Skip to content

Commit

Permalink
moved more code from core to plugins, eg Visit, Actions and some Conv…
Browse files Browse the repository at this point in the history
…ersion columns
  • Loading branch information
tsteur committed Jun 17, 2014
1 parent cb6ae9d commit 23fcf59
Show file tree
Hide file tree
Showing 35 changed files with 747 additions and 482 deletions.
23 changes: 0 additions & 23 deletions core/Db/Schema/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,6 @@ public function getTablesCreateSql()
idvisitor BINARY(8) NOT NULL,
visit_last_action_time DATETIME NOT NULL,
config_id BINARY(8) NOT NULL,
config_pdf TINYINT(1) NOT NULL,
config_flash TINYINT(1) NOT NULL,
config_java TINYINT(1) NOT NULL,
config_director TINYINT(1) NOT NULL,
config_quicktime TINYINT(1) NOT NULL,
config_realplayer TINYINT(1) NOT NULL,
config_windowsmedia TINYINT(1) NOT NULL,
config_gears TINYINT(1) NOT NULL,
config_silverlight TINYINT(1) NOT NULL,
config_cookie TINYINT(1) NOT NULL,
location_ip VARBINARY(16) NOT NULL,
PRIMARY KEY(idvisit),
INDEX index_idsite_config_datetime (idsite, config_id, visit_last_action_time),
Expand Down Expand Up @@ -198,19 +188,6 @@ public function getTablesCreateSql()
server_time datetime NOT NULL,
idaction_url int(11) default NULL,
idlink_va int(11) default NULL,
referer_visit_server_date date default NULL,
referer_type int(10) unsigned default NULL,
referer_name varchar(70) default NULL,
referer_keyword varchar(255) default NULL,
visitor_returning tinyint(1) NOT NULL,
visitor_count_visits SMALLINT(5) UNSIGNED NOT NULL,
visitor_days_since_first SMALLINT(5) UNSIGNED NOT NULL,
visitor_days_since_order SMALLINT(5) UNSIGNED NOT NULL,
location_country char(3) NOT NULL,
location_region char(2) DEFAULT NULL,
location_city varchar(255) DEFAULT NULL,
location_latitude float(10, 6) DEFAULT NULL,
location_longitude float(10, 6) DEFAULT NULL,
url text NOT NULL,
idgoal int(10) NOT NULL,
buster int unsigned NOT NULL,
Expand Down
49 changes: 46 additions & 3 deletions core/Plugin/VisitDimension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use Piwik\Common;
use Piwik\Db;
use Piwik\Tracker\Request;
use Piwik\Plugin\Manager as PluginManager;

/**
Expand All @@ -36,15 +35,39 @@ protected function init()

}

public function hasImplementedEvent($method)
{
$reflectionObject = new \ReflectionObject($this);
$declaringClass = $reflectionObject->getMethod($method)->getDeclaringClass();

return get_class() !== $declaringClass->name;
}

public function install()
{
if (empty($this->fieldName) || empty($this->fieldType)) {
return;
}

try {
$sql = "ALTER TABLE `" . Common::prefixTable("log_visit") . "` ADD `$this->fieldName` $this->fieldType";
Db::exec($sql);
if ($this->hasImplementedEvent('onNewVisit')
|| $this->hasImplementedEvent('onExistingVisit')
|| $this->hasImplementedEvent('onConvertedVisit') ) {
$sql = "ALTER TABLE `" . Common::prefixTable("log_visit") . "` ADD `$this->fieldName` $this->fieldType;";
Db::exec($sql);
}

} catch (\Exception $e) {
if (!Db::get()->isErrNo($e, '1060')) {
throw $e;
}
}

try {
if ($this->hasImplementedEvent('onRecordGoal')) {
$sql = "ALTER TABLE `" . Common::prefixTable("log_conversion") . "` ADD `$this->fieldName` $this->fieldType;";
Db::exec($sql);
}

} catch (\Exception $e) {
if (!Db::get()->isErrNo($e, '1060')) {
Expand Down Expand Up @@ -82,6 +105,26 @@ public function getFieldName()
return $this->fieldName;
}

public function onNewVisit()
{
return false;
}

public function onExistingVisit()
{
return false;
}

public function onConvertedVisit()
{
return false;
}

public function onRecordGoal()
{
return false;
}

abstract public function getName();

/** @return \Piwik\Plugin\VisitDimension[] */
Expand Down
4 changes: 3 additions & 1 deletion core/Tracker/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ private static function getPriority(Action $actionType)
{
$key = array_search($actionType->getActionType(), self::$factoryPriority);

if (!$key) {
if (false === $key) {
return -1;
}

return $key;
}

public function shouldHandle()
Expand Down
105 changes: 10 additions & 95 deletions core/Tracker/GoalManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Piwik\Common;
use Piwik\Config;
use Piwik\Piwik;
use Piwik\Plugin\VisitDimension;
use Piwik\Plugins\CustomVariables\CustomVariables;
use Piwik\Tracker;

Expand Down Expand Up @@ -186,43 +187,23 @@ function detectGoalId($idSite)
/**
* Records one or several goals matched in this request.
*
* @param int $idSite
* @param Visitor $visitor
* @param array $visitorInformation
* @param array $visitCustomVariables
* @param Action $action
*/
public function recordGoals($idSite, $visitorInformation, $visitCustomVariables, $action)
public function recordGoals(Visitor $visitor, $visitorInformation, $visitCustomVariables, $action)
{
$referrerTimestamp = $this->request->getParam('_refts');
$referrerUrl = $this->request->getParam('_ref');
$referrerCampaignName = trim(urldecode($this->request->getParam('_rcn')));
$referrerCampaignKeyword = trim(urldecode($this->request->getParam('_rck')));
$browserLanguage = $this->request->getBrowserLanguage();

$location_country = isset($visitorInformation['location_country'])
? $visitorInformation['location_country']
: Common::getCountry(
$browserLanguage,
$enableLanguageToCountryGuess = Config::getInstance()->Tracker['enable_language_to_country_guess'],
$visitorInformation['location_ip']
);

$goal = array(
'idvisit' => $visitorInformation['idvisit'],
'idsite' => $idSite,
'idvisitor' => $visitorInformation['idvisitor'],
'server_time' => 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'],
'idvisit' => $visitorInformation['idvisit'],
'idvisitor' => $visitorInformation['idvisitor'],
'server_time' => Tracker::getDatetimeFromTimestamp($visitorInformation['visit_last_action_time'])
);

$extraLocationCols = array('location_region', 'location_city', 'location_latitude', 'location_longitude');
foreach ($extraLocationCols as $col) {
if (isset($visitorInformation[$col])) {
$goal[$col] = $visitorInformation[$col];
foreach (VisitDimension::getAllDimensions() as $dimension) {
$value = $dimension->onRecordGoal($this->request, $visitor, $action);
if (false !== $value) {
$goal[$dimension->getFieldName()] = $value;
}
}

Expand All @@ -244,55 +225,6 @@ public function recordGoals($idSite, $visitorInformation, $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($referrerCampaignName)
&& $type == Common::REFERRER_TYPE_CAMPAIGN
&& !empty($name)
) {
// Use default values per above
} // 1) Campaigns from 1st party cookie
elseif (!empty($referrerCampaignName)) {
$type = Common::REFERRER_TYPE_CAMPAIGN;
$name = $referrerCampaignName;
$keyword = $referrerCampaignKeyword;
$time = $referrerTimestamp;
} // 2) Referrer URL parsing
elseif (!empty($referrerUrl)) {
$referrer = new Referrer();
$referrer = $referrer->getReferrerInformation($referrerUrl, $currentUrl = '', $idSite);

// if the parsed referrer is interesting enough, ie. website or search engine
if (in_array($referrer['referer_type'], array(Common::REFERRER_TYPE_SEARCH_ENGINE, Common::REFERRER_TYPE_WEBSITE))) {
$type = $referrer['referer_type'];
$name = $referrer['referer_name'];
$keyword = $referrer['referer_keyword'];
$time = $referrerTimestamp;
}
}
$this->setCampaignValuesToLowercase($type, $name, $keyword);

$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),
);

// some goals are converted, so must be ecommerce Order or Cart Update
if ($this->requestIsEcommerce) {
$this->recordEcommerceGoal($goal, $visitorInformation);
Expand Down Expand Up @@ -840,23 +772,6 @@ protected function updateExistingConversion($newGoal, $updateWhere)
return true;
}

/**
* @param $type
* @param $name
* @param $keyword
*/
protected function setCampaignValuesToLowercase($type, &$name, &$keyword)
{
if ($type === Common::REFERRER_TYPE_CAMPAIGN) {
if (!empty($name)) {
$name = Common::mb_strtolower($name);
}
if (!empty($keyword)) {
$keyword = Common::mb_strtolower($keyword);
}
}
}

/**
* @param $goal
* @param $pattern_type
Expand Down
Loading

0 comments on commit 23fcf59

Please sign in to comment.