Hook and URL

Hey there!

Is there a way to get the URL of the current session when working with hooks?
Basically I want to use beforeInsert hook to add the current URL of the user to my collection.
Is there a way to access the URL/pass it from the front?

Thanks,
Dor

So add it at the front end level:

import wixLocation from "wix-location";
import wixData from "wix-data";
let item = {
field1: valu1,
field2: value2
}
item.currentUrl = wixLocation.url;
wixData.insert("Collection", item);

Thanks for the comment!
I tried with ‘insert’ but this creates a completely new entry, and I wish to “catch” the values that were filled in by the user (i.e. email and another text field) and just add to the URL before I insert the data to the DB.
This works too, I now have two entries for every submission, but I believe there is a better way to do it?

This is how my code looks like now: (I have a form with two fields - ‘position’ and ‘email’. ‘referral’ is the user’s current URL, which I want to add to the object right before insertion to DB)
import wixLocation from ‘wix-location’ ;
import wixData from “wix-data” ;

// HOME CODE FILE

$w.onReady( function () {
console.log( "User entered site, url: " + wixLocation.url);

$w( #getSubscribers ).onWixFormSubmitted( function ({fields}) {
let position = fields[ 0 ].fieldValue;
let email = fields[ 1 ].fieldValue;
let referral = wixLocation.url;
let item = {
‘Position’ : position,
‘Email’ : email,
‘Referral’ : referral
}
wixData.insert( “lightboxSubscribe053” , item);
});
});

@dorhason3 if you wish to add the url, you’ll have to modify the data that the user create before they insert it (or before they update it). And don’t see a reason why wouldn’t be able to do it on the front end.
Anyway, that can’t be done by data hooks as the url is not part of the context.

@jonatandor35 Got it.
Indeed that snippet code is on the front end, I took your advice there.
Any advice on how to modify the data before the user inserts it?
I assumed I can only catch it after the form was submitted (that’s why I turned to ‘beforeInsert’ hook in the first place…)

@dorhason3 I can’t tell you because I don’t know which method you use in order to submit the form. I don’t even know if you use code or editor connection, not alone a direct insert or a saving via a dataset.