Replace UA-XXXXXXX-1 with your Google Analytics id
You can optional include this on selective pages or on every page in a header file.
<script>
console.log('Google Analytics is NOT tracking this page');
</script>
/*
Add this bit to header.php and it will only track the page if it is on your live site.
Then put the rest of the code in an "inc" directory (or wherever you want it).
The console log is a quick way to check if your live site pages are being tracked
*/
<?php
$server = $_SERVER['SERVER_NAME'];
If ( $server === "www.website.co.uk" ) :
include_once("inc/analyticstracking.php");
else :
include_once("inc/notracking.php");
endif;
<script>
console.log('Google Analytics is tracking this page');
(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-XXXXXXXX-1', 'auto');
ga('send', 'pageview');
var trackOutboundLink = function(url) {
ga('send', 'event', 'outbound', 'click', url, {
'transport': 'beacon',
'hitCallback': function(){document.location = url;}
});
}
</script>