Skip to content

Commit

Permalink
refs #9129 added feature Custom Dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteur committed Nov 12, 2015
1 parent e90d8e4 commit 6084599
Show file tree
Hide file tree
Showing 31 changed files with 408 additions and 101 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,6 @@
path = misc/log-analytics
url = https://github.com/piwik/piwik-log-analytics.git
branch = master
[submodule "plugins/CustomDimensions"]
path = plugins/CustomDimensions
url = https://github.com/piwik/plugin-CustomDimensions.git
1 change: 1 addition & 0 deletions core/API/DocumentationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ public function getExampleUrl($class, $methodName, $parametersToSet = array())
$aParameters['disable_queued_filters'] = false;
$aParameters['disable_generic_filters'] = false;
$aParameters['expanded'] = false;
$aParameters['idDimenson'] = false;

$moduleName = Proxy::getInstance()->getModuleNameFromClassName($class);
$aParameters = array_merge(array('module' => 'API', 'method' => $moduleName . '.' . $methodName), $aParameters);
Expand Down
74 changes: 54 additions & 20 deletions core/DataArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function getDataArrayWithTwoLevels()
public function sumMetricsVisits($label, $row)
{
if (!isset($this->data[$label])) {
$this->data[$label] = self::makeEmptyRow();
$this->data[$label] = static::makeEmptyRow();
}
$this->doSumVisitsMetrics($row, $this->data[$label]);
}
Expand Down Expand Up @@ -80,17 +80,14 @@ public static function makeEmptyRow()
*
* @return void
*/
protected function doSumVisitsMetrics($newRowToAdd, &$oldRowToUpdate, $onlyMetricsAvailableInActionsTable = false)
protected function doSumVisitsMetrics($newRowToAdd, &$oldRowToUpdate)
{
// Pre 1.2 format: string indexed rows are returned from the DB
// Left here for Backward compatibility with plugins doing custom SQL queries using these metrics as string
if (!isset($newRowToAdd[Metrics::INDEX_NB_VISITS])) {
$oldRowToUpdate[Metrics::INDEX_NB_VISITS] += $newRowToAdd['nb_visits'];
$oldRowToUpdate[Metrics::INDEX_NB_ACTIONS] += $newRowToAdd['nb_actions'];
$oldRowToUpdate[Metrics::INDEX_NB_UNIQ_VISITORS] += $newRowToAdd['nb_uniq_visitors'];
if ($onlyMetricsAvailableInActionsTable) {
return;
}
$oldRowToUpdate[Metrics::INDEX_NB_USERS] += $newRowToAdd['nb_users'];
$oldRowToUpdate[Metrics::INDEX_MAX_ACTIONS] = (float)max($newRowToAdd['max_actions'], $oldRowToUpdate[Metrics::INDEX_MAX_ACTIONS]);
$oldRowToUpdate[Metrics::INDEX_SUM_VISIT_LENGTH] += $newRowToAdd['sum_visit_length'];
Expand All @@ -107,9 +104,6 @@ protected function doSumVisitsMetrics($newRowToAdd, &$oldRowToUpdate, $onlyMetri
$oldRowToUpdate[Metrics::INDEX_NB_VISITS] += $newRowToAdd[Metrics::INDEX_NB_VISITS];
$oldRowToUpdate[Metrics::INDEX_NB_ACTIONS] += $newRowToAdd[Metrics::INDEX_NB_ACTIONS];
$oldRowToUpdate[Metrics::INDEX_NB_UNIQ_VISITORS] += $newRowToAdd[Metrics::INDEX_NB_UNIQ_VISITORS];
if ($onlyMetricsAvailableInActionsTable) {
return;
}

// In case the existing Row had no action metrics (eg. Custom Variable XYZ with "visit" scope)
// but the new Row has action metrics (eg. same Custom Variable XYZ this time with a "page" scope)
Expand All @@ -133,11 +127,50 @@ protected function doSumVisitsMetrics($newRowToAdd, &$oldRowToUpdate, $onlyMetri
$oldRowToUpdate[Metrics::INDEX_NB_VISITS_CONVERTED] += $newRowToAdd[Metrics::INDEX_NB_VISITS_CONVERTED];
}

/**
* Adds the given row $newRowToAdd to the existing $oldRowToUpdate passed by reference
* The rows are php arrays Name => value
*
* @param array $newRowToAdd
* @param array $oldRowToUpdate
* @param bool $onlyMetricsAvailableInActionsTable
*
* @return void
*/
protected function doSumActionsMetrics($newRowToAdd, &$oldRowToUpdate)
{
// Pre 1.2 format: string indexed rows are returned from the DB
// Left here for Backward compatibility with plugins doing custom SQL queries using these metrics as string
if (!isset($newRowToAdd[Metrics::INDEX_NB_VISITS])) {
$oldRowToUpdate[Metrics::INDEX_NB_VISITS] += $newRowToAdd['nb_visits'];
$oldRowToUpdate[Metrics::INDEX_NB_ACTIONS] += $newRowToAdd['nb_actions'];
$oldRowToUpdate[Metrics::INDEX_NB_UNIQ_VISITORS] += $newRowToAdd['nb_uniq_visitors'];
return;
}

// Edge case fail safe
if (!isset($oldRowToUpdate[Metrics::INDEX_NB_VISITS])) {
return;
}

$oldRowToUpdate[Metrics::INDEX_NB_VISITS] += $newRowToAdd[Metrics::INDEX_NB_VISITS];
if (array_key_exists(Metrics::INDEX_NB_ACTIONS, $newRowToAdd)) {
$oldRowToUpdate[Metrics::INDEX_NB_ACTIONS] += $newRowToAdd[Metrics::INDEX_NB_ACTIONS];
}
if (array_key_exists(Metrics::INDEX_PAGE_NB_HITS, $newRowToAdd)) {
if (!array_key_exists(Metrics::INDEX_PAGE_NB_HITS, $oldRowToUpdate)) {
$oldRowToUpdate[Metrics::INDEX_PAGE_NB_HITS] = 0;
}
$oldRowToUpdate[Metrics::INDEX_PAGE_NB_HITS] += $newRowToAdd[Metrics::INDEX_PAGE_NB_HITS];
}
$oldRowToUpdate[Metrics::INDEX_NB_UNIQ_VISITORS] += $newRowToAdd[Metrics::INDEX_NB_UNIQ_VISITORS];
}

public function sumMetricsGoals($label, $row)
{
$idGoal = $row['idgoal'];
if (!isset($this->data[$label][Metrics::INDEX_GOALS][$idGoal])) {
$this->data[$label][Metrics::INDEX_GOALS][$idGoal] = self::makeEmptyGoalRow($idGoal);
$this->data[$label][Metrics::INDEX_GOALS][$idGoal] = static::makeEmptyGoalRow($idGoal);
}
$this->doSumGoalsMetrics($row, $this->data[$label][Metrics::INDEX_GOALS][$idGoal]);
}
Expand Down Expand Up @@ -201,9 +234,10 @@ protected function doSumGoalsMetrics($newRowToAdd, &$oldRowToUpdate)
public function sumMetricsActions($label, $row)
{
if (!isset($this->data[$label])) {
$this->data[$label] = self::makeEmptyActionRow();
$this->data[$label] = static::makeEmptyActionRow();
}
$this->doSumVisitsMetrics($row, $this->data[$label], $onlyMetricsAvailableInActionsTable = true);

$this->doSumActionsMetrics($row, $this->data[$label]);
}

protected static function makeEmptyActionRow()
Expand All @@ -218,7 +252,7 @@ protected static function makeEmptyActionRow()
public function sumMetricsEvents($label, $row)
{
if (!isset($this->data[$label])) {
$this->data[$label] = self::makeEmptyEventRow();
$this->data[$label] = static::makeEmptyEventRow();
}
$this->doSumEventsMetrics($row, $this->data[$label], $onlyMetricsAvailableInActionsTable = true);
}
Expand Down Expand Up @@ -250,16 +284,16 @@ protected function doSumEventsMetrics($newRowToAdd, &$oldRowToUpdate)
$oldRowToUpdate[Metrics::INDEX_EVENT_NB_HITS] += $newRowToAdd[Metrics::INDEX_EVENT_NB_HITS];
$oldRowToUpdate[Metrics::INDEX_EVENT_NB_HITS_WITH_VALUE] += $newRowToAdd[Metrics::INDEX_EVENT_NB_HITS_WITH_VALUE];

$newRowToAdd[Metrics::INDEX_EVENT_SUM_EVENT_VALUE] = round($newRowToAdd[Metrics::INDEX_EVENT_SUM_EVENT_VALUE], self::EVENT_VALUE_PRECISION);
$newRowToAdd[Metrics::INDEX_EVENT_SUM_EVENT_VALUE] = round($newRowToAdd[Metrics::INDEX_EVENT_SUM_EVENT_VALUE], static::EVENT_VALUE_PRECISION);
$oldRowToUpdate[Metrics::INDEX_EVENT_SUM_EVENT_VALUE] += $newRowToAdd[Metrics::INDEX_EVENT_SUM_EVENT_VALUE];
$oldRowToUpdate[Metrics::INDEX_EVENT_MAX_EVENT_VALUE] = round(max($newRowToAdd[Metrics::INDEX_EVENT_MAX_EVENT_VALUE], $oldRowToUpdate[Metrics::INDEX_EVENT_MAX_EVENT_VALUE]), self::EVENT_VALUE_PRECISION);
$oldRowToUpdate[Metrics::INDEX_EVENT_MAX_EVENT_VALUE] = round(max($newRowToAdd[Metrics::INDEX_EVENT_MAX_EVENT_VALUE], $oldRowToUpdate[Metrics::INDEX_EVENT_MAX_EVENT_VALUE]), static::EVENT_VALUE_PRECISION);

// Update minimum only if it is set
if ($newRowToAdd[Metrics::INDEX_EVENT_MIN_EVENT_VALUE] !== false) {
if ($oldRowToUpdate[Metrics::INDEX_EVENT_MIN_EVENT_VALUE] === false) {
$oldRowToUpdate[Metrics::INDEX_EVENT_MIN_EVENT_VALUE] = round($newRowToAdd[Metrics::INDEX_EVENT_MIN_EVENT_VALUE], self::EVENT_VALUE_PRECISION);
$oldRowToUpdate[Metrics::INDEX_EVENT_MIN_EVENT_VALUE] = round($newRowToAdd[Metrics::INDEX_EVENT_MIN_EVENT_VALUE], static::EVENT_VALUE_PRECISION);
} else {
$oldRowToUpdate[Metrics::INDEX_EVENT_MIN_EVENT_VALUE] = round(min($newRowToAdd[Metrics::INDEX_EVENT_MIN_EVENT_VALUE], $oldRowToUpdate[Metrics::INDEX_EVENT_MIN_EVENT_VALUE]), self::EVENT_VALUE_PRECISION);
$oldRowToUpdate[Metrics::INDEX_EVENT_MIN_EVENT_VALUE] = round(min($newRowToAdd[Metrics::INDEX_EVENT_MIN_EVENT_VALUE], $oldRowToUpdate[Metrics::INDEX_EVENT_MIN_EVENT_VALUE]), static::EVENT_VALUE_PRECISION);
}
}
}
Expand Down Expand Up @@ -290,7 +324,7 @@ public function sumMetrics($label, $row)
public function sumMetricsVisitsPivot($parentLabel, $label, $row)
{
if (!isset($this->dataTwoLevels[$parentLabel][$label])) {
$this->dataTwoLevels[$parentLabel][$label] = self::makeEmptyRow();
$this->dataTwoLevels[$parentLabel][$label] = static::makeEmptyRow();
}
$this->doSumVisitsMetrics($row, $this->dataTwoLevels[$parentLabel][$label]);
}
Expand All @@ -299,7 +333,7 @@ public function sumMetricsGoalsPivot($parentLabel, $label, $row)
{
$idGoal = $row['idgoal'];
if (!isset($this->dataTwoLevels[$parentLabel][$label][Metrics::INDEX_GOALS][$idGoal])) {
$this->dataTwoLevels[$parentLabel][$label][Metrics::INDEX_GOALS][$idGoal] = self::makeEmptyGoalRow($idGoal);
$this->dataTwoLevels[$parentLabel][$label][Metrics::INDEX_GOALS][$idGoal] = static::makeEmptyGoalRow($idGoal);
}
$this->doSumGoalsMetrics($row, $this->dataTwoLevels[$parentLabel][$label][Metrics::INDEX_GOALS][$idGoal]);
}
Expand All @@ -309,7 +343,7 @@ public function sumMetricsActionsPivot($parentLabel, $label, $row)
if (!isset($this->dataTwoLevels[$parentLabel][$label])) {
$this->dataTwoLevels[$parentLabel][$label] = $this->makeEmptyActionRow();
}
$this->doSumVisitsMetrics($row, $this->dataTwoLevels[$parentLabel][$label], $onlyMetricsAvailableInActionsTable = true);
$this->doSumActionsMetrics($row, $this->dataTwoLevels[$parentLabel][$label]);
}

public function sumMetricsEventsPivot($parentLabel, $label, $row)
Expand Down Expand Up @@ -382,7 +416,7 @@ protected function enrichWithConversions(&$data)
*/
public static function isRowActions($row)
{
return (count($row) == count(self::makeEmptyActionRow())) && isset($row[Metrics::INDEX_NB_ACTIONS]);
return (count($row) == count(static::makeEmptyActionRow())) && isset($row[Metrics::INDEX_NB_ACTIONS]);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions core/Tracker/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ abstract class Action

private $idLinkVisitAction;
private $actionIdsCached = array();
private $customFields = array();
private $actionName;
private $actionType;

Expand Down Expand Up @@ -228,6 +229,16 @@ protected function getUrlAndType()
return false;
}

public function setCustomField($field, $value)
{
$this->customFields[$field] = $value;
}

public function getCustomFields()
{
return $this->customFields;
}

public function getIdActionUrl()
{
$idUrl = $this->actionIdsCached['idaction_url'];
Expand Down Expand Up @@ -386,6 +397,7 @@ public function record(Visitor $visitor, $idReferrerActionUrl, $idReferrerAction
}

$visitAction = array_merge($visitAction, $customVariables);
$visitAction = array_merge($visitAction, $this->customFields);

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

Expand Down
8 changes: 8 additions & 0 deletions core/Tracker/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ public static function clearCacheGeneral()
self::getCache()->delete(self::$cacheIdGeneral);
}

/**
* Clear website cache.
*/
public static function clearWebsiteCache($idSite)
{
self::getCache()->delete((int) $idSite);
}

/**
* Returns contents of general (global) cache.
* If the cache file tmp/cache/tracker/general.php does not exist yet, create it
Expand Down
32 changes: 30 additions & 2 deletions core/Tracker/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public function createVisit($visit)

public function updateVisit($idSite, $idVisit, $valuesToUpdate)
{
list($updateParts, $sqlBind) = $this->visitFieldsToQuery($valuesToUpdate);
list($updateParts, $sqlBind) = $this->fieldsToQuery($valuesToUpdate);

$parts = implode($updateParts, ', ');
$table = Common::prefixTable('log_visit');
Expand All @@ -312,6 +312,34 @@ public function updateVisit($idSite, $idVisit, $valuesToUpdate)
return $wasInserted;
}

public function updateAction($idLinkVa, $valuesToUpdate)
{
if (empty($idLinkVa)) {
return;
}

list($updateParts, $sqlBind) = $this->fieldsToQuery($valuesToUpdate);

$parts = implode($updateParts, ', ');
$table = Common::prefixTable('log_link_visit_action');

$sqlQuery = "UPDATE $table SET $parts WHERE idlink_va = ?";

$sqlBind[] = $idLinkVa;

$db = $this->getDb();
$result = $db->query($sqlQuery, $sqlBind);
$wasInserted = $db->rowCount($result) != 0;

if (!$wasInserted) {
Common::printDebug("Action with this idLinkVa wasn't found in the DB.");
Common::printDebug("$sqlQuery --- ");
Common::printDebug($sqlBind);
}

return $wasInserted;
}

public function findVisitor($idSite, $configId, $idVisitor, $fieldsToRead, $shouldMatchOneFieldOnly, $isVisitorIdToLookup, $timeLookBack, $timeLookAhead)
{
$selectCustomVariables = '';
Expand Down Expand Up @@ -396,7 +424,7 @@ public function isSiteEmpty($siteId)
return $result == null;
}

private function visitFieldsToQuery($valuesToUpdate)
private function fieldsToQuery($valuesToUpdate)
{
$updateParts = array();
$sqlBind = array();
Expand Down
11 changes: 11 additions & 0 deletions core/ViewDataTable/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,17 @@ public function setDefaultColumnsToDisplay($columns, $hasNbVisits, $hasNbUniqVis
$this->columns_to_display = array_filter($columnsToDisplay);
}

public function removeColumnToDisplay($columnToRemove)
{
if (!empty($this->columns_to_display)) {

$key = array_search($columnToRemove, $this->columns_to_display);
if (false !== $key) {
unset($this->columns_to_display[$key]);
}
}
}

/**
* @ignore
*/
Expand Down
Loading

0 comments on commit 6084599

Please sign in to comment.