Search This Blog

Jul 24, 2012

Google Analytics in Older Version of Magento

Magento is one the many platforms that use Google Analytics as a tracking system. They have a build core module that allows site owner to implement GA tracking code by simply putting their GA account in Magento Configuration panel. However, there's an issue that it is not working anymore in older version of magento for the reason that GA upadted their tracking code.

GA core module for older version of magento is still using the old GA tracking script. In order to make it functional again, It's either you update your magento or simply edit the GA core module file.

Editing GA core Module

1. Make a copy of this file (/app/code/core/Mage/GoogleAnalytics/Block/Ga.php) to your local folder. So, you will have this directory in your local folder - /app/code/local/Mage/GoogleAnalytics/Block/Ga.php
2. Open the copied file in an editor.
3. Go to line 171 and look for 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 -->
');
4. Replace it with this:
$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 -->
');
5. Save it. You will now be able to see data in your GA account. Thanks

0 comments: