Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve page conversion attribution performance with pre-calculated field #20526

Merged
merged 99 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from 95 commits
Commits
Show all changes
99 commits
Select commit Hold shift + click to select a range
f8e7526
Add immutable visitor property functions to the VisitProperties class…
bx80 Apr 18, 2023
1b4f0c8
Only use immutable properties for the isCurrentReferrerDirectEntry() …
bx80 Apr 20, 2023
c29f521
Fix referrer tests
bx80 Apr 24, 2023
11bc4a4
Migration to create new pageviews_before field on log_conversion, pop…
bx80 Mar 28, 2023
75ff249
Use a ConversionDimension class instead of directly modifying the con…
bx80 Mar 30, 2023
ea9b09f
Added tests
bx80 Apr 12, 2023
dc269e1
Fix missing parameters for ecommerce conversions
bx80 Apr 12, 2023
cbed710
Fix test
bx80 Apr 12, 2023
3e15247
Fix for UI test
bx80 Apr 12, 2023
6120c66
Update UI test screenshots, update privacy manager tests, update dime…
bx80 Apr 12, 2023
2f68418
Fix query
bx80 Apr 14, 2023
5e93e08
Fix archiving query segmentation support, fix update query, adjust ag…
bx80 Apr 14, 2023
9374cc4
Fix for page before metric aggregation
bx80 Apr 14, 2023
895eeec
Remove onCartUpdate calculation, remove unnecessary column creation.
bx80 Apr 17, 2023
9a5849c
Migration to create new pageviews_before field on log_conversion, pop…
bx80 Mar 28, 2023
0ec0030
Use a ConversionDimension class instead of directly modifying the con…
bx80 Mar 30, 2023
228ddf5
Added tests
bx80 Apr 12, 2023
16ecb67
Fix test
bx80 Apr 12, 2023
ecf6093
Fix query
bx80 Apr 14, 2023
79150e0
Fix archiving query segmentation support, fix update query, adjust ag…
bx80 Apr 14, 2023
7bcb55c
Fix for page before metric aggregation
bx80 Apr 14, 2023
4e9c3c7
Remove onCartUpdate calculation, remove unnecessary column creation.
bx80 Apr 17, 2023
2af9d7a
Update system test result
bx80 Apr 17, 2023
7ce5df5
Update to use getImmutableVisitorColumn() method
bx80 Apr 18, 2023
2417335
Migration to create new pageviews_before field on log_conversion, pop…
bx80 Mar 28, 2023
3f857bb
Added tests
bx80 Apr 12, 2023
f88bdf7
Fix for UI test
bx80 Apr 12, 2023
1da557e
Update UI test screenshots, update privacy manager tests, update dime…
bx80 Apr 12, 2023
0c1da9d
Fix query
bx80 Apr 14, 2023
0d9e60f
Fix archiving query segmentation support, fix update query, adjust ag…
bx80 Apr 14, 2023
ad19611
Fix for page before metric aggregation
bx80 Apr 14, 2023
88f767b
Update system test result
bx80 Apr 17, 2023
22015ed
Added last-n option to core:calculate-conversion-pages
bx80 May 8, 2023
60bf223
Update command for vendor proxy changes
bx80 May 8, 2023
4ceb006
Set parameter default value
bx80 May 8, 2023
211fdc7
Revert older immutable property changes, added back column creation f…
bx80 May 8, 2023
44bf242
Update system tests
bx80 May 9, 2023
6867744
Update system test
bx80 May 9, 2023
14cc0d0
Allow ecommerce calcs, use constants for action types, add parameter …
bx80 May 10, 2023
ce81af1
Use constant for ecommerce order goal id, only process if ecommerce e…
bx80 May 14, 2023
9b2c8f9
Merge branch '5.x-dev' into m20375-pages-before-field
sgiehl May 15, 2023
cac2a64
Remove update limit check
bx80 May 15, 2023
2128a6e
Use custom migration to call the CalculateConversionPages command dur…
bx80 May 17, 2023
a8c2d5e
Merge remote-tracking branch 'origin/m20375-pages-before-field' into …
bx80 May 17, 2023
2dd1258
Add immutable visitor property functions to the VisitProperties class…
bx80 Apr 18, 2023
aec9638
Only use immutable properties for the isCurrentReferrerDirectEntry() …
bx80 Apr 20, 2023
b17b80f
Fix referrer tests
bx80 Apr 24, 2023
0441d12
Migration to create new pageviews_before field on log_conversion, pop…
bx80 Mar 28, 2023
653a224
Use a ConversionDimension class instead of directly modifying the con…
bx80 Mar 30, 2023
669dd45
Added tests
bx80 Apr 12, 2023
4f1bb41
Fix missing parameters for ecommerce conversions
bx80 Apr 12, 2023
5f602e3
Fix test
bx80 Apr 12, 2023
a3bc3fa
Fix for UI test
bx80 Apr 12, 2023
2b0085e
Update UI test screenshots, update privacy manager tests, update dime…
bx80 Apr 12, 2023
800e992
Fix query
bx80 Apr 14, 2023
e93d0b2
Fix archiving query segmentation support, fix update query, adjust ag…
bx80 Apr 14, 2023
296fd1e
Fix for page before metric aggregation
bx80 Apr 14, 2023
9f85222
Remove onCartUpdate calculation, remove unnecessary column creation.
bx80 Apr 17, 2023
79a4e12
Migration to create new pageviews_before field on log_conversion, pop…
bx80 Mar 28, 2023
7b703b9
Use a ConversionDimension class instead of directly modifying the con…
bx80 Mar 30, 2023
6bc27f3
Added tests
bx80 Apr 12, 2023
5f85837
Fix test
bx80 Apr 12, 2023
e7f81cf
Fix query
bx80 Apr 14, 2023
1f8bf03
Fix archiving query segmentation support, fix update query, adjust ag…
bx80 Apr 14, 2023
ff9c611
Fix for page before metric aggregation
bx80 Apr 14, 2023
8537bf7
Remove onCartUpdate calculation, remove unnecessary column creation.
bx80 Apr 17, 2023
fd70762
Update system test result
bx80 Apr 17, 2023
8cc24f2
Update to use getImmutableVisitorColumn() method
bx80 Apr 18, 2023
581fe69
Migration to create new pageviews_before field on log_conversion, pop…
bx80 Mar 28, 2023
8bafad6
Added tests
bx80 Apr 12, 2023
c934369
Fix for UI test
bx80 Apr 12, 2023
d44637e
Update UI test screenshots, update privacy manager tests, update dime…
bx80 Apr 12, 2023
818d5bd
Fix query
bx80 Apr 14, 2023
5ab8cda
Fix archiving query segmentation support, fix update query, adjust ag…
bx80 Apr 14, 2023
19531ba
Fix for page before metric aggregation
bx80 Apr 14, 2023
a68ab6d
Update system test result
bx80 Apr 17, 2023
0d553a6
Added last-n option to core:calculate-conversion-pages
bx80 May 8, 2023
fde16be
Update command for vendor proxy changes
bx80 May 8, 2023
0225393
Set parameter default value
bx80 May 8, 2023
09f237e
Revert older immutable property changes, added back column creation f…
bx80 May 8, 2023
d58e895
Update system tests
bx80 May 9, 2023
202b1ab
Update system test
bx80 May 9, 2023
5352401
Allow ecommerce calcs, use constants for action types, add parameter …
bx80 May 10, 2023
0426f2c
Use constant for ecommerce order goal id, only process if ecommerce e…
bx80 May 14, 2023
a6f17e3
Remove update limit check
bx80 May 15, 2023
f5046e4
Use custom migration to call the CalculateConversionPages command dur…
bx80 May 17, 2023
1816227
Merge remote-tracking branch 'origin/m20375-pages-before-field' into …
bx80 May 17, 2023
c625088
Fix PHPCS
bx80 May 17, 2023
470d21f
Merge branch '5.x-dev' into m20375-pages-before-field
sgiehl May 17, 2023
09d1beb
Apply site timezone to update command date parameters
bx80 May 18, 2023
36ddfe8
Merge remote-tracking branch 'origin/m20375-pages-before-field' into …
bx80 May 18, 2023
fa6b247
Merge branch '5.x-dev' into m20375-pages-before-field
sgiehl May 19, 2023
89bbf06
Update core/Updates/5.0.0-b1.php
bx80 May 20, 2023
5d76518
Rework custom migration to return migration SQL object, fix segfault
bx80 May 22, 2023
67eb1df
Directly query active goal ids for sites
bx80 May 22, 2023
1c6282a
Update plugins/Goals/Commands/CalculateConversionPages.php
bx80 May 23, 2023
6561cd9
Add calculate conversion pages command tests, tweak update query to c…
bx80 May 24, 2023
0b40f35
Update query optimizations
bx80 May 24, 2023
51eaacd
remove duplicate test group
sgiehl May 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 28 additions & 65 deletions core/DataAccess/LogAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Piwik\Plugin\LogTablesProvider;
use Piwik\RankingQuery;
use Piwik\Segment;
use Piwik\Tracker\Action;
use Piwik\Tracker\GoalManager;
use Piwik\Log\LoggerInterface;

Expand Down Expand Up @@ -1101,80 +1102,42 @@ public function queryConversionsByDimension($dimensions = array(), $where = fals
public function queryConversionsByPageView(string $linkField, int $idGoal)
{

$generalBind = $this->getGeneralQueryBindParams();

$select = 'log_conversion.idvisit, COUNT(*) AS num_total';
$select = "
log_conversion.idvisit AS idvisit,
" . $idGoal . " AS idgoal,
" . ($linkField == 'idaction_url' ? Action::TYPE_PAGE_URL : Action::TYPE_PAGE_TITLE) . " AS `type`,
lac.idaction AS idaction,
COUNT(*) AS `1`,
" . sprintf("ROUND(SUM(log_conversion.revenue),2) AS `%d`,", Metrics::INDEX_GOAL_REVENUE) . "
" . sprintf("COUNT(log_conversion.idvisit) AS `%d`,", Metrics::INDEX_GOAL_NB_VISITS_CONVERTED) . "
" . sprintf("ROUND(SUM(1 / log_conversion.pageviews_before * log_conversion.revenue_subtotal),2) AS `%d`,", Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SUBTOTAL) . "
" . sprintf("ROUND(SUM(1 / log_conversion.pageviews_before * log_conversion.revenue_tax),2) AS `%d`,", Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_TAX) . "
" . sprintf("ROUND(SUM(1 / log_conversion.pageviews_before * log_conversion.revenue_shipping),2) AS `%d`,", Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SHIPPING) . "
" . sprintf("ROUND(SUM(1 / log_conversion.pageviews_before * log_conversion.revenue_discount),2) AS `%d`,", Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_DISCOUNT) . "
" . sprintf("SUM(1 / log_conversion.pageviews_before * log_conversion.items) AS `%d`,", Metrics::INDEX_GOAL_ECOMMERCE_ITEMS) . "
" . sprintf("log_conversion.pageviews_before AS `%d`,", Metrics::INDEX_GOAL_NB_PAGES_UNIQ_BEFORE) . "
" . sprintf("SUM(1 / log_conversion.pageviews_before) AS `%d`,", Metrics::INDEX_GOAL_NB_CONVERSIONS_ATTRIB) . "
" . sprintf("COUNT(*) AS `%d`,", Metrics::INDEX_GOAL_NB_CONVERSIONS_PAGE_UNIQ) . "
" . sprintf("ROUND(SUM(1 / log_conversion.pageviews_before * log_conversion.revenue),2) AS `%d`", Metrics::INDEX_GOAL_REVENUE_ATTRIB);

$from = [
'log_conversion',
['table' => 'log_link_visit_action', 'tableAlias' => 'log_vpast', 'join' => 'RIGHT JOIN',
'joinOn' => 'log_conversion.idvisit = log_vpast.idvisit'],
['table' => 'log_action', 'tableAlias' => 'lac_past',
'joinOn' => 'log_vpast.'.$linkField.' = lac_past.idaction']
['table' => 'log_link_visit_action', 'tableAlias' => 'logva', 'join' => 'RIGHT JOIN',
'joinOn' => 'log_conversion.idvisit = logva.idvisit'],
['table' => 'log_action', 'tableAlias' => 'lac',
'joinOn' => 'logva.'.$linkField.' = lac.idaction']
];

$where = $this->getWhereStatement('log_conversion', 'server_time');
$where .= sprintf('AND log_conversion.idgoal = %d
AND log_vpast.server_time <= log_conversion.server_time
AND lac_past.type = %s',
(int) $idGoal, ($linkField == 'idaction_url' ? '1' : '4'));
$where .= sprintf('AND log_conversion.idgoal = %d
AND logva.server_time <= log_conversion.server_time
AND lac.type = %s',
(int) $idGoal, ($linkField == 'idaction_url' ? Action::TYPE_PAGE_URL : Action::TYPE_PAGE_TITLE));

$groupBy = 'log_conversion.idvisit';
$groupBy = 'log_conversion.idvisit, lac.idaction';

$query = $this->generateQuery($select, $from, $where, $groupBy, false);

$dbSettings = new \Piwik\Db\Settings();
$tablePrefix = $dbSettings->getTablePrefix();

$sql = sprintf(
"SELECT
yyy.idvisit AS idvisit,
".$idGoal." AS idgoal,
".($linkField == 'idaction_url' ? '1' : '4')." AS `type`,
yyy.idaction AS idaction,
COUNT(*) AS `1`,
" . sprintf("ROUND(SUM(yyy.revenue_total),2) AS `%d`,", Metrics::INDEX_GOAL_REVENUE) . "
" . sprintf("COUNT(yyy.idvisit) AS `%d`,", Metrics::INDEX_GOAL_NB_VISITS_CONVERTED) . "
" . sprintf("ROUND(SUM(yyy.revenue_subtotal),2) AS `%d`,", Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SUBTOTAL) . "
" . sprintf("ROUND(SUM(yyy.revenue_tax),2) AS `%d`,", Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_TAX) . "
" . sprintf("ROUND(SUM(yyy.revenue_shipping),2) AS `%d`,", Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SHIPPING) . "
" . sprintf("ROUND(SUM(yyy.revenue_discount),2) AS `%d`,", Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_DISCOUNT) . "
" . sprintf("SUM(yyy.items) AS `%d`,", Metrics::INDEX_GOAL_ECOMMERCE_ITEMS) . "
" . sprintf("yyy.pages_before AS `%d`,", Metrics::INDEX_GOAL_NB_PAGES_UNIQ_BEFORE) . "
" . sprintf("SUM(yyy.attribution) AS `%d`,", Metrics::INDEX_GOAL_NB_CONVERSIONS_ATTRIB) . "
" . sprintf("COUNT(*) AS `%d`,", Metrics::INDEX_GOAL_NB_CONVERSIONS_PAGE_UNIQ) . "
" . sprintf("ROUND(SUM(yyy.revenue),2) AS `%d`", Metrics::INDEX_GOAL_REVENUE_ATTRIB) . "
FROM (
SELECT
num_total AS pages_before,
1 / num_total AS attribution,
r.idvisit AS idvisit,
lac.idaction AS idaction,
lvcon.revenue AS revenue_total,
1 / num_total * lvcon.revenue AS revenue,
1 / num_total * lvcon.revenue_subtotal AS revenue_subtotal,
1 / num_total * lvcon.revenue_tax AS revenue_tax,
1 / num_total * lvcon.revenue_shipping AS revenue_shipping,
1 / num_total * lvcon.revenue_discount AS revenue_discount,
1 / num_total * lvcon.items AS items
FROM (
%s
) AS r
LEFT JOIN ".Common::prefixTable('log_conversion')." lvcon ON lvcon.idgoal = ".$idGoal." AND lvcon.idvisit = r.idvisit
RIGHT JOIN ".Common::prefixTable('log_link_visit_action')." logv ON logv.idvisit = r.idvisit
LEFT JOIN ".Common::prefixTable('log_action')." lac ON logv.".$linkField." = lac.idaction
WHERE logv.server_time >= '%s'
AND logv.server_time <= '%s'
AND logv.idsite IN (%d)
AND lac.type = ".($linkField == 'idaction_url' ? '1' : '4')."
AND logv.server_time <= lvcon.server_time
) AS yyy
GROUP BY yyy.idaction
ORDER BY `9` DESC", $query['sql'], $generalBind[0], $generalBind[1], $generalBind[2]
);

return $this->getDb()->query($sql, $query['bind']);

return $this->getDb()->query($query['sql'], $query['bind']);
}

/**
Expand Down
1 change: 1 addition & 0 deletions core/Tracker/GoalManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,7 @@ private function makeRandomMySqlUnsignedInt($length)
* @param array $visitInformation
* @param Request $request
* @param Action|null $action
* @param int|null $convertedGoal
* @return bool
*/
protected function insertNewConversion($conversion, $visitInformation, Request $request, $action, $convertedGoal = null)
Expand Down
Loading