1
0
Fork 0
mirror of https://github.com/Findus23/plugin-BotTracking.git synced 2024-09-18 13:33:43 +02:00

complete WIP

This commit is contained in:
Lukas Winkler 2020-05-24 11:14:21 +02:00
commit 62b81dc916
Signed by: lukas
GPG key ID: 54DE4D798D244853
13 changed files with 331 additions and 0 deletions

51
BotTracking.php Normal file
View file

@ -0,0 +1,51 @@
<?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\BotTracking;
use DeviceDetector\DeviceDetector;
use Piwik\Common;
use Piwik\DeviceDetector\DeviceDetectorCache;
use Piwik\Plugin;
use Piwik\Tracker\Request;
class BotTracking extends Plugin
{
public function registerEvents()
{
return array(
'Tracker.isExcludedVisit' => 'isExcludedVisit'
);
}
public function isTrackerPlugin()
{
return true;
}
public function isExcludedVisit(bool &$excluded, Request $request)
{
$userAgent = $request->getUserAgent();
$userAgent = Common::unsanitizeInputValue($userAgent);
// $deviceDetector = StaticContainer::get(DeviceDetectorFactory::class)->makeInstance($userAgent);
// create new DeviceDetector with full data
$deviceDetector = new DeviceDetector($userAgent);
$deviceDetector->setCache(new DeviceDetectorCache(86400));
$deviceDetector->parse();
$isBot = $deviceDetector->isBot();
if ($isBot) {
$request->setMetadata("BotTracking", "isBot", $deviceDetector->isBot());
$botMeta = $deviceDetector->getBot();
$request->setMetadata("BotTracking", "botName", $botMeta["name"]);
$request->setMetadata("BotTracking", "botCategory", $botMeta["category"]);
$request->setMetadata("BotTracking", "botProducer", $botMeta["producer"]["name"]);
$excluded = false;
}
}
}

3
CHANGELOG.md Normal file
View file

@ -0,0 +1,3 @@
## Changelog
Here goes the changelog text.

71
Columns/BotCategory.php Normal file
View file

@ -0,0 +1,71 @@
<?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\BotTracking\Columns;
use Piwik\Plugin\Dimension\VisitDimension;
use Piwik\Tracker\Action;
use Piwik\Tracker\Request;
use Piwik\Tracker\Visitor;
class BotCategory extends VisitDimension
{
/**
* This will be the name of the column in the log_visit table if a $columnType is specified.
* @var string
*/
protected $columnName = 'bot_category';
/**
* If a columnType is defined, we will create this a column in the MySQL table having this type. Please make sure
* MySQL will understand this type. Once you change the column type the Piwik platform will notify the user to
* perform an update which can sometimes take a long time so be careful when choosing the correct column type.
* @var string
*/
protected $columnType = "VARCHAR(255)";
/**
* The type of the dimension is automatically detected by the columnType. If the type of the dimension is not
* detected correctly, you may want to adjust the type manually. The configured type will affect how the dimension
* is formatted in the UI.
* @var string
*/
protected $type = self::TYPE_TEXT;
/**
* The name of the dimension which will be visible for instance in the UI of a related report and in the mobile app.
* @return string
*/
protected $nameSingular = 'BotTracking_BotCategory';
/**
* By defining a segment a user will be able to filter their visitors by this column. For instance
* show all reports only considering users having more than 10 achievement points. If you do not want to define a
* segment for this dimension, simply leave the name empty.
*/
protected $segmentName = 'botCategory';
protected $acceptValues = 'Here you should explain which values are accepted/useful for segments: Any number, for instance 1, 2, 3 , 99';
/**
* The onNewVisit method is triggered when a new visitor is detected. This means here you can define an initial
* value for this user. By returning boolean false no value will be saved. Once the user makes another action the
* event "onExistingVisit" is executed. That means for each visitor this method is executed once. If you do not want
* to perform any action on a new visit you can just remove this method.
*
* @param Request $request
* @param Visitor $visitor
* @param Action|null $action
* @return mixed|false
*/
public function onNewVisit(Request $request, Visitor $visitor, $action)
{
return $request->getMetadata("BotTracking", "botCategory");
}
}

71
Columns/BotName.php Normal file
View file

@ -0,0 +1,71 @@
<?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\BotTracking\Columns;
use Piwik\Plugin\Dimension\VisitDimension;
use Piwik\Tracker\Action;
use Piwik\Tracker\Request;
use Piwik\Tracker\Visitor;
class BotName extends VisitDimension
{
/**
* This will be the name of the column in the log_visit table if a $columnType is specified.
* @var string
*/
protected $columnName = 'bot_name';
/**
* If a columnType is defined, we will create this a column in the MySQL table having this type. Please make sure
* MySQL will understand this type. Once you change the column type the Piwik platform will notify the user to
* perform an update which can sometimes take a long time so be careful when choosing the correct column type.
* @var string
*/
protected $columnType = "VARCHAR(255)";
/**
* The type of the dimension is automatically detected by the columnType. If the type of the dimension is not
* detected correctly, you may want to adjust the type manually. The configured type will affect how the dimension
* is formatted in the UI.
* @var string
*/
protected $type = self::TYPE_TEXT;
/**
* The name of the dimension which will be visible for instance in the UI of a related report and in the mobile app.
* @return string
*/
protected $nameSingular = 'BotTracking_BotName';
/**
* By defining a segment a user will be able to filter their visitors by this column. For instance
* show all reports only considering users having more than 10 achievement points. If you do not want to define a
* segment for this dimension, simply leave the name empty.
*/
protected $segmentName = 'botName';
protected $acceptValues = 'Here you should explain which values are accepted/useful for segments: Any number, for instance 1, 2, 3 , 99';
/**
* The onNewVisit method is triggered when a new visitor is detected. This means here you can define an initial
* value for this user. By returning boolean false no value will be saved. Once the user makes another action the
* event "onExistingVisit" is executed. That means for each visitor this method is executed once. If you do not want
* to perform any action on a new visit you can just remove this method.
*
* @param Request $request
* @param Visitor $visitor
* @param Action|null $action
* @return mixed|false
*/
public function onNewVisit(Request $request, Visitor $visitor, $action)
{
return $request->getMetadata("BotTracking", "botName");
}
}

84
Columns/BotProducer.php Normal file
View file

@ -0,0 +1,84 @@
<?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\BotTracking\Columns;
use Piwik\Plugin\Dimension\VisitDimension;
use Piwik\Tracker\Action;
use Piwik\Tracker\Request;
use Piwik\Tracker\Visitor;
class BotProducer extends VisitDimension
{
/**
* This will be the name of the column in the log_visit table if a $columnType is specified.
* @var string
*/
protected $columnName = 'bot_producer';
/**
* If a columnType is defined, we will create this a column in the MySQL table having this type. Please make sure
* MySQL will understand this type. Once you change the column type the Piwik platform will notify the user to
* perform an update which can sometimes take a long time so be careful when choosing the correct column type.
* @var string
*/
protected $columnType = "VARCHAR(255)";
/**
* The type of the dimension is automatically detected by the columnType. If the type of the dimension is not
* detected correctly, you may want to adjust the type manually. The configured type will affect how the dimension
* is formatted in the UI.
* @var string
*/
protected $type = self::TYPE_TEXT;
/**
* The name of the dimension which will be visible for instance in the UI of a related report and in the mobile app.
* @return string
*/
protected $nameSingular = 'BotTracking_BotProducer';
/**
* By defining a segment a user will be able to filter their visitors by this column. For instance
* show all reports only considering users having more than 10 achievement points. If you do not want to define a
* segment for this dimension, simply leave the name empty.
*/
protected $segmentName = 'botProducer';
protected $acceptValues = 'Here you should explain which values are accepted/useful for segments: Any number, for instance 1, 2, 3 , 99';
/**
* The onNewVisit method is triggered when a new visitor is detected. This means here you can define an initial
* value for this user. By returning boolean false no value will be saved. Once the user makes another action the
* event "onExistingVisit" is executed. That means for each visitor this method is executed once. If you do not want
* to perform any action on a new visit you can just remove this method.
*
* @param Request $request
* @param Visitor $visitor
* @param Action|null $action
* @return mixed|false
*/
public function onNewVisit(Request $request, Visitor $visitor, $action)
{
return $request->getMetadata("BotTracking", "botProducer");
}
/**
* Sometimes you may want to make sure another dimension is executed before your dimension so you can persist
* a value depending on the value of other dimensions. You can do this by defining an array of dimension names.
* If you access any value of any other column within your events, you should require them here. Otherwise those
* values may not be available.
* @return array
* public function getRequiredVisitFields()
* {
* return array('idsite', 'server_time');
* }
*/
}

6
README.md Normal file
View file

@ -0,0 +1,6 @@
# Matomo BotTracking Plugin
## Description
Add your plugin description here.

2
config/config.php Normal file
View file

@ -0,0 +1,2 @@
<?php
return array();

2
config/tracker.php Normal file
View file

@ -0,0 +1,2 @@
<?php
return array();

5
docs/faq.md Normal file
View file

@ -0,0 +1,5 @@
## FAQ
__My question?__
My answer

1
docs/index.md Normal file
View file

@ -0,0 +1 @@
## Documentation

5
lang/en.json Normal file
View file

@ -0,0 +1,5 @@
{
"BotTracking": {
"BotName": "BotName"
}
}

30
plugin.json Normal file
View file

@ -0,0 +1,30 @@
{
"name": "BotTracking",
"description": "Track statistics exclusivly about bots",
"version": "0.1.0",
"theme": false,
"require": {
"matomo": ">=4.0.0-b1,<5.0.0-b1",
"piwik": ">=4.0.0-b2,<5.0.0-b1"
},
"authors": [
{
"name": "Matomo",
"email": "",
"homepage": ""
}
],
"support": {
"email": "",
"issues": "",
"forum": "",
"irc": "",
"wiki": "",
"source": "",
"docs": "",
"rss": ""
},
"homepage": "",
"license": "GPL v3+",
"keywords": []
}

0
screenshots/.gitkeep Normal file
View file