Tracking contact by collecting email from URL is not working

Your software
My Mautic version is: v.4.44

Your problem
My URL will look like https://xyz.com/page.html?email=abc@abc.com

Using the following JS to fetch the email from URL and push to Mautic tracking.

<script>
  
 var url_string = window.location.href
 var url = new URL(url_string);
 var customer_email = url.searchParams.get("email");
  

 
    (function(w,d,t,u,n,a,m){w['MauticTrackingObject']=n;
        w[n]=w[n]||function(){(w[n].q=w[n].q||[]).push(arguments)},a=d.createElement(t),
        m=d.getElementsByTagName(t)[0];a.async=1;a.src=u;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://xyz.com/mtk/mtc.js','mt');

    if(customer_email != ''){
      alert(customer_email);
		mt('send', 'pageview', {email: customer_email});
	} else {
		mt('send', 'pageview');
	}
  
  
</script>

I have verified that the variable customer_email is correctly getting the email. But, Mautic is not identifying the contact.

Am I doing anything wrong?

check if the email field is set as “Publicly updatable” under settings → Custom Fields → Email

only the fields that are set as publicly updatable can be changed by the tracking code.

but keep in mind that this is a trade: users can manipulate this code to send the data they want, for example, if the user manually change the value of the customer_email on your url, you will include the value they input, not necessarily their actual email.

also, keep in mind that email is a personal data, and adding personal data to the url can be consider a data breach since that information is openly transmitted throughout the internet and logged into browsers and routers - if you need to transmit that value between 2 pages of your website, a more secure way would be to use session cookies.

oh, another thing you should be checking is that mautic will not track user navigation for people logged in mautic. in order to test the tracking you need to open a different browser or open the browser in incognito mode.

Yes, it’s working now. Thank you so much.

How can I use cookies to achieve this?

better and simpler than cookies is to use the local or session storage object, it would not work on very old browsers: "sessionStorage" | Can I use... Support tables for HTML5, CSS3, etc

assuming you have pages on the same domain,
on the page where you collected the e-mail, lets say to an input field called email:

<script>
//collect the e-mail from a field:
var userEmail = document.getElementById("emailField").value;

// Save data to sessionStorage
sessionStorage.setItem("email",  userEmail );
</script>

Now, on the page where do you want to send the data to mautic:

<script>
    (function(w,d,t,u,n,a,m){w['MauticTrackingObject']=n;
        w[n]=w[n]||function(){(w[n].q=w[n].q||[]).push(arguments)},a=d.createElement(t),
        m=d.getElementsByTagName(t)[0];a.async=1;a.src=u;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://xyz.com/mtk/mtc.js','mt');

// Get saved data from sessionStorage
var customer_email = sessionStorage.getItem("email");


    if(customer_email != ''){
      alert(customer_email);
		mt('send', 'pageview', {email: customer_email});
	} else {
		mt('send', 'pageview');
	}
</script>

Perfect. I will try this.
Thank you for this idea.