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) 2019 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\webperf\services; |
12
|
|
|
|
13
|
|
|
use nystudio107\webperf\Webperf; |
14
|
|
|
use nystudio107\webperf\base\CraftDataSample; |
15
|
|
|
use nystudio107\webperf\helpers\MultiSite; |
16
|
|
|
use nystudio107\webperf\helpers\PluginTemplate; |
17
|
|
|
use nystudio107\webperf\models\CraftDbErrorSample; |
18
|
|
|
use nystudio107\webperf\models\CraftDbDataSample; |
19
|
|
|
|
20
|
|
|
use nystudio107\seomatic\Seomatic; |
|
|
|
|
21
|
|
|
|
22
|
|
|
use Jaybizzle\CrawlerDetect\CrawlerDetect; |
|
|
|
|
23
|
|
|
|
24
|
|
|
use Craft; |
|
|
|
|
25
|
|
|
use craft\base\Component; |
|
|
|
|
26
|
|
|
use craft\errors\SiteNotFoundException; |
|
|
|
|
27
|
|
|
use craft\helpers\UrlHelper; |
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
|
|
|
|
30
|
|
|
* @author nystudio107 |
|
|
|
|
31
|
|
|
* @package Webperf |
|
|
|
|
32
|
|
|
* @since 1.0.0 |
|
|
|
|
33
|
|
|
*/ |
|
|
|
|
34
|
|
|
class Beacons extends Component |
35
|
|
|
{ |
36
|
|
|
// Constants |
37
|
|
|
// ========================================================================= |
38
|
|
|
|
39
|
|
|
const AMP_IFRAME_SCRIPT_URL = "https://cdn.ampproject.org/v0/amp-iframe-0.1.js"; |
40
|
|
|
const SEOMATIC_PLUGIN_HANDLE = 'seomatic'; |
41
|
|
|
|
42
|
|
|
// Public Methods |
43
|
|
|
// ========================================================================= |
44
|
|
|
|
45
|
|
|
/** |
|
|
|
|
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
|
public function includeHtmlBeacon() |
49
|
|
|
{ |
50
|
|
|
$view = Craft::$app->getView(); |
51
|
|
|
$script = $this->htmlBeaconScript(false); |
52
|
|
|
// Register the JavaScript |
53
|
|
|
$view->registerJs( |
54
|
|
|
$script, |
55
|
|
|
$view::POS_HEAD |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
|
|
|
|
60
|
|
|
* @param bool $headless |
|
|
|
|
61
|
|
|
* @param string|null $title |
|
|
|
|
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public function htmlBeaconScript(bool $headless = false, string $title = null): string |
66
|
|
|
{ |
67
|
|
|
$boomerangUrl = Craft::$app->assetManager->getPublishedUrl( |
68
|
|
|
'@nystudio107/webperf/assetbundles/boomerang/dist/js/boomerang-1.0.0.min.js', |
69
|
|
|
true |
70
|
|
|
); |
71
|
|
|
$boomerangTitle = $title ?? $this->getDocumentTitle(); |
72
|
|
|
$boomerangRequestId = Webperf::$settings->staticCachedSite ? null : Webperf::$requestUuid; |
73
|
|
|
$script = PluginTemplate::renderPluginTemplate( |
74
|
|
|
'_frontend/scripts/load-boomerang-iframe.twig', |
75
|
|
|
[ |
76
|
|
|
'headless' => $headless, |
77
|
|
|
'boomerangScriptUrl' => $boomerangUrl, |
78
|
|
|
'boomerangTitle' => $boomerangTitle, |
79
|
|
|
'boomerangRequestId' => $boomerangRequestId, |
80
|
|
|
], |
81
|
|
|
'jsMin' |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
return $script; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/* |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
|
public function includeAmpHtmlScript() |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$view = Craft::$app->getView(); |
93
|
|
|
$view->registerJsFile( |
94
|
|
|
self::AMP_IFRAME_SCRIPT_URL, |
95
|
|
|
[ |
96
|
|
|
'position' => $view::POS_HEAD, |
97
|
|
|
'async' => 'async', |
98
|
|
|
'custom-element' => 'amp-iframe', |
99
|
|
|
], |
100
|
|
|
'webperf-amp-iframe-script' |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/* |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
public function includeAmpHtmlBeacon() |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
$html = PluginTemplate::renderPluginTemplate( |
110
|
|
|
'_frontend/scripts/load-boomerang-amp-iframe.twig', |
111
|
|
|
[ |
112
|
|
|
'boomerangIframeUrl' => UrlHelper::siteUrl('/webperf/render/amp-iframe'), |
113
|
|
|
], |
114
|
|
|
'minify' |
115
|
|
|
); |
116
|
|
|
echo $html; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/* |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
|
|
public function ampHtmlIframe() |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
$boomerangUrl = Craft::$app->assetManager->getPublishedUrl( |
125
|
|
|
'@nystudio107/webperf/assetbundles/boomerang/dist/js/boomerang-1.0.0.min.js', |
126
|
|
|
true |
127
|
|
|
); |
128
|
|
|
$boomerangTitle = $this->getDocumentTitle(); |
129
|
|
|
$boomerangRequestId = Webperf::$settings->staticCachedSite ? null : Webperf::$requestUuid; |
130
|
|
|
$html = PluginTemplate::renderPluginTemplate( |
131
|
|
|
'_frontend/scripts/boomerang-amp-iframe-html.twig', |
132
|
|
|
[ |
133
|
|
|
'boomerangScriptUrl' => $boomerangUrl, |
134
|
|
|
'boomerangTitle' => $boomerangTitle, |
135
|
|
|
'boomerangRequestId' => $boomerangRequestId, |
136
|
|
|
], |
137
|
|
|
'minify' |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
return $html; |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
|
|
|
|
144
|
|
|
* @return void |
145
|
|
|
*/ |
146
|
|
|
public function includeCraftBeacon() |
147
|
|
|
{ |
148
|
|
|
// Filter out bot/spam requests via UserAgent |
149
|
|
|
if (Webperf::$settings->filterBotUserAgents) { |
150
|
|
|
$crawlerDetect = new CrawlerDetect; |
151
|
|
|
// Check the user agent of the current 'visitor' |
152
|
|
|
if ($crawlerDetect->isCrawler()) { |
153
|
|
|
return; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
$url = Webperf::$requestUrl ?? CraftDataSample::PLACEHOLDER_URL; |
157
|
|
|
// Get the site id |
158
|
|
|
try { |
159
|
|
|
$site = MultiSite::getSiteFromUrl($url); |
160
|
|
|
$siteId = $site->id; |
161
|
|
|
} catch (SiteNotFoundException $e) { |
162
|
|
|
$siteId = null; |
163
|
|
|
} |
164
|
|
|
$stats = Webperf::$plugin->profileTarget->stats; |
165
|
|
|
$request = Craft::$app->getRequest(); |
166
|
|
|
$pageLoad = (int)($stats['database']['duration'] |
167
|
|
|
+ $stats['twig']['duration'] |
168
|
|
|
+ $stats['other']['duration']); |
169
|
|
|
// Allocate a new DataSample, and fill it in |
170
|
|
|
$sample = new CraftDbDataSample([ |
|
|
|
|
171
|
|
|
'requestId' => Webperf::$requestUuid, |
172
|
|
|
'siteId' => $siteId, |
173
|
|
|
'url' => $url, |
174
|
|
|
'title' => $this->getDocumentTitle(), |
175
|
|
|
'queryString' => $request->getQueryString(), |
176
|
|
|
'pageLoad' => $pageLoad, |
177
|
|
|
'craftTotalMs' => $pageLoad, |
178
|
|
|
'craftDbMs' => (int)$stats['database']['duration'], |
179
|
|
|
'craftDbCnt' => (int)$stats['database']['count'], |
180
|
|
|
'craftTwigMs' => (int)$stats['twig']['duration'], |
181
|
|
|
'craftTwigCnt' => (int)$stats['twig']['count'], |
182
|
|
|
'craftOtherMs' => (int)$stats['other']['duration'], |
183
|
|
|
'craftOtherCnt' => (int)$stats['other']['count'], |
184
|
|
|
'craftTotalMemory' => (int)($stats['database']['memory'] |
185
|
|
|
+ $stats['twig']['memory'] |
186
|
|
|
+ $stats['other']['memory']), |
187
|
|
|
]); |
|
|
|
|
188
|
|
|
// Save the data sample |
189
|
|
|
Craft::debug('Saving Craft DataSample: '.print_r($sample->getAttributes(), true), __METHOD__); |
190
|
|
|
Webperf::$plugin->dataSamples->addDataSample($sample); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
|
|
|
|
194
|
|
|
* @return void |
195
|
|
|
*/ |
196
|
|
|
public function includeCraftErrorsBeacon() |
197
|
|
|
{ |
198
|
|
|
// Filter out bot/spam requests via UserAgent |
199
|
|
|
if (Webperf::$settings->filterBotUserAgents) { |
200
|
|
|
$crawlerDetect = new CrawlerDetect; |
201
|
|
|
// Check the user agent of the current 'visitor' |
202
|
|
|
if ($crawlerDetect->isCrawler()) { |
203
|
|
|
return; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
$url = Webperf::$requestUrl ?? CraftDataSample::PLACEHOLDER_URL; |
207
|
|
|
// Get the site id |
208
|
|
|
try { |
209
|
|
|
$site = MultiSite::getSiteFromUrl($url); |
210
|
|
|
$siteId = $site->id; |
211
|
|
|
} catch (SiteNotFoundException $e) { |
212
|
|
|
$siteId = null; |
213
|
|
|
} |
214
|
|
|
$messages = Webperf::$plugin->errorsTarget->pageErrors; |
215
|
|
|
$request = Craft::$app->getRequest(); |
216
|
|
|
// Allocate a new ErrorSample, and fill it in |
217
|
|
|
$sample = new CraftDbErrorSample([ |
|
|
|
|
218
|
|
|
'requestId' => Webperf::$requestUuid, |
219
|
|
|
'siteId' => $siteId, |
220
|
|
|
'url' => $url, |
221
|
|
|
'queryString' => $request->getQueryString(), |
222
|
|
|
'title' => $this->getDocumentTitle(), |
223
|
|
|
'pageErrors' => $messages, |
224
|
|
|
]); |
|
|
|
|
225
|
|
|
// Save the error sample |
226
|
|
|
Craft::debug('Saving Craft ErrorSample: '.print_r($sample->getAttributes(), true), __METHOD__); |
227
|
|
|
Webperf::$plugin->errorSamples->addErrorSample($sample); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
// Protected Methods |
231
|
|
|
// ========================================================================= |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Get the title of the currently rendering document |
235
|
|
|
* |
236
|
|
|
* @return string |
237
|
|
|
*/ |
238
|
|
|
protected function getDocumentTitle(): string |
239
|
|
|
{ |
240
|
|
|
// Default to whatever the view title is |
241
|
|
|
$view = Craft::$app->getView(); |
242
|
|
|
$docTitle = $view->title; |
243
|
|
|
if (empty($docTitle)) { |
244
|
|
|
$docTitle = ''; |
245
|
|
|
} |
246
|
|
|
// If SEOmatic is installed, get the title from it |
247
|
|
|
$seomatic = Craft::$app->getPlugins()->getPlugin(self::SEOMATIC_PLUGIN_HANDLE); |
248
|
|
|
if ($seomatic && Seomatic::$settings->renderEnabled) { |
249
|
|
|
$titleTag = Seomatic::$plugin->title->get('title'); |
250
|
|
|
if ($titleTag) { |
251
|
|
|
$titleArray = $titleTag->renderAttributes(); |
252
|
|
|
if (!empty($titleArray['title'])) { |
253
|
|
|
$docTitle = $titleArray['title']; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return $docTitle; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|