Setting Up Google Analytics With Magento
By cbell73
Magento has a very simple way to integrate your Google Analytics account into your store. Instead of having to add code to all the pages you want tracked, it is just a matter of turning it on in the admin and entering your Google account number.
Enabling Analytics in Magento
- Log into your Magento admin, select "Configuration" under the "System" drop down menu.
- Once in the Configuration section select "Google API" under the "Sales" section in the left menu.
- In this section you will be able to configure your Magento store with various Google features, the top one should be Google Analytics.
- Open the Google Analytics tab, enable it using the dropdown menu and enter your Analytics account number (this should be something like UA – 146584161-1).
- Once that is complete click the "Save Config" button on the top right and "Presto" you have just set up your Magento store to be tracked through your Google Analytics account.
Setting Up Goals in Analytics
Pretty simple isn't it? Well it is, but we are not quite done yet. We still need to set up goals and conversion tracking so we can see how our site is doing. Just monitoring traffic and keywords is good but what you really want to know is if your site is doing what you want it to (sell stuff, get sign-ups, etc.)
The first part of this is to go to your Google Analytics account and set up some goals. Here next to your profile you will see an edit and delete button in the far right column under "Actions", click on "Edit".
Here you can add up to 4 goals for your site, click on "Add goal" for set 1. This is where we are going to set up our goal to track purchases.
- Give the goal a name.
- Make sure it is active.
- Set goal position(Set 1, Goal 1 if you don't have any goals already).
- Goal Type - URL Destination.
- Match Type – Head Match
- Goal URL – This is the payment thank you page of Magento I am using PayPal as my payment processor so it is something like "http://www.yoursite.com/paypal/standard/success/". If you are not sure about this place a test transaction and copy the URL from your payment thank you page and paste it into this box.
- I wouldn't check the "Case Sensitive" box.
- Goal Value – For me I uses the average profit per order I expect to have but this is optional.
At this point the goal itself is set up. However going a little further and setting up the goal funnel can give you some good information on what is happening with your customers during the check out process. Let's continue on and finish setting up the goal funnel.
Setting Up the Goal Funnel
What we want here is to outline the steps we are considering to be the payment process. I don't include any pages outside of the payment process because I am not looking for shoppers, I want to know if users who begin a purchase follow through with it.
The pages we want in the funnel are:
- Shopping Cart Page – The customer has added a product to the cart -/checkout/cart/
- Customer Info Page - Without any module this should be - /onpage/checkout/
That should be it, you can go ahead and click "save goal" unless you are using a payment gateway like PayPal Standard where the user is taken to a separate site to make payment. If you are there are a couple more steps you need to take to finish the process.
Redirecting Payment Gateways Only
- You need to add the payment redirect page to the funnel as well. For PayPal this is /paypal/standard/redirect/ but if you are unsure just make a test purchase and when you are on the redirect page press cancel in your browser stopping the redirect and get the URL our of your address bar. Once you have this entered you can go ahead and save the goal. Now we are going to add the tracking code to the redirect page.
- Normally I don't recommend changing core code but this is the only way to be able to track if customers are getting to the payment gateway then abandoning.
Magento doesn't add the Google Analytics tracking code to the payment redirect page by default so you will need to manually add it. For PayPal the redirect file is located in /app/code/core/Mage/Paypal/Block/Standard/Redirect.php. Add your Analytics code like the example below, don't forget to enter "your" account number where the XX-XXXXXXXX-X is.
Replace:
$html = '<html><body>';
With:
$html = '<html>';
$html .= '<head>';
$html .= '<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push([\'_setAccount\', \'XX-XXXXXXXX-X\']);
_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\';
(document.getElementsByTagName(\'head\')[0] || document.getElementsByTagName(\'body\')[0]).appendChild(ga);
})();
</script>';
$html .= '</head><body>';
Once the redirect page is set up and uploaded to the server you should check to make sure the page is still redirecting properly. If it is still working you should be good and your funnel tracking is complete.
Tracking Goals in Magento
Now when you enter your Analytics account you can click on "Funnel Visualization" under the "Goals" tab on the right and see your check out process. You will be able to see how many customers begin the check out process, how many abandon and how many complete a purchase.
Comments
How should it be done when you need several Google analytics id on the same server.
@Conny; it is posible to add more than one ID to a page. On the admin panel, you can not write more than one ID account, so you need to change the following file:
app\code\core\Mage\GoogleAnalytics\Block\Ga.php
Open it and find the last function: protected function _toHtml().
Replace it with the following code:
protected function _toHtml()
{
if (!Mage::getStoreConfigFlag('google/analytics/active')) {
return '';
}
$google_accounts = explode( ',' , $this->getAccount() ) ;
$this->addText('
');
foreach($google_accounts as $account){
$this->addText('
//');
}
$this->addText('
');
$this->addText($this->getQuoteOrdersHtml());
if ($this->getGoogleCheckout()) {
$protocol = Mage::app()->getStore()->isCurrentlySecure() ? 'https' : 'http';
$this->addText('');
}
return parent::_toHtml();
}
Now, you can put your analytics ID on the Admin panel separated by commas. (UA-123123,UA-4234234 and so on...)
I Cannot get it to work. Get
Parse error: syntax error, unexpected $end, expecting T_FUNCTION in /data/www/*****/app/code/core/Mage/GoogleAnalytics/Block/Ga.php on line 204
I Replaced
protected function _toHtml()
{
if (!Mage::getStoreConfigFlag('google/analytics/active')) {
return '';
}
$this->addText('
//
//
');
$this->addText($this->getQuoteOrdersHtml());
if ($this->getGoogleCheckout()) {
$protocol = Mage::app()->getStore()->isCurrentlySecure() ? 'https' : 'http';
$this->addText('');
}
return parent::_toHtml();
}
}
with the code you suggested
Sorry, that was wrong, the code I replaced was:
protected function _toHtml()
{
if (!Mage::getStoreConfigFlag('google/analytics/active')) {
return '';
}
$this->addText('
//
//
');
$this->addText($this->getQuoteOrdersHtml());
if ($this->getGoogleCheckout()) {
$protocol = Mage::app()->getStore()->isCurrentlySecure() ? 'https' : 'http';
$this->addText('');
}
return parent::_toHtml();
}
}
Thank you for this article...I will give this a try...
Do you have the same instructions for setting up tracking on Google Checkout?
Hi Connie,
Were you able to get this to work by any chance? I'd received the same error massage at the same line....
Hi thanks for this it was very helpful and saved me a lot time searching around.
Stats are very important for every website. Your post will be very helpful for many who have magento website but having no idea about installation of Google analytics. Thanks
wow, I actually had no idea you could do this from within magento. I've been adding it manually to my header files!!! thanks
marcel 24 months ago
Maybe this is going to help someone.
With this Module you are able to track every Step in the Onepage-Checkout (billing, shipping, shipping_method, payment, review, review-placeOrderClicked, success).
http://www.magentocommerce.com/magento-connect/FOO
Have not testet it yet.