diff options
author | diosmosis <diosmosis@users.noreply.github.com> | 2020-06-26 16:17:54 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-26 16:17:54 +0300 |
commit | de5ae85e7a11f7bbd962256c890a32188c40d000 (patch) | |
tree | 2a5f303c83b6e80369d3ba77b2d9fea797c598d9 /plugins/VisitorInterest | |
parent | b12946909ff323ea2f7e7af04e4435d2dd503023 (diff) |
Track seconds instead of days for "days since" dimensions (#15774)
* Modify dimensions and add tests.
* unfinished debugging commit
* changes to fix test, remove tracker code, add update code
* fix some tests
* fix migration issue
* fixing some tests
* fix some bugs, update some expected test files, remove _idvc from some tests
* start replacing _ects
* Finish seconds since last ecommerce order change.
* fix couple issues
* fix a couple tests
* fix some more tests
* fixing a couple more tests
* apply review feedback
* unfinished commit
* add back dimensions =
* fixing tests & removing some code
* fix some bugs + tests and remove debugging code
* fixing some tests and issues
* Some debugging code for the tracker in case of exceptions.
* debug and fix another issue when tracking visits in the past
* fix test
* update JS and fix several tests
* Fixing more tests.
* Add missing files.
* update expected test files
* Add some unit tests.
* update more expected files
* update more test files
* update more test files
* update changelog and remove _ects query param
* apply review feedback
* fix merge issue
* clearer code
* fix migration issue
* update expected files
* update expected files again
* updating tests
* fix test
* rebuilt piwik.js
* unify previous visitor properties / original visit row approach
* fixing some tests
* Add new column version entries into the option table.
* try undoing 1.5 update change
* apply review feedback
* remove more unused cookie values
* rebuilt piwik.js
* Fix test count.
* update test files
* rebuilt piwik.js
* removel log analytics submodule update
* update submodule
* update some expected screenshots
* Fix versions in omnifixture since we were probably adding to the beta update after it changed.
* update screenshots
Diffstat (limited to 'plugins/VisitorInterest')
7 files changed, 169 insertions, 45 deletions
diff --git a/plugins/VisitorInterest/Archiver.php b/plugins/VisitorInterest/Archiver.php index c59fcfe2fb..4280a4a8ba 100644 --- a/plugins/VisitorInterest/Archiver.php +++ b/plugins/VisitorInterest/Archiver.php @@ -106,8 +106,9 @@ class Archiver extends \Piwik\Plugin\Archiver $selects = array_merge($selects, LogAggregator::getSelectsFromRangedColumn( 'visitor_count_visits', self::$visitNumberGap, 'log_visit', $prefixes[self::VISITS_COUNT_RECORD_NAME] )); + $selects = array_merge($selects, LogAggregator::getSelectsFromRangedColumn( - 'visitor_days_since_last', self::$daysSinceLastVisitGap, 'log_visit', $prefixes[self::DAYS_SINCE_LAST_RECORD_NAME], + 'FLOOR(log_visit.visitor_seconds_since_last / 86400)', self::$daysSinceLastVisitGap, 'log_visit', $prefixes[self::DAYS_SINCE_LAST_RECORD_NAME], $restrictToReturningVisitors = true )); diff --git a/plugins/VisitorInterest/Columns/VisitorDaysSinceLast.php b/plugins/VisitorInterest/Columns/VisitorDaysSinceLast.php new file mode 100644 index 0000000000..e1f886dd8a --- /dev/null +++ b/plugins/VisitorInterest/Columns/VisitorDaysSinceLast.php @@ -0,0 +1,23 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link https://matomo.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + */ + +namespace Piwik\Plugins\VisitorInterest\Columns; + +use Piwik\Plugin\Dimension\VisitDimension; +use Piwik\Plugin\Segment; + +class VisitorDaysSinceLast extends VisitDimension +{ + protected $category = 'General_Visitors'; + protected $type = self::TYPE_NUMBER; + protected $nameSingular = 'General_DaysSinceLastVisit'; + protected $columnName = 'visitor_seconds_since_last'; + protected $sqlSegment = 'FLOOR(log_visit.visitor_seconds_since_last / 86400)'; + protected $segmentName = 'daysSinceLastVisit'; +}
\ No newline at end of file diff --git a/plugins/VisitorInterest/Columns/VisitorSecondsSinceLast.php b/plugins/VisitorInterest/Columns/VisitorSecondsSinceLast.php new file mode 100644 index 0000000000..ffc9787f7a --- /dev/null +++ b/plugins/VisitorInterest/Columns/VisitorSecondsSinceLast.php @@ -0,0 +1,68 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link https://matomo.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + */ +namespace Piwik\Plugins\VisitorInterest\Columns; + +use Piwik\Common; +use Piwik\Date; +use Piwik\Piwik; +use Piwik\Plugin\Dimension\VisitDimension; +use Piwik\Plugin\Segment; +use Piwik\Tracker\Action; +use Piwik\Tracker\Request; +use Piwik\Tracker\Visitor; + +class VisitorSecondsSinceLast extends VisitDimension +{ + const COLUMN_TYPE = 'INT(11) UNSIGNED NULL'; + + protected $columnName = 'visitor_seconds_since_last'; + protected $columnType = self::COLUMN_TYPE; + protected $type = self::TYPE_NUMBER; + protected $segmentName = 'secondsSinceLastVisit'; + protected $nameSingular = 'General_SecondsSinceLastVisit'; + + public function getName() + { + return Piwik::translate('General_SecondsSinceLastVisit'); + } + + /** + * @param Request $request + * @param Visitor $visitor + * @param Action|null $action + * @return mixed + */ + public function onNewVisit(Request $request, Visitor $visitor, $action) + { + if (!$visitor->isVisitorKnown()) { + return 0; + } + + $currentTimestamp = $request->getCurrentTimestamp(); + + $previousVisitFirstActionTime = $visitor->getPreviousVisitColumn('visit_first_action_time'); + if (empty($previousVisitFirstActionTime)) { + return 0; + } + + $previousVisitFirstActionTime = Date::factory($previousVisitFirstActionTime)->getTimestamp(); + + if (empty($previousVisitFirstActionTime)) { + Common::printDebug("Found empty visit_first_action_time for last visit of known visitor, this is unexpected."); + return 0; + } + + $secondsSinceLast = $currentTimestamp - $previousVisitFirstActionTime; + if ($secondsSinceLast < 0) { // tracking a visit in the past + return null; + } + + return $secondsSinceLast; + } +}
\ No newline at end of file diff --git a/plugins/VisitorInterest/Columns/VisitsByDaysSinceLastVisit.php b/plugins/VisitorInterest/Columns/VisitsByDaysSinceLastVisit.php deleted file mode 100644 index 89cd4b64b9..0000000000 --- a/plugins/VisitorInterest/Columns/VisitsByDaysSinceLastVisit.php +++ /dev/null @@ -1,41 +0,0 @@ -<?php -/** - * Matomo - free/libre analytics platform - * - * @link https://matomo.org - * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later - * - */ -namespace Piwik\Plugins\VisitorInterest\Columns; - -use Piwik\Piwik; -use Piwik\Plugin\Dimension\VisitDimension; -use Piwik\Tracker\Action; -use Piwik\Tracker\Request; -use Piwik\Tracker\Visitor; - -class VisitsByDaysSinceLastVisit extends VisitDimension -{ - protected $columnName = 'visitor_days_since_last'; - protected $columnType = 'SMALLINT(5) UNSIGNED NULL'; - protected $type = self::TYPE_NUMBER; - protected $segmentName = 'daysSinceLastVisit'; - protected $nameSingular = 'General_DaysSinceLastVisit'; - - public function getName() - { - return Piwik::translate('VisitorInterest_VisitsByDaysSinceLast'); - } - - /** - * @param Request $request - * @param Visitor $visitor - * @param Action|null $action - * @return mixed - */ - public function onNewVisit(Request $request, Visitor $visitor, $action) - { - return $request->getDaysSinceLastVisit(); - } - -}
\ No newline at end of file diff --git a/plugins/VisitorInterest/Reports/GetNumberOfVisitsByDaysSinceLast.php b/plugins/VisitorInterest/Reports/GetNumberOfVisitsByDaysSinceLast.php index daa5c9954f..32769fb8f2 100644 --- a/plugins/VisitorInterest/Reports/GetNumberOfVisitsByDaysSinceLast.php +++ b/plugins/VisitorInterest/Reports/GetNumberOfVisitsByDaysSinceLast.php @@ -10,7 +10,7 @@ namespace Piwik\Plugins\VisitorInterest\Reports; use Piwik\Piwik; use Piwik\Plugin\ViewDataTable; -use Piwik\Plugins\VisitorInterest\Columns\VisitsByDaysSinceLastVisit; +use Piwik\Plugins\VisitorInterest\Columns\VisitorDaysSinceLast; use Piwik\Report\ReportWidgetFactory; use Piwik\Widget\WidgetsList; @@ -21,7 +21,7 @@ class GetNumberOfVisitsByDaysSinceLast extends Base protected function init() { parent::init(); - $this->dimension = new VisitsByDaysSinceLastVisit(); + $this->dimension = new VisitorDaysSinceLast(); $this->name = Piwik::translate('VisitorInterest_VisitsByDaysSinceLast'); $this->documentation = Piwik::translate('VisitorInterest_WidgetVisitsByDaysSinceLastDocumentation'); $this->metrics = array('nb_visits'); diff --git a/plugins/VisitorInterest/VisitorDetails.php b/plugins/VisitorInterest/VisitorDetails.php index 662ba163c1..688e0e47a3 100644 --- a/plugins/VisitorInterest/VisitorDetails.php +++ b/plugins/VisitorInterest/VisitorDetails.php @@ -14,6 +14,7 @@ class VisitorDetails extends VisitorDetailsAbstract { public function extendVisitorDetails(&$visitor) { - $visitor['daysSinceLastVisit'] = $this->details['visitor_days_since_last']; + $visitor['daysSinceLastVisit'] = floor($this->details['visitor_seconds_since_last'] / 86400); + $visitor['secondsSinceLastVisit'] = $this->details['visitor_seconds_since_last']; } }
\ No newline at end of file diff --git a/plugins/VisitorInterest/tests/Unit/Columns/VisitorSecondsSinceLastTest.php b/plugins/VisitorInterest/tests/Unit/Columns/VisitorSecondsSinceLastTest.php new file mode 100644 index 0000000000..a56d5c15c6 --- /dev/null +++ b/plugins/VisitorInterest/tests/Unit/Columns/VisitorSecondsSinceLastTest.php @@ -0,0 +1,72 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link https://matomo.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + */ + +namespace Piwik\Plugins\VisitorInterest\tests\Unit\Columns; + + +use PHPUnit\Framework\TestCase; +use Piwik\Plugins\VisitorInterest\Columns\VisitorSecondsSinceLast; +use Piwik\Tracker\Request; +use Piwik\Tracker\Visit\VisitProperties; +use Piwik\Tracker\Visitor; + +class VisitorSecondsSinceLastTest extends TestCase +{ + public function test_onNewVisit_returnsZeroIfVisitorIsUnknown() + { + $dim = new VisitorSecondsSinceLast(); + + $value = $dim->onNewVisit($this->makeMockRequest(), $this->makeMockVisitor($isKnown = false), null); + $this->assertEquals(0, $value); + } + + public function test_onNewVisit_returnsZeroIfPreviousVisitorLastActionTimeIsZero() + { + $dim = new VisitorSecondsSinceLast(); + + $value = $dim->onNewVisit($this->makeMockRequest(), $this->makeMockVisitor($isKnown = true), null); + $this->assertEquals(0, $value); + } + + public function test_onNewVisit_returnsTimeInBetweenIfKnownVisit() + { + $dim = new VisitorSecondsSinceLast(); + + $currentTime = time(); + $lastTime = $currentTime - 100; + + $value = $dim->onNewVisit($this->makeMockRequest($currentTime), $this->makeMockVisitor($isKnown = true, [], ['visit_first_action_time' => $lastTime]), null); + $this->assertEquals(100, $value); + } + + /** + * @return Request + */ + private function makeMockRequest($currentTime = null) + { + $mockBuilder = $this->getMockBuilder(Request::class)->disableOriginalConstructor(); + if ($currentTime) { + $mockBuilder->onlyMethods(['getCurrentTimestamp']); + } + $mock = $mockBuilder->getMock(); + if ($currentTime) { + $mock->method('getCurrentTimestamp')->willReturn($currentTime); + } + + /** @var Request $mock */ + $result = $mock; + return $result; + } + + private function makeMockVisitor($isKnown, $visitProps = [], $previousVisitProps = []) + { + $visitor = new Visitor(new VisitProperties($visitProps), $isKnown, new VisitProperties($previousVisitProps)); + return $visitor; + } +}
\ No newline at end of file |