Skip to content

Commit

Permalink
In the tracker, process dimension values (via Dimension class method)…
Browse files Browse the repository at this point in the history
… before inserting/updating in MySQL instead of directly after computing dimension values. This way, if plugins modify dimension values, they will still be processed correctly (ie, truncated so MySQL doesn't throw) w/o the plugins having to know about the processing logic.
  • Loading branch information
diosmosis committed Dec 22, 2014
1 parent 3522045 commit e64dbe4
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 16 deletions.
32 changes: 32 additions & 0 deletions core/Columns/Dimension.php
Expand Up @@ -240,4 +240,36 @@ public function getModule()
$parts = explode('.', $id);
return reset($parts);
}

/**
* Prepare a dimension value found in a tracker request for persistence in MySQL. Dimensions
* should use this method to, for example, truncate values that are too long for their MySQL
* database columns.
*
* Note: This method may be removed if/when a proper data access layer is added to Piwik.
*
* @param mixed $value
* @return mixed
*/
public function prepareDimensionValueForPersistence($value)
{
return $value;
}

/**
* Helper function for the tracker. Calls prepareDimensionValueForPersistence() on dimensions that
* are present in $dimensionValues.
* @param Dimension[] $dimensions
* @param array $dimensionValues
* @ignore
*/
public static function prepareDimensionValuesForPersistence($dimensions, &$dimensionValues)
{
foreach ($dimensions as $dimension) {
$dimensionName = $dimension->getColumnName();
if (isset($dimensionValues[$dimensionName])) {
$dimensionValues[$dimensionName] = $dimension->prepareDimensionValueForPersistence($dimensionValues[$dimensionName]);
}
}
}
}
3 changes: 1 addition & 2 deletions core/Plugin/Dimension/VisitDimension.php
Expand Up @@ -352,5 +352,4 @@ public static function getDimensions(Plugin $plugin)

return $instances;
}

}
}
3 changes: 3 additions & 0 deletions core/Tracker/Action.php
Expand Up @@ -10,6 +10,7 @@
namespace Piwik\Tracker;

use Exception;
use Piwik\Columns\Dimension;
use Piwik\Common;
use Piwik\Piwik;
use Piwik\Plugin\Dimension\ActionDimension;
Expand Down Expand Up @@ -378,6 +379,8 @@ public function record(Visitor $visitor, $idReferrerActionUrl, $idReferrerAction

$visitAction = array_merge($visitAction, $customVariables);

Dimension::prepareDimensionValuesForPersistence($dimensions, $visitAction);

$this->idLinkVisitAction = $this->getModel()->createAction($visitAction);

$visitAction['idlink_va'] = $this->idLinkVisitAction;
Expand Down
7 changes: 5 additions & 2 deletions core/Tracker/GoalManager.php
Expand Up @@ -9,6 +9,7 @@
namespace Piwik\Tracker;

use Exception;
use Piwik\Columns\Dimension;
use Piwik\Common;
use Piwik\Date;
use Piwik\Piwik;
Expand Down Expand Up @@ -298,14 +299,15 @@ protected function recordEcommerceGoal($conversion, Visitor $visitor, $action, $
Common::printDebug("There is an existing cart for this visit");
}

$conversionDimensions = ConversionDimension::getAllDimensions();

if ($this->isGoalAnOrder) {
$debugMessage = 'The conversion is an Ecommerce order';

$conversion['idorder'] = $this->orderId;
$conversion['idgoal'] = self::IDGOAL_ORDER;
$conversion['buster'] = Common::hashStringToInt($this->orderId);

$conversionDimensions = ConversionDimension::getAllDimensions();
$conversion = $this->triggerHookOnDimensions($conversionDimensions, 'onEcommerceOrderConversion', $visitor, $action, $conversion);
} // If Cart update, select current items in the previous Cart
else {
Expand All @@ -314,7 +316,6 @@ protected function recordEcommerceGoal($conversion, Visitor $visitor, $action, $
$conversion['buster'] = 0;
$conversion['idgoal'] = self::IDGOAL_CART;

$conversionDimensions = ConversionDimension::getAllDimensions();
$conversion = $this->triggerHookOnDimensions($conversionDimensions, 'onEcommerceCartUpdateConversion', $visitor, $action, $conversion);
}

Expand All @@ -334,6 +335,8 @@ protected function recordEcommerceGoal($conversion, Visitor $visitor, $action, $

$conversion['items'] = $itemsCount;

Dimension::prepareDimensionValuesForPersistence($conversionDimensions, $conversion);

if ($this->isThereExistingCartInVisit) {
$recorded = $this->getModel()->updateConversion($visitInformation['idvisit'], self::IDGOAL_CART, $conversion);
} else {
Expand Down
11 changes: 11 additions & 0 deletions core/Tracker/Visit.php
Expand Up @@ -9,6 +9,7 @@

namespace Piwik\Tracker;

use Piwik\Columns\Dimension;
use Piwik\Common;
use Piwik\Config;
use Piwik\DataAccess\ArchiveInvalidator;
Expand Down Expand Up @@ -437,6 +438,8 @@ private static function toCanonicalHost($host)
*/
protected function updateExistingVisit($valuesToUpdate)
{
$this->prepareDimensionValuesForPersistence($valuesToUpdate);

$idSite = $this->request->getIdSite();
$idVisit = (int) $this->visitorInfo['idvisit'];

Expand Down Expand Up @@ -603,6 +606,8 @@ private function setIdVisitorForExistingVisit($visitor, $valuesToUpdate)

protected function insertNewVisit($visit)
{
$this->prepareDimensionValuesForPersistence($visit);

return $this->getModel()->createVisit($visit);
}

Expand Down Expand Up @@ -650,4 +655,10 @@ private function markArchivedReportsAsInvalidIfArchiveAlreadyFinished()
$invalidReport->rememberToInvalidateArchivedReportsLater($idSite, $date);
}
}

private function prepareDimensionValuesForPersistence(&$dimensionValues)
{
$dimensions = $this->getAllVisitDimensions();
Dimension::prepareDimensionValuesForPersistence($dimensions, $dimensionValues);
}
}
12 changes: 6 additions & 6 deletions plugins/Referrers/Columns/Keyword.php
Expand Up @@ -42,11 +42,6 @@ public function getName()
public function onNewVisit(Request $request, Visitor $visitor, $action)
{
$information = $this->getReferrerInformationFromRequest($request);

if (!empty($information['referer_keyword'])) {
return substr($information['referer_keyword'], 0, 255);
}

return $information['referer_keyword'];
}

Expand All @@ -60,4 +55,9 @@ public function onAnyGoalConversion(Request $request, Visitor $visitor, $action)
{
return $this->getValueForRecordGoal($request, $visitor);
}
}

public function prepareDimensionValueForPersistence($value)
{
return substr($value, 0, 255);
}
}
11 changes: 5 additions & 6 deletions plugins/Referrers/Columns/ReferrerName.php
Expand Up @@ -36,12 +36,6 @@ protected function configureSegments()
public function onNewVisit(Request $request, Visitor $visitor, $action)
{
$information = $this->getReferrerInformationFromRequest($request);

if (!empty($information['referer_name'])) {

return substr($information['referer_name'], 0, 70);
}

return $information['referer_name'];
}

Expand All @@ -55,4 +49,9 @@ public function onAnyGoalConversion(Request $request, Visitor $visitor, $action)
{
return $this->getValueForRecordGoal($request, $visitor);
}

public function prepareDimensionValueForPersistence($value)
{
return substr($value, 0, 70);
}
}

0 comments on commit e64dbe4

Please sign in to comment.