Skip to content

Instantly share code, notes, and snippets.

@nmngt
Created October 30, 2018 16:25
Show Gist options
  • Save nmngt/3f793a1e1689225ae160be52bb823f9a to your computer and use it in GitHub Desktop.
Save nmngt/3f793a1e1689225ae160be52bb823f9a to your computer and use it in GitHub Desktop.
<?php
/**
* prueft, ob der Zugriff von einem Page Analyzer, wie Google PageSPeed Insights,
* aus erfolgt.
*
* Da wir keinen Einfluss auf den Brwoser-Cache der Google Analytics JS haben,
* spielen wir kein Google Analytics aus, wenn ein Page Analyzer auf die Seite
* zugreift. Das verschönert unsere Werte ;-)
*
* @param string $ua User-Agent
*
* @return bool true, wenn ein PageAnalyzer erkannt wird
*/
function is_pageanalyzer($ua){
if( ! isset($ua)){
return false;
}
// Google PageSpeed Insights
if(strpos($ua, 'Speed Insights') !== false){
return true;
}
// GTmetrix
if(strpos($ua, 'gtmetrix.com') !== false){
return true;
}
if(strpos($ua, 'Mozilla/5.0 (X11; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0') !== false){
return true;
}
// PINGdom
if(strpos($ua, 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36') !== false){
return true;
}
// else: normaler Besucher
return false;
}
@nmngt
Copy link
Author

nmngt commented Oct 30, 2018

<?php 
// CHEAT! Binde den Google Analytics Code nicht ein wenn ein 
// PageSpeed Analyzer auf die Seite zugreifen
if( ! is_pageanalyzer($_SERVER['HTTP_USER_AGENT'])):
 ?>
<script type="text/javascript">
<?php /* google analytics */?>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google-analytics.com/analytics.js','ga');ga('create','UA-62557852-5','fbo-healthcare.de');ga('set','anonymizeIp',true);ga('send','pageview');
<?php /* analytics bounce-back helper */?>setTimeout(function(){ga('send','event','Seitenansichtsdauer','mindestens 30 Sekunden',location.href);},30000);setTimeout(function(){ga('send','event','Seitenansichtsdauer','mindestens 60 Sekunden',location.href);},60000);setTimeout(function(){ga('send','event','Seitenansichtsdauer','mindestens 120 Sekunden',location.href);},120000);setTimeout(function(){ga('send','event','Seitenansichtsdauer','mindestens 300 Sekunden',location.href);},300000);
</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment