-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest-metrics.php
235 lines (203 loc) · 11.1 KB
/
request-metrics.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* @package Request Metrics
* @author Request Metrics <[email protected]>
* @license GPLv3
* @link https://requestmetrics.com/
*
* Plugin Name: Request Metrics
* Plugin URI: https://requestmetrics.com/docs/wordpress
* Description: Track real-user performance, fix Core Web Vitals, and boost your SEO.
* Version: 1.0.2
* Author: Request Metrics
* Author URI: https://requestmetrics.com/
* Text Domain: reqmtx
* License: GPLv3
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt
* Domain Path: /languages
* Requires PHP: 7.4
*/
// If this file is called directly, abort.
if ( !defined( 'ABSPATH' ) ) {
die( 'We\'re sorry, but you can not directly access this file.' );
}
/**
* Plugin Activation/Deactivation
*/
register_activation_hook(__FILE__, "reqmtx_plugin_activation");
function reqmtx_plugin_activation() {
$default_options = array(
'token' => ''
);
add_option('reqmtx_settings', $default_options);
}
register_deactivation_hook(__FILE__, "reqmtx_plugin_deactivation");
function reqmtx_plugin_deactivation() {
delete_option('reqmtx_settings');
}
/**
* Create admin settings
*/
add_action('admin_init', 'reqmtx_settings_init');
function reqmtx_settings_init() {
register_setting('reqmtx_plugin', 'reqmtx_settings');
add_settings_section(
'reqmtx_plugin_section',
__('Account Settings', 'reqmtx'),
'reqmtx_account_settings_section_render',
'reqmtx_plugin'
);
add_settings_field(
'token',
__('Site Token', 'reqmtx'),
'reqmtx_token_render',
'reqmtx_plugin',
'reqmtx_plugin_section'
);
}
function reqmtx_account_settings_section_render() {
echo esc_html(__('General settings to link your Request Metrics account.', 'reqmtx'));
}
function reqmtx_token_render() {
$options = get_option('reqmtx_settings');
?>
<input type='text' name='reqmtx_settings[token]' value='<?php echo esc_html($options['token']); ?>'
pattern="[a-z0-9]{7}:[a-z0-9]{7}" placeholder="xxxxxxx:yyyyyyy">
<p class="description">Get this from your <a href="https://app.requestmetrics.com/" target="_blank">Request Metrics Install Page</a>.
<?php
}
/**
* Create admin pages
*/
add_action("admin_menu", "reqmtx_create_admin_pages");
function reqmtx_create_admin_pages() {
global $submenu;
add_menu_page(
__('Request Metrics', 'reqmtx'),
"Request Metrics",
"manage_options",
"reqmtx_dashboard_page",
"reqmtx_render_dashboard_page",
"data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPCEtLSBHZW5lcmF0b3I6IEFkb2JlIElsbHVzdHJhdG9yIDI4LjEuMCwgU1ZHIEV4cG9ydCBQbHVnLUluIC4gU1ZHIFZlcnNpb246IDYuMDAgQnVpbGQgMCkgIC0tPgo8c3ZnIHZlcnNpb249IjEuMSIgaWQ9IkxheWVyXzEiIHhtbG5zOnhvZG09Imh0dHA6Ly93d3cuY29yZWwuY29tL2NvcmVsZHJhdy9vZG0vMjAwMyIKCSB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB4PSIwcHgiIHk9IjBweCIgdmlld0JveD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JYy0wLjksMS0xLjEsMi40LTAuNSwzLjZjOC4xLDE3LjIsNC4xLDI5LjMsMjIuOCw0NS42YzEwLjItMTIuNywxNS40LTE5LjQsMjMuOC0yOS40Yy00LjYtMTAtOS41LTI0LjctMTAuNy0zNC42CgkJYy0wLjItMS41LDAuNC0zLDEuNC00YzEuOS0yLjEsMjguMy0yOS41LDM5LjItMzEuOGM2LjMtMS40LDE0LjksMi4yLDExLDkuOWw5LjQsNy45YzkuNy04LjUsMTcuMi0xMy44LDI3LjYtMjIuNGwtMTYuMi02LjkKCQlDMTAyLjMsMTYzLjMsOTAuNywxNTMuNyw4Ni44LDE0OS40TDg2LjgsMTQ5LjR6IE0yNzAuOCwyNi4xYy03LjktMTcuNS0xMy0xMC45LTE5LjItMjQuOWMtMC4yLTAuNS0wLjctMC45LTEuMy0xcy0xLjEsMC0xLjUsMC40CgkJYy05LjEsOC44LTE2LDE1LjQtMjQuOCwyNC4zYzYuOCw0LjMsNC40LDkuOSw2LjIsMTIuMWM0LjcsNS43LDMuMSwxOC4xLTIuMSwyNC4ybC0yLjMsMS45bC0wLjcsMC4yYy0wLjksNC44LTEyLjQsMTIuMi0xNi42LDguMQoJCWMtMC41LDAuMi0xLDAuMy0xLjQsMC40Yy0xMC44LDIuNC02LjgtNy4yLTUuMS0xMC42bDAsMGMzLjMtNy43LTcuNy0zLjItMTAuNi0yYy0wLjYsMC4zLTEuMywwLjQtMS45LDAuNAoJCWMtMy43LDMuNy01LjYsNi4xLTkuMyw5LjdjLTIxLjgsMjEtNDAuNywzNS41LTYyLjYsNTIuNWMxMi44LDcuMSwxMy42LDguMiwyOS4yLDEyLjljMS45LDAuNiw4LjYsMi4xLDE2LjgsMy44CgkJQzIwNSwxMDAuOCwyMzYuOSw2NSwyNzAuOCwyNi4xTDI3MC44LDI2LjF6Ii8+CjwvZz4KPC9zdmc+Cg==");
add_submenu_page(
"reqmtx_dashboard_page",
__('Dashboard', 'reqmtx'),
"Dashboard",
"manage_options",
"reqmtx_dashboard_page",
"reqmtx_render_dashboard_page");
add_submenu_page(
"reqmtx_dashboard_page",
__('Settings', 'reqmtx'),
"Settings",
"manage_options",
"reqmtx_settings_page",
"reqmtx_render_settings_page");
}
function reqmtx_render_dashboard_page() {
$options = get_option('reqmtx_settings');
$parts = explode(":", $options['token'], 2);
if (count($parts) == 2) {
$appId = $parts[1];
}
?>
<div class="wrap">
<h1>Request Metrics Dashboard</h1>
<?php if (empty($appId)) { ?>
<h2>Please configure your token on the <a href="<?php echo esc_html(admin_url("admin.php?page=reqmtx_settings_page")); ?>">settings page</a>.</h2>
<?php } else { ?>
<a href="https://app.requestmetrics.com/app/<?php echo esc_html($appId) ?>?utm_source=wordpress_plugin" target="_blank" class="button button-primary" style="margin-top:20px;">
Launch Dashboard
</a>
<?php } ?>
</div>
<?php
}
function reqmtx_render_settings_page() {
?>
<div class="wrap">
<form action='options.php' method='post'>
<h1>Request Metrics Settings</h1>
<?php
settings_fields('reqmtx_plugin');
do_settings_sections('reqmtx_plugin');
submit_button();
?>
</form>
</div>
<?php
}
/**
* Install the agent into the footer for all requests
*/
add_action('wp_footer', 'reqmtx_install_js_snippet');
function reqmtx_install_js_snippet() {
$options = get_option('reqmtx_settings');
$page_group = reqmtx_get_page_group_name();
if (!empty($options['token'])) {
?>
<!-- Request Metrics -->
<script>
(function(t,e,n,r){function a(){return e&&e.now?e.now():null}if(!n.version){n._events=[];n._errors=[];n._metadata={};n._urlGroup=null;window.RM=n;n.install=function(e){n._options=e;var a=t.createElement("script");a.async=true;a.crossOrigin="anonymous";a.src=r;var o=t.getElementsByTagName("script")[0];o.parentNode.insertBefore(a,o)};n.identify=function(t,e){n._userId=t;n._identifyOptions=e};n.sendEvent=function(t,e){n._events.push({eventName:t,metadata:e,time:a()})};n.setUrlGroup=function(t){n._urlGroup=t};n.track=function(t,e){n._errors.push({error:t,metadata:e,time:a()})};n.addMetadata=function(t){n._metadata=Object.assign(n._metadata,t)}}})(document,window.performance,window.RM||{},"https://cdn.requestmetrics.com/agent/current/rm.js");
RM.install({
token: '<?php echo esc_js($options['token']); ?>'<?php if (!empty($page_group)) {?>,
urlGroup: '<?php echo esc_js($page_group); ?>'<?php } ?>
});
</script>
<?php
}
}
/**
* Track conversion events in WooCommerce
*/
add_action('woocommerce_thankyou', 'reqmtx_send_conversion');
function reqmtx_send_conversion($order_id) {
$order = wc_get_order($order_id);
$order_total = $order->get_total();
?>
<script>
setTimeout(() => {
var ORDER_TRACKED_KEY = "reqmtx_order_tracked_<?php echo esc_js($order_id); ?>";
if (!localStorage.getItem(ORDER_TRACKED_KEY)) {
window.RM && RM.sendEvent("purchase", {
isConversion: true,
conversionValue: <?php echo esc_js($order_total); ?>
});
localStorage.setItem(ORDER_TRACKED_KEY, true);
}
});
</script>
<?php
}
/**
* Helper function to try and identify the pageGroup
*/
function reqmtx_get_page_group_name() {
// is this a WooCommerce store?
if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
if (is_shop()) {
return 'shop';
}
if (is_product_category()) {
return 'category';
}
if (is_product()) {
return 'product';
}
if (is_cart()) {
return 'cart';
}
if (is_checkout()) {
return 'checkout';
}
if (is_order_received_page()) {
return 'order_received';
}
}
// Classic WordPress
if (is_page()) {
return get_page_template_slug();
}
return get_post_type();
}
?>