Track outbound links to GA on jekyll
- Add jQuery to your jekyll blog if its not there yet. I am adding it to _includes/head.html
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
- Create additional include where we will make our magick. I did this in _includes/google__analytics.html.
$(document).ready(function(){
$('.ga-link').on('click', function(){
if (typeof(ga)!=="undefined"){
ga('send',{
hitType: 'event',
eventCategory: 'click',
eventAction: 'outbound',
eventLabel: $(this).attr('href')
});
}
});
)};
File with this code is included in default.html
{% include google_analytics.html %}
- Add class=’ga-link’ to all links you want to track to google analytics.
<a href="https://github.com/timoshenkoav" class='ga-link' target='_blank'>
I`ve created such include to help generate links tracked:
<a href="" class="ga-link" target='_blank'></a>
Now if i want to have such link i can use:
{% include ga_link.html
title="GitHub"
url="https://github.com"
%}