How Can I Create A Shopify Contact Form That Will Know Which Product Page A User Came From?

- 1 answer

We created code to display "rental only" and a link to a rental/contact form when users land on a certain product. However, we need a way to pass in a hidden field that will tell us which product the user came from. How can we do that?



You can use browser cache to save the page/last visited page. Trigger the following code when someone click on the link to the contact form


Then follow the instruction on this page - Add Fields to the Liquid Contact Form

One a new field is added, hide it using css: display:none and give an id="something" to it.

On page load use JS to fill that field from the browser cache:

  var last_page = localStorage.getItem('last_visited_page')

If they directly contact you without visiting a required page it'll blank. Since localStorage is a permanent cookie, it'll stay there even if the user closes the brower (unless he clears local cache and cookies.

More details on usage of local storage/browser