I was trying to store the Google Analytics code in a single file that all scripts reference. But my file seems to be breaking the analytics code, and none of the pages that use this file show up in my google analytics report anymore.
Here is what I did in the head section of the scripts:
<?php
include ('global.php');
include ('connect.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN"
"http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
<html>
<head>
<?php
printGoogleAnalytics();
?>
<?php
printGoogleSiteVerification();
?>
</head>
Here is what the printGoogleAnalytics() looks like in global.php :
function printGoogleAnalytics()
{
if($production === true)
{
$str = <<<EOT
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-9425856-20']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement("script"); ga.type = "text/javascript"; ga.async = true;
ga.src = ("https:" == document.location.protocol ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js";
var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
EOT;
echo $str;
}
}
Any idea what may be breaking things? Is there any other code needed from me? I use PHP for this.
Since the Google Analytics code wasn’t appearing in the source of the generated page, it means that that block isn’t getting evaluated.
The likeliest suspect is
if($production===true). Using 3 equals signs means its a STRICT comparison. What’s probably happening is you’re not setting it precisely to true, but instead to something that’s kind of truthy.You could get around that by either fixing how
$productiongets set, or using==.