-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajdg-matomo-tracker-functions.php
395 lines (337 loc) · 18.1 KB
/
ajdg-matomo-tracker-functions.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
/* ------------------------------------------------------------------------------------
* COPYRIGHT NOTICE
* Copyright 2020-2023 Arnan de Gans. All Rights Reserved.
* COPYRIGHT NOTICES AND ALL THE COMMENTS SHOULD REMAIN INTACT.
* By using this code you agree to indemnify Arnan de Gans from any
* liability that might arise from its use.
------------------------------------------------------------------------------------ */
defined('ABSPATH') or die();
/*-------------------------------------------------------------
Name: ajdg_matomo_dashboard_styles
Since: 1.0
-------------------------------------------------------------*/
function ajdg_matomo_dashboard_styles() {
wp_enqueue_style('ajdg-matomo-admin-stylesheet', plugins_url('library/dashboard.css', __FILE__));
}
/*-------------------------------------------------------------
Name: ajdg_matomo_activate
Since: 1.0
-------------------------------------------------------------*/
function ajdg_matomo_activate() {
// Defaults
update_option('ajdg_matomo_siteid', '');
update_option('ajdg_matomo_siteurl', '');
update_option('ajdg_matomo_active', 'no');
update_option('ajdg_matomo_track_feed_clicks', 'no');
update_option('ajdg_matomo_track_feed_impressions', 'no');
update_option('ajdg_matomo_track_error_pages', 'no');
update_option('ajdg_matomo_track_incognito', 'no');
update_option('ajdg_matomo_heartbeat_enable', 'no');
update_option('ajdg_matomo_wc_downloads', 'no');
update_option('ajdg_matomo_high_accuracy', 'no');
update_option('ajdg_matomo_hide_review', current_time('timestamp'));
}
/*-------------------------------------------------------------
Name: ajdg_matomo_deactivate
Since: 1.0
-------------------------------------------------------------*/
function ajdg_matomo_deactivate() {
// Delete data
delete_option('ajdg_matomo_siteid');
delete_option('ajdg_matomo_siteurl');
delete_option('ajdg_matomo_active');
delete_option('ajdg_matomo_track_feed_clicks');
delete_option('ajdg_matomo_track_feed_impressions');
delete_option('ajdg_matomo_track_error_pages');
delete_option('ajdg_matomo_track_incognito');
delete_option('ajdg_matomo_heartbeat_enable');
delete_option('ajdg_matomo_wc_downloads');
delete_option('ajdg_matomo_high_accuracy');
delete_option('ajdg_matomo_hide_review');
}
/*-------------------------------------------------------------
Name: ajdg_matomo_save_settings
Since: 1.0
-------------------------------------------------------------*/
function ajdg_matomo_save_settings() {
if(wp_verify_nonce($_POST['matomo_nonce'],'matomo_nonce')) {
$siteid = $siteurl = $track_active = $track_feed = $track_error_pages = $track_heartbeat = $track_high_accuracy = '';
if(isset($_POST['matomo_siteid'])) $siteid = sanitize_text_field(trim($_POST['matomo_siteid'], "\t\n "));
if(isset($_POST['matomo_siteurl'])) $siteurl = filter_var(trim($_POST['matomo_siteurl'], "\t\n "), FILTER_SANITIZE_URL);
if(isset($_POST['matomo_tracker_active'])) $track_active = sanitize_key($_POST['matomo_tracker_active']);
if(isset($_POST['matomo_feed_clicks'])) $track_feed_clicks = sanitize_key($_POST['matomo_feed_clicks']);
if(isset($_POST['matomo_error_pages'])) $track_error_pages = sanitize_key($_POST['matomo_error_pages']);
if(isset($_POST['matomo_incognito'])) $track_incognito = sanitize_key($_POST['matomo_incognito']);
if(isset($_POST['matomo_heartbeat'])) $track_heartbeat = sanitize_key($_POST['matomo_heartbeat']);
if(isset($_POST['matomo_wc_downloads'])) $track_wc_downloads = sanitize_key($_POST['matomo_wc_downloads']);
if(isset($_POST['matomo_feed_impressions'])) $track_feed_impressions = sanitize_key($_POST['matomo_feed_impressions']);
if(isset($_POST['matomo_accuracy'])) $track_high_accuracy = sanitize_key($_POST['matomo_accuracy']);
// Cleanup
$siteid = (is_numeric($siteid)) ? $siteid : '';
$siteurl = str_ireplace(array('/index.php', '/matomo.php', '/piwik.php'), '', strtolower($siteurl)); // Remove files
$siteurl = preg_replace('/\?idsite=\d/i', '', $siteurl); // Remove idsite parameter
$siteurl = trim($siteurl, "\/"); // Remove trailing slashes
$siteurl = (strlen($siteurl) > 0) ? $siteurl : '';
$track_active = ($track_active == 'yes') ? 'yes' : 'no';
$track_feed_clicks = ($track_feed_clicks == 'yes') ? 'yes' : 'no';
$track_error_pages = ($track_error_pages == 'yes') ? 'yes' : 'no';
$track_incognito = ($track_incognito == 'yes') ? 'yes' : 'no';
$track_heartbeat = ($track_heartbeat == 'yes') ? 'yes' : 'no';
$track_wc_downloads = ($track_wc_downloads == 'yes') ? 'yes' : 'no';
$track_feed_impressions = ($track_feed_impressions == 'yes') ? 'yes' : 'no';
$track_high_accuracy = ($track_high_accuracy == 'yes') ? 'yes' : 'no';
// Process and response
update_option('ajdg_matomo_siteid', $siteid);
update_option('ajdg_matomo_siteurl', $siteurl);
update_option('ajdg_matomo_active', $track_active);
update_option('ajdg_matomo_track_feed_clicks', $track_feed_clicks);
update_option('ajdg_matomo_track_error_pages', $track_error_pages);
update_option('ajdg_matomo_track_incognito', $track_incognito);
update_option('ajdg_matomo_heartbeat_enable', $track_heartbeat);
update_option('ajdg_matomo_wc_downloads', $track_wc_downloads);
update_option('ajdg_matomo_track_feed_impressions', $track_feed_impressions);
update_option('ajdg_matomo_high_accuracy', $track_high_accuracy);
ajdg_matomo_return('matomo-tracker', 100);
exit;
} else {
ajdg_matomo_nonce_error();
exit;
}
}
/*-------------------------------------------------------------
Name: ajdg_matomo_check_config
Since: 1.0
-------------------------------------------------------------*/
function ajdg_matomo_check_config() {
$siteid = get_option('ajdg_matomo_siteid');
if(!$siteid) update_option('ajdg_matomo_siteid', '');
$siteurl = get_option('ajdg_matomo_siteurl');
if(!$siteurl) update_option('ajdg_matomo_siteurl', '');
$track_active = get_option('ajdg_matomo_active');
if(!$track_active) update_option('ajdg_matomo_active', 'no');
$track_feed_click = get_option('ajdg_matomo_track_feed_clicks');
if(!$track_feed_click) update_option('ajdg_matomo_track_feed_clicks', 'yes');
$track_error_pages = get_option('ajdg_matomo_track_error_pages');
if(!$track_error_pages) update_option('ajdg_matomo_track_error_pages', 'yes');
$track_incognito = get_option('ajdg_matomo_track_incognito');
if(!$track_incognito) update_option('ajdg_matomo_track_incognito', 'no');
$heartbeat_enable = get_option('ajdg_matomo_heartbeat_enable');
if(!$heartbeat_enable) update_option('ajdg_matomo_heartbeat_enable', 'no');
$track_wc_downloads = get_option('ajdg_matomo_wc_downloads');
if(!$track_wc_downloads) update_option('ajdg_matomo_wc_downloads', 'no');
$track_feed_impressions = get_option('ajdg_matomo_track_feed_impressions');
if(!$track_feed_impressions) update_option('ajdg_matomo_track_feed_impressions', 'no');
$high_accuracy = get_option('ajdg_matomo_high_accuracy');
if(!$high_accuracy) update_option('ajdg_matomo_high_accuracy', 'no');
}
/*-------------------------------------------------------------
Name: ajdg_matomo_notifications_dashboard
Since: 1.0
-------------------------------------------------------------*/
function ajdg_matomo_notifications_dashboard() {
global $current_user;
$displayname = (strlen($current_user->user_firstname) > 0) ? $current_user->user_firstname : $current_user->display_name;
if(isset($_GET['hide'])) {
if($_GET['hide'] == 1) update_option('ajdg_matomo_hide_review', 1);
}
$review_banner = get_option('ajdg_matomo_hide_review');
if($review_banner != 1 AND $review_banner < (current_time('timestamp') - (7 * DAY_IN_SECONDS))) {
echo '<div class="ajdg-notification notice" style="">';
echo ' <div class="ajdg-notification-logo" style="background-image: url(\''.plugins_url('/images/notification.png', __FILE__).'\');"><span></span></div>';
echo ' <div class="ajdg-notification-message">Hey there <strong>'.$displayname.'</strong>! You have been using <strong>AJdG Matomo Tracker</strong> for a few days.<br />If you like the plugin, please <strong>write a review</strong>.<br />If you have questions, complaints or something else that does not belong in a review, please use the <a href="https://wordpress.org/support/plugin/matomo-analytics/">support forum</a>!</div>';
echo ' <div class="ajdg-notification-cta">';
echo ' <a href="https://wordpress.org/support/plugin/matomo-analytics/reviews?rate=5#postform" class="ajdg-notification-act button-primary">Write a Review</a>';
echo ' <a href="tools.php?page=matomo-tracker&hide=1" class="ajdg-notification-dismiss">Maybe later</a>';
echo ' </div>';
echo '</div>';
}
$has_error = ajdg_matomo_has_error();
if($has_error) {
echo '<div class="ajdg-notification notice" style="">';
echo ' <div class="ajdg-notification-logo" style="background-image: url(\''.plugins_url('/images/notification.png', __FILE__).'\');"><span></span></div>';
echo ' <div class="ajdg-notification-message"><strong>Analytics for Matomo</strong> has detected '._n('one issue that requires', 'several issues that require', count($has_error), 'ajdg-matomo-tracker').' '.__('your attention:', 'ajdg-matomo-tracker').'<br />';
foreach($has_error as $error => $message) {
echo '» '.$message.'<br />';
}
echo ' <br /><a href="'.admin_url('/tools.php?page=matomo-tracker').'">'.__('Check your settings', 'ajdg-matomo-tracker').'</a>!';
echo ' </div>';
echo '</div>';
}
}
/*-------------------------------------------------------------
Name: ajdg_matomo_has_error
Since: 1.0
-------------------------------------------------------------*/
function ajdg_matomo_has_error() {
$siteid = get_option('ajdg_matomo_siteid');
$siteurl = get_option('ajdg_matomo_siteurl');
$track_active = get_option('ajdg_matomo_active');
if($track_active == 'yes' AND (empty($siteid) OR empty($siteurl))) {
$error['matomo_site_details'] = __('You activated the tracker but the Site ID and/or Site URL is empty.', 'ajdg-matomo-tracker');
}
$error = (isset($error) AND is_array($error)) ? $error : false;
return $error;
}
/*-------------------------------------------------------------
Name: ajdg_matomo_action_links
Since: 1.0
-------------------------------------------------------------*/
function ajdg_matomo_action_links($links) {
$links['ajdg-matomo-settings'] = sprintf('<a href="%s">%s</a>', admin_url('tools.php?page=matomo-tracker'), 'Settings');
$links['ajdg-matomo-help'] = sprintf('<a href="%s" target="_blank">%s</a>', 'https://ajdg.solutions/forums/forum/matomo-tracker/?mtm_campaign=matomo_tracker', 'Support');
$links['ajdg-matomo-ajdg'] = sprintf('<a href="%s" target="_blank">%s</a>', 'https://ajdg.solutions/?mtm_campaign=matomo_tracker', 'ajdg.solutions');
return $links;
}
/*-------------------------------------------------------------
Name: ajdg_matomo_return
Since: 1.0
-------------------------------------------------------------*/
function ajdg_matomo_return($page, $status, $args = null) {
if(strlen($page) > 0 AND ($status > 0 AND $status < 1000)) {
$defaults = array(
'status' => $status
);
$arguments = wp_parse_args($args, $defaults);
$redirect = 'tools.php?page=' . $page . '&'.http_build_query($arguments);
} else {
$redirect = 'tools.php?page=matomo-tracker';
}
wp_redirect($redirect);
}
/*-------------------------------------------------------------
Name: ajdg_matomo_status
Since: 1.0
-------------------------------------------------------------*/
function ajdg_matomo_status($status) {
switch($status) {
case '100' :
echo '<div class="updated"><p>'.__('Settings saved', 'ajdg-matomo-tracker').'</p></div>';
break;
default :
echo '<div class="error"><p>'.__('Unexpected error', 'ajdg-matomo-tracker').'</p></div>';
break;
}
}
/*-------------------------------------------------------------
Name: ajdg_matomo_nonce_error
Since: 1.0
-------------------------------------------------------------*/
function ajdg_matomo_nonce_error() {
echo ' <h2 style="text-align: center;">'.__('Oh no! Something went wrong!', 'ajdg-matomo-tracker').'</h2>';
echo ' <p style="text-align: center;">'.__('WordPress was unable to verify the authenticity of the url you have clicked. Verify if the url used is valid or log in via your browser.', 'ajdg-matomo-tracker').'</p>';
echo ' <p style="text-align: center;">'.__('If you have received the url you want to visit via email, you are being tricked!', 'ajdg-matomo-tracker').'</p>';
echo ' <p style="text-align: center;">'.__('Contact support if the issue persists:', 'ajdg-matomo-tracker').' <a href="https://ajdg.solutions/forums/forum/matomo-tracker/?mtm_campaign=matomo_tracker" title="AJdG Solutions Support" target="_blank">Support forum</a>.</p>';
}
/*-------------------------------------------------------------
Name: ajdg_matomo_useragent_filter
Since: 1.0
-------------------------------------------------------------*/
function ajdg_matomo_useragent_filter($user_agent) {
$blocked_user_agents = array(
'bot', 'crawler', 'spider',
'exabot', 'alexa', 'findlinks', 'ia_archiver', 'inktomi',
'slurp', 'YahooSeeker', 'yahoo',
'adsbot-google', 'googlebot', 'googleproducer', 'google-site-verification', 'google-test', 'mediapartners-google', 'feedfetcher-google',
'baidu', 'yandex', 'yandex', 'YandexImages',
'bingbot', 'bingpreview', 'msnbot',
'duckduckgo', 'aolbuild',
'sosospider', 'sosoimagespider', 'sogou', 'teoma',
'facebookexternalhit', 'facebook',
'TECNOSEEK', 'TechnoratiSnoop'
);
// You're a bot
if(preg_match('/'.implode('|', $blocked_user_agents).'/i', $user_agent)) return false;
// Or not...
return true;
}
/*-------------------------------------------------------------
Name: ajdg_matomo_feed_impressions
Since: 1.1
-------------------------------------------------------------*/
function ajdg_matomo_feed_impressions($content) {
global $post;
if(is_feed()) {
$user_agent = (isset($_SERVER['HTTP_USER_AGENT'])) ? trim(strip_tags(htmlspecialchars($_SERVER['HTTP_USER_AGENT']))) : 'n/a';
if(ajdg_matomo_useragent_filter($user_agent)) {
$siteid = get_option('ajdg_matomo_siteid');
$siteurl = get_option('ajdg_matomo_siteurl');
$protocol = (is_ssl()) ? 'https://' : 'http://';
$http_host = (isset($_SERVER['HTTP_HOST'])) ? trim(strip_tags(htmlspecialchars($_SERVER['HTTP_HOST']))) : 'n/a';
$request_url = (isset($_SERVER['REQUEST_URI'])) ? esc_url_raw($protocol.$http_host.$_SERVER['REQUEST_URI']) : 'n/a';
$title = $post->post_name;
$posturl = get_permalink($post->ID);
if(strpos($request_url, get_bloginfo('rss2_url')) !== false) {
$feed_type = get_bloginfo('rss2_url');
} elseif(strpos($request_url, get_bloginfo('atom_url')) !== false) {
$feed_type = get_bloginfo('atom_url');
} else {
$feed_type = home_url().'feed/';
}
$content .= '<img src="'.$siteurl.'/matomo.php?idsite='.$siteid.'&rec=1&url='.$posturl.'&action_name='.$title.'&urlref='.$feed_type.'&_rcn=feed_impression&_rck='.$title.'" style="border:0;width:0;height:0" width="0" height="0" alt="" />';
}
}
return $content;
}
/*-------------------------------------------------------------
Name: ajdg_matomo_feed_campaign
Since: 1.0
-------------------------------------------------------------*/
function ajdg_matomo_feed_clicks($permalink) {
global $post;
if(is_feed()) {
$user_agent = (isset($_SERVER['HTTP_USER_AGENT'])) ? trim(strip_tags(htmlspecialchars($_SERVER['HTTP_USER_AGENT']))) : 'n/a';
if(ajdg_matomo_useragent_filter($user_agent)) {
$protocol = (is_ssl()) ? 'https://' : 'http://';
$http_host = (isset($_SERVER['HTTP_HOST'])) ? trim(strip_tags(htmlspecialchars($_SERVER['HTTP_HOST']))) : 'n/a';
$request_url = (isset($_SERVER['REQUEST_URI'])) ? esc_url_raw($protocol.$http_host.$_SERVER['REQUEST_URI']) : 'n/a';
if(strpos($request_url, get_bloginfo('rss2_url')) !== false) {
$feed_type = 'RSS2';
} elseif(strpos($request_url, get_bloginfo('atom_url')) !== false) {
$feed_type = 'Atom';
} elseif(strpos($request_url, get_bloginfo('comments_rss2_url')) !== false) {
$feed_type = 'Comment';
} elseif(strpos($request_url, get_bloginfo('comments_atom_url')) !== false) {
$feed_type = 'Comment';
} else {
$feed_type = 'Other';
}
$separator = (strpos($permalink, '?') === false) ? '?' : '&';
$permalink .= $separator.'mtm_campaign=feed_click&mtm_kwd='.$post->post_name.'&mtm_source='.$feed_type.'&mtm_medium=feed';
}
}
return $permalink;
}
/*-------------------------------------------------------------
Name: ajdg_matomo_tracker
Since: 1.0
-------------------------------------------------------------*/
function ajdg_matomo_tracker() {
$siteid = get_option('ajdg_matomo_siteid');
$siteurl = get_option('ajdg_matomo_siteurl');
$track_error_pages = get_option('ajdg_matomo_track_error_pages');
$track_incognito = get_option('ajdg_matomo_track_incognito');
$track_heartbeat = get_option('ajdg_matomo_heartbeat_enable');
$track_wc_downloads = get_option('ajdg_matomo_wc_downloads');
$track_high_accuracy = get_option('ajdg_matomo_high_accuracy');
echo "<!-- Matomo -->\n";
echo "<script type=\"text/javascript\">\n";
echo "var _paq = window._paq || [];\n";
if(is_404() AND $track_error_pages == 'yes') echo "_paq.push(['setDocumentTitle', '404/URL = ' + encodeURIComponent(document.location.pathname + document.location.search) + ' Referrer = ' + encodeURIComponent(document.referrer)]);\n";
if($track_incognito == 'yes') echo "_paq.push(['setDoNotTrack', true]);\n";
if($track_heartbeat == 'yes') echo "_paq.push(['enableHeartBeatTimer']);\n";
if($track_wc_downloads == 'yes') echo "_paq.push(['setDownloadClasses', 'woocommerce-MyAccount-downloads-file']);\n";
if($track_high_accuracy == 'yes') echo "_paq.push(['alwaysUseSendBeacon']);\n";
echo "_paq.push(['trackPageView']);\n";
echo "_paq.push(['enableLinkTracking']);\n";
echo "(function() {\n";
echo "\t_paq.push(['setTrackerUrl', '$siteurl/matomo.php']);\n";
echo "\t_paq.push(['setSiteId', '$siteid']);\n";
echo "\tvar d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];\n";
echo "\tg.type='text/javascript'; g.async=true; g.defer=true; g.src='$siteurl/matomo.js'; s.parentNode.insertBefore(g,s);\n";
echo "})();\n";
echo "</script>\n";
echo "<noscript><img src=\"$siteurl/matomo.php?idsite=$siteid&rec=1\" style=\"border:0\" alt=\"\" /></noscript>\n";
echo "<!-- /Matomo -->\n\n";
}
?>