Tracking User Page Visits
ATTENTION: THIS IS OUR OLD SITE. TO SEE OUR NEW WEBSITE, CLICK HERE.
It is often useful to know and record which users are visiting which pages and when on your website. This code example shows you how to record and track your users page visits for later analysis, as is done for WixCodeBank.
The first step is to create a collection to store the page view history, similar to the screenshot below. Add the fields which are important for you to keep a history of. The created date field is automatically recorded and does not need to be added explicitly. The collection needs to allow anyone to update and add to it. This example creates a collection called ViewHist for saving historical page view information.
For this example, a page view is recorded every time a user visits a dynamic item page in the WixCodeBank (as you are doing now). The following code creates a function to insert data into the ViewHist table from the dbCustomer and dbCodeBank datasets of the dbCodeBank dynamic item page. If the current user is not logged in, then the emailAddress inserted into ViewHist is set to 'Unknown', else the users email address is inserted from dbCustomer. Title and item number are other data points inserted from dbCodeBank. The saveViews() function can be called anywhere on your dynamic item page to trigger a view save, and is best used with a time delay to ensure it is not an accidental page visit, e.g.: setTimeout(saveViews,3000);