Here is a simple JavaScript script that uses localStorage to keep track of visitor count, unique daily visitor count, and unique monthly visitor count:
javascript
// Get today's date
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();
// Check if localStorage item exists for today's date
if (!localStorage.getItem(yyyy + '-' + mm + '-' + dd)) {
// If it doesn't exist, set it to 1
localStorage.setItem(yyyy + '-' + mm + '-' + dd, 1);
} else {
// If it does exist, increment it by 1
var dailyVisitorCount = localStorage.getItem(yyyy + '-' + mm + '-' + dd);
localStorage.setItem(yyyy + '-' + mm + '-' + dd, parseInt(dailyVisitorCount) + 1);
}
// Check if localStorage item exists for this month
if (!localStorage.getItem(mm + '-' + yyyy)) {
// If it doesn't exist, set it to 1
localStorage.setItem(mm + '-' + yyyy, 1);
} else {
// If it does exist, check if today's date is the first of the month
if (dd == '01') {
// If it is, increment it by 1
var monthlyVisitorCount = localStorage.getItem(mm + '-' + yyyy);
localStorage.setItem(mm + '-' + yyyy, parseInt(monthlyVisitorCount) + 1);
}
}
// Get the total visitor count
var totalVisitorCount = 0;
for (var i = 0; i < localStorage.length; i++) {
totalVisitorCount += parseInt(localStorage.getItem(localStorage.key(i)));
}
// Get the unique daily visitor count
var uniqueDailyVisitorCount = 0;
for (var i = 0; i < localStorage.length; i++) {
if (localStorage.key(i).includes(yyyy + '-' + mm + '-' + dd)) {
uniqueDailyVisitorCount += 1;
}
}
// Get the unique monthly visitor count
var uniqueMonthlyVisitorCount = 0;
for (var i = 0; i < localStorage.length; i++) {
if (localStorage.key(i).includes(mm + '-' + yyyy)) {
uniqueMonthlyVisitorCount += 1;
}
}
// Log the results
console.log('Total visitor count: ' totalVisitorCount);
console.log('Unique daily visitor count: ' uniqueDailyVisitorCount);
console.log('Unique monthly visitor count: ' uniqueMonthlyVisitorCount);
This script uses localStorage to store the number of visitors for each day and month. It first checks if there is an existing localStorage item for today's date and month. If there is, it increments the count by 1. If there isn't, it sets the count to 1.
The script then loops through all the localStorage items to get the total visitor count, unique daily visitor count, and unique monthly visitor count. It logs the results to the console.
Note that this script assumes that the user's browser supports localStorage. If you need to support older browsers, you may need to use a different method to store the data.