Uncategorized

Why is my Google Analytics Code on EVERY PAGE OF EVERY SITE Hosted on my Server?

So, I am getting serious about some analytics on a couple sites I am managing (and I had designed), and for some reason the pages from both sites are showing in the same chart.  Why would the Google Analytics code for one site be applied to the other site (it was showing on the right site and the wrong site)?

Google Analytics screenshot.
The red ones do not belong.

Okay, I start reviewing the sites.  They are both WordPress sites, being hosted on the same Ubuntu/Apache server, both are being served HTTPs, I am using the Monster Insights plugin to add the analytics code to the sites… That must be the issue!  Right?  Wrong.  I start disabling other plugins, no success.  I view the source of the page, the script is being inserted right before the closing body tag (suggested place for performance reasons), and looks exactly the same on both sites.

<script>if(window.parent==window){(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-17622373-7′,’auto’,{‘siteSpeedSampleRate’:100});ga(‘send’,’pageview’);}</script></body>
</html>

I ask in a WordPress forum; got some good ideas, but still no success.  Changing themes didn’t work, uninstalling plugins didn’t work…  I take a look at the source for some non-wordpress sites on the same server, there is nothing in the HTML file referencing that Google Analytics script, but when I view the source in the browser it is there!

Problem code is highlighted.
Problem code is highlighted.

This has to be something on the server level, something that is being inserted into the pages without being called by them.  I was searching everywhere in the server files I could think of to figure this out – but wasn’t finding the cause.  You know how sometimes you can know something, know where to look, but you just aren’t seeing what’s right in front of you?   I asked for opinions on serverfault. I knew it had to do with the Apache configuration, and the first clue is in the script right before the Google Analytics one…

Delete both lines to fully remove the Google Analytics reference.
Delete both lines to fully remove the Google Analytics reference.

It was the PageSpeed Mod configuration than was enabled in Apache that was causing this.  The server had begun as a simple project serving a single site, but has since grown and this configuration file wasn’t updated.  The sites that didn’t use this Property ID had their own tracking on them and were showing accurate statistics (The Google Analytics code was on there twice with separate IDs, the PageSpeed load and the plugins I was using), but the site with this Property ID had way inflated numbers.  Restarting the Apache Server after modifying the file fixed the issue.

sudo service apache2 restart

And with the plugins reenabled everything is copasetic.

 

P.S. – Fun test to do with the PageSpeed tests from Google, I tested https://developers.google.com/speed/pagespeed/ which scored a 63% on mobile and (more importantly) a 79% on desktop.  Interesting stuff 😉

Leave a Reply

Your email address will not be published. Required fields are marked *