Here's a simple JavaScript script that uses local storage to store user visit information:
javascript
// Get the current date
var today = new Date();
var todayString = today.toISOString().split('T')[0];
// Get the visitor count from local storage
var visitorCount = parseInt(localStorage.getItem('visitorCount')) || 0;
// Increment the visitor count
visitorCount++;
// Set the visitor count in local storage
localStorage.setItem('visitorCount', visitorCount);
// Get the unique daily visitor count from local storage
var uniqueDailyVisitorCount = parseInt(localStorage.getItem('uniqueDailyVisitorCount')) || 0;
// Check if the visitor count for today is new
var uniqueDailyVisitorCountToday = localStorage.getItem('uniqueDailyVisitorCount' + todayString);
if (!uniqueDailyVisitorCountToday) {
// If it's a new day, increment the unique daily visitor count
uniqueDailyVisitorCount++;
// Set the unique daily visitor count for today in local storage
localStorage.setItem('uniqueDailyVisitorCount' + todayString, true);
}
// Get the unique monthly visitor count from local storage
var uniqueMonthlyVisitorCount = parseInt(localStorage.getItem('uniqueMonthlyVisitorCount')) || 0;
// Check if the visitor count for this month is new
var thisMonth = today.getFullYear() + '-' + (today.getMonth() + 1);
var uniqueMonthlyVisitorCountThisMonth = localStorage.getItem('uniqueMonthlyVisitorCount' + thisMonth);
if (!uniqueMonthlyVisitorCountThisMonth) {
// If it's a new month, increment the unique monthly visitor count
uniqueMonthlyVisitorCount++;
// Set the unique monthly visitor count for this month in local storage
localStorage.setItem('uniqueMonthlyVisitorCount' + thisMonth, true);
}
// Set the unique monthly visitor count in local storage
localStorage.setItem('uniqueMonthlyVisitorCount', uniqueMonthlyVisitorCount);
This script uses local storage to store the visitor count, unique daily visitor count, and unique monthly visitor count. It first gets the current date and converts it to a string. It then retrieves the visitor count from local storage, increments it, and sets it back in local storage.
The script then checks if the visitor count for today is new by checking if a value for today's date has been stored in local storage. If it's a new day, the unique daily visitor count is incremented and stored in local storage.
Finally, the script checks if the visitor count for this month is new by checking if a value for this month has been stored in local storage. If it's a new month, the unique monthly visitor count is incremented and stored in local storage. The unique monthly visitor count is also stored in local storage.
// Get the current date
var today = new Date();
var todayString = today.toISOString().split('T')[0];
// Get the last visit date from local storage
let lastVisit = localStorage.getItem('lastVisit');
// Check if there is a last visit date stored
if (!lastVisit) {
// If there is no last visit date, set it to today's date
localStorage.setItem('lastVisit', todayString);
} else {
// If there is a last visit date, compare it with today's date
let lastVisitDate = new Date(lastVisit);
if (today > lastVisitDate) {
// If today's date is greater than the last visit date, increment the visitor count
let visitorCount = parseInt(localStorage.getItem('visitorCount')) || 0;
visitorCount++;
localStorage.setItem('visitorCount', visitorCount);
// Increment the unique daily visitor count
let uniqueDailyVisitorCount = parseInt(localStorage.getItem('uniqueDailyVisitorCount')) || 0;
uniqueDailyVisitorCount++;
localStorage.setItem('uniqueDailyVisitorCount', uniqueDailyVisitorCount);
// Increment the unique monthly visitor count
let monthlyVisit = parseInt(localStorage.getItem('monthlyVisit')) || 0;
monthlyVisit++;
localStorage.setItem('monthlyVisit', monthlyVisit);
}
}
// Store the current date in local storage as the last visit date
localStorage.setItem('lastVisit', todayString);
This code first gets the current date and stores it in the today variable. It then retrieves the last visit date from local storage and checks if it exists. If there is no last visit date, it sets it to today's date.
If there is a last visit date, the code compares it with today's date. If today's date is greater than the last visit date, it means the user has visited the website again. In this case, the visitor count, unique daily visitor count, and unique monthly visitor count are incremented and stored in local storage.
Finally, the current date is stored in local storage as the last visit date.