|
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\helpers\PluginTemplate; |
|
14
|
|
|
|
|
15
|
|
|
use nystudio107\seomatic\Seomatic; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
use Craft; |
|
|
|
|
|
|
18
|
|
|
use craft\base\Component; |
|
|
|
|
|
|
19
|
|
|
use craft\helpers\UrlHelper; |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
|
|
|
|
|
22
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
23
|
|
|
* @package Webperf |
|
|
|
|
|
|
24
|
|
|
* @since 1.0.0 |
|
|
|
|
|
|
25
|
|
|
*/ |
|
|
|
|
|
|
26
|
|
|
class Beacons extends Component |
|
27
|
|
|
{ |
|
28
|
|
|
// Constants |
|
29
|
|
|
// ========================================================================= |
|
30
|
|
|
|
|
31
|
|
|
const AMP_IFRAME_SCRIPT_URL = "https://cdn.ampproject.org/v0/amp-iframe-0.1.js"; |
|
32
|
|
|
const SEOMATIC_PLUGIN_HANDLE = 'seomatic'; |
|
33
|
|
|
|
|
34
|
|
|
// Public Methods |
|
35
|
|
|
// ========================================================================= |
|
36
|
|
|
|
|
37
|
|
|
/* |
|
38
|
|
|
* @return void |
|
39
|
|
|
*/ |
|
40
|
|
|
public function includeHtmlBeacon() |
|
|
|
|
|
|
41
|
|
|
{ |
|
42
|
|
|
$view = Craft::$app->getView(); |
|
43
|
|
|
$boomerangUrl = Craft::$app->assetManager->getPublishedUrl( |
|
44
|
|
|
'@nystudio107/webperf/assetbundles/boomerang/dist/js/boomerang-1.0.0.min.js', |
|
45
|
|
|
true |
|
46
|
|
|
); |
|
47
|
|
|
$boomerangTitle = $this->getDocumentTitle(); |
|
48
|
|
|
$script = PluginTemplate::renderPluginTemplate( |
|
49
|
|
|
'_frontend/scripts/load-boomerang-iframe.twig', |
|
50
|
|
|
[ |
|
51
|
|
|
'boomerangScriptUrl' => $boomerangUrl, |
|
52
|
|
|
'boomerangTitle' => $boomerangTitle, |
|
53
|
|
|
] |
|
54
|
|
|
); |
|
55
|
|
|
$view->registerJs( |
|
56
|
|
|
$script, |
|
57
|
|
|
$view::POS_HEAD |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/* |
|
62
|
|
|
* @return void |
|
63
|
|
|
*/ |
|
64
|
|
|
public function includeAmpHtmlScript() |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
|
|
$view = Craft::$app->getView(); |
|
67
|
|
|
$view->registerJsFile( |
|
68
|
|
|
self::AMP_IFRAME_SCRIPT_URL, |
|
69
|
|
|
[ |
|
70
|
|
|
'position' => $view::POS_HEAD, |
|
71
|
|
|
'async' => 'async', |
|
72
|
|
|
'custom-element' => 'amp-iframe', |
|
73
|
|
|
], |
|
74
|
|
|
'webperf-amp-iframe-script' |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/* |
|
79
|
|
|
* @return void |
|
80
|
|
|
*/ |
|
81
|
|
|
public function includeAmpHtmlBeacon() |
|
|
|
|
|
|
82
|
|
|
{ |
|
83
|
|
|
$html = PluginTemplate::renderPluginTemplate( |
|
84
|
|
|
'_frontend/scripts/load-boomerang-amp-iframe.twig', |
|
85
|
|
|
[ |
|
86
|
|
|
'boomerangIframeUrl' => UrlHelper::siteUrl('/webperf/render/amp-iframe'), |
|
87
|
|
|
] |
|
88
|
|
|
); |
|
89
|
|
|
echo $html; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/* |
|
93
|
|
|
* @return void |
|
94
|
|
|
*/ |
|
95
|
|
|
public function ampHtmlIframe() |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
|
|
$boomerangUrl = Craft::$app->assetManager->getPublishedUrl( |
|
98
|
|
|
'@nystudio107/webperf/assetbundles/boomerang/dist/js/boomerang-1.0.0.min.js', |
|
99
|
|
|
true |
|
100
|
|
|
); |
|
101
|
|
|
$html = PluginTemplate::renderPluginTemplate( |
|
102
|
|
|
'_frontend/scripts/boomerang-amp-iframe-html.twig', |
|
103
|
|
|
[ |
|
104
|
|
|
'boomerangScriptUrl' => $boomerangUrl, |
|
105
|
|
|
] |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
return $html; |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// Protected Methods |
|
112
|
|
|
// ========================================================================= |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get the title of the currently rendering document |
|
116
|
|
|
* |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function getDocumentTitle(): string |
|
120
|
|
|
{ |
|
121
|
|
|
// Default to whatever the view title is |
|
122
|
|
|
$view = Craft::$app->getView(); |
|
123
|
|
|
$docTitle = $view->title; |
|
124
|
|
|
// If SEOmatic is installed, get the title from it |
|
125
|
|
|
$seomatic = Craft::$app->getPlugins()->getPlugin(self::SEOMATIC_PLUGIN_HANDLE); |
|
126
|
|
|
if ($seomatic && Seomatic::$settings->renderEnabled) { |
|
127
|
|
|
$titleTag = Seomatic::$plugin->title->get('title'); |
|
128
|
|
|
if ($titleTag) { |
|
129
|
|
|
$titleArray = $titleTag->renderAttributes(); |
|
130
|
|
|
if (!empty($titleArray['title'])) { |
|
131
|
|
|
$docTitle = $titleArray['title']; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $docTitle; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|