Analytics Storage Utils
A tiny storage utility library with fallback mechanism in 904 bytes
.
By default, @analytics/storage-utils
will persist values in browser in this order:
localStorage
- If no
localStorage
, use cookies
- If no
cookies
, use global
window
If you want to specify which storage mechanism to use, use the options
parameter.
See live demo.
setItem
Set a value.
import { setItem } from '@analytics/storage-utils'
setItem('key', 'value')
setItem('key', 'otherValue', { storage: 'localStorage' })
setItem('keyTwo', 'cookieVal', { storage: 'cookie' })
setItem('keyThree', 'xyz', { storage: 'global' })
getItem
Get a value.
import { getItem } from '@analytics/storage-utils'
const value = getItem('key')
const getLocalStorageValue = getItem('key', { storage: 'localStorage' })
const getCookieValue = getItem('key', { storage: 'cookie' })
const getGlobalValue = getItem('key', { storage: 'global' })
const valueObj = getItem('otherKey', { storage: '*' })
removeItem
Remote a value.
import { removeItem } from '@analytics/storage-utils'
removeItem('key')
removeItem('key', { storage: 'localStorage' })
removeItem('keyTwo', { storage: 'cookie' })
removeItem('keyThree', { storage: 'global' })
removeItem('otherKey', { storage: '*' })