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
|
|
|
$boomerangUrl = Craft::$app->assetManager->getPublishedUrl( |
46
|
|
|
'@nystudio107/webperf/assetbundles/boomerang/dist/js/boomerang-1.0.0.min.js', |
47
|
|
|
true |
48
|
|
|
); |
49
|
|
|
$boomerangTitle = $this->getDocumentTitle(); |
50
|
|
|
$script = PluginTemplate::renderPluginTemplate( |
51
|
|
|
'_frontend/scripts/load-boomerang-iframe.twig', |
52
|
|
|
[ |
53
|
|
|
'boomerangScriptUrl' => $boomerangUrl, |
54
|
|
|
'boomerangTitle' => $boomerangTitle, |
55
|
|
|
'boomerangRequestId' => Webperf::$requestUuid, |
56
|
|
|
] |
57
|
|
|
); |
58
|
|
|
$view->registerJs( |
59
|
|
|
$script, |
60
|
|
|
$view::POS_HEAD |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/* |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
public function includeAmpHtmlScript() |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$view = Craft::$app->getView(); |
70
|
|
|
$view->registerJsFile( |
71
|
|
|
self::AMP_IFRAME_SCRIPT_URL, |
72
|
|
|
[ |
73
|
|
|
'position' => $view::POS_HEAD, |
74
|
|
|
'async' => 'async', |
75
|
|
|
'custom-element' => 'amp-iframe', |
76
|
|
|
], |
77
|
|
|
'webperf-amp-iframe-script' |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/* |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
public function includeAmpHtmlBeacon() |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$html = PluginTemplate::renderPluginTemplate( |
87
|
|
|
'_frontend/scripts/load-boomerang-amp-iframe.twig', |
88
|
|
|
[ |
89
|
|
|
'boomerangIframeUrl' => UrlHelper::siteUrl('/webperf/render/amp-iframe'), |
90
|
|
|
] |
91
|
|
|
); |
92
|
|
|
echo $html; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/* |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
|
public function ampHtmlIframe() |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$boomerangUrl = Craft::$app->assetManager->getPublishedUrl( |
101
|
|
|
'@nystudio107/webperf/assetbundles/boomerang/dist/js/boomerang-1.0.0.min.js', |
102
|
|
|
true |
103
|
|
|
); |
104
|
|
|
$boomerangTitle = $this->getDocumentTitle(); |
105
|
|
|
$html = PluginTemplate::renderPluginTemplate( |
106
|
|
|
'_frontend/scripts/boomerang-amp-iframe-html.twig', |
107
|
|
|
[ |
108
|
|
|
'boomerangScriptUrl' => $boomerangUrl, |
109
|
|
|
'boomerangTitle' => $boomerangTitle, |
110
|
|
|
'boomerangRequestId' => Webperf::$requestUuid, |
111
|
|
|
] |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
return $html; |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
|
|
|
|
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
|
|
public function includeCraftBeacon() |
121
|
|
|
{ |
122
|
|
|
if (Webperf::$beaconIncluded) { |
123
|
|
|
$stats = Webperf::$plugin->profileTarget->stats; |
124
|
|
|
// Allocate a new DataSample, and fill it in |
125
|
|
|
$sample = new DataSample([ |
|
|
|
|
126
|
|
|
'requestId' => Webperf::$requestUuid, |
127
|
|
|
'url' =>DataSample::PLACEHOLDER_URL, |
128
|
|
|
'craftTotalMs' => (int)($stats['database']['duration'] |
129
|
|
|
+ $stats['twig']['duration'] |
130
|
|
|
+ $stats['other']['duration']), |
131
|
|
|
'craftDbMs' => (int)$stats['database']['duration'], |
132
|
|
|
'craftDbCnt' => (int)$stats['database']['count'], |
133
|
|
|
'craftTwigMs' => (int)$stats['twig']['duration'], |
134
|
|
|
'craftTwigCnt' => (int)$stats['twig']['count'], |
135
|
|
|
'craftOtherMs' => (int)$stats['other']['duration'], |
136
|
|
|
'craftOtherCnt' => (int)$stats['other']['count'], |
137
|
|
|
'craftTotalMemory' => (int)($stats['database']['memory'] |
138
|
|
|
+ $stats['twig']['memory'] |
139
|
|
|
+ $stats['other']['memory']), |
140
|
|
|
]); |
|
|
|
|
141
|
|
|
// Save the data sample |
142
|
|
|
$sample->setScenario(DataSample::SCENARIO_CRAFT_BEACON); |
143
|
|
|
Craft::debug('Saving Craft DataSample: '.print_r($sample, true), __METHOD__); |
144
|
|
|
Webperf::$plugin->dataSamples->addDataSample($sample); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// Protected Methods |
149
|
|
|
// ========================================================================= |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get the title of the currently rendering document |
153
|
|
|
* |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
|
|
protected function getDocumentTitle(): string |
157
|
|
|
{ |
158
|
|
|
// Default to whatever the view title is |
159
|
|
|
$view = Craft::$app->getView(); |
160
|
|
|
$docTitle = $view->title; |
161
|
|
|
// If SEOmatic is installed, get the title from it |
162
|
|
|
$seomatic = Craft::$app->getPlugins()->getPlugin(self::SEOMATIC_PLUGIN_HANDLE); |
163
|
|
|
if ($seomatic && Seomatic::$settings->renderEnabled) { |
164
|
|
|
$titleTag = Seomatic::$plugin->title->get('title'); |
165
|
|
|
if ($titleTag) { |
166
|
|
|
$titleArray = $titleTag->renderAttributes(); |
167
|
|
|
if (!empty($titleArray['title'])) { |
168
|
|
|
$docTitle = $titleArray['title']; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $docTitle; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|