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 Craft; |
|
|
|
|
16
|
|
|
use craft\base\Component; |
|
|
|
|
17
|
|
|
use craft\helpers\UrlHelper; |
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
|
|
|
|
20
|
|
|
* @author nystudio107 |
|
|
|
|
21
|
|
|
* @package Webperf |
|
|
|
|
22
|
|
|
* @since 1.0.0 |
|
|
|
|
23
|
|
|
*/ |
|
|
|
|
24
|
|
|
class Beacons extends Component |
25
|
|
|
{ |
26
|
|
|
// Constants |
27
|
|
|
// ========================================================================= |
28
|
|
|
|
29
|
|
|
const AMP_IFRAME_SCRIPT_URL = "https://cdn.ampproject.org/v0/amp-iframe-0.1.js"; |
30
|
|
|
|
31
|
|
|
// Public Methods |
32
|
|
|
// ========================================================================= |
33
|
|
|
|
34
|
|
|
/* |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
public function includeHtmlBeacon() |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$view = Craft::$app->getView(); |
40
|
|
|
$boomerangUrl = Craft::$app->assetManager->getPublishedUrl( |
41
|
|
|
'@nystudio107/webperf/assetbundles/boomerang/dist/js/boomerang-1.0.0.min.js', |
42
|
|
|
true |
43
|
|
|
); |
44
|
|
|
$script = PluginTemplate::renderPluginTemplate( |
45
|
|
|
'_frontend/scripts/load-boomerang-iframe.twig', |
46
|
|
|
[ |
47
|
|
|
'boomerangScriptUrl' => $boomerangUrl, |
48
|
|
|
] |
49
|
|
|
); |
50
|
|
|
$view->registerJs( |
51
|
|
|
$script, |
52
|
|
|
$view::POS_HEAD |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/* |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
public function includeAmpHtmlScript() |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$view = Craft::$app->getView(); |
62
|
|
|
$view->registerJsFile( |
63
|
|
|
self::AMP_IFRAME_SCRIPT_URL, |
64
|
|
|
[ |
65
|
|
|
'position' => $view::POS_HEAD, |
66
|
|
|
'async' => 'async', |
67
|
|
|
'custom-element' => 'amp-iframe', |
68
|
|
|
], |
69
|
|
|
'webperf-amp-iframe-script' |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/* |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
public function includeAmpHtmlBeacon() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$html = PluginTemplate::renderPluginTemplate( |
79
|
|
|
'_frontend/scripts/load-boomerang-amp-iframe.twig', |
80
|
|
|
[ |
81
|
|
|
'boomerangIframeUrl' => UrlHelper::siteUrl('/webperf/render/amp-iframe'), |
82
|
|
|
] |
83
|
|
|
); |
84
|
|
|
echo $html; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/* |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
|
public function ampHtmlIframe() |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$boomerangUrl = Craft::$app->assetManager->getPublishedUrl( |
93
|
|
|
'@nystudio107/webperf/assetbundles/boomerang/dist/js/boomerang-1.0.0.min.js', |
94
|
|
|
true |
95
|
|
|
); |
96
|
|
|
$html = PluginTemplate::renderPluginTemplate( |
97
|
|
|
'_frontend/scripts/boomerang-amp-iframe-html.twig', |
98
|
|
|
[ |
99
|
|
|
'boomerangScriptUrl' => $boomerangUrl, |
100
|
|
|
] |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
return $html; |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
|