-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
śledzenie linków wychodzących - pierwsze podejście
- Loading branch information
Showing
2 changed files
with
29 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Track outbound links | ||
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 */ | ||
ga("send", "event", "Outgoing Links", el.href, document.location.pathname + document.location.search); | ||
/* if target not set then 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; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
leafnode
Author
Contributor
|
||
}.bind(el),500); | ||
/* Prevent standard click */ | ||
event.preventDefault ? event.preventDefault() : event.returnValue = !1; | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
/* Attach the event to all clicks in the document after page has loaded */ | ||
var w = window; | ||
w.addEventListener ? w.addEventListener("load",function(){document.body.addEventListener("click",_gaLt,!1)},!1) | ||
: w.attachEvent && w.attachEvent("onload",function(){document.body.attachEvent("onclick",_gaLt)}); |
This way middle clicks also get affected, ie. I can't click a link with the mouse wheel to open in a new tab, because the script prevents it.
I think it's enough to just register the
onclick
event on allA
elements, and set it to trigger thega("send", "event"...)
function, without preventing the default action.