WordPress: Track a visitor referrer URL on an Elementor Form or contact form 7

Facebook
Twitter
Reddit
WhatsApp

Elementor Form - JS Code

				
					<script>
document.addEventListener('DOMContentLoaded', () => {

    // Function to get the referrer URL and save it to local storage with an expiry
    function saveInitialReferrer() {
        if (!localStorage.getItem('initial_referrer')) {
            const referrer = document.referrer || "direct";
            const referrerData = {
                url: referrer,
                expiry: new Date().getTime() + 30 * 24 * 60 * 60 * 1000 // 30 days in milliseconds
            };
            localStorage.setItem('initial_referrer', JSON.stringify(referrerData));
        }
    }

    // Function to get the saved referrer from local storage if it's not expired
    function getSavedReferrer() {
        const referrerData = JSON.parse(localStorage.getItem('initial_referrer'));
        if (referrerData) {
            if (new Date().getTime() < referrerData.expiry) {
                return referrerData.url;
            } else {
                localStorage.removeItem('initial_referrer');
            }
        }
        return null;
    }

    // Function to set the referrer URL to the hidden input field
    function setReferrerInFormField() {
        const referrer = getSavedReferrer();

        if (referrer) {
            // Set the referrer value in the form field if it exists
            const referrerField = document.querySelector('input[name="form_fields[referrer_url]"]');
            if (referrerField) {
                referrerField.value = referrer;
            }
        }
    }

    // Call the function to save the referrer when the script runs
    saveInitialReferrer();

    // Specify the specific pages where you want to set the referrer in the form field
    const specificPages = ['/contact/', '/other-page/']; // Add more paths as needed

    // Check if the current page is one of the specific pages
    if (specificPages.includes(window.location.pathname)) {
        setReferrerInFormField();
    }
});
</script>
				
			

contact Form 7 - JS Code

				
					<script>
document.addEventListener('DOMContentLoaded', () => {

    // Function to get the referrer URL and save it to local storage with an expiry
    function saveInitialReferrer() {
        if (!localStorage.getItem('initial_referrer')) {
            const referrer = document.referrer || "direct";
            const referrerData = {
                url: referrer,
                expiry: new Date().getTime() + 30 * 24 * 60 * 60 * 1000 // 30 days in milliseconds
            };
            localStorage.setItem('initial_referrer', JSON.stringify(referrerData));
        }
    }

    // Function to get the saved referrer from local storage if it's not expired
    function getSavedReferrer() {
        const referrerData = JSON.parse(localStorage.getItem('initial_referrer'));
        if (referrerData) {
            if (new Date().getTime() < referrerData.expiry) {
                return referrerData.url;
            } else {
                localStorage.removeItem('initial_referrer');
            }
        }
        return null;
    }

    // Function to set the referrer URL to the hidden input field
    function setReferrerInFormField() {
        const referrer = getSavedReferrer();

        if (referrer) {
            // Set the referrer value in the form field if it exists
            const referrerField = document.querySelector('input[name="referrer_url"]');
            if (referrerField) {
                referrerField.value = referrer;
            }
        }
    }

    // Call the function to save the referrer when the script runs
    saveInitialReferrer();

    // Specify the specific pages where you want to set the referrer in the form field
    const specificPages = ['/contact/', '/other-page/']; // Add more paths as needed

    // Check if the current page is one of the specific pages
    if (specificPages.includes(window.location.pathname)) {
        setReferrerInFormField();
    }
});
</script>
				
			
Subscribe
Notify of
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

More to explorer

How To Make a Hover Card Like a PRO in Elementor

https://youtu.be/pja-7Sfn8WI CSS selector .card-content{ height: 56%; margin-bottom: -134px; transition: all 0.5s ease; } selector .card-wrapper:hover .card-content{ margin-bottom: 0px; height: 100%; } selector

Elementor Button Border Gradient Tutorial

https://youtu.be/I9am2K_yopc Border Gradient Button CSS selector .elementor-button{ position: relative; overflow: hidden; z-index: 1; margin-bottom: -15px } selector .elementor-button:before{ content: ”; position: absolute;

0
Would love your thoughts, please comment.x
()
x

45 Minute Wordpress Support

Get your Wordpress questions answered and your Wordpress issues resolved.

Hello!

Enter your question below and a representative will get right back to you.

Speak to a team member
Leave us a review
Hi there, have a question? Text us here.