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
561 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 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

691 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