How do I know if a user has clicked 3 times through my site using Javascript

Is there a way in JavaScript to tell if a user has clicked on the same domain 2 or more times?

I need a popup after the user has clicked 3 times anywhere on the site. I know how to do it after one click - with document.referrer

or addEventListener

, but then I got lost.

I need something that will capture all click events (not just links) and count them.

+1


source to share


3 answers


I tried this and it worked fine:



window.onload = function() {
    var clicked = readCookie('popunder');
    if(clicked == null) {
        clicked = 0;
    }

    var allLinks = document.getElementsByTagName("a");
    for(i=0;i<=allLinks.length;i++) {
        allLinks[i].addEventListener("click",countClicks,true);
    }

    function countClicks() {           
        if(clicked == 2) {
            popunder(); //something to do
        } else {
            clicked++;
            doCookie('popunder', clicked, 1);
            alert(clicked);
        }
    }

    function popunder() { alert('thats 3 clicks!'); }

    function doCookie(name,value,days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
        } else {
            var expires = "";
        }
        document.cookie = name+"="+value+expires+"; path=/";
    }

    function readCookie(name) {
        var readName = name + "=";
        var cSplit = document.cookie.split(';');
        for(var i=0;i < cSplit.length;i++) {
            var sc = cSplit[i];
            while (sc.charAt(0)==' ') sc = sc.substring(1,sc.length);
            if (sc.indexOf(readName) == 0) return sc.substring(readName.length,sc.length);
        }
        return null;
    }
}

      

+1


source


Sure. You need to store the list of user click events, either in a cookie or in a server side data store. On each click recorded, increase the score by one and do your thing when the number reaches 3.



Try using session cookies to store state between pages - they are fast, fairly widely compatible, and will be zeroed out when the browser is turned off to avoid spamming your users' cookies.

+2


source


Thanks for all your advice. I have tried this code. But after the update the variable is clicked again goes to 0. I need to store each new value by clicking on the cookie (or whatever), so its number will grow with each click on the link on the page. Is it possible to change the value of a variable in a cookie this way?

window.onload = function () {

var **allLinks** = document.getElementsByTagName("a");

var **clicked** = 0;

**doCookie**('popunder',clicked,1);

for(i=0;i<=allLinks.length;i++){
    allLinks[i].addEventListener("click",countClicks,true);
}

function **countClicks**(){           
        if(clicked == 3){
            popunder(); //something to do
        }
        else{
            alert(readCookie('popunder'));
            return clicked++;
        }
}

function **doCookie**(name,value,days) {
if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

function **readCookie**(name) {
    var readName = name + "=";
    var cSplit = document.cookie.split(';');
    for(var i=0;i < cSplit.length;i++) {
        var sc = cSplit[i];
        while (sc.charAt(0)==' ') sc = sc.substring(1,sc.length);
        if (sc.indexOf(readName) == 0) return sc.substring(readName.length,sc.length);
    }
        return null;
}

      

0


source







All Articles