0

I have followed the below link and added the code in success.phtml but my orders are not tracked in Google Analytics. In Chrome, the below error comes

https://developers.google.com/analytics/devguides/collection/analyticsjs/enhanced-ecommerce

success.phtml

<script type="text/javascript">
    ga('create', '<?php echo Mage::getStoreConfig('google/analytics/account') ?>');
    ga('require', 'ec');
    <?php
        foreach ($items as &$item) {
            echo $block->getItemJs($trans['id'], $item);
        }
        echo $block->getTransactionJs($trans);

    ?>
    ga('send', 'pageview');
</script>

Uncaught ReferenceError: ga is not defined

Can anyone help me,?

3 Answers 3

2

I think you will need to call analytics.js also

As per description here it should be like below

<!-- Google Analytics -->
<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','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
1

analytics.js file is misssing on your page. It should be like this

<script type="text/javascript">
      (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', '<?php echo Mage::getStoreConfig('google/analytics/account') ?>');
        ga('require', 'ec');
        <?php
            foreach ($items as &$item) {
                echo $block->getItemJs($trans['id'], $item);
            }
            echo $block->getTransactionJs($trans);

        ?>
        ga('send', 'pageview');
    </script>
1

Did you recently upgrade your Magento version? Magento added (I believe in 1.9.1) a configuration setting in the admin that lets you select different Google Analytics code types to use throughout your site.

After this upgrade, the code setting on my site switched from a "Universal Analytics" code to a "Google Analytics" code and gave me the same error: Uncaught ReferenceError: ga is not defined

Try switching it back to Universal Analytics. Go To:

Admin > Configuration > Sales > Google API > Google Analytics > Type

Set it to Universal Analytics

You can see the conditional for the two different GA code types and what the code looks like in:

app\design\frontend\base\default\template\googleanalytics\ga.phtml

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.