Getting started
gamma-cookie is a JavaScript utility that provides as a fast and efficient way to get and set browser cookies.
Usage
setCookie()
const COOKIE_NAME = "recently_viewed";
const COOKIE_SKUS_NAME = "recently_viewed_skus";
const EXPIRE_TIME = 60 * 60 * 24 * 7;
const MAX_LIMIT = 10;
const setRecentlyProducts = (handle) => {
let recentlyViewed = getRecentlyProducts();
recentlyViewed = recentlyViewed.filter((i) => i !== handle);
recentlyViewed.unshift(handle);
const newCookieValue = recentlyViewed.slice(0, MAX_LIMIT).join("|");
setCookie(COOKIE_NAME, newCookieValue, { expires: EXPIRE_TIME });
};
getCookie()
const getRecentlyProducts = () => {
const cookieValue = getCookie(COOKIE_NAME) || "";
return cookieValue.length ? cookieValue.split("|") : [];
};