Skip to content

Commit 860026e

Browse files
diosmosissgiehl
andauthored
add actions goal metrics to processed reports in metadata and report data (matomo-org#20817)
* remove code that excludes goal metrics from appearing in Actions processed API output * set appropriate idGoal for AddColumnsProcessedMetricsGoal in processed reports for actions * remove goal specific revenue metric in actions processed report if it is a page report * move processOnlyIdGoal overriding logic to common method in AddColumnsProcessedMetricsGoal * make sure correct metrics display in report metadata for specific Actions reports * update expected test files and document a method * fix displayType setting condition --------- Co-authored-by: Stefan Giehl <[email protected]>
1 parent a3e05ba commit 860026e

21 files changed

Lines changed: 722 additions & 24 deletions

core/DataTable/Filter/AddColumnsProcessedMetricsGoal.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ class AddColumnsProcessedMetricsGoal extends AddColumnsProcessedMetrics
101101
*/
102102
const GOALS_FULL_TABLE = 0;
103103

104+
const ACTIONS_PAGE_REPORTS_WITH_GOAL_METRICS = ['Actions.getPageUrls', 'Actions.getPageTitles'];
105+
106+
const ACTIONS_ENTRY_PAGE_REPORTS_WITH_GOAL_METRICS = ['Actions.getEntryPageUrls', 'Actions.getEntryPageTitles'];
107+
104108
/**
105109
* @var string
106110
*/
@@ -218,4 +222,32 @@ private function getGoalsInTable(DataTable $table)
218222
}
219223
return array_unique($result);
220224
}
225+
226+
/**
227+
* Returns an idGoal override to use for the processOnlyIdGoal parameter of this filter if $requestMethod
228+
* is for a Actions page report or an Actions entry page report.
229+
*
230+
* @param int|string|null $idGoal if set to ecommerceOrder or ecommerceAbandonedCart, returns a processOnlyIdGoal value
231+
* that will result in extra ecommerce metrics being computed
232+
* @return int|string|null
233+
*/
234+
public static function getProcessOnlyIdGoalToUseForReport($idGoal, string $requestMethod)
235+
{
236+
// Check if one of the pages display types should be used
237+
if (in_array($requestMethod, self::ACTIONS_PAGE_REPORTS_WITH_GOAL_METRICS)) {
238+
if ($idGoal === Piwik::LABEL_ID_GOAL_IS_ECOMMERCE_ORDER || $idGoal === Piwik::LABEL_ID_GOAL_IS_ECOMMERCE_CART) {
239+
return self::GOALS_PAGES_ECOMMERCE;
240+
} else {
241+
return self::GOALS_PAGES;
242+
}
243+
} elseif (in_array($requestMethod, self::ACTIONS_ENTRY_PAGE_REPORTS_WITH_GOAL_METRICS)) {
244+
if ($idGoal === Piwik::LABEL_ID_GOAL_IS_ECOMMERCE_ORDER || $idGoal === Piwik::LABEL_ID_GOAL_IS_ECOMMERCE_CART) {
245+
return self::GOALS_ENTRY_PAGES_ECOMMERCE;
246+
} else {
247+
return self::GOALS_ENTRY_PAGES;
248+
}
249+
}
250+
251+
return $idGoal;
252+
}
221253
}

plugins/API/ProcessedReport.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Piwik\Common;
1717
use Piwik\Container\StaticContainer;
1818
use Piwik\DataTable;
19+
use Piwik\DataTable\Filter\AddColumnsProcessedMetricsGoal;
1920
use Piwik\DataTable\Row;
2021
use Piwik\DataTable\Simple;
2122
use Piwik\Date;
@@ -369,6 +370,11 @@ public function getProcessedReport($idSite, $period, $date, $apiModule, $apiActi
369370
$parameters['segment'] = $segment;
370371
}
371372

373+
$actionsIdGoalOverride = $this->getIdGoalToUseForActionsReports($idGoal,$apiModule . '.' . $apiAction);
374+
if ($actionsIdGoalOverride) {
375+
$parameters['idGoal'] = $actionsIdGoalOverride;
376+
}
377+
372378
if (!empty($reportMetadata['processedMetrics'])
373379
&& !empty($reportMetadata['metrics']['nb_visits'])
374380
&& @$reportMetadata['category'] != Piwik::translate('Goals_Ecommerce')
@@ -895,4 +901,12 @@ private function getComparisonColumns(array $metadataColumns)
895901
}
896902
return $result;
897903
}
904+
905+
private function getIdGoalToUseForActionsReports($idGoal, string $requestMethod)
906+
{
907+
if (\Piwik\Request::fromRequest()->getStringParameter('filter_show_goal_columns_process_goals', '')) {
908+
return AddColumnsProcessedMetricsGoal::getProcessOnlyIdGoalToUseForReport($idGoal, $requestMethod);
909+
}
910+
return $idGoal;
911+
}
898912
}

plugins/Goals/Columns/Metrics/GoalSpecific/ConversionsEntry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function getName()
2929

3030
public function getTranslatedName()
3131
{
32-
return Piwik::translate('Goals_Conversions', $this->getGoalNameForDocs());
32+
return Piwik::translate('Goals_Conversions', $this->getGoalName());
3333
}
3434

3535
public function getDocumentation()

plugins/Goals/Goals.php

Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Piwik\Columns\Dimension;
1414
use Piwik\Columns\MetricsList;
1515
use Piwik\Common;
16+
use Piwik\DataTable\Filter\AddColumnsProcessedMetricsGoal;
1617
use Piwik\Piwik;
1718
use Piwik\Plugin\ArchivedMetric;
1819
use Piwik\Plugin\ComputedMetric;
@@ -295,24 +296,92 @@ public function getReportMetadataEnd(&$reports, $info)
295296
'revenue' => Dimension::TYPE_MONEY,
296297
];
297298

299+
// special goal metrics for Actions page reports
300+
$pageGoalMetrics = array_merge($goalMetrics, [
301+
'nb_conversions_attrib' => Piwik::translate('Goals_ColumnConversions'),
302+
'revenue_attrib' => Piwik::translate('General_ColumnRevenue'),
303+
]);
304+
unset($pageGoalMetrics['revenue']);
305+
306+
$pageGoalProcessedMetrics = array_merge($goalProcessedMetrics, [
307+
'nb_conversions_page_rate' => Piwik::translate('Goals_ConversionRatePageViewedBeforeGeneric'),
308+
]);
309+
310+
$pageGoalMetricTypes = array_merge($goalMetricTypes, [
311+
'nb_conversions_attrib' => Dimension::TYPE_NUMBER,
312+
'revenue_attrib' => Dimension::TYPE_MONEY,
313+
'nb_conversions_page_rate' => Dimension::TYPE_PERCENT,
314+
]);
315+
unset($pageGoalMetricTypes['revenue']);
316+
317+
// special goal metrics for Actions entry page reports
318+
$entryPageGoalMetrics = array_merge($goalMetrics, [
319+
'nb_conversions_entry' => Piwik::translate('Goals_ColumnConversions'),
320+
'revenue_entry' => Piwik::translate('General_ColumnRevenue'),
321+
]);
322+
unset($entryPageGoalMetrics['revenue']);
323+
324+
$entryPageGoalProcessedMetrics = array_merge($goalProcessedMetrics, [
325+
'revenue_per_entry' => Piwik::translate('General_ColumnValuePerEntry'),
326+
'nb_conversions_entry_rate' => Piwik::translate('General_ColumnConversionRate'),
327+
]);
328+
329+
$entryPageGoalMetricTypes = array_merge($goalMetricTypes, [
330+
'nb_conversions_entry' => Dimension::TYPE_NUMBER,
331+
'revenue_entry' => Dimension::TYPE_MONEY,
332+
'revenue_per_entry' => Dimension::TYPE_NUMBER,
333+
'nb_conversions_entry_rate' => Dimension::TYPE_PERCENT,
334+
]);
335+
unset($entryPageGoalMetricTypes['revenue']);
336+
337+
// add ecommerce metrics if idGoal is an ecommerce goal
338+
$idGoal = \Piwik\Request::fromRequest()->getParameter('idGoal', '');
339+
if ($idGoal === Piwik::LABEL_ID_GOAL_IS_ECOMMERCE_ORDER || $idGoal === Piwik::LABEL_ID_GOAL_IS_ECOMMERCE_CART) {
340+
$extraEcommerceProcessedMetrics = [
341+
'avg_order_revenue' => Piwik::translate('General_AverageOrderValue'),
342+
'items' => Piwik::translate('General_PurchasedProducts'),
343+
];
344+
345+
$extraEcommerceMetricTypes = [
346+
'avg_order_revenue' => Dimension::TYPE_MONEY,
347+
'itmes' => Dimension::TYPE_NUMBER,
348+
];
349+
350+
$pageGoalProcessedMetrics = array_merge($pageGoalProcessedMetrics, $extraEcommerceProcessedMetrics);
351+
$entryPageGoalProcessedMetrics = array_merge($entryPageGoalProcessedMetrics, $extraEcommerceProcessedMetrics);
352+
353+
$pageGoalMetricTypes = array_merge($pageGoalMetricTypes, $extraEcommerceMetricTypes);
354+
$entryPageGoalMetricTypes = array_merge($entryPageGoalMetricTypes, $extraEcommerceMetricTypes);
355+
}
356+
298357
$reportsWithGoals = self::getAllReportsWithGoalMetrics();
299358

300359
foreach ($reportsWithGoals as $reportWithGoals) {
360+
$goalMetricsToUse = $goalMetrics;
361+
$goalProcessedMetricsToUse = $goalProcessedMetrics;
362+
$goalMetricTypesToUse = $goalMetricTypes;
363+
364+
$request = $reportWithGoals['module'] . '.' . $reportWithGoals['action'];
365+
if (in_array($request, AddColumnsProcessedMetricsGoal::ACTIONS_PAGE_REPORTS_WITH_GOAL_METRICS)) {
366+
$goalMetricsToUse = $pageGoalMetrics;
367+
$goalProcessedMetricsToUse = $pageGoalProcessedMetrics;
368+
$goalMetricTypesToUse = $pageGoalMetricTypes;
369+
} else if (in_array($request, AddColumnsProcessedMetricsGoal::ACTIONS_ENTRY_PAGE_REPORTS_WITH_GOAL_METRICS)) {
370+
$goalMetricsToUse = $entryPageGoalMetrics;
371+
$goalProcessedMetricsToUse = $entryPageGoalProcessedMetrics;
372+
$goalMetricTypesToUse = $entryPageGoalMetricTypes;
373+
}
374+
301375
// Select this report from the API metadata array
302376
// and add the Goal metrics to it
303377
foreach ($reports as &$apiReportToUpdate) {
304-
// We do not add anything for Action reports, as no overall metrics are processed there at the moment
305-
if ($apiReportToUpdate['module'] === 'Actions') {
306-
continue;
307-
}
308-
309378
if ($apiReportToUpdate['module'] == $reportWithGoals['module']
310379
&& $apiReportToUpdate['action'] == $reportWithGoals['action']
311380
&& empty($apiReportToUpdate['parameters'])
312381
) {
313-
$apiReportToUpdate['metricsGoal'] = $goalMetrics;
314-
$apiReportToUpdate['processedMetricsGoal'] = $goalProcessedMetrics;
315-
$apiReportToUpdate['metricTypesGoal'] = $goalMetricTypes;
382+
$apiReportToUpdate['metricsGoal'] = $goalMetricsToUse;
383+
$apiReportToUpdate['processedMetricsGoal'] = $goalProcessedMetricsToUse;
384+
$apiReportToUpdate['metricTypesGoal'] = $goalMetricTypesToUse;
316385
break;
317386
}
318387
}

plugins/Goals/Visualizations/Goals.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,17 @@ public function beforeLoadDataTable()
3939
$request = $this->getRequestArray();
4040
$idGoal = $request['idGoal'] ?? null;
4141

42-
// Check if one of the pages display types should be used
4342
$requestMethod = $this->requestConfig->getApiModuleToRequest() . '.' . $this->requestConfig->getApiMethodToRequest();
44-
if (in_array($requestMethod, ['Actions.getPageUrls', 'Actions.getPageTitles'])) {
45-
$this->displayType = self::GOALS_DISPLAY_PAGES;
46-
$this->config->filters[] = ['Piwik\Plugins\Goals\DataTable\Filter\RemoveUnusedGoalRevenueColumns'];
47-
if ($idGoal === Piwik::LABEL_ID_GOAL_IS_ECOMMERCE_ORDER || $idGoal === Piwik::LABEL_ID_GOAL_IS_ECOMMERCE_CART) {
48-
$this->requestConfig->request_parameters_to_modify['idGoal'] = AddColumnsProcessedMetricsGoal::GOALS_ENTRY_PAGES_ECOMMERCE;
49-
} else {
50-
$this->requestConfig->request_parameters_to_modify['idGoal'] = AddColumnsProcessedMetricsGoal::GOALS_PAGES;
51-
}
52-
} elseif (in_array($requestMethod, ['Actions.getEntryPageUrls', 'Actions.getEntryPageTitles'])) {
53-
$this->displayType = self::GOALS_DISPLAY_ENTRY_PAGES;
43+
$idGoal = AddColumnsProcessedMetricsGoal::getProcessOnlyIdGoalToUseForReport($idGoal, $requestMethod);
44+
45+
if (!empty($idGoal)) {
5446
$this->config->filters[] = ['Piwik\Plugins\Goals\DataTable\Filter\RemoveUnusedGoalRevenueColumns'];
55-
if ($idGoal === Piwik::LABEL_ID_GOAL_IS_ECOMMERCE_ORDER || $idGoal === Piwik::LABEL_ID_GOAL_IS_ECOMMERCE_CART) {
56-
$this->requestConfig->request_parameters_to_modify['idGoal'] = AddColumnsProcessedMetricsGoal::GOALS_ENTRY_PAGES_ECOMMERCE;
57-
} else {
58-
$this->requestConfig->request_parameters_to_modify['idGoal'] = AddColumnsProcessedMetricsGoal::GOALS_ENTRY_PAGES;
47+
$this->requestConfig->request_parameters_to_modify['idGoal'] = $idGoal;
48+
49+
if ($idGoal == AddColumnsProcessedMetricsGoal::GOALS_PAGES || $idGoal == AddColumnsProcessedMetricsGoal::GOALS_PAGES_ECOMMERCE) {
50+
$this->displayType = self::GOALS_DISPLAY_PAGES;
51+
} else if ($idGoal == AddColumnsProcessedMetricsGoal::GOALS_ENTRY_PAGES || $idGoal == AddColumnsProcessedMetricsGoal::GOALS_ENTRY_PAGES_ECOMMERCE) {
52+
$this->displayType = self::GOALS_DISPLAY_ENTRY_PAGES;
5953
}
6054
}
6155

plugins/Goals/lang/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"ConversionByTypeReportDocumentation": "This report provides detailed information about the goal performance (conversions, conversion rates and revenue per visit) for each of the categories available in the left panel. %1$s Please click on one of the categories to view the report. %2$s For more information, read the %3$sTracking Goals documentation%4$s",
4242
"ConversionRate": "%s conversion rate",
4343
"ConversionRatePageViewedBefore": "Viewed before %s rate",
44+
"ConversionRatePageViewedBeforeGeneric": "Viewed before conversion rate",
4445
"Conversions": "%s conversions",
4546
"ConversionsDescription": "conversions",
4647
"ConversionsOverview": "Conversions Overview",

plugins/Goals/tests/System/TrackGoalsPagesTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
*/
2525
class TrackGoalsPagesTest extends SystemTestCase
2626
{
27+
/**
28+
* @var SomePageGoalVisitsWithConversions
29+
*/
2730
public static $fixture = null;
2831

2932
/**
@@ -71,6 +74,19 @@ public function getApiForTesting()
7174
'apiAction' => 'getEntryPageTitles',
7275
],
7376
]],
77+
78+
['API.getProcessedReport', [
79+
'idSite' => self::$fixture->idSite,
80+
'date' => self::$fixture->dateTime,
81+
'period' => 'day',
82+
'testSuffix' => 'showGoalsMetricsPageReport',
83+
'otherRequestParameters' => [
84+
'filter_update_columns_when_show_all_goals' => '1',
85+
'filter_show_goal_columns_process_goals' => '1',
86+
'apiModule' => 'Actions',
87+
'apiAction' => 'getPageTitles',
88+
],
89+
]],
7490
];
7591
}
7692

plugins/Goals/tests/System/expected/test_trackGoals_pagesshowGoalsMetricsAllGoals__API.getProcessedReport_day.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,26 @@
4444
<action>getEntryPageUrls</action>
4545
</row>
4646
</relatedReports>
47+
<metricsGoal>
48+
<nb_conversions>Conversions</nb_conversions>
49+
<conversion_rate>Conversion Rate</conversion_rate>
50+
<nb_conversions_entry>Conversions</nb_conversions_entry>
51+
<revenue_entry>Revenue</revenue_entry>
52+
</metricsGoal>
53+
<processedMetricsGoal>
54+
<revenue_per_visit>Revenue per Visit</revenue_per_visit>
55+
<revenue_per_entry>Revenue per Entry</revenue_per_entry>
56+
<nb_conversions_entry_rate>Conversion Rate</nb_conversions_entry_rate>
57+
</processedMetricsGoal>
58+
<metricTypesGoal>
59+
<revenue_per_visit>money</revenue_per_visit>
60+
<nb_conversions>number</nb_conversions>
61+
<conversion_rate>percent</conversion_rate>
62+
<nb_conversions_entry>number</nb_conversions_entry>
63+
<revenue_entry>money</revenue_entry>
64+
<revenue_per_entry>number</revenue_per_entry>
65+
<nb_conversions_entry_rate>percent</nb_conversions_entry_rate>
66+
</metricTypesGoal>
4767
<imageGraphUrl>index.php?module=API&amp;method=ImageGraph.get&amp;idSite=1&amp;apiModule=Actions&amp;apiAction=getEntryPageTitles&amp;period=day&amp;date=2009-01-05</imageGraphUrl>
4868
<imageGraphEvolutionUrl>index.php?module=API&amp;method=ImageGraph.get&amp;idSite=1&amp;apiModule=Actions&amp;apiAction=getEntryPageTitles&amp;period=day&amp;date=2008-12-07,2009-01-05</imageGraphEvolutionUrl>
4969
<uniqueId>Actions_getEntryPageTitles</uniqueId>

0 commit comments

Comments
 (0)