Magento: Solution for Google Analytics not tracking website

I had an old version of Magento. I had enabled google analytics tracking from Magento admin. But, google analytics was not tracking my webshop.

The main reason behind this was that Google had modified its analytics tracking code and the GoogleAnalytics core module of Magento had old tracking code.

To fix this issue, do the following:-

– Make a local copy of /app/code/core/Mage/GoogleAnalytics/Block/Ga.php

i.e. COPY /app/code/core/Mage/GoogleAnalytics/Block/Ga.php
TO /app/code/local/Mage/GoogleAnalytics/Block/Ga.php

– Open the file

/app/code/local/Mage/GoogleAnalytics/Block/Ga.php

– Go to line 171

– You will see the following code:


$this->addText('
	<!-- BEGIN GOOGLE ANALYTICS CODE -->
	<script type="text/javascript">
	//<![CDATA[
		(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\';
			(document.getElementsByTagName(\'head\')[0] || document.getElementsByTagName(\'body\')[0]).appendChild(ga);
		})();

		_gaq.push(["_setAccount", "' . $this->getAccount() . '"]);
		_gaq.push(["_trackPageview", "'.$this->getPageName().'"]);
	//]]>
	</script>
	<!-- END GOOGLE ANALYTICS CODE -->
');

– Replace it with the following code:


$this->addText('
	<!-- BEGIN GOOGLE ANALYTICS CODE -->
	<script type="text/javascript">
	var _gaq = _gaq || [];
	_gaq.push([\'_setAccount\', \'' . $this->getAccount() . '\']);
	_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>
	<!-- END GOOGLE ANALYTICS CODE -->
');

Now, Google Analytics should be able to track your webshop.

Thanks.