?
Solved

How do I Update Javascript Event Tracking Code to Not Track Internal Javascript Links as Outgoing Links in Google Analytics

Posted on 2013-12-17
4
Medium Priority
?
578 Views
Last Modified: 2016-02-24
Hello, I have been using the following javascript to track outgoing links on our asp.net website www.competitiveedgeproducts.com and it is working pretty good except that it is also tracking javascript links on the website that don't take people to another site as Outgoing links.  For example it is tracking a lot of javascript links like this:
javascript:{if (typeof(Page_ClientValidate) != 'function' || Page_ClientValidate()) __doPostBack('SearchTemplate13$DataGrid1$_ctl19$_ctl1','')}

and classifying them as an Outgoing Link which I don't want.  Could anyone tell me what to change in this code to track internal javascript links as Internal links instead of outgoing links or not even track them with this code?
 
<script type="text/javascript">
/* 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


Thanks in advance!
Shawn
0
Comment
Question by:smower
  • 2
4 Comments
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 39726412
replace :
if(el.href.indexOf(location.host) == -1){ /* external link */

Open in new window

by :
if(el.href.indexOf(location.host) == -1 && el.href.indexOf("javascript:")!=0 && el.onclick == null ){ /* external link, href don't have javascript and link don't have onclick attribute set */

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39726577
Or MUCH better:

change

<a href="javascript:{if (typeof(Page_ClientValidate) != 'function' || Page_ClientValidate()) __doPostBack('SearchTemplate13$DataGrid1$_ctl19$_ctl1','')}"

to

<a href="#" onclick="if (typeof(Page_ClientValidate) != 'function' || Page_ClientValidate()) __doPostBack('SearchTemplate13$DataGrid1$_ctl19$_ctl1',''); return false">
0
 

Author Closing Comment

by:smower
ID: 39727359
Ok. It seems to be working in my limited tests so far.  Thank you so much for your help on this!
0
 

Author Comment

by:smower
ID: 39727374
Thank you mplungjan,

Your suggestion sounds great also, but I am using an old asp.net 1.1 site and it seems to be generating those javascript links automatically and I am sure how to change those or comfortable trying to tweak that programmatic part of the code.  The javascript change seems to be working so I am going to monitor those results for a while to see if it will keep performing properly.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Building a cohesive image for your brand is vital to making an impression on consumers. When the economy is tough, brands do better than unbranded  products. This can have a huge impact on your long-term profits, as the economy goes up and down.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

809 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