This script enables visitor tracking by appending a visitorId and sessionId to a form as hidden inputs, and sending a beacon request when the page becomes hidden.
- A web page with a form element
- Add the script to your web page:
<script src="visitor-tracking.js"></script>- Ensure that your server has an endpoint for handling the beacon request:
let url = "/api/tracking";-
When the page loads, the script generates a session ID and appends it to the form as a hidden input.
-
When the page becomes hidden, the script measures the elapsed time since the page was loaded and sends a beacon request containing the
visitorId,sessionId,rid, andelapsedTimeto the server.
This project is licensed under the MIT License.