Q:

How to count when user don't click on button?

I am doing a metric about:
"Number of daily transactions abandoned (not completed) by end users"
So I am thinking about count when the user doesn't click on the button 
<form action="next.php">
  First name:<br>
  <input type="text" name="firstname" value="Mickey">
  <br>
  Last name:<br>
  <input type="text" name="lastname" value="Mouse">
  <br><br>
  <input type="submit" value="Submit">
</form> 

I've been trying to implement something like this
html>
    <head>
        <script>
            function clickCounter() {
                if (typeof (Storage) !== "undefined") {
                    if (localStorage.clickcount) {
                        localStorage.clickcount = Number(localStorage.clickcount) + 1;
                    } else {
                        localStorage.clickcount = 1;
                    }
                    document.getElementById("result").innerHTML = "You have clicked the button " + localStorage.clickcount + " time(s).";
                } else {
                    document.getElementById("result").innerHTML = "Sorry, your browser does not support web storage...";
                }
            }
        </script>
    </head>
    <body>
        <p><button onclick="clickCounter()" type="button">Click me!</button></p>
        <div id="result"></div>
        <p>Click the button to see the counter increase.</p>
        <p>Close the browser tab (or window), and try again, and the counter will continue to count (is not reset).</p>
    </body>
</html>

but it doesn't work properly. Help me please

A:

localStorage is not going to be useful here, because the data is kept locally on each individual user's machine; you'd still need some secondary script to send that information to the server where you can get at it... which could more easily be done without storing it locally in the first place.   
The button's click event is definitely not going to be any use, because you're trying to track when that event does not occur.  
The onbeforeunload event is also of limited use here: it won't count what you're trying to count, because it fires when the page unloads for any reason, whether that's closing the window or navigating to the next page.  More importantly, most browsers won't allow asynchronous code (such as an ajax request) to run on that event, so you wouldn't be able to capture any information on the server anyway.
Fortunately, you probably already have all the information you need without adding any code on the client side at all!  Look at your server logs. Count the number of times page 1 was served. Count the number of times page 2 was served. Subtract one from the other, and you'll get the number of times somebody abandoned after step 1.

