Referrer URL Tracking

I'm trying to get the inital/referrer url of the site where the user entered and store it in a hidden value in the pop-up(which is same on all pages) from and take it as a submission when form is submitted. tried this but wasn't working
<script>
$(document).ready(function () {
// Get the Referrer URL
var referrerURL = document.referrer;

// Set the Referrer URL as the value of the hidden input field
$("#name=Last_Conversion_Page_URL__c").val(referrerURL);
});
</script>
<script>
$(document).ready(function () {
// Get the Referrer URL
var referrerURL = document.referrer;

// Set the Referrer URL as the value of the hidden input field
$("#name=Last_Conversion_Page_URL__c").val(referrerURL);
});
</script>
<input type="hidden" class="last-conversion-fl last_con_url w-input" id="name=Last_Conversion_Page_URL__c" name="Last_Conversion_Page_URL__c" data-name="Last_Conversion_Page_URL__c" value="https://www.wesite.com/pop-up ">
<input type="hidden" class="last-conversion-fl last_con_url w-input" id="name=Last_Conversion_Page_URL__c" name="Last_Conversion_Page_URL__c" data-name="Last_Conversion_Page_URL__c" value="https://www.wesite.com/pop-up ">
how can I achieve this? Is this possible to achieve something like this?
4 Replies
13eck
13eck15mo ago
Well, step 1 is to not use jQuery and instead use the JS that the browser gives you (unless $ is a custom function you use). You can use document.querySelector() to get the element you're looking for. And finally, your ID has an = in it. That's what's causing the problem
<input type="hidden" class="last-conversion-fl last_con_url w-input" id="Last_Conversion_Page_URL__c" name="Last_Conversion_Page_URL__c" data-name="Last_Conversion_Page_URL__c" value="https://www.wesite.com/pop-up ">
<input type="hidden" class="last-conversion-fl last_con_url w-input" id="Last_Conversion_Page_URL__c" name="Last_Conversion_Page_URL__c" data-name="Last_Conversion_Page_URL__c" value="https://www.wesite.com/pop-up ">
<script>
document.addEventListener("DOMContentLoaded", () =>{
const referrerURL = document.referrer;
document.querySelector("#Last_Conversion_Page_URL__c").value = referrerURL;
});
</script>
<script>
document.addEventListener("DOMContentLoaded", () =>{
const referrerURL = document.referrer;
document.querySelector("#Last_Conversion_Page_URL__c").value = referrerURL;
});
</script>
Of course, if the script is not inline (being added with <script src="./path/toJs/file.js">) then you can add the defer attribute instead of using the DOMContentLoaded event. Or you can place the script tag immediately before the </body> tag so it's parsed and run after the entire page has been loaded and parsed.
Abdul Ahad⚡
Abdul Ahad⚡OP15mo ago
Okay, got it. Now I'm facing another problem, when the user clicks on another page, this url gets lost and the previous page(internal page) gets saved as referrer url. how do I pass that initial referrer url to the clicked page which has same form as pop up.
13eck
13eck15mo ago
I would suggest saving the referrer to session storage:
if (!sessionStorage.getItem(“referrer”)) {
sessionStorage.setItem(“referrer”, document.referrer);
}
if (!sessionStorage.getItem(“referrer”)) {
sessionStorage.setItem(“referrer”, document.referrer);
}
Then grab that data from storage. Using session storage means it’ll populate once per session and when the user closes the browser tab the info will be removed. That way you can repopulate it on every “new” visit
Abdul Ahad⚡
Abdul Ahad⚡OP15mo ago
understood, thank you mate
Want results from more Discord servers?
Add your server