Skip to content

Commit

Permalink
this should fix the tests, in case of new visits the idvisitor was no…
Browse files Browse the repository at this point in the history
…t set
  • Loading branch information
tsteur committed Jun 17, 2014
1 parent 2fce8c6 commit e6459f1
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 36 deletions.
7 changes: 7 additions & 0 deletions core/Plugin/ActionDimension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@

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

/**
* @api
Expand Down Expand Up @@ -139,4 +141,9 @@ public static function getDimensions(\Piwik\Plugin $plugin)
return $instances;
}

public function onNewAction(Request $request, Visitor $visitor, Action $action)
{
return false;
}

}
40 changes: 24 additions & 16 deletions core/Plugin/VisitDimension.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,63 +48,71 @@ public function hasImplementedEvent($method)

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

try {
if ($this->hasImplementedEvent('onNewVisit')
|| $this->hasImplementedEvent('onExistingVisit')
|| $this->hasImplementedEvent('onConvertedVisit') ) {
if ($this->isHandlingLogVisit()) {
$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')) {
if ($this->isHandlingLogConversion()) {
$sql = "ALTER TABLE `" . Common::prefixTable("log_conversion") . "` ADD `$this->fieldName` $this->fieldType";
Db::exec($sql);
}

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

private function isHandlingLogVisit()
{
if (empty($this->fieldName) || empty($this->fieldType)) {
return false;
}

return $this->hasImplementedEvent('onNewVisit')
|| $this->hasImplementedEvent('onExistingVisit')
|| $this->hasImplementedEvent('onConvertedVisit');
}

private function isHandlingLogConversion()
{
if (empty($this->fieldName) || empty($this->fieldType)) {
return false;
}

return $this->hasImplementedEvent('onRecordGoal');
}

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

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

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

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

} catch (\Exception $e) {
if (!Db::get()->isErrNo($e, '1091')) {
throw $e;
Expand Down
10 changes: 4 additions & 6 deletions core/Tracker/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ static private function getAllActions(Request $request)
/** @var \Piwik\Tracker\Action $instance */
$instance = new $action($request);

if ($instance->shouldHandle($request)) {
if ($instance->shouldHandle()) {
$instances[] = $instance;
}
}
Expand Down Expand Up @@ -314,12 +314,10 @@ public function record(Visitor $visitor, $idReferrerActionUrl, $idReferrerAction
$dimensions = ActionDimension::getAllDimensions();

foreach ($dimensions as $dimension) {
if (method_exists($dimension, 'onNewAction')) {
$value = $dimension->onNewAction($this->request, $this, $visitor);
$value = $dimension->onNewAction($this->request, $visitor, $this);

if ($value !== false) {
$visitAction[$dimension->getFieldName()] = $value;
}
if ($value !== false) {
$visitAction[$dimension->getFieldName()] = $value;
}
}

Expand Down
19 changes: 11 additions & 8 deletions core/Tracker/Visit.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,9 @@ public function setRequest(Request $request)
public function handle()
{
// the IP is needed by isExcluded() and GoalManager->recordGoals()
$ip = $this->request->getIp();
$this->visitorInfo['location_ip'] = $ip;
$this->visitorInfo['location_ip'] = $this->request->getIp();

$excluded = new VisitExcluded($this->request, $ip);
$excluded = new VisitExcluded($this->request, $this->visitorInfo['location_ip']);
if ($excluded->isExcluded()) {
return;
}
Expand Down Expand Up @@ -186,7 +185,7 @@ public function handle()
} // When the row wasn't found in the logs, and this is a pageview or
// goal matching URL, we force a new visitor
else {
$visitor->setIsVisitorKonwn(false);
$visitor->setIsVisitorKnown(false);
}
}
}
Expand Down Expand Up @@ -295,12 +294,15 @@ protected function handleNewVisit($visitor, $action, $visitIsConverted)
{
Common::printDebug("New Visit (IP = " . IP::N2P($this->getVisitorIp()) . ")");

$idVisitor = $this->getVisitorIdcookie($visitor);
$this->visitorInfo = $this->getNewVisitorInformation($idVisitor);
$this->visitorInfo = $this->getNewVisitorInformation($visitor);

// Add Custom variable key,value to the visitor array
$this->visitorInfo = array_merge($this->visitorInfo, $this->visitorCustomVariables);

foreach ($this->visitorInfo as $key => $value) {
$visitor->setVisitorColumn($key, $value);
}

$dimensions = VisitDimension::getAllDimensions();

$this->triggerHookOnDimensions($dimensions, 'onNewVisit', $visitor, $action);
Expand Down Expand Up @@ -485,10 +487,10 @@ protected function printVisitorInformation()
Common::printDebug($debugVisitInfo);
}

protected function getNewVisitorInformation($idVisitor)
protected function getNewVisitorInformation($visitor)
{
return array(
'idvisitor' => $idVisitor,
'idvisitor' => $this->getVisitorIdcookie($visitor),
'config_id' => $this->getSettingsObject()->getConfigId(),
'location_ip' => $this->getVisitorIp(),
);
Expand All @@ -509,6 +511,7 @@ protected function getExistingVisitFieldsToUpdate($visitor, $action, $visitIsCon
// Might update the idvisitor when it was forced or overwritten for this visit
if (strlen($this->visitorInfo['idvisitor']) == Tracker::LENGTH_BINARY_ID) {
$valuesToUpdate['idvisitor'] = $this->visitorInfo['idvisitor'];
$visitor->setVisitorColumn('idvisitor', $this->visitorInfo['idvisitor']);
}

$dimensions = VisitDimension::getAllDimensions();
Expand Down
6 changes: 3 additions & 3 deletions core/Tracker/Visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(Request $request, $configId, $visitorInfo = array(),
*/
public function recognize()
{
$this->setIsVisitorKonwn(false);
$this->setIsVisitorKnown(false);

$configId = $this->configId;

Expand Down Expand Up @@ -188,7 +188,7 @@ public function recognize()
}
}

$this->setIsVisitorKonwn(true);
$this->setIsVisitorKnown(true);
Common::printDebug("The visitor is known (idvisitor = " . bin2hex($this->visitorInfo['idvisitor']) . ",
config_id = " . bin2hex($configId) . ",
idvisit = {$this->visitorInfo['idvisit']},
Expand Down Expand Up @@ -323,7 +323,7 @@ public function isVisitorKnown()
return $this->visitorKnown === true;
}

public function setIsVisitorKonwn($isVisitorKnown)
public function setIsVisitorKnown($isVisitorKnown)
{
return $this->visitorKnown = $isVisitorKnown;
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/Actions/Columns/ServerTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function getName()
return '';
}

public function onNewAction(Request $request, Action $action, Visitor $visitor)
public function onNewAction(Request $request, Visitor $visitor, Action $action)
{
$timestamp = $request->getCurrentTimestamp();

Expand Down
4 changes: 2 additions & 2 deletions plugins/Actions/Columns/TimeSpentRefAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Piwik\Plugin\ActionDimension;
use Piwik\Tracker\Action;
use Piwik\Tracker\Request;
use Piwik\Tracker;
use Piwik\Tracker\Visitor;

class TimeSpentRefAction extends ActionDimension
{
Expand All @@ -23,7 +23,7 @@ public function getName()
return '';
}

public function onNewAction(Request $request, Action $action, Tracker\Visitor $visitor)
public function onNewAction(Request $request, Visitor $visitor, Action $action)
{
$timeSpent = $visitor->getVisitorColumn('time_spent_ref_action');

Expand Down

0 comments on commit e6459f1

Please sign in to comment.