| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Simple Matomo Tracking Code |
| 4 |
* Plugin URI: http://www.rolandbaer.ch/software/wordpress/simple-matomo-tracking-code/ |
| 5 |
* Description: This plugin makes it simple to add Matomo Web Analytics code to your WebSite. |
| 6 |
* Version: 1.0.0 |
| 7 |
* Author: Roland Bär |
| 8 |
* Author URI: http://www.rolandbaer.ch/ |
| 9 |
* Text Domain: simple-matomo-tracking-code |
| 10 |
* License: GPLv3 |
| 11 |
* |
| 12 |
* Based on Jules Stuifbergen's Piwik Analytics plugin |
| 13 |
*/ |
| 14 |
|
| 15 |
// If this file is called directly, abort. |
| 16 |
if ( ! defined( 'WPINC' ) ) { |
| 17 |
die; |
| 18 |
} |
| 19 |
|
| 20 |
/* |
| 21 |
* Admin User Interface |
| 22 |
*/ |
| 23 |
|
| 24 |
if ( ! class_exists( 'SMTC_Admin' ) ) { |
| 25 |
|
| 26 |
class SMTC_Admin { |
| 27 |
|
| 28 |
static function add_config_page() { |
| 29 |
global $wpdb; |
| 30 |
if ( function_exists('add_options_page') ) { |
| 31 |
add_options_page( |
| 32 |
__('Simple Matomo Tracking Code Configuration', 'simple-matomo-tracking-code'), |
| 33 |
__('Simple Matomo Tracking Code', 'simple-matomo-tracking-code'), |
| 34 |
'manage_options', |
| 35 |
basename(__FILE__), |
| 36 |
array('SMTC_Admin','config_page') |
| 37 |
); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
function restore_defaults() { |
| 42 |
$options['siteid'] = 1; |
| 43 |
$options['matomo_host'] = ''; |
| 44 |
$options['matomo_baseurl'] = '/matomo/'; |
| 45 |
$options['admintracking'] = false; |
| 46 |
$options['dltracking'] = true; |
| 47 |
update_option('MatomoAnalyticsPP',$options); |
| 48 |
} |
| 49 |
|
| 50 |
function init() { |
| 51 |
$options = get_option('MatomoAnalyticsPP'); |
| 52 |
if (empty($options)) { |
| 53 |
$this->restore_defaults(); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
static function sanitize_siteid( $value ) { |
| 58 |
// remove invalid characters |
| 59 |
$value = preg_replace( '$[^0-9]*$', '', $value ); |
| 60 |
return (int) $value; |
| 61 |
} |
| 62 |
|
| 63 |
static function config_page() { |
| 64 |
if (isset($_GET['reset']) && $_GET['reset'] == "true") { |
| 65 |
restore_defaults(); |
| 66 |
} |
| 67 |
|
| 68 |
if ( isset($_POST['submit']) ) { |
| 69 |
if (!current_user_can('manage_options')) die(__('You cannot edit the Simple Matomo Tracking Code options.', 'simple-matomo-tracking-code')); |
| 70 |
check_admin_referer('analyticspp-config'); |
| 71 |
$siteid = SMTC_Admin::sanitize_siteid($_POST['siteid']); |
| 72 |
if( $siteid> 0 ) { |
| 73 |
$options['siteid'] = $siteid; |
| 74 |
} |
| 75 |
|
| 76 |
if (isset($_POST['matomo_baseurl'])) { |
| 77 |
$options['matomo_baseurl'] = strtolower(sanitize_text_field($_POST['matomo_baseurl'])); |
| 78 |
} |
| 79 |
|
| 80 |
if (isset($_POST['matomo_host'])) { |
| 81 |
$options['matomo_host'] = strtolower(sanitize_text_field($_POST['matomo_host'])); |
| 82 |
} |
| 83 |
|
| 84 |
if (isset($_POST['dltracking'])) { |
| 85 |
$options['dltracking'] = true; |
| 86 |
} else { |
| 87 |
$options['dltracking'] = false; |
| 88 |
} |
| 89 |
|
| 90 |
if (isset($_POST['admintracking'])) { |
| 91 |
$options['admintracking'] = true; |
| 92 |
} else { |
| 93 |
$options['admintracking'] = false; |
| 94 |
} |
| 95 |
|
| 96 |
update_option('MatomoAnalyticsPP', $options); |
| 97 |
} |
| 98 |
|
| 99 |
$options = get_option('MatomoAnalyticsPP'); |
| 100 |
?> |
| 101 |
<div class="wrap"> |
| 102 |
<h2>Simple Matomo Tracking Code Configuration</h2> |
| 103 |
<form action="" method="post" id="analytics-conf"> |
| 104 |
<?php |
| 105 |
if ( function_exists('wp_nonce_field') ) |
| 106 |
wp_nonce_field('analyticspp-config'); |
| 107 |
?> |
| 108 |
|
| 109 |
<p> |
| 110 |
<?php _e("Matomo, formerly known as Piwik, is a downloadable web analytics software platform free of charge under the GPL license.", 'simple-matomo-tracking-code'); ?> |
| 111 |
<br /> |
| 112 |
<?php _e('If you don\'t have Matomo installed, you can get it at <a href="https://matomo.org/">matomo.org</a>.', 'simple-matomo-tracking-code'); ?> |
| 113 |
</p> |
| 114 |
|
| 115 |
<table class="form-table" style="width:100%;"> |
| 116 |
<tr> |
| 117 |
<th scope="row" valign="top"> |
| 118 |
<label for="siteid"><?php _e('Matomo site id', 'simple-matomo-tracking-code'); ?></label> |
| 119 |
</th> |
| 120 |
<td> |
| 121 |
<input id="siteid" name="siteid" class="small-text" type="number" size="3" maxlength="4" value="<?php echo $options['siteid']; ?>" /><br/> |
| 122 |
<div id="expl"> |
| 123 |
<p> |
| 124 |
<?php _e('In the Matomo interface, when you "Add Website" you are shown a piece of JavaScript that you are told to insert into the page, in that script is a unique string that identifies the website you just defined, that is your site ID (usually "1").', 'simple-matomo-tracking-code'); ?> |
| 125 |
</p> |
| 126 |
<p> |
| 127 |
<?php _e('Once you have entered your site id in the box above your pages will be trackable by Matomo Web Analytics.', 'simple-matomo-tracking-code'); ?> |
| 128 |
</p> |
| 129 |
</div> |
| 130 |
</td> |
| 131 |
</tr> |
| 132 |
<tr> |
| 133 |
<th scope="row" valign="top"> |
| 134 |
<label for="dltracking"><?php _e('Track downloads', 'simple-matomo-tracking-code'); ?></label><br/> |
| 135 |
<small><?php _e('(default is YES)', 'simple-matomo-tracking-code'); ?></small> |
| 136 |
</th> |
| 137 |
<td> |
| 138 |
<input type="checkbox" id="dltracking" name="dltracking" <?php if ($options['dltracking']) echo ' checked="unchecked" '; ?>/> |
| 139 |
</td> |
| 140 |
</tr> |
| 141 |
<tr> |
| 142 |
<th scope="row" valign="top"> |
| 143 |
<label for="matomo_host"><?php _e('Hostname of the matomo server (optional)', 'simple-matomo-tracking-code'); ?></label> |
| 144 |
</th> |
| 145 |
<td> |
| 146 |
<input id="matomo_host" name="matomo_host" type="text" size="40" maxlength="99" value="<?php echo $options['matomo_host']; ?>" /><br/> |
| 147 |
<div id="expl3"> |
| 148 |
<p> |
| 149 |
<?php _e('Example: www.yourdomain.com -- Leave blank (default) if this is the same as your website. Do NOT include the http(s):// bit.', 'simple-matomo-tracking-code'); ?> |
| 150 |
</p> |
| 151 |
</div> |
| 152 |
</td> |
| 153 |
</tr> |
| 154 |
<tr> |
| 155 |
<th scope="row" valign="top"> |
| 156 |
<label for="matomo_baseurl"><?php _e('Base URL path of matomo installation', 'simple-matomo-tracking-code'); ?></label> |
| 157 |
</th> |
| 158 |
<td> |
| 159 |
<input id="matomo_baseurl" name="matomo_baseurl" type="text" size="40" maxlength="99" value="<?php echo $options['matomo_baseurl']; ?>" /><br/> |
| 160 |
<div id="expl2" style="display:none;"> |
| 161 |
<p> |
| 162 |
<?php _e("The URL path to the matomo installation. E.g. /matomo/ or /stats/. Don't forget the trailing slash!", 'simple-matomo-tracking-code'); ?> |
| 163 |
</p> |
| 164 |
</div> |
| 165 |
</td> |
| 166 |
</tr> |
| 167 |
<tr> |
| 168 |
<th scope="row" valign="top"> |
| 169 |
<label for="admintracking"><?php _e('Track the admin user too', 'simple-matomo-tracking-code'); ?></label><br/> |
| 170 |
<small><?php _e('(default is not to)', 'simple-matomo-tracking-code'); ?></small> |
| 171 |
</th> |
| 172 |
<td> |
| 173 |
<input type="checkbox" id="admintracking" name="admintracking" <?php if ($options['admintracking']) echo ' checked="checked" '; ?>/> |
| 174 |
</td> |
| 175 |
</tr> |
| 176 |
</table> |
| 177 |
<p style="border:0;" class="submit"><input type="submit" name="submit" value="<?php echo esc_html__('Save settings', 'simple-matomo-tracking-code'); ?>" /></p> |
| 178 |
</form> |
| 179 |
<p> |
| 180 |
<?php |
| 181 |
if ( $options['matomo_host'] ) { |
| 182 |
$matomo_url = "//" . $options['matomo_host']; |
| 183 |
$matomo_url = rtrim($matomo_url, '/') . '/'; |
| 184 |
$matomo_url = $matomo_url . ltrim($options['matomo_baseurl'], '/'); |
| 185 |
$matomo_url = rtrim($matomo_url, '/') . '/'; |
| 186 |
} else { |
| 187 |
$matomo_url = $options['matomo_baseurl']; |
| 188 |
$matomo_url = rtrim($matomo_url, '/') . '/'; |
| 189 |
} |
| 190 |
printf( |
| 191 |
/* translators: %s: URL of the Matomo installation */ |
| 192 |
__('All options set? Then <a href="%s" title="Matomo admin url" target="_blank">check out your stats!</a>', 'simple-matomo-tracking-code'), |
| 193 |
$matomo_url |
| 194 |
); |
| 195 |
?> |
| 196 |
</div> |
| 197 |
<?php |
| 198 |
if (isset($options['siteid'])) { |
| 199 |
if ($options['siteid'] == "") { |
| 200 |
add_action('admin_footer', array('SMTC_Admin','warning')); |
| 201 |
} else { |
| 202 |
if (isset($_POST['submit'])) { |
| 203 |
add_action('admin_footer', array('SMTC_Admin','success')); |
| 204 |
} |
| 205 |
} |
| 206 |
} else { |
| 207 |
add_action('admin_footer', array('SMTC_Admin','warning')); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
function success() { |
| 212 |
echo " |
| 213 |
<div id='analytics-warning' class='updated'><p><strong>"; |
| 214 |
_e('Simple Matomo Tracking Code Configuration successfully updated.', 'simple-matomo-tracking-code'); |
| 215 |
echo "</strong></p></div>"; |
| 216 |
} |
| 217 |
|
| 218 |
function warning() { |
| 219 |
echo " |
| 220 |
<div id='analytics-warning' class='notice notice-warning'><p><strong>"; |
| 221 |
_e('Matomo Web Analytics is not active.', 'simple-matomo-tracking-code'); |
| 222 |
echo "</strong> "; |
| 223 |
_e('You must enter your Site ID for it to work.', 'simple-matomo-tracking-code'); |
| 224 |
echo "</p></div>"; |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
/** |
| 231 |
* Code that actually inserts stuff into pages. |
| 232 |
*/ |
| 233 |
if ( ! class_exists( 'SMTC_Filter' ) ) { |
| 234 |
class SMTC_Filter { |
| 235 |
|
| 236 |
/* |
| 237 |
* Insert the tracking code into the page |
| 238 |
*/ |
| 239 |
static function spool_analytics() { |
| 240 |
?><!-- Simple Matomo Tracking Code plugin active --><?php |
| 241 |
|
| 242 |
$script_template = "<!-- Matomo --> |
| 243 |
<script type=\"text/javascript\"> |
| 244 |
var _paq = window._paq = window._paq || []; |
| 245 |
_paq.push(['trackPageView']); |
| 246 |
{LINK_TRACKING} |
| 247 |
(function() { |
| 248 |
var u=\"{MATOMO_URL}\"; |
| 249 |
_paq.push(['setTrackerUrl', u+'matomo.php']); |
| 250 |
_paq.push(['setSiteId', {IDSITE}]); |
| 251 |
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; |
| 252 |
g.type='text/javascript'; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); |
| 253 |
})(); |
| 254 |
</script> |
| 255 |
<!-- End Matomo Code -->"; |
| 256 |
|
| 257 |
$options = get_option('MatomoAnalyticsPP'); |
| 258 |
|
| 259 |
if ($options["siteid"] != "" && (!current_user_can('edit_users') || $options["admintracking"]) && !is_preview() ) { |
| 260 |
|
| 261 |
if ( $options['matomo_host'] ) { |
| 262 |
$matomo_url = "//" . $options['matomo_host']; |
| 263 |
$matomo_url = rtrim($matomo_url, '/') . '/'; |
| 264 |
$matomo_url = $matomo_url . ltrim($options['matomo_baseurl'], '/'); |
| 265 |
$matomo_url = rtrim($matomo_url, '/') . '/'; |
| 266 |
} else { |
| 267 |
$matomo_url = $options['matomo_baseurl']; |
| 268 |
$matomo_url = rtrim($matomo_url, '/') . '/'; |
| 269 |
} |
| 270 |
|
| 271 |
if ( $options["dltracking"]) { |
| 272 |
$link_tracking = "_paq.push(['enableLinkTracking']);"; |
| 273 |
} |
| 274 |
|
| 275 |
$transitions = array( |
| 276 |
"{MATOMO_URL}" => $matomo_url, |
| 277 |
"{IDSITE}" => $options["siteid"], |
| 278 |
"{LINK_TRACKING}" => $link_tracking); |
| 279 |
echo strtr($script_template, $transitions); |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
// adds the menu item to the admin interface |
| 286 |
add_action('admin_menu', array('SMTC_Admin','add_config_page')); |
| 287 |
|
| 288 |
// adds the footer so the javascript is loaded |
| 289 |
add_action('wp_footer', array('SMTC_Filter','spool_analytics')); |
| 290 |
|
| 291 |
/** |
| 292 |
* Register the "book" custom post type |
| 293 |
*/ |
| 294 |
function simple_matomo_tracking_code_setup_post_type() { |
| 295 |
register_post_type( 'book', ['public' => true ] ); |
| 296 |
} |
| 297 |
add_action( 'init', 'simple_matomo_tracking_code_setup_post_type' ); |
| 298 |
|
| 299 |
|
| 300 |
/** |
| 301 |
* Activate the plugin. |
| 302 |
*/ |
| 303 |
function simple_matomo_tracking_code_activate() { |
| 304 |
$admin = new SMTC_Admin(); |
| 305 |
$admin->init(); |
| 306 |
} |
| 307 |
|
| 308 |
register_activation_hook( __FILE__, 'simple_matomo_tracking_code_activate' ); |
| 309 |
?> |
| 310 |
|