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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This article discusses how to create an extensible mechanism for linked drop downs.
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 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)

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now