mirror of
https://github.com/DanielSiepmann/tracking.git
synced 2024-12-22 16:16:10 +01:00
Daniel Siepmann
5f0490f493
Allow each tracking record to contain arbitrary tags. Those tags are generated via an API and can be extended by foreign extensions or for individual projects. Existing operating_system was moved to this new feature. The update command allows to migrate existing records to this new feature. Those flags can be used when configuring widgets. A new flag was added bot:yes and bot:no. Bots are now tracked but flagged. That new feature allows to build fine grained reports and makes the extension way more flexible. Possible new implications: - Show visits from none bots - Show visits from bots - Show visits from specific bot - Add color to page views per page (bar chart) to color bot or none bot WIP: - Update Yaml file to work like before, no bots in widgets - Add documentation (widgets) - Add documentation (migration *.yaml)
322 lines
11 KiB
PHP
322 lines
11 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/*
|
|
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version 2
|
|
* of the License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
* 02110-1301, USA.
|
|
*/
|
|
|
|
namespace DanielSiepmann\Tracking\Tests\Functional\Dashboard\Provider;
|
|
|
|
use DanielSiepmann\Tracking\Dashboard\Provider\Demand;
|
|
use DanielSiepmann\Tracking\Dashboard\Provider\Demand\Tag;
|
|
use DanielSiepmann\Tracking\Dashboard\Provider\PageviewsPerDay;
|
|
use TYPO3\CMS\Core\Database\ConnectionPool;
|
|
use TYPO3\CMS\Core\Localization\LanguageService;
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
|
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase as TestCase;
|
|
|
|
/**
|
|
* @covers \DanielSiepmann\Tracking\Dashboard\Provider\PageviewsPerDay
|
|
*/
|
|
class PageviewsPerDayTest extends TestCase
|
|
{
|
|
protected $testExtensionsToLoad = [
|
|
'typo3conf/ext/tracking',
|
|
];
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function listsResultsForLast31DaysByDefault(): void
|
|
{
|
|
$connection = $this->getConnectionPool()->getConnectionForTable('tx_tracking_pageview');
|
|
for ($i = 1; $i <= 10; $i++) {
|
|
$connection->insert('tx_tracking_pageview', [
|
|
'pid' => $i,
|
|
'crdate' => strtotime('-' . $i . ' days'),
|
|
]);
|
|
}
|
|
|
|
$subject = new PageviewsPerDay(
|
|
GeneralUtility::makeInstance(LanguageService::class),
|
|
GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_tracking_pageview'),
|
|
new Demand()
|
|
);
|
|
|
|
$result = $subject->getChartData();
|
|
self::assertCount(32, $result['labels']);
|
|
self::assertCount(32, $result['datasets'][0]['data']);
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function respectedNumberOfDays(): void
|
|
{
|
|
$this->importDataSet('EXT:tracking/Tests/Functional/Fixtures/Pages.xml');
|
|
$connection = $this->getConnectionPool()->getConnectionForTable('tx_tracking_pageview');
|
|
for ($i = 1; $i <= 10; $i++) {
|
|
$connection->insert('tx_tracking_pageview', [
|
|
'pid' => $i,
|
|
'crdate' => strtotime('-' . $i . ' days'),
|
|
]);
|
|
}
|
|
|
|
$subject = new PageviewsPerDay(
|
|
GeneralUtility::makeInstance(LanguageService::class),
|
|
GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_tracking_pageview'),
|
|
new Demand(3)
|
|
);
|
|
|
|
$result = $subject->getChartData();
|
|
self::assertCount(4, $result['labels']);
|
|
self::assertSame([
|
|
1,
|
|
1,
|
|
1,
|
|
0,
|
|
], $result['datasets'][0]['data']);
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function respectedExcludedPages(): void
|
|
{
|
|
$this->importDataSet('EXT:tracking/Tests/Functional/Fixtures/Pages.xml');
|
|
$connection = $this->getConnectionPool()->getConnectionForTable('tx_tracking_pageview');
|
|
for ($i = 1; $i <= 10; $i++) {
|
|
$connection->insert('tx_tracking_pageview', [
|
|
'pid' => $i,
|
|
'crdate' => strtotime('-' . $i . ' days'),
|
|
]);
|
|
}
|
|
|
|
$subject = new PageviewsPerDay(
|
|
GeneralUtility::makeInstance(LanguageService::class),
|
|
GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_tracking_pageview'),
|
|
new Demand(3, 0, [2])
|
|
);
|
|
|
|
$result = $subject->getChartData();
|
|
self::assertCount(4, $result['labels']);
|
|
self::assertSame([
|
|
1,
|
|
0,
|
|
1,
|
|
0,
|
|
], $result['datasets'][0]['data']);
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function respectedDateFormat(): void
|
|
{
|
|
$this->importDataSet('EXT:tracking/Tests/Functional/Fixtures/Pages.xml');
|
|
$connection = $this->getConnectionPool()->getConnectionForTable('tx_tracking_pageview');
|
|
|
|
$subject = new PageviewsPerDay(
|
|
GeneralUtility::makeInstance(LanguageService::class),
|
|
GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_tracking_pageview'),
|
|
new Demand(1, 0, [], []),
|
|
'd.m.Y'
|
|
);
|
|
|
|
$result = $subject->getChartData();
|
|
self::assertSame([
|
|
date('d.m.Y', strtotime('-1 day')),
|
|
date('d.m.Y'),
|
|
], $result['labels']);
|
|
self::assertCount(2, $result['datasets'][0]['data']);
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function respectsLimitToLanguages(): void
|
|
{
|
|
$connection = $this->getConnectionPool()->getConnectionForTable('tx_tracking_pageview');
|
|
for ($i = 1; $i <= 10; $i++) {
|
|
$connection->insert('tx_tracking_pageview', [
|
|
'pid' => $i,
|
|
'crdate' => strtotime('-' . $i . ' days'),
|
|
'sys_language_uid' => $i % 2,
|
|
]);
|
|
}
|
|
|
|
$subject = new PageviewsPerDay(
|
|
GeneralUtility::makeInstance(LanguageService::class),
|
|
GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_tracking_pageview'),
|
|
new Demand(11, 0, [], [1])
|
|
);
|
|
|
|
$result = $subject->getChartData();
|
|
self::assertSame([
|
|
0 => 0,
|
|
1 => 0,
|
|
2 => 1,
|
|
3 => 0,
|
|
4 => 1,
|
|
5 => 0,
|
|
6 => 1,
|
|
7 => 0,
|
|
8 => 1,
|
|
9 => 0,
|
|
10 => 1,
|
|
11 => 0,
|
|
], $result['datasets'][0]['data']);
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function respectedConfiguredTagRuleToNotIncludeBots(): void
|
|
{
|
|
$connection = $this->getConnectionPool()->getConnectionForTable('tx_tracking_pageview');
|
|
for ($i = 1; $i <= 10; $i++) {
|
|
$connection->insert('tx_tracking_pageview', [
|
|
'pid' => $i,
|
|
'uid' => $i,
|
|
'crdate' => strtotime('-' . $i . ' days'),
|
|
]);
|
|
$connection->insert('tx_tracking_tag', [
|
|
'pid' => $i,
|
|
'record_uid' => $i,
|
|
'record_table_name' => 'tx_tracking_pageview',
|
|
'name' => 'bot',
|
|
'value' => 'no',
|
|
'crdate' => strtotime('-' . $i . ' days'),
|
|
]);
|
|
$connection->insert('tx_tracking_pageview', [
|
|
'pid' => $i,
|
|
'uid' => $i * 20,
|
|
'crdate' => strtotime('-' . $i . ' days'),
|
|
]);
|
|
$connection->insert('tx_tracking_tag', [
|
|
'pid' => $i,
|
|
'record_uid' => $i * 20,
|
|
'record_table_name' => 'tx_tracking_pageview',
|
|
'name' => 'bot',
|
|
'value' => 'no',
|
|
'crdate' => strtotime('-' . $i . ' days'),
|
|
]);
|
|
|
|
$connection->insert('tx_tracking_pageview', [
|
|
'pid' => $i,
|
|
'uid' => $i * 300,
|
|
'crdate' => strtotime('-' . $i . ' days'),
|
|
]);
|
|
$connection->insert('tx_tracking_tag', [
|
|
'pid' => $i,
|
|
'record_uid' => $i * 300,
|
|
'record_table_name' => 'tx_tracking_pageview',
|
|
'name' => 'bot',
|
|
'value' => 'yes',
|
|
'crdate' => strtotime('-' . $i . ' days'),
|
|
]);
|
|
}
|
|
|
|
$subject = new PageviewsPerDay(
|
|
GeneralUtility::makeInstance(LanguageService::class),
|
|
GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_tracking_pageview'),
|
|
new Demand(2, 0, [], [], [
|
|
Tag::createFromArray([
|
|
'name' => 'bot',
|
|
'value' => 'no',
|
|
]),
|
|
])
|
|
);
|
|
|
|
$result = $subject->getChartData();
|
|
self::assertSame([
|
|
0 => 2,
|
|
1 => 2,
|
|
2 => 0,
|
|
], $result['datasets'][0]['data']);
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function respectedConfiguredTagRuleToIncludeBots(): void
|
|
{
|
|
$connection = $this->getConnectionPool()->getConnectionForTable('tx_tracking_pageview');
|
|
for ($i = 1; $i <= 10; $i++) {
|
|
$connection->insert('tx_tracking_pageview', [
|
|
'pid' => $i,
|
|
'uid' => $i,
|
|
'crdate' => strtotime('-' . $i . ' days'),
|
|
]);
|
|
$connection->insert('tx_tracking_tag', [
|
|
'pid' => $i,
|
|
'record_uid' => $i,
|
|
'record_table_name' => 'tx_tracking_pageview',
|
|
'name' => 'bot',
|
|
'value' => 'no',
|
|
'crdate' => strtotime('-' . $i . ' days'),
|
|
]);
|
|
$connection->insert('tx_tracking_pageview', [
|
|
'pid' => $i,
|
|
'uid' => $i * 20,
|
|
'crdate' => strtotime('-' . $i . ' days'),
|
|
]);
|
|
$connection->insert('tx_tracking_tag', [
|
|
'pid' => $i,
|
|
'record_uid' => $i * 20,
|
|
'record_table_name' => 'tx_tracking_pageview',
|
|
'name' => 'bot',
|
|
'value' => 'no',
|
|
'crdate' => strtotime('-' . $i . ' days'),
|
|
]);
|
|
|
|
$connection->insert('tx_tracking_pageview', [
|
|
'pid' => $i,
|
|
'uid' => $i * 300,
|
|
'crdate' => strtotime('-' . $i . ' days'),
|
|
]);
|
|
$connection->insert('tx_tracking_tag', [
|
|
'pid' => $i,
|
|
'record_uid' => $i * 300,
|
|
'record_table_name' => 'tx_tracking_pageview',
|
|
'name' => 'bot',
|
|
'value' => 'yes',
|
|
'crdate' => strtotime('-' . $i . ' days'),
|
|
]);
|
|
}
|
|
|
|
$subject = new PageviewsPerDay(
|
|
GeneralUtility::makeInstance(LanguageService::class),
|
|
GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_tracking_pageview'),
|
|
new Demand(2, 0, [], [], [
|
|
Tag::createFromArray([
|
|
'name' => 'bot',
|
|
'value' => 'yes',
|
|
]),
|
|
])
|
|
);
|
|
|
|
$result = $subject->getChartData();
|
|
self::assertSame([
|
|
0 => 1,
|
|
1 => 1,
|
|
2 => 0,
|
|
], $result['datasets'][0]['data']);
|
|
}
|
|
}
|