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
553 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 82

Accepted Solution

by:
leakim971 earned 500 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

770 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