Magento Google Analytics Code Has Errors

Last week we deployed another successful Magento ecommerce website. To track the visitors we signed up for a free Google Analytics account, and then plugged their tracking code into the admin section of the Magento administrator area. When we applied the API, the site quickly confirmed that the tracking code was in place through the Google Analytics dashboard.

We then waited 48 hours, and then logged in to see how many visitors hit the site. Turns out that none of the visitors were being tracked! Using the error console within Firefox we reloaded the page, and found the problem instantly. Turns out the Magento coders totally messed up when creating their Google Analytics API. Take a look for yourself, here’s the JavaScript they used:

screenshot-www jvfconsulting com 2015-02-20 11-58-49
This is how it should read:

screenshot-www jvfconsulting com 2015-02-20 12-00-09
As you can see the Magento coders have it all out of order, and forgot the most important line of code that created the “_gaq” object:

screenshot-www jvfconsulting com 2015-02-20 12-00-49
We can only assume that this API is broken on all Magento builds! To fix this, remove the tracking code from the Magento Google API in the admin area, then use the JavaScript given to you by Google Analytics, and place it in the footer of your website. Whala! Your site is now tracking visitors appropriately with no errors in the error console.