Visit Counter Cookie
Quantify your audience's loyalty. This simple yet powerful script tracks repeat visits by incrementing a counter stored within a user's browser cookies, allowing you to acknowledge and greet your most frequent visitors.
Loyalty Counter Demo
1
Visits to this Page
Refresh the page to see the counter increase!
Copy the Script
<script>
// 1. Set Expiration (1 Year)
var exp = new Date();
exp.setTime(exp.getTime() + (365 * 24 * 60 * 60 * 1000));
// 2. Helper to Read Cookie
function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}
// 3. Logic: Read, Increment, Save
var visits = getCookie("counter");
if (!visits) {
visits = 1; // First visit
} else {
visits = parseInt(visits) + 1; // Increment
}
// 4. Update Cookie
document.cookie = "counter=" + visits + "; expires=" + exp.toUTCString() + "; path=/";
// 5. Display Result
document.write("You have visited this page " + visits + " times!");
</script>
Frequently Asked Questions
Yes. Since cookies are stored locally on the user's device, a knowledgeable user can easily edit the cookie value or delete it entirely to reset their count.
This script tracks visits per browser, not unique people. If one person uses three different browsers (e.g., Chrome, Safari, and Firefox), they will have three separate visit counts.
Yes. Simply remove the document.write(...) line at the end of the script. The cookie will still update silently in the background, allowing you to use the variable for other logic.