Hi there,
I have a website that displays a listing of items from a dataset. Each item has a contact form. When a contact form is submitted, I want to track the URL from the item page so I know which item the user has submitted the form on. I have the code needed to implement this below:
$w("#datasetName"#dataset#datasetName").onBeforeSave(function() {
$w("#datasetName"#datasetName").setFieldValue('pageUrl', wixLocation.url);
});
Question 1: I know I must replace the names above to match my website. Which areas need replacing?
Question 2: I know I must add this code on my page’s $w.onReady()
. Which page do I add this code to? Is it my listings page, my item page, or another page?
Question 3: I know I must add an import for wix-location:
import wixLocation from 'wix-location';
Which page do I add this code to? Is it the same page as question 2?
Thanks for your help in advance!
Cheers,
Cameron
I do suggest that you read about using on before save first before you do anything else.
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#onBeforeSave
As for the start of your code, it should look something like this below, which should really be placed on the same page that the form is on too.
import wixLocation from 'wix-location';
$w.onReady(function () {
$w("#datast1").onBeforeSave(() => {
You can also look at adding a datahook to the dataset if that is a better option.
Hooks - Velo API Reference - Wix.com
Velo: Using Data Hooks | Help Center | Wix.com
Velo Tutorial: Processing User Input Before it is Stored in a Collection with Data Hooks | Help Center | Wix.com
Hey there,
Thanks for your reply, really appreciate it! It looks like a datahook would be a better option. I am not experienced with code but from my understanding from the articles linked it looks like I need to use a beforeInsert() hook.
Am I on the right path with the code below? I know I must replace myCollection with my dataset name, but what extra code must I add, to collect the full URL of the current page, and add that URL to a field in the dataset?
export function myCollection_beforeInsert(item, context) {
let hookContext = context; // see below
// some change to the received item
return item;
}
Cheers,
Cameron
Hi there,
Thanks for your help. I have tried adding the following code:
import wixLocation from 'wix-location';
let url = wixLocation.url;
$w.onReady(function () {
$w("#dataset1").onBeforeSave(() => {
$w("#dataset1").setFieldValue("candidateurl", url);
});
});
candidateurl is the field in my dataset I want the URL to fill.
No luck though. Any suggestions?
Cheers,
Cameron
@cameron54828 HI, could you find the solution for your question? I have the same issue