aus
analytics-util-storage
npm i analytics-util-storage
aus

analytics-util-storage

Lightweight analytics abstraction layer for tracking page views, custom events, & identifying visitors

by David Wells

0.1.1 (see all)License:MITTypeScript:Not Found
npm i analytics-util-storage
Readme

Analytics Storage Utils

Note: This package has moved to @analytics/storage-utils

Storage utilities for analytics

Storage tries to use localStorage then cookies then defaults to global window.

It's also possible to specify exactly where to set, get, and remove data from with the options parameter.

getItem

import { getItem } from '@analytics/storage-utils'

// Default lookup, will try resolve value from `localStorage` -> `cookies` -> `global`
const value = getItem('key')

// Get value from specifically localStorage
const getLSValue = getItem('key', 'localStorage')

// Get value from specifically a cookie
const getLSValue = getItem('key', 'cookie')

// Get value from specifically the global window (or global this in Node)
const getLSValue = getItem('key', 'global')

// Get value from all locations
const valueObj = getItem('otherKey', 'all')
// {cookie: undefined, localStorage: "hahaha", global: null}

setItem

import { setItem } from '@analytics/storage-utils'

// Will try save value to `localStorage` -> `cookies` -> `global`
setItem('key', 'value')
// -> { value: "value", oldValue: "old", location: "localStorage" }

// Set value to specifically localStorage
setItem('key', 'otherValue', 'localStorage')
// -> { value: "otherValue", oldValue: "value", location: "localStorage" }

// Set value from specifically a cookie
setItem('keyTwo', 'cookieVal', 'cookie')
// -> { value: "cookieVal", oldValue: "null", location: "cookie" }

// Set value from specifically the global window (or global this in Node)
setItem('keyThree', 'xyz', 'global')
// -> { value: "xyz", oldValue: "foobar", location: "global" }

removeItem

import { removeItem } from '@analytics/storage-utils'

// Will try save value to `localStorage` -> `cookies` -> `global`
setItem('key')
// -> { location: "localStorage" }

// Set value to specifically localStorage
setItem('key', 'otherValue', 'localStorage')
// -> { location: "localStorage" }

// Set value from specifically a cookie
setItem('keyTwo', 'cookieVal', 'cookie')
// -> { value: "cookieVal", oldValue: "null", location: "cookie" }

// Set value from specifically the global window (or global this in Node)
setItem('keyThree', 'xyz', 'global')
// -> { value: "xyz", oldValue: "foobar", location: "global" }

Downloads/wk

2

GitHub Stars

1.8K

LAST COMMIT

9mos ago

MAINTAINERS

1

CONTRIBUTORS

50

OPEN ISSUES

56

OPEN PRs

14
VersionTagPublished
0.1.1
latest
3yrs ago
No alternatives found
No tutorials found
Add a tutorial
No dependencies found

Rate & Review

100
No reviews found
Be the first to rate