PluginProbe
Include Matomo Tracking, by Jonas Hellmann / trunk
Include Matomo Tracking, by Jonas Hellmann vtrunk
trunk 1.0 1.1 1.2 1.3 1.4 1.5 1.5.1 1.5.2
include-matomo / include-matomo.php

include-matomo.php in Include Matomo Tracking, by Jonas Hellmann trunk, at include-matomo.php

131 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Include Matomo Tracking, by Jonas Hellmann
4 Plugin URI: https://github.com/jonashellmann/include-matomo/
5 Description: This plugin includes Matomo into your Wordpress site
6 Version: 1.5.2
7 Author: Jonas Hellmann
8 Author URI: https://jonas-hellmann.de/en/
9 Text Domain: include-matomo
10 License: GPL3
11 */
12
13 if( get_option('matomo_url') !== '' && get_option('matomo_site_id') !== '0' ) {
14 add_action( 'wp_footer', 'include_matomo_script' );
15 }
16
17 function include_matomo_script() {
18 echo "<!-- Include Matomo -->\n";
19 echo "<script type='text/javascript'>\n";
20 echo " var _paq = _paq || [];\n";
21 echo " _paq.push(['trackPageView']);\n";
22 echo " _paq.push(['enableLinkTracking']);\n";
23 echo " (function() {\n";
24 echo " var u='//" . get_matomo_url() . "/';\n";
25 echo " _paq.push(['setTrackerUrl', u+'piwik.php']);\n";
26 echo " _paq.push(['setSiteId', '" . get_option('matomo_site_id') . "']);\n";
27 if (get_option('matomo_disable_cookies', 'n') == 'y') {
28 echo " _paq.push(['disableCookies']);\n";
29 }
30 echo " var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];\n";
31 echo " g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);\n";
32 echo " })();\n";
33 echo "</script>\n";
34 echo "<!-- End Matomo Code-->\n";
35 }
36
37 function get_matomo_url() {
38 $matomo_url = get_option('matomo_url');
39 if( substr_compare( $matomo_url, '/', -1, 1 ) == 0 ) {
40 $matomo_url = substr( $matomo_url, 0, -1 );
41 }
42 if( substr_compare( $matomo_url, 'https://', 0, 8 ) == 0 ) {
43 $matomo_url = substr( $matomo_url, 8 );
44 }
45 if( substr_compare( $matomo_url, 'http://', 0, 7 ) == 0 ) {
46 $matomo_url = substr( $matomo_url, 7 );
47 }
48 return $matomo_url;
49 }
50
51 // TODO: Only include if this should be used
52 add_filter( 'the_permalink_rss', 'add_matomo_campaign_to_rss' );
53
54 function add_matomo_campaign_to_rss($guid) {
55 global $post;
56 $get_vars = array(
57 'pk_campaign=' . urlencode( get_option('matomo_rss_campaign') ),
58 'pk_source=' . urlencode( get_option('matomo_rss_source') )
59 );
60 return $guid . '?' . implode( '&', $get_vars );
61 }
62
63
64 add_action('admin_menu', 'include_matomo_menu');
65
66 function include_matomo_menu() {
67 add_submenu_page('options-general.php', 'Include Matomo - Settings', 'Include Matomo', 'administrator', 'include-matomo-settings', 'include_matomo_settings_page');
68 }
69
70 add_action( 'admin_init', 'my_plugin_settings' );
71
72 function my_plugin_settings() {
73 register_setting( 'include-matomo-settings-group', 'matomo_url' );
74 register_setting( 'include-matomo-settings-group', 'matomo_site_id' );
75 register_setting( 'include-matomo-settings-group', 'matomo_rss_campaign' );
76 register_setting( 'include-matomo-settings-group', 'matomo_rss_source' );
77 register_setting( 'include-matomo-settings-group', 'matomo_disable_cookies' );
78 }
79
80 function include_matomo_settings_page() { ?>
81 <div class="wrap">
82 <h2><?php _e('Matomo Settings', 'include-matomo'); ?></h2>
83
84 <form method="post" action="options.php">
85 <?php settings_fields( 'include-matomo-settings-group' ); ?>
86 <?php do_settings_sections( 'include-matomo-settings-group' ); ?>
87 <table class="form-table">
88 <tr valign="top">
89 <th scope="row">Matomo URL</th>
90 <td>
91 <input type="text" name="matomo_url" value="<?php echo esc_attr( get_option('matomo_url') ); ?>" />
92 </td>
93 </tr>
94
95 <tr valign="top">
96 <th scope="row"><?php _e('Matomo Page ID', 'include-matomo' ); ?></th>
97 <td>
98 <input type="number" name="matomo_site_id" value="<?php echo esc_attr( get_option('matomo_site_id') ); ?>" />
99 </td>
100 </tr>
101
102 <tr valign="top">
103 <th scope="row"><?php _e('Matomo RSS Campaign', 'include-matomo' );?></th>
104 <td>
105 <input type="text" name="matomo_rss_campaign" value="<?php echo esc_attr( get_option('matomo_rss_campaign') ); ?>" />
106 </td>
107 </tr>
108
109 <tr valign="top">
110 <th scope="row"><?php _e( 'Matomo RSS Source', 'include-matomo' ); ?></th>
111 <td>
112 <input type="text" name="matomo_rss_source" value="<?php echo esc_attr( get_option('matomo_rss_source') ); ?>" />
113 </td>
114 </tr>
115
116 <tr valign="top">
117 <th scope="row"><?php _e( 'Matomo Disable Cookies? (y/n)', 'include-matomo' ); ?></th>
118 <td>
119 <input type="text" name="matomo_disable_cookies" value="<?php echo esc_attr( get_option('matomo_disable_cookies', 'n') ); ?>" />
120 </td>
121 </tr>
122 </table>
123
124 <?php submit_button(); ?>
125
126 </form>
127 </div> <?php
128 }
129
130 ?>
131