I developed this js code, that should detect when a site is loaded for the first time or is refreshed. I send this information tho a django view.
The problem is that it works on PC but not on mobile devices like phones, tablets, etc…
Here is the javascript code:
window.addEventListener('load', function() {
// Check if the page has been reloaded
var isReloaded = sessionStorage.getItem('isReloaded');
if (!isReloaded) {
// It's the first visit or a new session
sessionStorage.setItem('isReloaded', 'true');
// Perform actions for the first visit here
}
// Send the reload information to the server
var isRefreshed = isReloaded ? 'true' : 'false';
var csrfToken = '{{ csrf_token }}';
$.ajax({
type: "POST",
url: '',
data: {
'is_refreshed': isRefreshed,
'csrfmiddlewaretoken': '{{ csrf_token }}' // Include CSRF token for security
},
dataType: 'json',
success: function(data) {
// Handle the response from the server here, if needed
console.log(data.message);
},
error: function(xhr, status, error) {
// Handle errors here, if needed
console.error(error);
}
});// Replace with your CSRF token
// Make an AJAX request here to send 'isRefreshed' and 'csrfToken' to the server.
});