Magento And Google Analytics

Magento And Google Analytics

magento google analytics Magento And Google Analytics

Magento is one the most popular Ecommerce platforms, but there is very little written in a comprehensive way to get the most out of your Google Analytics via customization. In this post I hope to go through the modifications you can make to ensure you get the most from your Google Analytics install.

Working with Magento the right way

The most important thing to remember with Magento is to never overwrite core files. As an MVC system, it’s important to use Magento properly, so you can be upgrade proof for future upgrades. Furthermore, to ensure your code is launched first, you want to be in the “Local” pool. If you are not aware Google Analytics is a core file, so to make changes, you need to core files. We will show you how to do this the right way.

Modify Block

The first item you need to work on is a block file, Ga.php which controls some of tracking code the output. If you did not modify this, Google’s _trackPageview will fire before your events and custom variables and tracking will not work properly.

You can find Ga.php at the following location: \app\code\core\Mage\GoogleAnalytics\Block\.

Make a copy of the file ga.php and place it in the following folder structure in your “Local” code pool: app\code\local\Mage\GoogleAnalytics\Block\Ga.php

Once you are ready, open up Ga.php and remove the following:

1 _gaq.push(['_trackPageview'{$optPageURL}]);

That’s it for the block file. In the next step we will add in what you need to get the proper tracking.

Modify GA View

To get Google Analytics to work properly, you will need to modify a template file. To start, grab a copy of ga.phtml

the default file location at: \app\design\frontend\base\default\template\googleanalytics\

Your next step is to make a copy of ga.phtml and place it in the template you will use for Magento. So for example to use our version of the file in the modern theme, you will create it at the following location:\app\design\frontend\default\modern\template\googleanalytics\ga.phtml

Before you copy the file over, you need to open it up. Find the following:

1 <;;?php echo $this->;;_getPageTrackingCode($accountId) ?>;;

And add the below right after:

1 _gaq.push(['_trackPageview'<;;?php if(!empty($optPageURL)){echo '{'.$optPageURL.'}';} ?>;;]);

Now you are ready to have some fun in ga.phtml. Here is just a small sampling of the things you can do (at the end we put it together in one file for your convenience):

Customer Group

The below code, will allow you to grab which Customer Group your visitor is part of. If they are not logged in, the will be marked as “Guest”

1 <;;?php
2 //get customer group
3 if(Mage::getSingleton('customer/session')->;;isLoggedIn()){
4       // Get group Id
5       $groupId = Mage::getSingleton('customer/session')->;;getCustomerGroupId();
6       //Get customer Group name
7       $group = Mage::getModel('customer/group')->;;load($groupId);
8       $groupname=$group->;;getCode();
9 }
10 else
11 {$groupname= &amp;quot;Guest&amp;quot;;}
12 ?>;;

The code in your Google Analytics tracking code to be placed in ga.phtml:

1 _gaq.push(['_setCustomVar', 1, 'Customer Type', '<;;?php echo $groupname ?>;;', 2]);

404 Page Tracking

The below code, will allow you to determine what pages lead to a 404, and if they are referred from another website, you will know where the errors are coming from.

1 //This code helps you get a title in Magento
2 $GTitle=$this->;;getLayout()->;;getBlock('head')->;;getTitle();

Then here is how in the code, we can trigger an event to record 404 errors.

1 <;;?php if (strpos($GTitle,'404 Not Found') !== false){  echo&amp;quot;_gaq.push(['_trackEvent', 'Error', '404', 'page: ' + document.location.pathname + + ' ref: ' + document.referrer,1,true]);&amp;quot;;} else {}?>;;

Category Report

In Magento reports, you can get what are the most popular parts, but what if you wanted the most popular categories? Unfortunately,Google needs a key to indicate a category to use their built in Category search. You could use the Content Drill Down in Google Analytics to, but what if you want to see all catgeories and sub-categories at a glance? I have hacked together the below code to make this happen. It will grab categories, and sub categories, and place it in one chart. With one glance you can know what areas are most popular and what product areas you might wish to focus on.

Here is how we grab the category name:

1 //Catgeory Name
2 if(Mage::registry('current_category')){
3 $mcategory = Mage::registry('current_category')->;;getURLPath();
4 $category = str_replace (&amp;quot;.html&amp;quot;,&amp;quot;&amp;quot;,$mcategory);
5 }

This is the GA code to get the data in Google Analytics

1 <;;?php if(!empty($category)) {echo&amp;quot;_gaq.push(['_trackEvent', 'Product Family', 'Pageview', '&amp;quot;.$category.&amp;quot;',1, true]);&amp;quot;;}?>;;

No Products Found

The last one is a functionality Magento has, but I thought, it’s better to have directly in Google Analytics. What if you wanted to know what customer groups were searching for and not finding? Maybe your VIP customers are looking for a brand you do not carry. now you can know. I must commend Drew Hunter for placing me on the right path to getting the number of query results.

Here is the code to grab the search term when reuslts are “0″:

1 <;;?php
2 if($this->;;getRequest()->;;getControllerName()=='result')
3 {
4 $nores = Mage::helper('catalogsearch')->;;getEngine()->;;getResultCollection()->;;addSearchFilter(Mage::helper('catalogsearch')->;;getQuery()->;;getQueryText())->;;getSize();
5 }
7 //Search Term
8 if (isset($nores)){
9 if ($nores ==0)
10 //Search Term
11 {$SQuText = Mage::helper('catalogSearch')->;;getQueryText();
12 }
13 }
14 ?>;;

The code to make this record in Google Analytics is the following:

1 <;;?php if (isset($nores)){if ($nores ==0 &amp;amp;&amp;amp; (!empty($SQuText)) ) {echo&amp;quot;_gaq.push(['_trackEvent', 'No Result', 'Search', '&amp;quot;.$SQuText.&amp;quot;',1, true]);&amp;quot;;}}?>;;


There you have it! Your Google Analytics in Magento is now on steroids. Your data will be in Events report (like so):

event magento ga Magento And Google Analytics

And your customer groups can be used as an advanced segment you can use to slice and dice your data:

advanced segments magento Magento And Google Analytics

To make things even easier, we have added all the above to one file, so you just need to grab it from here and go. Please note, the ga.phtml file assumes you are using the Modern theme, so you should copy/place to the appropriate theme location. Also use what we need, comment out what you don’t and make sure to test in your dev environment before launching. This code was written using the latest Magento release

I should also mention, Google has an event limit of 10 events every 5 seconds (also know as the Token Bucket Limit), so for a high traffic site, you might not have all events recorded, but it should be good enough to get going.

Happy Tracking!