Back to all resources
user journey
localStorage
form population
URL tracking

Track User Localstorage

By Jared Malan
The provided code consists of two main functions: trackUserJourney and populateUserHistory. The trackUserJourney function is designed to track the user's navigation history on a website. It retrieves the current URL and checks if a 'userJourney' array exists in the browser's localStorage. If it does, the function parses this array; if not, it initializes an empty array. The current URL is then added to this array if it is not already present, and the updated array is saved back to localStorage. This function is called immediately to ensure that the user's journey is tracked as soon as the script runs.

The populateUserHistory function is intended to populate a form field with the user's navigation history. It retrieves the 'userJourney' array from localStorage and, if it exists, finds a form field with the ID 'user-history'. The function then sets the value of this form field to a comma-separated string of the URLs in the 'userJourney' array. This function is meant to be called when the contact form page is loaded, ensuring that the user's navigation history is displayed in the form.