Skip to content

Commit

Permalink
śledzenie linków wychodzących - pierwsze podejście
Browse files Browse the repository at this point in the history
  • Loading branch information
leafnode committed Apr 12, 2014
1 parent 4699ba0 commit b3d84e5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ <h2><span class="glyphicon glyphicon-globe"></span> Media</h2>
</div>
</div> <!-- /container -->

<!-- Google Analytics: change UA-XXXXX-X to be your site's ID. -->
<script>
(function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
Expand All @@ -144,5 +143,6 @@ <h2><span class="glyphicon glyphicon-globe"></span> Media</h2>
r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
ga('create','UA-49963542-1');ga('send','pageview');
</script>
<script src="js/main.js"></script>
</body>
</html>
28 changes: 28 additions & 0 deletions js/main.js
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.

Copy link
@mpaluchowski

mpaluchowski Apr 16, 2014

Contributor

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 all A elements, and set it to trigger the ga("send", "event"...) function, without preventing the default action.

This comment has been minimized.

Copy link
@leafnode

leafnode Apr 17, 2014

Author Contributor

True, I'll look into this. I thought onclick would not happen for those links, but didn't test it tough.

This comment has been minimized.

Copy link
@wojtekerbetowski

wojtekerbetowski Apr 21, 2014

Contributor

I've added an issue for that. #14

}.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)});

0 comments on commit b3d84e5

Please sign in to comment.