Created
March 11, 2013 18:49
-
-
Save ethitter/5136596 to your computer and use it in GitHub Desktop.
Piwik for Multisite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin name: Piwik Stats | |
Description: Implement Piwik tracking code | |
Version: 1.0 | |
Author: Erick Hitter | |
Author URI: http://www.ethitter.com | |
License: GPLv2 | |
*/ | |
/* | |
This program is free software; you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation; version 2 of the License. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
*/ | |
/** | |
* Add Piwik tracking code to page footer | |
* | |
* @uses is_user_logged_in, get_current_blog_id | |
* @action wp_footer | |
* @return string or null | |
*/ | |
function eth_piwik_tracking_code() { | |
// Don't let me screw up the Analytics | |
if ( is_user_logged_in() ) | |
return; | |
// Map of blog ID to Piwik ID | |
$tracking_ids = array( | |
1 => 3, | |
2 => 1, | |
3 => 2, | |
4 => 5, | |
5 => 6, | |
7 => 4, | |
8 => 7, | |
9 => 11 | |
); | |
$current_blog_id = get_current_blog_id(); | |
if ( array_key_exists( $current_blog_id, $tracking_ids ) ) : | |
$tracking_id = (int) $tracking_ids[ $current_blog_id ]; | |
?> | |
<!-- Piwik --> | |
<script type="text/javascript"> | |
var pkBaseURL = (("https:" == document.location.protocol) ? "https://stats.erick.me/" : "http://stats.erick.me/"); | |
document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E")); | |
</script> | |
<script type="text/javascript"> | |
try { | |
var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", <?php echo $tracking_id; ?>); | |
<?php if ( is_404() ) : ?>piwikTracker.setDocumentTitle('404/URL = '+String(document.location.href).replace(/\//g,"%2f") + '/From = ' + String(document.referrer).replace(/\//g,"%2f"));<?php echo "\n"; endif; ?> | |
piwikTracker.trackPageView(); | |
piwikTracker.enableLinkTracking(); | |
} catch( err ) {} | |
</script> | |
<noscript><p><img src="http://stats.erick.me/piwik.php?idsite=<?php echo $tracking_id; ?>" style="border:0" alt="" /></p></noscript> | |
<!-- End Piwik Tracking Code --> | |
<?php endif; | |
} | |
add_action( 'wp_footer', 'eth_piwik_tracking_code', 9999 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment