Skip to content

Commit

Permalink
feat: storage option to change priority (#908)
Browse files Browse the repository at this point in the history
  • Loading branch information
oscb authored Aug 23, 2023
1 parent 658f321 commit 1b95946
Show file tree
Hide file tree
Showing 18 changed files with 936 additions and 634 deletions.
5 changes: 5 additions & 0 deletions .changeset/smooth-seahorses-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-next': minor
---

Adds storage option in analytics client to specify priority of storage (e.g use cookies over localstorage)
58 changes: 58 additions & 0 deletions packages/browser/src/core/analytics/__tests__/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import {
import { Context } from '../../context'
import { Plugin } from '../../plugin'
import { EventQueue } from '../../queue/event-queue'
import { StoreType } from '../../storage'
import { Analytics } from '../index'
import jar from 'js-cookie'
import {
TestAfterPlugin,
TestBeforePlugin,
Expand Down Expand Up @@ -271,4 +273,60 @@ describe('Analytics', () => {
expect(fn).toHaveBeenCalledTimes(1)
})
})

describe('storage', () => {
beforeEach(() => {
clearAjsBrowserStorage()
})

it('handles custom priority storage', async () => {
const setCookieSpy = jest.spyOn(jar, 'set')
const expected = 'CookieValue'
jar.set('ajs_anonymous_id', expected)
localStorage.setItem('ajs_anonymous_id', 'localStorageValue')

const analytics = new Analytics(
{ writeKey: '' },
{
storage: {
stores: [
StoreType.Cookie,
StoreType.LocalStorage,
StoreType.Memory,
],
},
}
)

expect(analytics.user().anonymousId()).toEqual(expected)

analytics.user().id('known-user')
expect(analytics.user().id()).toEqual('known-user')
expect(setCookieSpy).toHaveBeenCalled()
})

it('handles disabling storage', async () => {
const setCookieSpy = jest.spyOn(jar, 'set')
const expected = 'CookieValue'
jar.set('ajs_anonymous_id', expected)
localStorage.setItem('ajs_anonymous_id', 'localStorageValue')

const analytics = new Analytics(
{ writeKey: '' },
{
storage: {
stores: [StoreType.Cookie, StoreType.Memory],
},
}
)

expect(analytics.user().anonymousId()).toEqual(expected)

analytics.user().id('known-user')
expect(analytics.user().id()).toEqual('known-user')
expect(setCookieSpy).toHaveBeenCalled()
// Local storage shouldn't change
expect(localStorage.getItem('ajs_anonymous_id')).toBe('localStorageValue')
})
})
})
88 changes: 67 additions & 21 deletions packages/browser/src/core/analytics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,7 @@ import {
} from '../events'
import type { Plugin } from '../plugin'
import { EventQueue } from '../queue/event-queue'
import {
CookieOptions,
getAvailableStorageOptions,
Group,
ID,
UniversalStorage,
User,
UserOptions,
} from '../user'
import { Group, ID, User, UserOptions } from '../user'
import autoBind from '../../lib/bind-all'
import { PersistedPriorityQueue } from '../../lib/priority-queue/persisted'
import type { LegacyDestination } from '../../plugins/ajs-destination'
Expand All @@ -50,6 +42,16 @@ import { getGlobal } from '../../lib/get-global'
import { AnalyticsClassic, AnalyticsCore } from './interfaces'
import { HighEntropyHint } from '../../lib/client-hints/interfaces'
import type { LegacySettings } from '../../browser'
import {
CookieOptions,
MemoryStorage,
UniversalStorage,
StorageSettings,
StoreType,
applyCookieOptions,
initializeStorages,
isArrayOfStoreType,
} from '../storage'

const deprecationWarning =
'This is being deprecated and will be not be available in future releases of Analytics JS'
Expand Down Expand Up @@ -93,6 +95,7 @@ export interface InitOptions {
disableAutoISOConversion?: boolean
initialPageview?: boolean
cookie?: CookieOptions
storage?: StorageSettings
user?: UserOptions
group?: UserOptions
integrations?: Integrations
Expand Down Expand Up @@ -133,9 +136,7 @@ export class Analytics
private _group: Group
private eventFactory: EventFactory
private _debug = false
private _universalStorage: UniversalStorage<{
[k: string]: unknown
}>
private _universalStorage: UniversalStorage

initialized = false
integrations: Integrations
Expand All @@ -162,25 +163,33 @@ export class Analytics
disablePersistance
)

this._universalStorage = new UniversalStorage(
disablePersistance ? ['memory'] : ['localStorage', 'cookie', 'memory'],
getAvailableStorageOptions(cookieOptions)
const storageSetting = options?.storage
this._universalStorage = this.createStore(
disablePersistance,
storageSetting,
cookieOptions
)

this._user =
user ??
new User(
disablePersistance
? { ...options?.user, persist: false }
: options?.user,
{
persist: !disablePersistance,
storage: options?.storage,
// Any User specific options override everything else
...options?.user,
},
cookieOptions
).load()
this._group =
group ??
new Group(
disablePersistance
? { ...options?.group, persist: false }
: options?.group,
{
persist: !disablePersistance,
storage: options?.storage,
// Any group specific options override everything else
...options?.group,
},
cookieOptions
).load()
this.eventFactory = new EventFactory(this._user)
Expand All @@ -194,6 +203,43 @@ export class Analytics
return this._user
}

/**
* Creates the storage system based on the settings received
* @returns Storage
*/
private createStore(
disablePersistance: boolean,
storageSetting: InitOptions['storage'],
cookieOptions?: CookieOptions | undefined
): UniversalStorage {
// DisablePersistance option overrides all, no storage will be used outside of memory even if specified
if (disablePersistance) {
return new UniversalStorage([new MemoryStorage()])
} else {
if (storageSetting) {
if (isArrayOfStoreType(storageSetting)) {
// We will create the store with the priority for customer settings
return new UniversalStorage(
initializeStorages(
applyCookieOptions(storageSetting.stores, cookieOptions)
)
)
}
}
}
// We default to our multi storage with priority
return new UniversalStorage(
initializeStorages([
StoreType.LocalStorage,
{
name: StoreType.Cookie,
settings: cookieOptions,
},
StoreType.Memory,
])
)
}

get storage(): UniversalStorage {
return this._universalStorage
}
Expand Down
58 changes: 58 additions & 0 deletions packages/browser/src/core/storage/__tests__/cookieStorage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { CookieStorage } from '../cookieStorage'
import jar from 'js-cookie'

describe('cookieStorage', () => {
function clearCookies() {
document.cookie.split(';').forEach(function (c) {
document.cookie = c
.replace(/^ +/, '')
.replace(/=.*/, '=;expires=' + new Date().toUTCString() + ';path=/')
})
}

afterEach(() => {
clearCookies()
})

describe('cookie options', () => {
it('should have default cookie options', () => {
const cookie = new CookieStorage()
expect(cookie['options'].domain).toBe(undefined)
expect(cookie['options'].maxage).toBe(365)
expect(cookie['options'].path).toBe('/')
expect(cookie['options'].sameSite).toBe('Lax')
expect(cookie['options'].secure).toBe(undefined)
})

it('should set options properly', () => {
const cookie = new CookieStorage({
domain: 'foo',
secure: true,
path: '/test',
})
expect(cookie['options'].domain).toBe('foo')
expect(cookie['options'].secure).toBe(true)
expect(cookie['options'].path).toBe('/test')
expect(cookie['options'].secure).toBe(true)
})

it('should pass options when creating cookie', () => {
const jarSpy = jest.spyOn(jar, 'set')
const cookie = new CookieStorage({
domain: 'foo',
secure: true,
path: '/test',
})

cookie.set('foo', 'bar')

expect(jarSpy).toHaveBeenCalledWith('foo', 'bar', {
domain: 'foo',
expires: 365,
path: '/test',
sameSite: 'Lax',
secure: true,
})
})
})
})
70 changes: 70 additions & 0 deletions packages/browser/src/core/storage/__tests__/localStorage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { LocalStorage } from '../localStorage'

describe('LocalStorage', function () {
let store: LocalStorage

beforeEach(() => {
jest.spyOn(console, 'warn').mockImplementation(() => {}) // silence console spam.
store = new LocalStorage()
})

afterEach(() => {
localStorage.clear()
})

describe('#get', function () {
it('should return null if localStorage throws an error (or does not exist)', function () {
const getItemSpy = jest
.spyOn(global.Storage.prototype, 'getItem')
.mockImplementationOnce(() => {
throw new Error('getItem fail.')
})
store.set('foo', 'some value')
expect(store.get('foo')).toBeNull()
expect(getItemSpy).toBeCalledTimes(1)
})

it('should not get an empty record', function () {
expect(store.get('abc')).toBe(null)
})

it('should get an existing record', function () {
store.set('x', { a: 'b' })
store.set('a', 'hello world')
store.set('b', '')
store.set('c', false)
store.set('d', null)
store.set('e', undefined)

expect(store.get('x')).toStrictEqual({ a: 'b' })
expect(store.get('a')).toBe('hello world')
expect(store.get('b')).toBe('')
expect(store.get('c')).toBe(false)
expect(store.get('d')).toBe(null)
expect(store.get('e')).toBe('undefined')
})
})

describe('#set', function () {
it('should be able to set a record', function () {
store.set('x', { a: 'b' })
expect(store.get('x')).toStrictEqual({ a: 'b' })
})

it('should catch localStorage quota exceeded errors', () => {
const val = 'x'.repeat(10 * 1024 * 1024)
store.set('foo', val)

expect(store.get('foo')).toBe(null)
})
})

describe('#clear', function () {
it('should be able to remove a record', function () {
store.set('x', { a: 'b' })
expect(store.get('x')).toStrictEqual({ a: 'b' })
store.remove('x')
expect(store.get('x')).toBe(null)
})
})
})
27 changes: 27 additions & 0 deletions packages/browser/src/core/storage/__tests__/test-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import jar from 'js-cookie'
/**
* Disables Cookies
* @returns jest spy
*/
export function disableCookies(): void {
jest.spyOn(window.navigator, 'cookieEnabled', 'get').mockReturnValue(false)
jest.spyOn(jar, 'set').mockImplementation(() => {
throw new Error()
})
jest.spyOn(jar, 'get').mockImplementation(() => {
throw new Error()
})
}

/**
* Disables LocalStorage
* @returns jest spy
*/
export function disableLocalStorage(): void {
jest.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
throw new Error()
})
jest.spyOn(Storage.prototype, 'getItem').mockImplementation(() => {
throw new Error()
})
}
Loading

0 comments on commit 1b95946

Please sign in to comment.