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