/** * @param Piwik_ArchiveProcessing_Day $archiveProcessing */ function archiveGeneralGoalMetrics($archiveProcessing) { // extra aggregate selects for the visits to conversion report $visitToConvExtraCols = Piwik_ArchiveProcessing_Day::buildReduceByRangeSelect('visitor_count_visits', self::$visitCountRanges, 'log_conversion', 'vcv'); // extra aggregate selects for the days to conversion report $daysToConvExtraCols = Piwik_ArchiveProcessing_Day::buildReduceByRangeSelect('visitor_days_since_first', self::$daysToConvRanges, 'log_conversion', 'vdsf'); $query = $archiveProcessing->queryConversionsByDimension(array(), '', array_merge($visitToConvExtraCols, $daysToConvExtraCols)); if ($query === false) { return; } $goals = array(); $visitsToConvReport = array(); $daysToConvReport = array(); // Get a standard empty goal row $overall = $archiveProcessing->getNewGoalRow($idGoal = 1); while ($row = $query->fetch()) { $idgoal = $row['idgoal']; if (!isset($goals[$idgoal])) { $goals[$idgoal] = $archiveProcessing->getNewGoalRow($idgoal); $visitsToConvReport[$idgoal] = new Piwik_DataTable(); $daysToConvReport[$idgoal] = new Piwik_DataTable(); } $archiveProcessing->updateGoalStats($row, $goals[$idgoal]); // We don't want to sum Abandoned cart metrics in the overall revenue/conversions/converted visits // since it is a "negative conversion" if ($idgoal != Piwik_Tracker_GoalManager::IDGOAL_CART) { $archiveProcessing->updateGoalStats($row, $overall); } // map the goal + visit number of a visitor with the # of conversions that happened on that visit $table = $archiveProcessing->getSimpleDataTableFromRow($row, Piwik_Archive::INDEX_NB_CONVERSIONS, 'vcv'); $visitsToConvReport[$idgoal]->addDataTable($table); // map the goal + day number of a visit with the # of conversion that happened on that day $table = $archiveProcessing->getSimpleDataTableFromRow($row, Piwik_Archive::INDEX_NB_CONVERSIONS, 'vdsf'); $daysToConvReport[$idgoal]->addDataTable($table); } // these data tables hold reports for every goal of a site $visitsToConvOverview = new Piwik_DataTable(); $daysToConvOverview = new Piwik_DataTable(); // Stats by goal, for all visitors foreach ($goals as $idgoal => $values) { foreach ($values as $metricId => $value) { $metricName = Piwik_Archive::$mappingFromIdToNameGoal[$metricId]; $recordName = self::getRecordName($metricName, $idgoal); $archiveProcessing->insertNumericRecord($recordName, $value); } $conversion_rate = $this->getConversionRate($values[Piwik_Archive::INDEX_GOAL_NB_VISITS_CONVERTED], $archiveProcessing); $recordName = self::getRecordName('conversion_rate', $idgoal); $archiveProcessing->insertNumericRecord($recordName, $conversion_rate); // if the goal is not a special goal (like ecommerce) add it to the overview report if ($idgoal !== Piwik_Tracker_GoalManager::IDGOAL_CART && $idgoal !== Piwik_Tracker_GoalManager::IDGOAL_ORDER) { $visitsToConvOverview->addDataTable($visitsToConvReport[$idgoal]); $daysToConvOverview->addDataTable($daysToConvReport[$idgoal]); } // visit count until conversion stats $archiveProcessing->insertBlobRecord(self::getRecordName(self::VISITS_UNTIL_RECORD_NAME, $idgoal), $visitsToConvReport[$idgoal]->getSerialized()); // day count until conversion stats $archiveProcessing->insertBlobRecord(self::getRecordName(self::DAYS_UNTIL_CONV_RECORD_NAME, $idgoal), $daysToConvReport[$idgoal]->getSerialized()); } // archive overview reports $archiveProcessing->insertBlobRecord(self::getRecordName(self::VISITS_UNTIL_RECORD_NAME), $visitsToConvOverview->getSerialized()); $archiveProcessing->insertBlobRecord(self::getRecordName(self::DAYS_UNTIL_CONV_RECORD_NAME), $daysToConvOverview->getSerialized()); // Stats for all goals $totalAllGoals = array(self::getRecordName('conversion_rate') => $this->getConversionRate($archiveProcessing->getNumberOfVisitsConverted(), $archiveProcessing), self::getRecordName('nb_conversions') => $overall[Piwik_Archive::INDEX_GOAL_NB_CONVERSIONS], self::getRecordName('nb_visits_converted') => $archiveProcessing->getNumberOfVisitsConverted(), self::getRecordName('revenue') => $overall[Piwik_Archive::INDEX_GOAL_REVENUE]); foreach ($totalAllGoals as $recordName => $value) { $archiveProcessing->insertNumericRecord($recordName, $value); } }