How do I set properties on session storage/local storage/cookies?

I am trying to set my own properties that can persist on multiple pages from my website, such as dark mode/or your own name that persist on multiple pages without having to log in.

I am using the visual editor by the way.
Someone please help

I did everything I could to set cookies, but it never worked. It seems like it’s not allowed through the Studio editor.

For local storage, run an interaction and choose “Run code”:

// Write to local storage
localStorage.setItem(“keyname”, JSON.stringify(value name))

Here is an example of how I have it set up to store user details after logging in and receiving data from an API:

// Existing data from user sign-in response
const dataResponse = $state.userSignIn.data.response;

// Prepare the user details for local storage
const userDetails = {
tk: dataResponse.access_token,
ei: dataResponse.expires_in,
user: {
id: dataResponse.user.id,
email: dataResponse.user.email,
role: dataResponse.user.role
}
};

// Write to local storage
localStorage.setItem(“key_name”, JSON.stringify(userDetails));

To remove data from local storage, you can use this in another “Run Code” interaction when a user logs out or when you want to remove the key from local storage:

// Remove from local storage.
localStorage.removeItem(“key_name”);

Key_name can be what ever you want it to be.

Thank you so much!
It worked

1 Like