George Mason University Antonin Scalia Law School

PHP Script Swapping out GA codes

This script was used to delineate Google Analytic Tracking codes between four different sites sharing the same wordpress theme files. Originally placed in footer.php above the GA tracking code provided by Google.

<?php
$host = 'https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($host,'sls') !== false)
{
$gacode ="UA-792782-19";
}

elseif (strpos($host,'gai') !== false)
{
$gacode ="UA-792782-21";
}

elseif (strpos($host,'pep') !== false)
{
$gacode ="UA-792782-22";
}

elseif (strpos($host,'mvets') !== false)
{
$gacode ="UA-792782-23";
}

else
{
$gacode ="UA-792782-XX";
}
?>

The script checks the URL and replaces the $gacode variable with the correct analytics code inside the provided GA script.


<script>
(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','https://www.google-analytics.com/analytics.js','ga');

ga('create', '<?phpecho $gacode; ?>', 'auto');
ga('send', 'pageview');

</script>