Skip to content

Instantly share code, notes, and snippets.

@DasLampe
Created January 30, 2014 14:59
Show Gist options
  • Save DasLampe/8710274 to your computer and use it in GitHub Desktop.
Save DasLampe/8710274 to your computer and use it in GitHub Desktop.
Automatic include piwik tracking code to HTML code via nginx
<?php
// +----------------------------------------------------------------------+
// | Copyright (c) 2012 DasLampe <[email protected]> |
// | Encoding: UTF-8 |
// +----------------------------------------------------------------------+
if(!preg_match("/^piwik$!", $_SERVER['SERVER_NAME'])) {
$url = $_SERVER['SERVER_NAME'];
if(substr_count($url, '.') >= 2)
{
$url = substr($url, strrpos($url, '.', -4)+1);
}
$result = simplexml_load_file("http://piwik/index.php?module=API&method=SitesManager.getSitesIdFromSiteUrl&url=http://".$url."/&format=XML&token_auth=[secret token]");
if(!empty($result))
{
?>
<!-- Piwik -->
<script type="text/javascript">
var pkBaseURL = (("https:" == document.location.protocol) ? "https://piwik.nixmuss-design.de/" : "http://piwik.nixmuss-design.de/");
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", <?= $result->row[0]->idsite; ?>);
piwikTracker.trackPageView();
piwikTracker.enableLinkTracking();
} catch( err ) {}
</script><noscript><p><img src="http://piwik/piwik.php?idsite=<?= $result->row[0]->idsite; ?>" style="border:0" alt="" /></p></noscript>
<!-- End Piwik Tracking Code -->
<?php
}
?>
<?php
}
?>
ssi on;
sub_filter '</body>' '<!--# include virtual="/piwiktracking/index.php" --></body>';
location ~ /piwiktracking/ {
root /srv/http;
location ~ ^(.*)\.php$ {
if (!-f $request_filename) {
return 404;
}
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:/run/php-fpm/php-fpm.socket;
#fastcgi_pass 127.0.0.1:10001;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment