Open source alternative to Google Analytics (server side.)
MIT License
Basically what it does is aside from showing real-time total active clients in the node console it also logs the following information to a MongoDB database:
These are the versions that I had so it may work on your PC if you had an older version installed:
Add this code to your client HTML page (Example):
<script src="https://cdn.socket.io/4.4.0/socket.io.js"></script>
<script>
const socket = io("ADDRESS"); // Enter URL with port
socket.on('socketClientID', function (socketClientID) {
socket.emit('clientMessage', {url: window.location.href, referrer: document.referrer}); // Send url and referrer as an object
});
</script>
Run npm install
in the main folder.
And finally run node index
also in the main folder.
If your website is using HTTPS instead of HTTP you'll need to do the steps which are shown below:
To set up HTTPS support you'll need an SSL certificate. To generate a self-signed certificate, run the following in your shell:
openssl genrsa -out key.pem
openssl req -new -key key.pem -out csr.pem
openssl x509 -req -days 9999 -in csr.pem -signkey key.pem -out cert.pem
rm csr.pem
After getting your .pem
files just change line 7 to 12 (index.js file) with this:
const options = {
key: fs.readFileSync('key.pem'), // Edit key.pem file path if needed (Default: Main folder)
cert: fs.readFileSync('cert.pem'), // Edit key.pem file path if needed (Default: Main folder)
cors: {
origin: '*',
}
}
const https = require('https').createServer(options, app);
const io = require('socket.io')(https, {
cors: {
origin: '*',
}
});
You'll need to add the fs
module so add const fs = require('fs');
at top of the index.js file.
Change line 64 to 66 (bottom of the index.js file) with this:
https.listen(PORT, () => {
console.log("ALL SET!");
});
Lastly change const socket = io("ADDRESS");
to const socket = io("ADDRESS", {secure: true});
where ever your using the client script.