I want to load a script only once when the user visits (a success page).
My idea was to write something with local storage:
if ( localStorage.getItem("beenHere")) {
localStorage.setItem('beenHere', 1);
window.addEventListener('load', (event) => {
fathom.trackGoal('XXX', {{ order.totalPrice * 100 }});
});
}
But that doesnt work because load is always loaded. How would I approach that?
>Solution :
It should be this instead? Initially localStorage.getItem("beenHere") is null so it evaluates to false, therefore the code inside the if condition never runs and beenHere is never set to 1.
if (!localStorage.getItem("beenHere")) {
...
}