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\Webperf; |
14
|
|
|
use nystudio107\webperf\helpers\PluginTemplate; |
15
|
|
|
use nystudio107\webperf\assetbundles\boomerang\BoomerangAsset; |
16
|
|
|
|
17
|
|
|
use Craft; |
|
|
|
|
18
|
|
|
use craft\base\Component; |
|
|
|
|
19
|
|
|
|
20
|
|
|
use yii\base\InvalidConfigException; |
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
|
|
|
|
23
|
|
|
* @author nystudio107 |
|
|
|
|
24
|
|
|
* @package Webperf |
|
|
|
|
25
|
|
|
* @since 1.0.0 |
|
|
|
|
26
|
|
|
*/ |
|
|
|
|
27
|
|
|
class Beacons extends Component |
28
|
|
|
{ |
29
|
|
|
// Constants |
30
|
|
|
// ========================================================================= |
31
|
|
|
|
32
|
|
|
const AMP_IFRAME_SCRIPT_URL = "https://cdn.ampproject.org/v0/amp-iframe-0.1.js"; |
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
|
|
|
$script = PluginTemplate::renderPluginTemplate( |
48
|
|
|
'_frontend/scripts/load-boomerang-iframe.twig', |
49
|
|
|
[ |
50
|
|
|
'boomerangScriptUrl' => $boomerangUrl, |
51
|
|
|
] |
52
|
|
|
); |
53
|
|
|
$view->registerJs( |
54
|
|
|
$script, |
55
|
|
|
$view::POS_HEAD |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/* |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
public function includeAmpHtmlScript() |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$view = Craft::$app->getView(); |
65
|
|
|
$view->registerJsFile( |
66
|
|
|
self::AMP_IFRAME_SCRIPT_URL, |
67
|
|
|
[ |
68
|
|
|
'position' => $view::POS_HEAD, |
69
|
|
|
'async' => 'async', |
70
|
|
|
'custom-element' => 'amp-iframe', |
71
|
|
|
], |
72
|
|
|
'webperf-amp-iframe-script' |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/* |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
|
|
public function includeAmpHtmlBeacon() |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$view = Craft::$app->getView(); |
|
|
|
|
82
|
|
|
$boomerangUrl = Craft::$app->assetManager->getPublishedUrl( |
83
|
|
|
'@nystudio107/webperf/assetbundles/boomerang/dist/js/boomerang-1.0.0.min.js', |
84
|
|
|
true |
85
|
|
|
); |
86
|
|
|
$html = PluginTemplate::renderPluginTemplate( |
87
|
|
|
'_frontend/scripts/load-boomerang-amp-iframe.twig', |
88
|
|
|
[ |
89
|
|
|
'boomerangScriptUrl' => $boomerangUrl, |
90
|
|
|
] |
91
|
|
|
); |
92
|
|
|
echo $html; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|