Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
572 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 82

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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
One event, two days, a great line-up of speakers, and 48% female presence. Still have no idea what I’m talking about?
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

636 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