I want to store page visits to my own custom post type. I want to do this entirely in JavaScript. When a visitor arrives on my page, I want to immediately generate a new 'visit' post and store the referring URL, along with a timestamp.
I realize that the web log will already contain this information, but I will be doing some filtering and only generating new visit posts under certain circumstances, so this will be useful to me.
How do I make a new, custom post entirely in Javascript?
You could initiate the process with an ajax submission that passes the required data to a custom function on the server that generates the post. There are a lot of tutorials about that online, see for example: hidden link
Or you could dispense with JavaScript and simply handle everything with PHP.
I need to display a form for the user to generate a custom post. It's on that same page that I need to automatically generate a different custom post to record the visit to the page, along with the URL variable info. If I use PHP, I think I'll end up with unwanted page reloads.
If I understand this correctly, the REST API should allow me to generate the custom post to record the visit without having to do any reloading and then the visitor would optionally use the form from there.
If I use PHP, I think I'll end up with unwanted page reloads.
Not necessarily, the solution Nigel recommended would allow you to generate a post programmatically during fulfillment of the initial page request. In that scenario, the programmatically generated post would technically be generated before the page load occurs, at an earlier point in the request lifecycle. No redirects are required, because the whole post generation process occurs before the page loads. If your requirement is that the visit-logging post is technically generated after the page load occurs, then a JavaScript solution with the REST API is more appropriate, or a PHP script you call as the src of an iframe that exists in the same page as the Form.
If I understand this correctly, the REST API should allow me to generate the custom post to record the visit without having to do any reloading and then the visitor would optionally use the form from there.
Yes, that is correct. Your page should include JavaScript that triggers an AJAX request to the REST API. The REST API method you call should insert a new post programmatically without requiring any reload or redirect, it can be completely unknown to the end user. Your visitors could optionally use the Form to submit a new post, completely independent of the post created automatically.