This repository contains example code for sending data to a Google Form using sendBeacon() after the user is finished with the page, as determined with visibilitychange. This is preferable to using beforeunload or unload, as explained here. Overall, this code may be useful if you need to submit data such as analytics or time-tracking, right as the user exits the page.
On the edit page of your Google Form, click the three dots on the upper right, then Select “get pre-filled link.” This link should look like https://docs.google.com/forms/d/e/[1]/[2], where [1] is your form ID, and [2] contains additional information about your form. From [2], select the query string, where keys are of the form entry.xxx, and values are the values that you want to submit to your form. In your script, Set these values to whatever you wish. Modify beacon.js to include your form ID and query string.