Reputation: 3441
I'm storing the UTM parameters on first-time user lands into the website. He/She can visit any page of the website and during signup, I'm sending these parameters to the backend(storing to the database).
$(document).ready(function() {
if(window.location.href.match('utm_*')) {
var utmData = {
utm_source: getParameterByName('utm_source'),
utm_medium: getParameterByName('utm_medium'),
utm_campaign: getParameterByName('utm_campaign'),
gclid: getParameterByName('gclid')
}
if(!localStorage.getItem('utmData')) {
localStorage.setItem('utmData', JSON.stringify(utmData));
}else if(localStorage.getItem('utmData')){
localStorage.removeItem('utmData');
localStorage.setItem('utmData', JSON.stringify(utmData));
}
}
});
But the problem is When the user not signup but these parameters are still there. I want to remove when he/she close the browser.
Upvotes: 1
Views: 528
Reputation: 3895
If you dont want items to persist, you can save the data in SessionStorage
:
$(document).ready(function() {
if(window.location.href.match('utm_*')) {
var utmData = {
utm_source: getParameterByName('utm_source'),
utm_medium: getParameterByName('utm_medium'),
utm_campaign: getParameterByName('utm_campaign'),
gclid: getParameterByName('gclid')
}
if(!sessionStorage.getItem('utmData')) {
sessionStorage.setItem('utmData', JSON.stringify(utmData));
}else if(SessionStorage.getItem('utmData')){
sessionStorage.removeItem('utmData');
sessionStorage.setItem('utmData', JSON.stringify(utmData));
}
}
});
Upvotes: 2
Reputation: 14992
Use window.onbeforeunload handler.
window.onbeforeunload = function(e) {
localStorage.removeItem('utmData');
// return nothing for no «quit confirmation»
};
Upvotes: 3