Need Help with Google Analytics Outbound Link Tracking JavaScript.


I have a blog and a plug-in that allows me to build bundles for Amazon items. I have the site track outbound links with the following javascript code to track in Google Analytics:

<script type="text/javascript">// <![CDATA[
/* Attach tracking to all download & external links */
var _gaq = _gaq || [];

function _gaLt(event){
        var el = event.srcElement ||;

        /* Loop up the tree through parent elements if clicked element is not a link (eg: an image in a link) */
        while(el && (typeof el.tagName == 'undefined' || el.tagName.toLowerCase() != 'a' || !el.href))
                el = el.parentNode;

        if(el && el.href){
                if(el.href.indexOf( == -1){ /* external link */
                        _gaq.push(["_trackEvent", "Outgoing Links", el.href, document.location.pathname +]);
                        /* if target not set delay opening of window by 0.5s to allow tracking */
                        if(! ||^_(self|parent|top)$/i)){
                                        document.location.href = el.href;
                                /* Prevent standard click */
                                event.preventDefault ? event.preventDefault() : event.returnValue = !1;


/* Attach the event to all clicks in the document */
var d = document;
d.addEventListener ? d.addEventListener("click",_gaLt,!1) : d.attachEvent && d.attachEvent("onclick",_gaLt);
// ]]></script>

Open in new window

This code works great tracking most of my outbound links however the "buy now" button that this plug-in builds is not getting tracked. Can somebody show me what I need to update my code to to get it to track the buy now button on the following page?
Who is Participating?
smowerConnect With a Mentor Author Commented:
I discovered that this can be done with Google tag manager. I set up a click event tracking code and narrowed it down to the element ID.
Ray PaseurCommented:
No points for this, but with a "buy now" button you wouldn't really be tracking clicks, you would be fulfilling orders, right?
smowerAuthor Commented:
I wouldn't be fulfilling those. They are fulfilled by Amazon if the customer completes checkout at Amazon. I just want to know when people click that button. That button is created by a wordpress plugin so I don't know a way to modify it and need some on the fly JavaScript to tag it or something.
smowerAuthor Commented:
If I can't get the actual link, is there a way to just get an event that they clicked on the button?  It seems very doable but my javascript experience isn't that great.
smowerAuthor Commented:
I got it  working.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.