fbpx Magento en Google Analytics problemen | Younify

Magento en Google Analytics problemen

Magento 1.4.0.1 has a known bug concerning Google Analytics. If you search Google you will find a lot of questions and a lot of same answers, but these answers are not always the answers that will fix your issue. For example we have had web shops that, even with this fix, are still not getting all the information that client wants processed by Google Analytics.

There are few steps that when you follow you can make sure that Google Analytics will work 100% the way you want it to work. I will try to explain this process as simple as possible.

The first thing that you want to do is to go to your Web Shop home page (or any other page) and view it’s source code. We are looking for Google Analytics code on this page that looks something like this:

<!– 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’ : ‘https://www’) + ‘.google-analytics.com/ga.js’;

(document.getElementsByTagName(‘head’)[0] || document.getElementsByTagName(‘body’)[0]).appendChild(ga);

})();

 

var _gaq = _gaq || [];

_gaq.push([“_setAccount”, “UA-XXXXXXX-XX”]);

_gaq.push([“_trackPageview”, “/”]);

//]]>

</script>

<!– END GOOGLE ANALYTICS CODE –>

If you see code like the one above then you know that your Google Analytics is good, if not then you need to see what is wrong. In case that you don’t see the Google Analytics code at all then you didn’t set the backend correctly. I say that your backend is not set up correctly because Magento 1.4.0.1 has Google Analytics supported by default. To get to Google Analytics configuration for Magento, log in to backend and go to System > Configuration. Under tab Sales you will see Google API. Clicking on it you will go to page where you can set the Google Analytics and Google Base. In Google Analytics select Yes for Enable and in Account Number insert your Google Analytics Account Number ( e.g. UA-358257-3 ). In Google Base enter you Google Analytics information. Now when you have activated Google Analytics go back to your Web Shop and look at the code again. Now you should see the code above on the page but with one difference. At the same time you will get an error that _gaq is undefined and this is the Magento bug that i was talking about at the beginning of this post. To fix this bug login to your web shop via FTP and go to /app/code/core/Mage/GoogleAnalytics/Block/Ga.php and scroll down around 173 and add

var _gaq = _gaq || [];

before

_gaq.push([“_setAccount”, “‘ . $this->getAccount() . ‘”]);

_gaq.push([“_trackPageview”, “‘.$this->getPageName().'”]);

This will fix the Magento bug, also it is bad practice to change the core files in Magento i think that in this case it is the easiest solution. Once you fix the bug go back to your Web Shop and look at the code again, now you should see the code like the one above. This means that the first and main step is finished.

If the code was there in the first place but it was different then the one that i posted you should take a look if the code is there because someone entered it in some .phtml that is loaded on every page or by adding the code in backend in System > Configuration under tab General in Design, section HTML Head in Miscellaneous script text area or in section Footer in Miscellaneous HTML text area. If this is the case i strongly suggest that you backup all the scripts that you found in there, clean everything up and use the default Magento Google API. Then when your finished setting Google API compare the codes to see if something is different and decide witch one is best to be used. Keep in mind that since you already changed Magento’s core file it is not that big of a deal if you change it some more to add some additional information.

Now the next step is to go over the whole Buy process and compare the codes on each page. Starting from Home Page > Category Page > Product Page > My Cart Page > OnePage / OneStepCheckout Page > Success Page.

Code for Home Page, Category Page, Product Page, My Cart and OnePage / OneStepCheckout should be the same, since we are not using any custom codes:

<!– BEGIN GOOGLE ANALYTICS CODE –>

<script type=”text/javascript”>

//<![CDATA[

 

var _gaq = _gaq || [];

_gaq.push([“_setAccount”, “UA-1484882-1”]);

_gaq.push([“_trackPageview”, “/”]);

 

(function() {

var ga = document.createElement(‘script’); ga.type = ’text/javascript’; ga.async = true;

ga.src = (‘https:’ == document.location.protocol ? ‘https://ssl’ : ‘https://www’) + ‘.google-analytics.com/ga.js’;

(document.getElementsByTagName(‘head’)[0] || document.getElementsByTagName(‘body’)[0]).appendChild(ga);

})();

//]]>

</script>

<!– END GOOGLE ANALYTICS CODE –>

Now this is the important part, the Success Page. Here the code is a little bit different since we need to push the products that were sold to Google Analytics:

<!– BEGIN GOOGLE ANALYTICS CODE 1–>

<script type=”text/javascript”>

//<![CDATA[

 

var _gaq = _gaq || [];

_gaq.push([“_setAccount”, “UA-854189-3”]);

_gaq.push([“_trackPageview”, “/checkout/onepage/success/”]);

 

(function() {

var ga = document.createElement(‘script’); ga.type = ’text/javascript’; ga.async = true;

ga.src = (‘https:’ == document.location.protocol ? ‘https://ssl’ : ‘https://www’) + ‘.google-analytics.com/ga.js’;

(document.getElementsByTagName(‘head’)[0] || document.getElementsByTagName(‘body’)[0]).appendChild(ga);

})();

 

//]]>

</script>

<!– END GOOGLE ANALYTICS CODE –>

 

<script type=”text/javascript”>

//<![CDATA[

_gaq.push([“_addTrans”,”100000139″,””,”448.7500″,”71.6500″,”0.0000″,”Amsterdam”,”-“,”NL”]);

_gaq.push([“_addItem”,”100000139″,”160001″,”Voeghamer MD16″,””,”377.1000″,”1.0000″]);

_gaq.push([“_trackTrans”]);

//]]></script>

As you can see at the end of the code on Success Page we have _gaq.push that is adding transaction and adding item. This is what we want on our Success Page, this code is again generated automatically if you have configured the usage of Google API, if you decided to do it the other way (by hardcoding the script yourself) then make sure that you add this code on Success Page.

The Success Page code is generated with function getOrderHtml() inside /app/code/core/Mage/GoogleAnalytics/Block/Ga.php. If you find any error in the script above or you want to add some additional information this is the place that you want to change it, also as i already said, it is not good practice to change Magento core files but since this file has already been changed to fix the known Magento bug i don’t see a reason why not.

Now that we have Google Analytics generating the correct code trough the whole Web Shop, there are few steps that we can take to improve the quality of the code and lower the chances of something going wrong:

– Go thought all pages in your Web Shop and make sure that Google Analytics code is not duplicated.

– Copy googleanalytics.xml from base/default/layout into default/your_theme/layout folder, witch will basically overwrite the file so that we can change it a bit. When you open the file you will see that it will load the GA block by default on every page. I find it a good practice to put this block at the end of every page instead of the beginning. The reason why i practice this is because, when we develop custom Magento themes, sometimes we load some blocks that holds some of the information that we want in Google Analytics after the GA block is loaded and the script will not pick them up, and one other reason is that i’m just used to have the JS scripts at the end of the pages. Now to do this you need to change the <reference name=”after_body_start”> to <reference name=”before_body_end”>

– To make sure that all your orders are pushed to Google Analytics you can do one more thing. Go to /app/code/core/Mage/GoogleAnalytics/Model/ and backup file Observer.php, after backup open it and change

$analyticsBlock->setQuote($quote);

Into:

if ($quoteId == $quote->getId()) {

$analyticsBlock->setQuote($quote);

} else {

$analyticsBlock->setQuote($quoteId);

}

 

After doing all this you can go to Google Analytics and create a Funnel. You will find a good guide on how to do that on Google Analytics web site so i will not go into that now. When you finish all, make several orders in Magento and wait for Google Analytics to show them. Keep in mind that it can take 30 mins to 1 day for Google Analytics to process your infromation.

X