PluginProbe
Simple Matomo Tracking Code / 1.0.2
Simple Matomo Tracking Code v1.0.2
trunk 0.5.1 0.5.2 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1
simple-matomo-tracking-code / simple-matomo-tracking-code.php

simple-matomo-tracking-code.php in Simple Matomo Tracking Code 1.0.2, at simple-matomo-tracking-code.php

307 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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.2
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><?php _e('Simple Matomo Tracking Code', 'simple-matomo-tracking-code'); ?></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 $matomo_url = SMTC_Admin::build_matomo_url($options);
182 printf(
183 /* translators: %s: URL of the Matomo installation */
184 __('All options set? Then <a href="%s" title="Matomo admin url" target="_blank">check out your stats</a>!', 'simple-matomo-tracking-code'),
185 $matomo_url
186 );
187 ?>
188 </div>
189 <?php
190 if ( isset($options['siteid']) ) {
191 if ( $options['siteid'] == "" ) {
192 add_action('admin_footer', array('SMTC_Admin','warning'));
193 } else {
194 if ( isset($_POST['submit']) ) {
195 add_action('admin_footer', array('SMTC_Admin','success'));
196 }
197 }
198 } else {
199 add_action('admin_footer', array('SMTC_Admin','warning'));
200 }
201 }
202
203 static function success() {
204 echo "
205 <div id='analytics-warning' class='updated'><p><strong>";
206 _e('Simple Matomo Tracking Code Configuration successfully updated.', 'simple-matomo-tracking-code');
207 echo "</strong></p></div>";
208 }
209
210 static function warning() {
211 echo "
212 <div id='analytics-warning' class='notice notice-warning'><p><strong>";
213 _e('Matomo Web Analytics is not active.', 'simple-matomo-tracking-code');
214 echo "</strong> ";
215 _e('You must enter your site ID for it to work.', 'simple-matomo-tracking-code');
216 echo "</p></div>";
217 }
218
219 static function build_matomo_url($options) {
220 if ( $options['matomo_host'] ) {
221 $matomo_url = "//" . $options['matomo_host'];
222 $matomo_url = rtrim($matomo_url, '/') . '/';
223 $matomo_url = $matomo_url . ltrim($options['matomo_baseurl'], '/');
224 $matomo_url = rtrim($matomo_url, '/') . '/';
225 } else {
226 $matomo_url = $options['matomo_baseurl'];
227 $matomo_url = rtrim($matomo_url, '/') . '/';
228 }
229
230 return $matomo_url;
231 }
232 }
233 }
234
235
236 /**
237 * Code that actually inserts stuff into pages.
238 */
239 if ( ! class_exists( 'SMTC_Filter' ) ) {
240 class SMTC_Filter {
241
242 /*
243 * Insert the tracking code into the page
244 */
245 static function spool_analytics() {
246 ?><!-- Simple Matomo Tracking Code plugin active --><?php
247
248 $script_template = "<!-- Matomo -->
249 <script type=\"text/javascript\">
250 var _paq = window._paq = window._paq || [];
251 _paq.push(['trackPageView']);
252 {LINK_TRACKING}
253 (function() {
254 var u=\"{MATOMO_URL}\";
255 _paq.push(['setTrackerUrl', u+'matomo.php']);
256 _paq.push(['setSiteId', {IDSITE}]);
257 var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
258 g.type='text/javascript'; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
259 })();
260 </script>
261 <!-- End Matomo Code -->";
262
263 $options = get_option('MatomoAnalyticsPP');
264
265 if ( $options["siteid"] != "" && (!current_user_can('edit_users') || $options["admintracking"]) && !is_preview() ) {
266 $matomo_url = SMTC_Admin::build_matomo_url($options);
267
268 if ( $options["dltracking"] ) {
269 $link_tracking = "_paq.push(['enableLinkTracking']);";
270 }
271
272 $transitions = array(
273 "{MATOMO_URL}" => $matomo_url,
274 "{IDSITE}" => $options["siteid"],
275 "{LINK_TRACKING}" => $link_tracking);
276 echo strtr($script_template, $transitions);
277 }
278 }
279 }
280 }
281
282 // adds the menu item to the admin interface
283 add_action('admin_menu', array('SMTC_Admin','add_config_page'));
284
285 // adds the footer so the javascript is loaded
286 add_action('wp_footer', array('SMTC_Filter','spool_analytics'));
287
288 /**
289 * Register the "book" custom post type
290 */
291 function simple_matomo_tracking_code_setup_post_type() {
292 register_post_type( 'book', ['public' => true ] );
293 }
294 add_action( 'init', 'simple_matomo_tracking_code_setup_post_type' );
295
296
297 /**
298 * Activate the plugin.
299 */
300 function simple_matomo_tracking_code_activate() {
301 $admin = new SMTC_Admin();
302 $admin->init();
303 }
304
305 register_activation_hook( __FILE__, 'simple_matomo_tracking_code_activate' );
306 ?>
307