Solved

Need Help with Google Analytics Outbound Link Tracking JavaScript.

Posted on 2014-07-26
7
489 Views
Last Modified: 2016-02-24
Hello,

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 || event.target;

        /* 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(location.host) == -1){ /* external link */
                        _gaq.push(["_trackEvent", "Outgoing Links", el.href, document.location.pathname + document.location.search]);
                        /* if target not set delay opening of window by 0.5s to allow tracking */
                        if(!el.target || el.target.match(/^_(self|parent|top)$/i)){
                                setTimeout(function(){
                                        document.location.href = el.href;
                                }.bind(el),500);
                                /* 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?

http://www.largeoutdoorstoragesheds.com/blog/lifetime-8x10-storage-shed-windows-skylights-shelving-shutters-bundle/
0
Comment
Question by:smower
  • 4
7 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40222981
No points for this, but with a "buy now" button you wouldn't really be tracking clicks, you would be fulfilling orders, right?
0
 

Author Comment

by:smower
ID: 40223865
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.
0
 

Author Comment

by:smower
ID: 40245262
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.
0
 

Accepted Solution

by:
smower earned 0 total points
ID: 40247889
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.
0
 

Author Closing Comment

by:smower
ID: 40255154
I got it  working.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Concerto Cloud Services, a provider of fully managed private, public and hybrid cloud solutions, announced today it was named to the 20 Coolest Cloud Infrastructure Vendors Of The 2017 Cloud  (http://www.concertocloud.com/about/in-the-news/2017/02/0…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question