• ⚠️ INFORMATION: SAFETY & SUPPORT Resources here are generally safe, but false positives may occur on Virustotal due to certain coding techniques. Exercise caution and test before use.

javascript Show pop-up form when visitor comes from utm

Joined
Dec 31, 2024
Messages
379
Reaction score
7
Points
18
User icon
<svg xmlns="http://www.w3.org/2000/svg" height="14" width="15.75" viewBox="0 0 576 512"><!--!Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2025 Fonticons, Inc.--><path fill="#63E6BE" d="M309 106c11.4-7 19-19.7 19-34c0-22.1-17.9-40-40-40s-40 17.9-40 40c0 14.4 7.6 27 19 34L209.7 220.6c-9.1 18.2-32.7 23.4-48.6 10.7L72 160c5-6.7 8-15 8-24c0-22.1-17.9-40-40-40S0 113.9 0 136s17.9 40 40 40c.2 0 .5 0 .7 0L86.4 427.4c5.5 30.4 32 52.6 63 52.6l277.2 0c30.9 0 57.4-22.1 63-52.6L535.3 176c.2 0 .5 0 .7 0c22.1 0 40-17.9 40-40s-17.9-40-40-40s-40 17.9-40 40c0 9 3 17.3 8 24l-89.1 71.3c-15.9 12.7-39.5 7.5-48.6-10.7L309 106z"/></svg>
To show a pop-up form when a visitor comes to your site with specific UTM parameters (e.g., utm_source, utm_medium, etc.), you can implement this in React using the following steps:


---

Steps to Implement

1. Detect UTM Parameters

Use the browser's window.location.search to extract UTM parameters from the URL.

2. Check for Specific UTM

Once you extract the UTM parameters, check if they match the desired values (e.g., utm_source=google).

3. Show the Pop-Up Form

If the desired UTM is present, display a pop-up form. You can use a modal component (like from Material-UI, React Bootstrap, or a custom one).


---

Code Example

Here’s an example implementation in React:

1. Create a Helper to Parse UTM Parameters

JavaScript:
Please, Log in or Register to view codes content!

2. Display Pop-Up Form if UTM Parameters Are Present
JavaScript:
Please, Log in or Register to view codes content!














How It Works

1. URL with UTM: When the user visits your site with a URL like:

JavaScript:
Please, Log in or Register to view codes content!
The getUTMParams function will extract these values.


2. Check and Show:

useEffect checks for the presence of utm_source.

If a valid UTM parameter is found, the state (showPopup) is set to true, triggering the pop-up.



3. Close Functionality:

Clicking the close button sets showPopup to false, hiding the form.





---

Using a Library for the Modal

If you'd rather use a library for styling and accessibility, you can replace the custom PopupForm with a library like:

React Modal

Material-UI Dialog

React Bootstrap Modal


Example with react-modal:

JavaScript:
Please, Log in or Register to view codes content!
JavaScript:
Please, Log in or Register to view codes content!

Additional Tips

1. Save UTM Parameters:

You might want to store UTM parameters in localStorage or state for later use (e.g., analytics or form submission).

localStorage.setItem("utm_source", utmParams.utm_source);



2. Prevent Repeated Pop-Ups:

Use localStorage to check if the pop-up has already been shown:

JavaScript:
Please, Log in or Register to view codes content!


3. Customization:

Tailor the pop-up to display different forms or messages depe
nding on the UTM values.





---

This implementation ensures the pop-up only appears for visitors arriving with specific UTM parameters, improving targeting and user experience.
 
Last edited:
Top