|
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 nystudio107\webperf\services; |
|
12
|
|
|
|
|
13
|
|
|
use nystudio107\webperf\models\DataSample; |
|
14
|
|
|
use nystudio107\webperf\Webperf; |
|
15
|
|
|
use nystudio107\webperf\helpers\PluginTemplate; |
|
16
|
|
|
|
|
17
|
|
|
use nystudio107\seomatic\Seomatic; |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
use Craft; |
|
|
|
|
|
|
20
|
|
|
use craft\base\Component; |
|
|
|
|
|
|
21
|
|
|
use craft\helpers\UrlHelper; |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
|
|
|
|
|
24
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
25
|
|
|
* @package Webperf |
|
|
|
|
|
|
26
|
|
|
* @since 1.0.0 |
|
|
|
|
|
|
27
|
|
|
*/ |
|
|
|
|
|
|
28
|
|
|
class Beacons extends Component |
|
29
|
|
|
{ |
|
30
|
|
|
// Constants |
|
31
|
|
|
// ========================================================================= |
|
32
|
|
|
|
|
33
|
|
|
const AMP_IFRAME_SCRIPT_URL = "https://cdn.ampproject.org/v0/amp-iframe-0.1.js"; |
|
34
|
|
|
const SEOMATIC_PLUGIN_HANDLE = 'seomatic'; |
|
35
|
|
|
|
|
36
|
|
|
// Public Methods |
|
37
|
|
|
// ========================================================================= |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
|
|
|
|
|
40
|
|
|
* @return void |
|
41
|
|
|
*/ |
|
42
|
|
|
public function includeHtmlBeacon() |
|
43
|
|
|
{ |
|
44
|
|
|
$view = Craft::$app->getView(); |
|
45
|
|
|
$script = $this->htmlBeaconScript(false); |
|
46
|
|
|
// Register the JavaScript |
|
47
|
|
|
$view->registerJs( |
|
48
|
|
|
$script, |
|
49
|
|
|
$view::POS_HEAD |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
|
|
|
|
|
54
|
|
|
* @param bool $headless |
|
|
|
|
|
|
55
|
|
|
* @param string|null $title |
|
|
|
|
|
|
56
|
|
|
* |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
public function htmlBeaconScript(bool $headless = false, string $title = null): string |
|
60
|
|
|
{ |
|
61
|
|
|
$boomerangUrl = Craft::$app->assetManager->getPublishedUrl( |
|
62
|
|
|
'@nystudio107/webperf/assetbundles/boomerang/dist/js/boomerang-1.0.0.min.js', |
|
63
|
|
|
true |
|
64
|
|
|
); |
|
65
|
|
|
$boomerangTitle = $title ?? $this->getDocumentTitle(); |
|
66
|
|
|
$script = PluginTemplate::renderPluginTemplate( |
|
67
|
|
|
'_frontend/scripts/load-boomerang-iframe.twig', |
|
68
|
|
|
[ |
|
69
|
|
|
'headless' => $headless, |
|
70
|
|
|
'boomerangScriptUrl' => $boomerangUrl, |
|
71
|
|
|
'boomerangTitle' => $boomerangTitle, |
|
72
|
|
|
'boomerangRequestId' => Webperf::$requestUuid, |
|
73
|
|
|
], |
|
74
|
|
|
'jsMin' |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
return $script; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/* |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
|
|
public function includeAmpHtmlScript() |
|
|
|
|
|
|
84
|
|
|
{ |
|
85
|
|
|
$view = Craft::$app->getView(); |
|
86
|
|
|
$view->registerJsFile( |
|
87
|
|
|
self::AMP_IFRAME_SCRIPT_URL, |
|
88
|
|
|
[ |
|
89
|
|
|
'position' => $view::POS_HEAD, |
|
90
|
|
|
'async' => 'async', |
|
91
|
|
|
'custom-element' => 'amp-iframe', |
|
92
|
|
|
], |
|
93
|
|
|
'webperf-amp-iframe-script' |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/* |
|
98
|
|
|
* @return void |
|
99
|
|
|
*/ |
|
100
|
|
|
public function includeAmpHtmlBeacon() |
|
|
|
|
|
|
101
|
|
|
{ |
|
102
|
|
|
$html = PluginTemplate::renderPluginTemplate( |
|
103
|
|
|
'_frontend/scripts/load-boomerang-amp-iframe.twig', |
|
104
|
|
|
[ |
|
105
|
|
|
'boomerangIframeUrl' => UrlHelper::siteUrl('/webperf/render/amp-iframe'), |
|
106
|
|
|
], |
|
107
|
|
|
'minify' |
|
108
|
|
|
); |
|
109
|
|
|
echo $html; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/* |
|
113
|
|
|
* @return void |
|
114
|
|
|
*/ |
|
115
|
|
|
public function ampHtmlIframe() |
|
|
|
|
|
|
116
|
|
|
{ |
|
117
|
|
|
$boomerangUrl = Craft::$app->assetManager->getPublishedUrl( |
|
118
|
|
|
'@nystudio107/webperf/assetbundles/boomerang/dist/js/boomerang-1.0.0.min.js', |
|
119
|
|
|
true |
|
120
|
|
|
); |
|
121
|
|
|
$boomerangTitle = $this->getDocumentTitle(); |
|
122
|
|
|
$html = PluginTemplate::renderPluginTemplate( |
|
123
|
|
|
'_frontend/scripts/boomerang-amp-iframe-html.twig', |
|
124
|
|
|
[ |
|
125
|
|
|
'boomerangScriptUrl' => $boomerangUrl, |
|
126
|
|
|
'boomerangTitle' => $boomerangTitle, |
|
127
|
|
|
'boomerangRequestId' => Webperf::$requestUuid, |
|
128
|
|
|
], |
|
129
|
|
|
'minify' |
|
130
|
|
|
); |
|
131
|
|
|
|
|
132
|
|
|
return $html; |
|
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
|
|
|
|
|
136
|
|
|
* @return void |
|
137
|
|
|
*/ |
|
138
|
|
|
public function includeCraftBeacon() |
|
139
|
|
|
{ |
|
140
|
|
|
$stats = Webperf::$plugin->profileTarget->stats; |
|
141
|
|
|
// Allocate a new DataSample, and fill it in |
|
142
|
|
|
$sample = new DataSample([ |
|
|
|
|
|
|
143
|
|
|
'requestId' => Webperf::$requestUuid, |
|
144
|
|
|
'url' => Webperf::$requestUrl ?? DataSample::PLACEHOLDER_URL, |
|
145
|
|
|
'craftTotalMs' => (int)($stats['database']['duration'] |
|
146
|
|
|
+ $stats['twig']['duration'] |
|
147
|
|
|
+ $stats['other']['duration']), |
|
148
|
|
|
'craftDbMs' => (int)$stats['database']['duration'], |
|
149
|
|
|
'craftDbCnt' => (int)$stats['database']['count'], |
|
150
|
|
|
'craftTwigMs' => (int)$stats['twig']['duration'], |
|
151
|
|
|
'craftTwigCnt' => (int)$stats['twig']['count'], |
|
152
|
|
|
'craftOtherMs' => (int)$stats['other']['duration'], |
|
153
|
|
|
'craftOtherCnt' => (int)$stats['other']['count'], |
|
154
|
|
|
'craftTotalMemory' => (int)($stats['database']['memory'] |
|
155
|
|
|
+ $stats['twig']['memory'] |
|
156
|
|
|
+ $stats['other']['memory']), |
|
157
|
|
|
]); |
|
|
|
|
|
|
158
|
|
|
// Save the data sample |
|
159
|
|
|
$sample->setScenario(DataSample::SCENARIO_CRAFT_BEACON); |
|
160
|
|
|
Craft::debug('Saving Craft DataSample: '.print_r($sample, true), __METHOD__); |
|
161
|
|
|
Webperf::$plugin->dataSamples->addDataSample($sample); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
// Protected Methods |
|
165
|
|
|
// ========================================================================= |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Get the title of the currently rendering document |
|
169
|
|
|
* |
|
170
|
|
|
* @return string |
|
171
|
|
|
*/ |
|
172
|
|
|
protected function getDocumentTitle(): string |
|
173
|
|
|
{ |
|
174
|
|
|
// Default to whatever the view title is |
|
175
|
|
|
$view = Craft::$app->getView(); |
|
176
|
|
|
$docTitle = $view->title; |
|
177
|
|
|
if (empty($docTitle)) { |
|
178
|
|
|
$docTitle = ''; |
|
179
|
|
|
} |
|
180
|
|
|
// If SEOmatic is installed, get the title from it |
|
181
|
|
|
$seomatic = Craft::$app->getPlugins()->getPlugin(self::SEOMATIC_PLUGIN_HANDLE); |
|
182
|
|
|
if ($seomatic && Seomatic::$settings->renderEnabled) { |
|
183
|
|
|
$titleTag = Seomatic::$plugin->title->get('title'); |
|
184
|
|
|
if ($titleTag) { |
|
185
|
|
|
$titleArray = $titleTag->renderAttributes(); |
|
186
|
|
|
if (!empty($titleArray['title'])) { |
|
187
|
|
|
$docTitle = $titleArray['title']; |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
return $docTitle; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|