From b1a7226ed86262196427947f54f566aef3cbb299 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Sun, 23 Feb 2020 22:41:41 +0100 Subject: [PATCH] Add tracking widgets --- Classes/Dashboard/Widgets/PageViewsBar.php | 102 ++++++++++++++++++ .../Widgets/PageViewsPerPageDoughnut.php | 99 +++++++++++++++++ .../Backend/DashboardWidgetGroups.php | 6 ++ Configuration/Services.yaml | 18 ++++ Resources/Private/Language/locallang.xlf | 26 +++++ 5 files changed, 251 insertions(+) create mode 100644 Classes/Dashboard/Widgets/PageViewsBar.php create mode 100644 Classes/Dashboard/Widgets/PageViewsPerPageDoughnut.php create mode 100644 Configuration/Backend/DashboardWidgetGroups.php create mode 100644 Resources/Private/Language/locallang.xlf diff --git a/Classes/Dashboard/Widgets/PageViewsBar.php b/Classes/Dashboard/Widgets/PageViewsBar.php new file mode 100644 index 0000000..af05804 --- /dev/null +++ b/Classes/Dashboard/Widgets/PageViewsBar.php @@ -0,0 +1,102 @@ + + * + * 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. + */ + +use TYPO3\CMS\Core\Database\Connection; +use TYPO3\CMS\Core\Database\ConnectionPool; +use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Dashboard\Widgets\AbstractBarChartWidget; + +class PageViewsBar extends AbstractBarChartWidget +{ + protected $title = 'LLL:EXT:tracking/Resources/Private/Language/locallang.xlf:dashboard.widgets.pageViewsBar.title'; + + protected $description = 'LLL:EXT:tracking/Resources/Private/Language/locallang.xlf:dashboard.widgets.pageViewsBar.description'; + + protected $width = 2; + + protected $height = 4; + + protected function prepareChartData(): void + { + list($labels, $data) = $this->calculateDataForLastDays(31); + + $this->chartData = [ + 'labels' => $labels, + 'datasets' => [ + [ + 'label' => $this->getLanguageService()->sL( + 'LLL:EXT:tracking/Resources/Private/Language/locallang.xlf:widgets.pageViewsBar.chart.dataSet.0' + ), + 'backgroundColor' => $this->chartColors[0], + 'border' => 0, + 'data' => $data + ] + ] + ]; + } + + protected function getPageViewsInPeriod(int $start, int $end): int + { + $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) + ->getQueryBuilderForTable('tx_tracking_pageview'); + + return (int)$queryBuilder + ->count('*') + ->from('tx_tracking_pageview') + ->where( + $queryBuilder->expr()->gte('tstamp', $start), + $queryBuilder->expr()->lte('tstamp', $end), + $queryBuilder->expr()->notIn( + 'tx_tracking_pageview.pid', + $queryBuilder->createNamedParameter([ + 1, + 11, + 38, + ], Connection::PARAM_INT_ARRAY) + ) + ) + ->execute() + ->fetchColumn(); + } + + protected function calculateDataForLastDays(int $days): array + { + $labels = []; + $data = []; + + $format = $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] ?: 'Y-m-d'; + + for ($daysBefore = $days; $daysBefore > 0; $daysBefore--) { + $labels[] = date($format, strtotime('-' . $daysBefore . ' day')); + $startPeriod = strtotime('-' . $daysBefore . ' day 0:00:00'); + $endPeriod = strtotime('-' . $daysBefore . ' day 23:59:59'); + + $data[] = $this->getPageViewsInPeriod($startPeriod, $endPeriod); + } + + return [ + $labels, + $data, + ]; + } +} diff --git a/Classes/Dashboard/Widgets/PageViewsPerPageDoughnut.php b/Classes/Dashboard/Widgets/PageViewsPerPageDoughnut.php new file mode 100644 index 0000000..a3d5e84 --- /dev/null +++ b/Classes/Dashboard/Widgets/PageViewsPerPageDoughnut.php @@ -0,0 +1,99 @@ + + * + * 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. + */ + +use Doctrine\DBAL\ParameterType; +use TYPO3\CMS\Core\Database\Connection; +use TYPO3\CMS\Core\Database\ConnectionPool; +use TYPO3\CMS\Core\Database\Query\QueryBuilder; +use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Dashboard\Widgets\AbstractDoughnutChartWidget; + +class PageViewsPerPageDoughnut extends AbstractDoughnutChartWidget +{ + protected $title = 'LLL:EXT:tracking/Resources/Private/Language/locallang.xlf:dashboard.widgets.pageViewsPerPageDoughnut.title'; + + protected $description = 'LLL:EXT:tracking/Resources/Private/Language/locallang.xlf:dashboard.widgets.pageViewsPerPageDoughnut.description'; + + protected function prepareChartData(): void + { + list($labels, $data) = $this->getPageViewsPerPage(31); + + $this->chartData = [ + 'labels' => $labels, + 'datasets' => [ + [ + 'backgroundColor' => $this->chartColors, + 'data' => $data, + ] + ], + ]; + } + + private function getPageViewsPerPage(int $period): array + { + $labels = []; + $data = []; + $queryBuilder = $this->getQueryBuilder(); + + $result = $queryBuilder + ->selectLiteral('count(tx_tracking_pageview.pid) as total') + ->addSelect('pages.title', 'pages.uid') + ->from('tx_tracking_pageview') + ->leftJoin( + 'tx_tracking_pageview', + 'pages', + 'pages', + $queryBuilder->expr()->eq('tx_tracking_pageview.pid', $queryBuilder->quoteIdentifier('pages.uid')) + ) + ->where( + $queryBuilder->expr()->notIn( + 'tx_tracking_pageview.pid', + $queryBuilder->createNamedParameter([ + 1, + 11, + 38, + ], Connection::PARAM_INT_ARRAY) + ) + ) + ->groupBy('tx_tracking_pageview.pid') + ->orderBy('total', 'desc') + ->setMaxResults(6) // Because 6 colors are defined + ->execute() + ->fetchAll(); + + foreach ($result as $row) { + $labels[] = $row['title'] . ' [' . $row['uid'] . ']'; + $data[] = $row['total']; + } + + return [ + $labels, + $data, + ]; + } + + private function getQueryBuilder(): QueryBuilder + { + return GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_tracking_pageview'); + } +} diff --git a/Configuration/Backend/DashboardWidgetGroups.php b/Configuration/Backend/DashboardWidgetGroups.php new file mode 100644 index 0000000..eaf82ca --- /dev/null +++ b/Configuration/Backend/DashboardWidgetGroups.php @@ -0,0 +1,6 @@ + [ + 'title' => 'LLL:EXT:tracking/Resources/Private/Language/locallang.xlf:dashboard.widget.group.tracking', + ], +]; diff --git a/Configuration/Services.yaml b/Configuration/Services.yaml index 184d2d6..cf497d9 100644 --- a/Configuration/Services.yaml +++ b/Configuration/Services.yaml @@ -26,3 +26,21 @@ services: not (context.getAspect("backend.user").isLoggedIn()) and not (request.getHeader("User-Agent")[0] matches "/^Wget|TYPO3|TYPO3 linkvalidator/") and not (request.getHeader("User-Agent")[0] matches "/Googlebot|Bingbot|Slurp|DuckDuckBot|Baiduspider|YandexBot|Sogou|Exabot|NextCloud-News|Feedly|XING FeedReader|CCBot/") + + # Dashboard Widgets + + DanielSiepmann\Tracking\Dashboard\Widgets\PageViewsBar: + class: DanielSiepmann\Tracking\Dashboard\Widgets\PageViewsBar + arguments: [pageViewsBar] + tags: + - name: dashboard.widget + identifier: pageViewsBar + widgetGroups: tracking + + DanielSiepmann\Tracking\Dashboard\Widgets\PageViewsPerPageDoughnut: + class: DanielSiepmann\Tracking\Dashboard\Widgets\PageViewsPerPageDoughnut + arguments: [pageViewsPerPageDoughnut] + tags: + - name: dashboard.widget + identifier: pageViewsPerPageDoughnut + widgetGroups: tracking diff --git a/Resources/Private/Language/locallang.xlf b/Resources/Private/Language/locallang.xlf new file mode 100644 index 0000000..b1d2f3b --- /dev/null +++ b/Resources/Private/Language/locallang.xlf @@ -0,0 +1,26 @@ + + + +
+ + + Tracking + + + Page Views / Day + + + Displays total page views per day. + + + Total Page Views + + + Page Views / Page + + + Displays total page views per page. + + + +