|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Webperf plugin for Craft CMS 3.x |
|
4
|
|
|
* |
|
5
|
|
|
* Monitor the performance of your webpages through real-world user timing data |
|
6
|
|
|
* |
|
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2018 nystudio107 |
|
|
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace { |
|
12
|
|
|
|
|
13
|
|
|
require_once __DIR__ . '/../lib/geoiploc.php'; |
|
|
|
|
|
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
namespace nystudio107\webperf\controllers { |
|
17
|
|
|
|
|
18
|
|
|
use nystudio107\webperf\helpers\MultiSite; |
|
19
|
|
|
use nystudio107\webperf\Webperf; |
|
20
|
|
|
use nystudio107\webperf\models\DataSample; |
|
21
|
|
|
|
|
22
|
|
|
use Jaybizzle\CrawlerDetect\CrawlerDetect; |
|
|
|
|
|
|
23
|
|
|
use WhichBrowser\Parser; |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
use Craft; |
|
|
|
|
|
|
26
|
|
|
use craft\errors\SiteNotFoundException; |
|
|
|
|
|
|
27
|
|
|
use craft\web\Controller; |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
use yii\base\InvalidConfigException; |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/** |
|
|
|
|
|
|
32
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
33
|
|
|
* @package Webperf |
|
|
|
|
|
|
34
|
|
|
* @since 1.0.0 |
|
|
|
|
|
|
35
|
|
|
*/ |
|
|
|
|
|
|
36
|
|
|
class MetricsController extends Controller |
|
37
|
|
|
{ |
|
38
|
|
|
// Public Properties |
|
39
|
|
|
// ========================================================================= |
|
40
|
|
|
|
|
41
|
|
|
public $enableCsrfValidation = false; |
|
42
|
|
|
|
|
43
|
|
|
// Protected Properties |
|
44
|
|
|
// ========================================================================= |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
|
|
|
|
|
47
|
|
|
* @var bool|array Allows anonymous access to this controller's actions. |
|
48
|
|
|
* The actions must be in 'kebab-case' |
|
49
|
|
|
* @access protected |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $allowAnonymous = ['beacon']; |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
// Public Methods |
|
55
|
|
|
// ========================================================================= |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
|
|
|
|
|
58
|
|
|
* @return mixed |
|
59
|
|
|
*/ |
|
60
|
|
|
public function actionBeacon() |
|
61
|
|
|
{ |
|
62
|
|
|
// Get the incoming params from the beacon |
|
63
|
|
|
try { |
|
64
|
|
|
$params = Craft::$app->getRequest()->getBodyParams(); |
|
65
|
|
|
} catch (InvalidConfigException $e) { |
|
66
|
|
|
$params = []; |
|
67
|
|
|
} |
|
68
|
|
|
// Ensure the beacon has at least the URL parameter |
|
69
|
|
|
if (empty($params) || empty($params['u'])) { |
|
70
|
|
|
return; |
|
71
|
|
|
} |
|
72
|
|
|
// Filter out bot/spam requests via UserAgent |
|
73
|
|
|
if (Webperf::$settings->filterBotUserAgents) { |
|
74
|
|
|
$crawlerDetect = new CrawlerDetect; |
|
75
|
|
|
// Check the user agent of the current 'visitor' |
|
76
|
|
|
if ($crawlerDetect->isCrawler()) { |
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
// Allocate a new DataSample, and fill it in |
|
81
|
|
|
$sample = new DataSample(); |
|
82
|
|
|
$sample->url = $params['u']; |
|
83
|
|
|
// Get the site id |
|
84
|
|
|
try { |
|
85
|
|
|
$site = MultiSite::getSiteFromUrl($sample->url); |
|
86
|
|
|
$sample->siteId = $site->id; |
|
87
|
|
|
} catch (SiteNotFoundException $e) { |
|
88
|
|
|
$sample->siteId = null; |
|
89
|
|
|
} |
|
90
|
|
|
// Fill in all of the timing information that's available |
|
91
|
|
|
$sample->pageLoad = $params['t_done'] ?? null; |
|
92
|
|
|
if (!empty($params['nt_dns_end']) && !empty($params['nt_dns_st'])) { |
|
93
|
|
|
$sample->dns = $params['nt_dns_end'] - $params['nt_dns_st']; |
|
94
|
|
|
} |
|
95
|
|
|
if (!empty($params['nt_con_end']) && !empty($params['nt_con_st'])) { |
|
96
|
|
|
$sample->connect = $params['nt_con_end'] - $params['nt_con_st']; |
|
97
|
|
|
} |
|
98
|
|
|
if (!empty($params['t_resp'])) { |
|
99
|
|
|
$sample->firstByte = $params['t_resp']; |
|
100
|
|
|
} |
|
101
|
|
|
if (!empty($params['pt_fp'])) { |
|
102
|
|
|
$sample->firstPaint = $params['pt_fp']; |
|
103
|
|
|
} |
|
104
|
|
|
if (!empty($params['pt_fcp'])) { |
|
105
|
|
|
$sample->firstContentfulPaint = $params['pt_fcp']; |
|
106
|
|
|
} |
|
107
|
|
|
if (!empty($params['nt_domint']) && !empty($params['nt_nav_st'])) { |
|
108
|
|
|
$sample->domInteractive = $params['nt_domint'] - $params['nt_nav_st']; |
|
109
|
|
|
} |
|
110
|
|
|
if (!empty($params['nt_domcomp']) && !empty($params['nt_nav_st'])) { |
|
111
|
|
|
$sample->pageLoad = $params['nt_domcomp'] - $params['nt_nav_st']; |
|
112
|
|
|
} |
|
113
|
|
|
// Fill in information from the current request |
|
114
|
|
|
$request = Craft::$app->getRequest(); |
|
115
|
|
|
$ip = $request->userIP; |
|
116
|
|
|
if ($ip) { |
|
117
|
|
|
$sample->countryCode = getCountryFromIP($ip); |
|
|
|
|
|
|
118
|
|
|
// getCountryFromIP returns 'ZZ' for unknown countries, map to '??' |
|
119
|
|
|
if ($sample->countryCode === 'ZZ') { |
|
120
|
|
|
$sample->countryCode = '??'; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
$userAgent = $request->userAgent; |
|
124
|
|
|
if ($userAgent) { |
|
125
|
|
|
$parser = new Parser($userAgent); |
|
126
|
|
|
$sample->browser = $parser->browser->name; |
|
127
|
|
|
$sample->os = $parser->os->name; |
|
128
|
|
|
$sample->mobile = $parser->isMobile(); |
|
129
|
|
|
} |
|
130
|
|
|
// Save the data sample |
|
131
|
|
|
Craft::debug('Saving DataSample: '.print_r($sample, true), __METHOD__); |
|
132
|
|
|
Webperf::$plugin->dataSamples->addDataSample($sample); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|