Categories
tracking

Add visitor pathway to your contact form

The idea here is to add the pages the visitor view before he arrived to your contact form directly in the message you received. It’s practical when you want to know the path of your lead without have to go to your analytics tool, moreover since cookies are not authorized by browser (Safari…) or by the visitor.

To do that, I use localstorage to save urls loaded, the date of the day of the visit to limit the amount of data saved in localstorage and an hidden input in the form tha will be charged with Javascript, or jQuery in this example, to pass the data in the confirmation email.

You can copy this code in your app.js file and don’t forget to add an hidden value with the ID “cf7_path” and to show it in the email if you want to exactly follow this example.

var date = new Date();
var month = date.getMonth() + 1; 
var day = date.getDate();
var year = date.getFullYear();
var today = day+"-"+month+"-"+year;
	
if( (localStorage.getItem("PATH_DATE")!=null) && (today===localStorage.getItem("PATH_DATE")) ){
	var path = JSON.parse(localStorage.getItem("PATH"));
	path.push(window.location.pathname);
	localStorage.setItem("PATH", JSON.stringify(path));
	if(typeof(jQuery("#cf7_path"))!='undefined'){
		jQuery("#cf7_path").val(localStorage.getItem("PATH"));
	}
} else {
	localStorage.setItem("PATH_DATE", today);
	var path = [];
	path.push(window.location.pathname);
	localStorage.setItem("PATH", JSON.stringify(path));	
}