I've been fiddling with various snippets of Javascript for the purposes of Google Analytics event tracking. I'm using Google Tag Manager. This question is also similar to some questions I posted recently so apologies to the smaller pool of users who follow the google analytics tag and are seeing the same thing.
Currently I'm working with this snippet:
<script type="text/javascript">
$(document).ready(function(){
$('.app-cta a').onClick=_gaq.push(['_trackEvent', 'App', 'Click', 'iOS']);
});
</script>
Within httpfox the event parameters (App, Click, iOS) are all showing. But not in Google Analytics.
I'm told that it is fairly mon practice to add a delay to the link click of anywhere between 5 and 500 milliseconds. This is because, I'm told, that sometimes the browser hits the new site before it's had time to pass the analytics parameters.
There may be alternative means of correcting this but for my own curiosity of learning how to use Javascript for analytics, how would I integrate setTimeout to the above code?
I tried this:
<script type="text/javascript">
$(document).ready(function(){
setTimeout(function(){
$('.app-cta a').onClick=_gaq.push(['_trackEvent', 'App', 'Click', 'iOS']);
});
},500);
</script>
But surely this delays the Google Analytics tag from passing the data, rather than delaying the click? I checked and it did not solve my problem.
I've been fiddling with various snippets of Javascript for the purposes of Google Analytics event tracking. I'm using Google Tag Manager. This question is also similar to some questions I posted recently so apologies to the smaller pool of users who follow the google analytics tag and are seeing the same thing.
Currently I'm working with this snippet:
<script type="text/javascript">
$(document).ready(function(){
$('.app-cta a').onClick=_gaq.push(['_trackEvent', 'App', 'Click', 'iOS']);
});
</script>
Within httpfox the event parameters (App, Click, iOS) are all showing. But not in Google Analytics.
I'm told that it is fairly mon practice to add a delay to the link click of anywhere between 5 and 500 milliseconds. This is because, I'm told, that sometimes the browser hits the new site before it's had time to pass the analytics parameters.
There may be alternative means of correcting this but for my own curiosity of learning how to use Javascript for analytics, how would I integrate setTimeout to the above code?
I tried this:
<script type="text/javascript">
$(document).ready(function(){
setTimeout(function(){
$('.app-cta a').onClick=_gaq.push(['_trackEvent', 'App', 'Click', 'iOS']);
});
},500);
</script>
But surely this delays the Google Analytics tag from passing the data, rather than delaying the click? I checked and it did not solve my problem.
Share Improve this question asked Aug 12, 2013 at 14:56 Doug FirDoug Fir 21.4k54 gold badges192 silver badges341 bronze badges3 Answers
Reset to default 3Use Google Analytics' hitCallback
You can set a custom callback on the tracker by using the code documented here.
You might end up with something like:
$( ".app-cta a" ).click(function(e) {
var location = $(this).attr('href'); //get the link location
e.preventDefault(); //disable the link action
_gaq.push(['_set','hitCallback',function() {
window.location = location; //action link after callback
}]);
_gaq.push(['_trackEvent', 'App', 'Click', 'iOS'); //trigger Track Event
return !window._gat; //fallback in case Google Analytics hasn't loaded.
});
I'm not extremely well-versed with Google Analytics but the code below will execute your _gaq.push()
method and then redirect the page 500ms later.
$(document).ready(function() {
$('.app-cta a').on('click', function (e) {
var location = $(this).attr('href');
e.preventDefault();
_gaq.push(['_trackEvent', 'App', 'Click', 'iOS']);
setTimeout(function () {
window.location = location;
}, 500);
});
});
Something like that should work
$(function () {
$('.app-cta a').click(function (e) {
e.preventDefault();
var $this = $(this),
timer = 500,
target = $this.attr('target'),
href = $this.attr('href');
_gaq.push(['_trackEvent', 'App', 'Click', 'iOS']);
setTimeout(function () {
if (target) {
window.open(href, target);
} else {
window.location = href;
}
}, timer);
});
});