简体   繁体   中英

Google Analytics Event Tracking not working Magento 1.5.0.1

Event tracking is not working with my Magento 1.5.0.1 CE installation. I have updated the code app/code/local/Mage/GoogleAnalytics/Block/GA.php to:

 <!-- BEGIN GOOGLE ANALYTICS CODE v2 -->
<script type="text/javascript">
//<![CDATA[

var _gaq = _gaq || [];
' . $this->_getPageTrackingCode($accountId) . '
' . $this->_getOrdersTrackingCode() . '
_gaq.push(["_trackPageLoadTime"]);

(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>
<!-- END GOOGLE ANALYTICS CODE -->';

Then I added an event tracking link to my homepage:

<a href="/page-to-link-to" onClick="_gaq.push(['_trackEvent', 'Homepage Events', 'Category Headings', 'Event Im Tracking']);">LINK</a>

So I tested this in firefox via firebug and the events are not working. Can someone please help a brother out?

Also the code is being inserted after the opening tag.

This is how it renders:

<!-- BEGIN GOOGLE ANALYTICS CODE v2 -->
<script type="text/javascript">
//<![CDATA[
    var _gaq = _gaq || [];

_gaq.push(['_setAccount', 'UA-XXXXXX-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>
<!-- END GOOGLE ANALYTICS CODE -->

Thanks!

You might not see it in Firebug because it executes so quickly. I would recommend testing via a proxy tool or the Live HTTP Headers plugin. See the tools recommended here.

Your syntax is correct.

Additionally, you may need to add a setTimeout() of 500ms or so to delay the click so that you don't encounter a race condition where the browser goes to that link prior to completing the execution of the tracking call. I have an example of this on a blog post I wrote:

<a href="/page-to-link-to" onclick="var that=this;_gaq.push(['_trackEvent', 'Homepage Events', 'Category Headings', 'Event Im Tracking']);setTimeout(function(){location.href=that.href;},500);return false;">LINK</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM