Save UTM Parameters for 30 Days In Elementor

Facebook
Twitter
Reddit
WhatsApp

Load script on every page

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

    // Function to save UTM parameters to local storage with an expiry of 30 days
    function saveUTMParameters() {
        const urlParams = new URLSearchParams(window.location.search);
        const utmParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'utm_id'];

        utmParams.forEach(param => {
            const paramValue = urlParams.get(param);
            if (paramValue) {
                const utmData = {
                    value: paramValue,
                    expiry: new Date().getTime() + 30 * 24 * 60 * 60 * 1000 // 30 days expiry
                };
                localStorage.setItem(param, JSON.stringify(utmData));
            }
        });
    }

    // Function to retrieve UTM parameters from local storage if they are not expired
    function getUTMParameter(param) {
        const paramData = JSON.parse(localStorage.getItem(param));
        if (paramData) {
            if (new Date().getTime() < paramData.expiry) {
                return paramData.value;
            } else {
                localStorage.removeItem(param);
            }
        }
        return null;
    }

    // Function to save the initial referrer URL in 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 expiry
            };
            localStorage.setItem('initial_referrer', JSON.stringify(referrerData));
        }
    }

    // Function to retrieve the saved referrer from local storage if 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 referrer and UTM parameters in hidden input fields
    function setReferrerAndUTMInForm() {
        const referrer = getSavedReferrer();
        const formField = document.querySelector('input[name="form_fields[referrer_url]"]');

        if (referrer && formField) {
            formField.value = referrer;
        }

        const utmFields = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'utm_id'];
        utmFields.forEach(utm => {
            const utmValue = getUTMParameter(utm);
            if (utmValue) {
                const utmInput = document.querySelector(`input[name="form_fields[${utm}]"]`);
                if (utmInput) {
                    utmInput.value = utmValue;
                }
            }
        });
    }

    // Call functions to save referrer and UTM parameters when the script runs
    saveInitialReferrer();
    saveUTMParameters();

    // Set the referrer and UTM parameters in the form on all pages
    setReferrerAndUTMInForm();
});
</script>
				
			

Load script on specific pages

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

    // Function to save UTM parameters to local storage with an expiry of 30 days
    function saveUTMParameters() {
        const urlParams = new URLSearchParams(window.location.search);
        const utmParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'utm_id'];

        utmParams.forEach(param => {
            const paramValue = urlParams.get(param);
            if (paramValue) {
                const utmData = {
                    value: paramValue,
                    expiry: new Date().getTime() + 30 * 24 * 60 * 60 * 1000 // 30 days expiry
                };
                localStorage.setItem(param, JSON.stringify(utmData));
            }
        });
    }

    // Function to retrieve UTM parameters from local storage if they are not expired
    function getUTMParameter(param) {
        const paramData = JSON.parse(localStorage.getItem(param));
        if (paramData) {
            if (new Date().getTime() < paramData.expiry) {
                return paramData.value;
            } else {
                localStorage.removeItem(param);
            }
        }
        return null;
    }

    // Function to save the initial referrer URL in 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 expiry
            };
            localStorage.setItem('initial_referrer', JSON.stringify(referrerData));
        }
    }

    // Function to retrieve the saved referrer from local storage if 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 referrer and UTM parameters in hidden input fields
    function setReferrerAndUTMInForm() {
        const referrer = getSavedReferrer();
        const formField = document.querySelector('input[name="form_fields[referrer_url]"]');

        if (referrer && formField) {
            formField.value = referrer;
        }

        const utmFields = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'utm_id'];
        utmFields.forEach(utm => {
            const utmValue = getUTMParameter(utm);
            if (utmValue) {
                const utmInput = document.querySelector(`input[name="form_fields[${utm}]"]`);
                if (utmInput) {
                    utmInput.value = utmValue;
                }
            }
        });
    }

    // Call functions to save referrer and UTM parameters when the script runs
    saveInitialReferrer();
    saveUTMParameters();

    // Specify the pages where referrer and UTM parameters should be set in form inputs
    const specificPages = ['/contact/', '/']; // Add more paths as needed

    // Check if the current page matches the specified pages and set the form fields
    if (specificPages.includes(window.location.pathname)) {
        setReferrerAndUTMInForm();
    }
});
</script>
				
			
Subscribe
Notify of
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

More to explorer

Elementor Multi-Column Dropdown

https://youtu.be/72L8eLoIrCU Target all dropdown menus @media(min-width:767px) { /* When hovering over a menu item that has children, display its submenu as a

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

45 Minute Wordpress Support

Get your Wordpress questions answered and your Wordpress issues resolved.