/contador.js Secret
Created
March 2, 2021 02:24
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(() => { | |
/* | |
Script que cuenta la visita y la envía al servidor con PHP | |
Solo tienes que incluir este script o código en todas las páginas en donde quieras registrar las visitas | |
y los visitantes | |
https://parzibyte.me/blog | |
*/ | |
document.addEventListener("DOMContentLoaded", async () => { | |
try { | |
// Preferiblemente debería ser la URL absoluta | |
// Ejemplo: http://localhost/contador_visitas_php_avanzado/contador/registrar_visita.php | |
const url = "./contador/registrar_visita.php"; | |
const payload = { | |
pagina: document.title, | |
url: window.location.href, | |
}; | |
const respuestaRaw = await fetch(url, { | |
method: "POST", | |
body: JSON.stringify(payload), | |
}); | |
const respuesta = await respuestaRaw.json(); | |
if (!respuesta) { | |
console.log("Error registrando visita"); | |
} | |
} catch (e) { | |
console.log("Error registrando visita: " + e); | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment