diff options
author | mattab <matthieu.aubry@gmail.com> | 2013-03-28 03:42:39 +0400 |
---|---|---|
committer | mattab <matthieu.aubry@gmail.com> | 2013-03-28 03:42:40 +0400 |
commit | ae4b03163792f0b6e933933e5d37df87dc3fd566 (patch) | |
tree | d1d7510a9728f587d3d63ebd03e4ecf3d904838b /plugins/CustomVariables | |
parent | 158c2150f5f2e13ece459b8d131244c11b763997 (diff) |
Mass conversion of all files to the newly agreed coding standard: PSR 1/2
Converting Piwik core source files, PHP, JS, TPL, CSS
More info: http://piwik.org/participate/coding-standards/
Diffstat (limited to 'plugins/CustomVariables')
-rw-r--r-- | plugins/CustomVariables/API.php | 144 | ||||
-rw-r--r-- | plugins/CustomVariables/Controller.php | 78 | ||||
-rw-r--r-- | plugins/CustomVariables/CustomVariables.php | 631 |
3 files changed, 419 insertions, 434 deletions
diff --git a/plugins/CustomVariables/API.php b/plugins/CustomVariables/API.php index 1a340e4964..3a47c837c5 100644 --- a/plugins/CustomVariables/API.php +++ b/plugins/CustomVariables/API.php @@ -1,111 +1,105 @@ <?php /** * Piwik - Open source web analytics - * + * * @link http://piwik.org * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later - * + * * @category Piwik_Plugins * @package Piwik_CustomVariables */ /** * The Custom Variables API lets you access reports for your <a href='http://piwik.org/docs/custom-variables/' target='_blank'>Custom Variables</a> names and values. - * + * * @package Piwik_CustomVariables */ -class Piwik_CustomVariables_API +class Piwik_CustomVariables_API { - static private $instance = null; - - /** - * @return Piwik_CustomVariables_API - */ - static public function getInstance() - { - if (self::$instance == null) - { - self::$instance = new self; - } - return self::$instance; - } + static private $instance = null; /** - * @param int $idSite - * @param string $period + * @return Piwik_CustomVariables_API + */ + static public function getInstance() + { + if (self::$instance == null) { + self::$instance = new self; + } + return self::$instance; + } + + /** + * @param int $idSite + * @param string $period * @param Piwik_Date $date - * @param string $segment - * @param bool $expanded - * @param int $idSubtable + * @param string $segment + * @param bool $expanded + * @param int $idSubtable * * @return Piwik_DataTable|Piwik_DataTable_Array */ - protected function getDataTable($idSite, $period, $date, $segment, $expanded, $idSubtable) - { - $dataTable = Piwik_Archive::getDataTableFromArchive('CustomVariables_valueByName', $idSite, $period, $date, $segment, $expanded, $idSubtable); - $dataTable->filter('Sort', array(Piwik_Archive::INDEX_NB_VISITS, 'desc', $naturalSort = false, $expanded)); - $dataTable->queueFilter('ReplaceColumnNames'); - return $dataTable; - } + protected function getDataTable($idSite, $period, $date, $segment, $expanded, $idSubtable) + { + $dataTable = Piwik_Archive::getDataTableFromArchive('CustomVariables_valueByName', $idSite, $period, $date, $segment, $expanded, $idSubtable); + $dataTable->filter('Sort', array(Piwik_Archive::INDEX_NB_VISITS, 'desc', $naturalSort = false, $expanded)); + $dataTable->queueFilter('ReplaceColumnNames'); + return $dataTable; + } /** - * @param int $idSite - * @param string $period - * @param Piwik_Date $date - * @param string|bool $segment - * @param bool $expanded - * @param bool $_leavePiwikCoreVariables + * @param int $idSite + * @param string $period + * @param Piwik_Date $date + * @param string|bool $segment + * @param bool $expanded + * @param bool $_leavePiwikCoreVariables * * @return Piwik_DataTable|Piwik_DataTable_Array */ - public function getCustomVariables($idSite, $period, $date, $segment = false, $expanded = false, $_leavePiwikCoreVariables = false) - { - $dataTable = $this->getDataTable($idSite, $period, $date, $segment, $expanded, $idSubtable = null); + public function getCustomVariables($idSite, $period, $date, $segment = false, $expanded = false, $_leavePiwikCoreVariables = false) + { + $dataTable = $this->getDataTable($idSite, $period, $date, $segment, $expanded, $idSubtable = null); - if($dataTable instanceof Piwik_DataTable - && !$_leavePiwikCoreVariables) - { - $mapping = array('_pks', '_pkn', '_pkc', '_pkp', Piwik_Tracker_Action::CVAR_KEY_SEARCH_COUNT, Piwik_Tracker_Action::CVAR_KEY_SEARCH_CATEGORY ); - foreach($mapping as $name) - { - $row = $dataTable->getRowFromLabel($name); - if($row) - { - $dataTable->deleteRow($dataTable->getRowIdFromLabel($name)); - } - } - } - return $dataTable; - } + if ($dataTable instanceof Piwik_DataTable + && !$_leavePiwikCoreVariables + ) { + $mapping = array('_pks', '_pkn', '_pkc', '_pkp', Piwik_Tracker_Action::CVAR_KEY_SEARCH_COUNT, Piwik_Tracker_Action::CVAR_KEY_SEARCH_CATEGORY); + foreach ($mapping as $name) { + $row = $dataTable->getRowFromLabel($name); + if ($row) { + $dataTable->deleteRow($dataTable->getRowIdFromLabel($name)); + } + } + } + return $dataTable; + } /** - * @param int $idSite - * @param string $period - * @param Piwik_Date $date - * @param int $idSubtable - * @param string|bool $segment - * @param bool $_leavePriceViewedColumn + * @param int $idSite + * @param string $period + * @param Piwik_Date $date + * @param int $idSubtable + * @param string|bool $segment + * @param bool $_leavePriceViewedColumn * * @return Piwik_DataTable|Piwik_DataTable_Array */ - public function getCustomVariablesValuesFromNameId($idSite, $period, $date, $idSubtable, $segment = false, $_leavePriceViewedColumn = false) - { - $dataTable = $this->getDataTable($idSite, $period, $date, $segment, $expanded = false, $idSubtable); + public function getCustomVariablesValuesFromNameId($idSite, $period, $date, $idSubtable, $segment = false, $_leavePriceViewedColumn = false) + { + $dataTable = $this->getDataTable($idSite, $period, $date, $segment, $expanded = false, $idSubtable); - if(!$_leavePriceViewedColumn) - { - $dataTable->deleteColumn('price_viewed'); - } - else - { - // Hack Ecommerce product price tracking to display correctly - $dataTable->renameColumn('price_viewed', 'price'); - } - $dataTable->queueFilter('ColumnCallbackReplace', array('label', create_function('$label', ' + if (!$_leavePriceViewedColumn) { + $dataTable->deleteColumn('price_viewed'); + } else { + // Hack Ecommerce product price tracking to display correctly + $dataTable->renameColumn('price_viewed', 'price'); + } + $dataTable->queueFilter('ColumnCallbackReplace', array('label', create_function('$label', ' return $label == Piwik_CustomVariables::LABEL_CUSTOM_VALUE_NOT_DEFINED - ? "'. Piwik_Translate( 'General_NotDefined', Piwik_Translate('CustomVariables_ColumnCustomVariableValue')) .'" + ? "' . Piwik_Translate('General_NotDefined', Piwik_Translate('CustomVariables_ColumnCustomVariableValue')) . '" : $label;'))); - return $dataTable; - } + return $dataTable; + } } diff --git a/plugins/CustomVariables/Controller.php b/plugins/CustomVariables/Controller.php index eca9bc2b62..ea00259d3e 100644 --- a/plugins/CustomVariables/Controller.php +++ b/plugins/CustomVariables/Controller.php @@ -15,44 +15,44 @@ */ class Piwik_CustomVariables_Controller extends Piwik_Controller { - - function index($fetch = false) - { - return Piwik_View::singleReport( - Piwik_Translate('CustomVariables_CustomVariables'), - $this->getCustomVariables(true), $fetch); - } - - function getCustomVariables($fetch = false) - { - $view = Piwik_ViewDataTable::factory(); - $view->init( $this->pluginName, __FUNCTION__, "CustomVariables.getCustomVariables", "getCustomVariablesValuesFromNameId" ); - - $this->setPeriodVariablesView($view); - $view->enableShowGoals(); - - $view->setColumnsToDisplay( array('label','nb_visits', 'nb_actions') ); - $view->setColumnTranslation('label', Piwik_Translate('CustomVariables_ColumnCustomVariableName')); - $view->setSortedColumn( 'nb_visits' ); - $view->setLimit( 10 ); - $view->setFooterMessage( Piwik_Translate('CustomVariables_TrackingHelp', array('<a target="_blank" href="http://piwik.org/docs/custom-variables/">', '</a>')) ); - $this->setMetricsVariablesView($view); - return $this->renderView($view, $fetch); - } - - function getCustomVariablesValuesFromNameId( $fetch = false) - { - $view = Piwik_ViewDataTable::factory(); - $view->init( $this->pluginName, __FUNCTION__, 'CustomVariables.getCustomVariablesValuesFromNameId' ); - - $view->disableSearchBox(); - $view->enableShowGoals(); - $view->disableExcludeLowPopulation(); - $view->setColumnsToDisplay( array('label','nb_visits', 'nb_actions') ); - $view->setColumnTranslation('label', Piwik_Translate('CustomVariables_ColumnCustomVariableValue')); - - return $this->renderView($view, $fetch); - } - + + function index($fetch = false) + { + return Piwik_View::singleReport( + Piwik_Translate('CustomVariables_CustomVariables'), + $this->getCustomVariables(true), $fetch); + } + + function getCustomVariables($fetch = false) + { + $view = Piwik_ViewDataTable::factory(); + $view->init($this->pluginName, __FUNCTION__, "CustomVariables.getCustomVariables", "getCustomVariablesValuesFromNameId"); + + $this->setPeriodVariablesView($view); + $view->enableShowGoals(); + + $view->setColumnsToDisplay(array('label', 'nb_visits', 'nb_actions')); + $view->setColumnTranslation('label', Piwik_Translate('CustomVariables_ColumnCustomVariableName')); + $view->setSortedColumn('nb_visits'); + $view->setLimit(10); + $view->setFooterMessage(Piwik_Translate('CustomVariables_TrackingHelp', array('<a target="_blank" href="http://piwik.org/docs/custom-variables/">', '</a>'))); + $this->setMetricsVariablesView($view); + return $this->renderView($view, $fetch); + } + + function getCustomVariablesValuesFromNameId($fetch = false) + { + $view = Piwik_ViewDataTable::factory(); + $view->init($this->pluginName, __FUNCTION__, 'CustomVariables.getCustomVariablesValuesFromNameId'); + + $view->disableSearchBox(); + $view->enableShowGoals(); + $view->disableExcludeLowPopulation(); + $view->setColumnsToDisplay(array('label', 'nb_visits', 'nb_actions')); + $view->setColumnTranslation('label', Piwik_Translate('CustomVariables_ColumnCustomVariableValue')); + + return $this->renderView($view, $fetch); + } + } diff --git a/plugins/CustomVariables/CustomVariables.php b/plugins/CustomVariables/CustomVariables.php index 3bf3d184c1..d76adab5ea 100644 --- a/plugins/CustomVariables/CustomVariables.php +++ b/plugins/CustomVariables/CustomVariables.php @@ -14,324 +14,315 @@ */ class Piwik_CustomVariables extends Piwik_Plugin { - public $archiveProcessing; - protected $columnToSortByBeforeTruncation; - protected $maximumRowsInDataTableLevelZero; - protected $maximumRowsInSubDataTable; - - public function getInformation() - { - $info = array( - 'description' => Piwik_Translate('CustomVariables_PluginDescription') - . ' <br/>Required to use <a href="http://piwik.org/docs/ecommerce-analytics/">Ecommerce Analytics</a> feature!', - 'author' => 'Piwik', - 'author_homepage' => 'http://piwik.org/', - 'version' => Piwik_Version::VERSION, - ); - - return $info; - } - - function getListHooksRegistered() - { - $hooks = array( - 'ArchiveProcessing_Day.compute' => 'archiveDay', - 'ArchiveProcessing_Period.compute' => 'archivePeriod', - 'WidgetsList.add' => 'addWidgets', - 'Menu.add' => 'addMenus', - 'Goals.getReportsWithGoalMetrics' => 'getReportsWithGoalMetrics', - 'API.getReportMetadata' => 'getReportMetadata', - 'API.getSegmentsMetadata' => 'getSegmentsMetadata', - ); - return $hooks; - } - - function addWidgets() - { - Piwik_AddWidget( 'General_Visitors', 'CustomVariables_CustomVariables', 'CustomVariables', 'getCustomVariables'); - } - - function addMenus() - { - Piwik_AddMenu('General_Visitors', 'CustomVariables_CustomVariables', array('module' => 'CustomVariables', 'action' => 'index'), $display = true, $order = 50); - } - - /** - * Returns metadata for available reports - * - * @param Piwik_Event_Notification $notification notification object - */ - public function getReportMetadata($notification) - { - $reports = &$notification->getNotificationObject(); - - $documentation = Piwik_Translate('CustomVariables_CustomVariablesReportDocumentation', - array('<br />', '<a href="http://piwik.org/docs/custom-variables/" target="_blank">', '</a>')); - - $reports[] = array( 'category' => Piwik_Translate('General_Visitors'), - 'name' => Piwik_Translate('CustomVariables_CustomVariables'), - 'module' => 'CustomVariables', - 'action' => 'getCustomVariables', - 'actionToLoadSubTables' => 'getCustomVariablesValuesFromNameId', - 'dimension' => Piwik_Translate('CustomVariables_ColumnCustomVariableName'), - 'documentation' => $documentation, - 'order' => 10 ); - - $reports[] = array( 'category' => Piwik_Translate('General_Visitors'), - 'name' => Piwik_Translate('CustomVariables_CustomVariables'), - 'module' => 'CustomVariables', - 'action' => 'getCustomVariablesValuesFromNameId', - 'dimension' => Piwik_Translate('CustomVariables_ColumnCustomVariableValue'), - 'documentation' => $documentation, - 'isSubtableReport' => true, - 'order' => 15 ); - } - - /** - * @param Piwik_Event_Notification $notification notification object - */ - public function getSegmentsMetadata($notification) - { - $segments =& $notification->getNotificationObject(); - for($i=1; $i <= Piwik_Tracker::MAX_CUSTOM_VARIABLES; $i++) - { - $segments[] = array( - 'type' => 'dimension', - 'category' => 'CustomVariables_CustomVariables', - 'name' => Piwik_Translate('CustomVariables_ColumnCustomVariableName').' '.$i - .' ('.Piwik_Translate('CustomVariables_ScopeVisit').')', - 'segment' => 'customVariableName'.$i, - 'sqlSegment' => 'log_visit.custom_var_k'.$i, - ); - $segments[] = array( - 'type' => 'dimension', - 'category' => 'CustomVariables_CustomVariables', - 'name' => Piwik_Translate('CustomVariables_ColumnCustomVariableValue').' '.$i - .' ('.Piwik_Translate('CustomVariables_ScopeVisit').')', - 'segment' => 'customVariableValue'.$i, - 'sqlSegment' => 'log_visit.custom_var_v'.$i, - ); - $segments[] = array( - 'type' => 'dimension', - 'category' => 'CustomVariables_CustomVariables', - 'name' => Piwik_Translate('CustomVariables_ColumnCustomVariableName').' '.$i - .' ('.Piwik_Translate('CustomVariables_ScopePage').')', - 'segment' => 'customVariablePageName'.$i, - 'sqlSegment' => 'log_link_visit_action.custom_var_k'.$i, - ); - $segments[] = array( - 'type' => 'dimension', - 'category' => 'CustomVariables_CustomVariables', - 'name' => Piwik_Translate('CustomVariables_ColumnCustomVariableValue').' '.$i - .' ('.Piwik_Translate('CustomVariables_ScopePage').')', - 'segment' => 'customVariablePageValue'.$i, - 'sqlSegment' => 'log_link_visit_action.custom_var_v'.$i, - ); - } - } - - /** - * Adds Goal dimensions, so that the dimensions are displayed in the UI Goal Overview page - * - * @param Piwik_Event_Notification $notification notification object - */ - function getReportsWithGoalMetrics( $notification ) - { - $dimensions =& $notification->getNotificationObject(); - $dimensions = array_merge($dimensions, array( - array( 'category' => Piwik_Translate('General_Visit'), - 'name' => Piwik_Translate('CustomVariables_CustomVariables'), - 'module' => 'CustomVariables', - 'action' => 'getCustomVariables', - ), - )); - } - - function __construct() - { - $this->maximumRowsInDataTableLevelZero = Piwik_Config::getInstance()->General['datatable_archiving_maximum_rows_referers']; - $this->maximumRowsInSubDataTable = Piwik_Config::getInstance()->General['datatable_archiving_maximum_rows_subtable_referers']; - } - - protected $interestByCustomVariables = array(); - protected $interestByCustomVariablesAndValue = array(); - - /** - * Hooks on daily archive to trigger various log processing - * - * @param Piwik_Event_Notification $notification notification object - * @return void - */ - public function archiveDay( $notification ) - { - $this->interestByCustomVariables = $this->interestByCustomVariablesAndValue = array(); - - /** - * @var Piwik_ArchiveProcessing_Day - */ - $this->archiveProcessing = $notification->getNotificationObject(); - - if(!$this->archiveProcessing->shouldProcessReportsForPlugin($this->getPluginName())) return; - - $this->archiveDayAggregate($this->archiveProcessing); - $this->archiveDayRecordInDatabase($this->archiveProcessing); - destroy($this->interestByCustomVariables); - destroy($this->interestByCustomVariablesAndValue); - } - - const LABEL_CUSTOM_VALUE_NOT_DEFINED = "Value not defined"; - /** - * @param Piwik_ArchiveProcessing_Day $archiveProcessing - * @return void - */ - protected function archiveDayAggregate(Piwik_ArchiveProcessing_Day $archiveProcessing) - { - for($i = 1; $i <= Piwik_Tracker::MAX_CUSTOM_VARIABLES; $i++ ) - { - $keyField = "custom_var_k".$i; - $valueField = "custom_var_v".$i; - $dimensions = array($keyField, $valueField); - $where = "%s.$keyField != ''"; - - // Custom Vars names and values metrics for visits - $query = $archiveProcessing->queryVisitsByDimension($dimensions, $where); - - while($row = $query->fetch() ) - { - // Handle case custom var value is empty - $row[$valueField] = $this->cleanCustomVarValue($row[$valueField]); - - // Aggregate - if(!isset($this->interestByCustomVariables[$row[$keyField]])) $this->interestByCustomVariables[$row[$keyField]]= $archiveProcessing->getNewInterestRow(); - if(!isset($this->interestByCustomVariablesAndValue[$row[$keyField]][$row[$valueField]])) $this->interestByCustomVariablesAndValue[$row[$keyField]][$row[$valueField]] = $archiveProcessing->getNewInterestRow(); - $archiveProcessing->updateInterestStats( $row, $this->interestByCustomVariables[$row[$keyField]]); - $archiveProcessing->updateInterestStats( $row, $this->interestByCustomVariablesAndValue[$row[$keyField]][$row[$valueField]]); - } - - // Custom Vars names and values metrics for page views - $query = $archiveProcessing->queryActionsByDimension($dimensions, $where); - $onlyMetricsAvailableInActionsTable = true; - while($row = $query->fetch() ) - { - // Handle case custom var value is empty - $row[$valueField] = $this->cleanCustomVarValue($row[$valueField]); - - $label = $row[$valueField]; - - // Remove price tracked if it's zero or we if we are not currently tracking an ecommerce var - if(!in_array($row[$keyField], array('_pks', '_pkn', '_pkc'))) - { - unset($row[Piwik_Archive::INDEX_ECOMMERCE_ITEM_PRICE_VIEWED]); - } - - // when custom variable value is a JSON array of categories - // possibly JSON value - $mustInsertCustomVariableValue = true; - if($row[$keyField] == '_pkc' - && $label[0] == '[' && $label[1] == '"') - { - // In case categories were truncated, try closing the array - if(substr($label, -2) != '"]') { - $label .= '"]'; - } - $decoded = @Piwik_Common::json_decode($label); - if(is_array($decoded)) - { - $count = 0; - foreach($decoded as $category) - { - if(empty($category) - || $count >= Piwik_Tracker_GoalManager::MAXIMUM_PRODUCT_CATEGORIES) { - continue; - } - if(!isset($this->interestByCustomVariablesAndValue[$row[$keyField]][$category])) { - $this->interestByCustomVariablesAndValue[$row[$keyField]][$category] = $archiveProcessing->getNewInterestRow($onlyMetricsAvailableInActionsTable); - } - $archiveProcessing->updateInterestStats( $row, $this->interestByCustomVariablesAndValue[$row[$keyField]][$category], $onlyMetricsAvailableInActionsTable); - $mustInsertCustomVariableValue = false; - $count++; - } - } - } // end multi categories hack - - if($mustInsertCustomVariableValue) - { - if(!isset($this->interestByCustomVariablesAndValue[$row[$keyField]][$row[$valueField]])) $this->interestByCustomVariablesAndValue[$row[$keyField]][$row[$valueField]] = $archiveProcessing->getNewInterestRow($onlyMetricsAvailableInActionsTable); - $archiveProcessing->updateInterestStats( $row, $this->interestByCustomVariablesAndValue[$row[$keyField]][$row[$valueField]], $onlyMetricsAvailableInActionsTable); - } - - // Do not report on Price viewed for the Custom Variable names - unset($row[Piwik_Archive::INDEX_ECOMMERCE_ITEM_PRICE_VIEWED]); - - // When tracking Custom Variables with scope=page we do not add up visits numbers - // as it is incorrect to sum visits this way - // for scope=visit this is allowed, since there is supposed to be one custom var value per custom variable name for a given visit - $doNotSumVisits = true; - - if(!isset($this->interestByCustomVariables[$row[$keyField]])) $this->interestByCustomVariables[$row[$keyField]]= $archiveProcessing->getNewInterestRow($onlyMetricsAvailableInActionsTable, $doNotSumVisits); - $archiveProcessing->updateInterestStats( $row, $this->interestByCustomVariables[$row[$keyField]], $onlyMetricsAvailableInActionsTable, $doNotSumVisits); - } - - // Custom Vars names and values metrics for Goals - $query = $archiveProcessing->queryConversionsByDimension($dimensions, $where); - - if($query !== false) - { - while($row = $query->fetch() ) - { - // Handle case custom var value is empty - $row[$valueField] = $this->cleanCustomVarValue($row[$valueField]); - - if(!isset($this->interestByCustomVariables[$row[$keyField]][Piwik_Archive::INDEX_GOALS][$row['idgoal']])) $this->interestByCustomVariables[$row[$keyField]][Piwik_Archive::INDEX_GOALS][$row['idgoal']] = $archiveProcessing->getNewGoalRow($row['idgoal']); - if(!isset($this->interestByCustomVariablesAndValue[$row[$keyField]][$row[$valueField]][Piwik_Archive::INDEX_GOALS][$row['idgoal']])) $this->interestByCustomVariablesAndValue[$row[$keyField]][$row[$valueField]][Piwik_Archive::INDEX_GOALS][$row['idgoal']] = $archiveProcessing->getNewGoalRow($row['idgoal']); - - $archiveProcessing->updateGoalStats( $row, $this->interestByCustomVariables[$row[$keyField]][Piwik_Archive::INDEX_GOALS][$row['idgoal']]); - $archiveProcessing->updateGoalStats( $row, $this->interestByCustomVariablesAndValue[$row[$keyField]][$row[$valueField]][Piwik_Archive::INDEX_GOALS][$row['idgoal']]); - } - } - } - $archiveProcessing->enrichConversionsByLabelArray($this->interestByCustomVariables); - $archiveProcessing->enrichConversionsByLabelArrayHasTwoLevels($this->interestByCustomVariablesAndValue); - } - - protected function cleanCustomVarValue($value) - { - if(strlen($value)) - { - return $value; - } - return self::LABEL_CUSTOM_VALUE_NOT_DEFINED; - } - - /** - * @param Piwik_ArchiveProcessing $archiveProcessing - * @return void - */ - protected function archiveDayRecordInDatabase($archiveProcessing) - { - $recordName = 'CustomVariables_valueByName'; - $table = $archiveProcessing->getDataTableWithSubtablesFromArraysIndexedByLabel($this->interestByCustomVariablesAndValue, $this->interestByCustomVariables); - - $blob = $table->getSerialized( - $this->maximumRowsInDataTableLevelZero, $this->maximumRowsInSubDataTable, - $columnToSort = Piwik_Archive::INDEX_NB_VISITS); - $archiveProcessing->insertBlobRecord($recordName, $blob); - destroy($table); - } - - /** - * @param Piwik_Event_Notification $notification notification object - * @return mixed - */ - function archivePeriod( $notification ) - { - $archiveProcessing = $notification->getNotificationObject(); - - if(!$archiveProcessing->shouldProcessReportsForPlugin($this->getPluginName())) return; - - $dataTableToSum = 'CustomVariables_valueByName'; - $nameToCount = $archiveProcessing->archiveDataTable( - $dataTableToSum, null, $this->maximumRowsInDataTableLevelZero, $this->maximumRowsInSubDataTable, - $columnToSort = Piwik_Archive::INDEX_NB_VISITS); - } + public $archiveProcessing; + protected $columnToSortByBeforeTruncation; + protected $maximumRowsInDataTableLevelZero; + protected $maximumRowsInSubDataTable; + + public function getInformation() + { + $info = array( + 'description' => Piwik_Translate('CustomVariables_PluginDescription') + . ' <br/>Required to use <a href="http://piwik.org/docs/ecommerce-analytics/">Ecommerce Analytics</a> feature!', + 'author' => 'Piwik', + 'author_homepage' => 'http://piwik.org/', + 'version' => Piwik_Version::VERSION, + ); + + return $info; + } + + function getListHooksRegistered() + { + $hooks = array( + 'ArchiveProcessing_Day.compute' => 'archiveDay', + 'ArchiveProcessing_Period.compute' => 'archivePeriod', + 'WidgetsList.add' => 'addWidgets', + 'Menu.add' => 'addMenus', + 'Goals.getReportsWithGoalMetrics' => 'getReportsWithGoalMetrics', + 'API.getReportMetadata' => 'getReportMetadata', + 'API.getSegmentsMetadata' => 'getSegmentsMetadata', + ); + return $hooks; + } + + function addWidgets() + { + Piwik_AddWidget('General_Visitors', 'CustomVariables_CustomVariables', 'CustomVariables', 'getCustomVariables'); + } + + function addMenus() + { + Piwik_AddMenu('General_Visitors', 'CustomVariables_CustomVariables', array('module' => 'CustomVariables', 'action' => 'index'), $display = true, $order = 50); + } + + /** + * Returns metadata for available reports + * + * @param Piwik_Event_Notification $notification notification object + */ + public function getReportMetadata($notification) + { + $reports = & $notification->getNotificationObject(); + + $documentation = Piwik_Translate('CustomVariables_CustomVariablesReportDocumentation', + array('<br />', '<a href="http://piwik.org/docs/custom-variables/" target="_blank">', '</a>')); + + $reports[] = array('category' => Piwik_Translate('General_Visitors'), + 'name' => Piwik_Translate('CustomVariables_CustomVariables'), + 'module' => 'CustomVariables', + 'action' => 'getCustomVariables', + 'actionToLoadSubTables' => 'getCustomVariablesValuesFromNameId', + 'dimension' => Piwik_Translate('CustomVariables_ColumnCustomVariableName'), + 'documentation' => $documentation, + 'order' => 10); + + $reports[] = array('category' => Piwik_Translate('General_Visitors'), + 'name' => Piwik_Translate('CustomVariables_CustomVariables'), + 'module' => 'CustomVariables', + 'action' => 'getCustomVariablesValuesFromNameId', + 'dimension' => Piwik_Translate('CustomVariables_ColumnCustomVariableValue'), + 'documentation' => $documentation, + 'isSubtableReport' => true, + 'order' => 15); + } + + /** + * @param Piwik_Event_Notification $notification notification object + */ + public function getSegmentsMetadata($notification) + { + $segments =& $notification->getNotificationObject(); + for ($i = 1; $i <= Piwik_Tracker::MAX_CUSTOM_VARIABLES; $i++) { + $segments[] = array( + 'type' => 'dimension', + 'category' => 'CustomVariables_CustomVariables', + 'name' => Piwik_Translate('CustomVariables_ColumnCustomVariableName') . ' ' . $i + . ' (' . Piwik_Translate('CustomVariables_ScopeVisit') . ')', + 'segment' => 'customVariableName' . $i, + 'sqlSegment' => 'log_visit.custom_var_k' . $i, + ); + $segments[] = array( + 'type' => 'dimension', + 'category' => 'CustomVariables_CustomVariables', + 'name' => Piwik_Translate('CustomVariables_ColumnCustomVariableValue') . ' ' . $i + . ' (' . Piwik_Translate('CustomVariables_ScopeVisit') . ')', + 'segment' => 'customVariableValue' . $i, + 'sqlSegment' => 'log_visit.custom_var_v' . $i, + ); + $segments[] = array( + 'type' => 'dimension', + 'category' => 'CustomVariables_CustomVariables', + 'name' => Piwik_Translate('CustomVariables_ColumnCustomVariableName') . ' ' . $i + . ' (' . Piwik_Translate('CustomVariables_ScopePage') . ')', + 'segment' => 'customVariablePageName' . $i, + 'sqlSegment' => 'log_link_visit_action.custom_var_k' . $i, + ); + $segments[] = array( + 'type' => 'dimension', + 'category' => 'CustomVariables_CustomVariables', + 'name' => Piwik_Translate('CustomVariables_ColumnCustomVariableValue') . ' ' . $i + . ' (' . Piwik_Translate('CustomVariables_ScopePage') . ')', + 'segment' => 'customVariablePageValue' . $i, + 'sqlSegment' => 'log_link_visit_action.custom_var_v' . $i, + ); + } + } + + /** + * Adds Goal dimensions, so that the dimensions are displayed in the UI Goal Overview page + * + * @param Piwik_Event_Notification $notification notification object + */ + function getReportsWithGoalMetrics($notification) + { + $dimensions =& $notification->getNotificationObject(); + $dimensions = array_merge($dimensions, array( + array('category' => Piwik_Translate('General_Visit'), + 'name' => Piwik_Translate('CustomVariables_CustomVariables'), + 'module' => 'CustomVariables', + 'action' => 'getCustomVariables', + ), + )); + } + + function __construct() + { + $this->maximumRowsInDataTableLevelZero = Piwik_Config::getInstance()->General['datatable_archiving_maximum_rows_referers']; + $this->maximumRowsInSubDataTable = Piwik_Config::getInstance()->General['datatable_archiving_maximum_rows_subtable_referers']; + } + + protected $interestByCustomVariables = array(); + protected $interestByCustomVariablesAndValue = array(); + + /** + * Hooks on daily archive to trigger various log processing + * + * @param Piwik_Event_Notification $notification notification object + * @return void + */ + public function archiveDay($notification) + { + $this->interestByCustomVariables = $this->interestByCustomVariablesAndValue = array(); + + /** + * @var Piwik_ArchiveProcessing_Day + */ + $this->archiveProcessing = $notification->getNotificationObject(); + + if (!$this->archiveProcessing->shouldProcessReportsForPlugin($this->getPluginName())) return; + + $this->archiveDayAggregate($this->archiveProcessing); + $this->archiveDayRecordInDatabase($this->archiveProcessing); + destroy($this->interestByCustomVariables); + destroy($this->interestByCustomVariablesAndValue); + } + + const LABEL_CUSTOM_VALUE_NOT_DEFINED = "Value not defined"; + + /** + * @param Piwik_ArchiveProcessing_Day $archiveProcessing + * @return void + */ + protected function archiveDayAggregate(Piwik_ArchiveProcessing_Day $archiveProcessing) + { + for ($i = 1; $i <= Piwik_Tracker::MAX_CUSTOM_VARIABLES; $i++) { + $keyField = "custom_var_k" . $i; + $valueField = "custom_var_v" . $i; + $dimensions = array($keyField, $valueField); + $where = "%s.$keyField != ''"; + + // Custom Vars names and values metrics for visits + $query = $archiveProcessing->queryVisitsByDimension($dimensions, $where); + + while ($row = $query->fetch()) { + // Handle case custom var value is empty + $row[$valueField] = $this->cleanCustomVarValue($row[$valueField]); + + // Aggregate + if (!isset($this->interestByCustomVariables[$row[$keyField]])) $this->interestByCustomVariables[$row[$keyField]] = $archiveProcessing->getNewInterestRow(); + if (!isset($this->interestByCustomVariablesAndValue[$row[$keyField]][$row[$valueField]])) $this->interestByCustomVariablesAndValue[$row[$keyField]][$row[$valueField]] = $archiveProcessing->getNewInterestRow(); + $archiveProcessing->updateInterestStats($row, $this->interestByCustomVariables[$row[$keyField]]); + $archiveProcessing->updateInterestStats($row, $this->interestByCustomVariablesAndValue[$row[$keyField]][$row[$valueField]]); + } + + // Custom Vars names and values metrics for page views + $query = $archiveProcessing->queryActionsByDimension($dimensions, $where); + $onlyMetricsAvailableInActionsTable = true; + while ($row = $query->fetch()) { + // Handle case custom var value is empty + $row[$valueField] = $this->cleanCustomVarValue($row[$valueField]); + + $label = $row[$valueField]; + + // Remove price tracked if it's zero or we if we are not currently tracking an ecommerce var + if (!in_array($row[$keyField], array('_pks', '_pkn', '_pkc'))) { + unset($row[Piwik_Archive::INDEX_ECOMMERCE_ITEM_PRICE_VIEWED]); + } + + // when custom variable value is a JSON array of categories + // possibly JSON value + $mustInsertCustomVariableValue = true; + if ($row[$keyField] == '_pkc' + && $label[0] == '[' && $label[1] == '"' + ) { + // In case categories were truncated, try closing the array + if (substr($label, -2) != '"]') { + $label .= '"]'; + } + $decoded = @Piwik_Common::json_decode($label); + if (is_array($decoded)) { + $count = 0; + foreach ($decoded as $category) { + if (empty($category) + || $count >= Piwik_Tracker_GoalManager::MAXIMUM_PRODUCT_CATEGORIES + ) { + continue; + } + if (!isset($this->interestByCustomVariablesAndValue[$row[$keyField]][$category])) { + $this->interestByCustomVariablesAndValue[$row[$keyField]][$category] = $archiveProcessing->getNewInterestRow($onlyMetricsAvailableInActionsTable); + } + $archiveProcessing->updateInterestStats($row, $this->interestByCustomVariablesAndValue[$row[$keyField]][$category], $onlyMetricsAvailableInActionsTable); + $mustInsertCustomVariableValue = false; + $count++; + } + } + } // end multi categories hack + + if ($mustInsertCustomVariableValue) { + if (!isset($this->interestByCustomVariablesAndValue[$row[$keyField]][$row[$valueField]])) $this->interestByCustomVariablesAndValue[$row[$keyField]][$row[$valueField]] = $archiveProcessing->getNewInterestRow($onlyMetricsAvailableInActionsTable); + $archiveProcessing->updateInterestStats($row, $this->interestByCustomVariablesAndValue[$row[$keyField]][$row[$valueField]], $onlyMetricsAvailableInActionsTable); + } + + // Do not report on Price viewed for the Custom Variable names + unset($row[Piwik_Archive::INDEX_ECOMMERCE_ITEM_PRICE_VIEWED]); + + // When tracking Custom Variables with scope=page we do not add up visits numbers + // as it is incorrect to sum visits this way + // for scope=visit this is allowed, since there is supposed to be one custom var value per custom variable name for a given visit + $doNotSumVisits = true; + + if (!isset($this->interestByCustomVariables[$row[$keyField]])) $this->interestByCustomVariables[$row[$keyField]] = $archiveProcessing->getNewInterestRow($onlyMetricsAvailableInActionsTable, $doNotSumVisits); + $archiveProcessing->updateInterestStats($row, $this->interestByCustomVariables[$row[$keyField]], $onlyMetricsAvailableInActionsTable, $doNotSumVisits); + } + + // Custom Vars names and values metrics for Goals + $query = $archiveProcessing->queryConversionsByDimension($dimensions, $where); + + if ($query !== false) { + while ($row = $query->fetch()) { + // Handle case custom var value is empty + $row[$valueField] = $this->cleanCustomVarValue($row[$valueField]); + + if (!isset($this->interestByCustomVariables[$row[$keyField]][Piwik_Archive::INDEX_GOALS][$row['idgoal']])) $this->interestByCustomVariables[$row[$keyField]][Piwik_Archive::INDEX_GOALS][$row['idgoal']] = $archiveProcessing->getNewGoalRow($row['idgoal']); + if (!isset($this->interestByCustomVariablesAndValue[$row[$keyField]][$row[$valueField]][Piwik_Archive::INDEX_GOALS][$row['idgoal']])) $this->interestByCustomVariablesAndValue[$row[$keyField]][$row[$valueField]][Piwik_Archive::INDEX_GOALS][$row['idgoal']] = $archiveProcessing->getNewGoalRow($row['idgoal']); + + $archiveProcessing->updateGoalStats($row, $this->interestByCustomVariables[$row[$keyField]][Piwik_Archive::INDEX_GOALS][$row['idgoal']]); + $archiveProcessing->updateGoalStats($row, $this->interestByCustomVariablesAndValue[$row[$keyField]][$row[$valueField]][Piwik_Archive::INDEX_GOALS][$row['idgoal']]); + } + } + } + $archiveProcessing->enrichConversionsByLabelArray($this->interestByCustomVariables); + $archiveProcessing->enrichConversionsByLabelArrayHasTwoLevels($this->interestByCustomVariablesAndValue); + } + + protected function cleanCustomVarValue($value) + { + if (strlen($value)) { + return $value; + } + return self::LABEL_CUSTOM_VALUE_NOT_DEFINED; + } + + /** + * @param Piwik_ArchiveProcessing $archiveProcessing + * @return void + */ + protected function archiveDayRecordInDatabase($archiveProcessing) + { + $recordName = 'CustomVariables_valueByName'; + $table = $archiveProcessing->getDataTableWithSubtablesFromArraysIndexedByLabel($this->interestByCustomVariablesAndValue, $this->interestByCustomVariables); + + $blob = $table->getSerialized( + $this->maximumRowsInDataTableLevelZero, $this->maximumRowsInSubDataTable, + $columnToSort = Piwik_Archive::INDEX_NB_VISITS); + $archiveProcessing->insertBlobRecord($recordName, $blob); + destroy($table); + } + + /** + * @param Piwik_Event_Notification $notification notification object + * @return mixed + */ + function archivePeriod($notification) + { + $archiveProcessing = $notification->getNotificationObject(); + + if (!$archiveProcessing->shouldProcessReportsForPlugin($this->getPluginName())) return; + + $dataTableToSum = 'CustomVariables_valueByName'; + $nameToCount = $archiveProcessing->archiveDataTable( + $dataTableToSum, null, $this->maximumRowsInDataTableLevelZero, $this->maximumRowsInSubDataTable, + $columnToSort = Piwik_Archive::INDEX_NB_VISITS); + } } |