jquerygoogle-analyticsweb-analyticsdouble-submit-prevention

Google Analytics - event tracking - prevent multiple button clicking


I have contact button on page. Button on click shows contact number. Button is linked with Google Analytics goal > event track > on button click > fires script.

If i want to prevent multiple number of clicks (script firing) and have more relevant data from analytics, i wrote the code, that prevenets script firing more than 1 click.

here is the link of website http://www.seoptimizacija.net/#kontakt

here is jquery code

var counter = 0;
$("#kontakt-dugme").click(function (){

if (counter < 1){
$(this).val("(+381)61 64 87 420");
_gaq.push(['_trackEvent', 'kontakt', 'klik-na-dugme', 'prikaz']);
counter++;
}
else stop();

});

Need opinion?


Solution

  • Just use the Jquery .one event handler

    Attach a handler to an event for the elements. The handler is executed at most once per element per event type.

    $(document).ready(function() {
        $("#kontakt-dugme").one('click', function (){
           _gaq.push(['_trackEvent', 'kontakt', 'klik-na-dugme', 'prikaz']);
        });
    });