Skip to main content

Browser store

The BrowserAnalyticsStore is an IAnalyticsStore implementation that sits on top of MemoryAnalyticsStore but adds an IndexedDB plugin for persistence.

Construction

A default implementation of the BrowserAnalyticsStore may be created with no arguments, or options are provided for specialized needs.

// creates a database named "analytics"
const store = new BrowserAnalyticsStore();

Create with a specific database name.

const store = new BrowserAnalyticsStore({ databaseName: "analytics" });

It may also be created with optional contructor arguments that may be helpful for debugging or metrics collection.

const store = new BrowserAnalyticsStore({
databaseName: "analytics",

queryLogger: defaultQueryLogger("browser"),
resultsLogger: defaultResultsLogger("browser"),
profiler: new PassthroughAnalyticsProfiler(),
});

For more details on these optional constructor parameters, see the Utilities section.

Since the constructor options argument extends the MemoryAnalyticsStore options argument, see the MemoryAnalyticsStore documentation for further details on other optional parameters.

Initialization

Similar to the MemoryAnalyticsStore, this implementation requires an asynchronous initialization step.

Note that this method is not available on the IAnalyticsStore interface, but only on the concrete type.

// create the store
const store = new BrowserAnalyticsStore();

// initialize it
await store.init();

Persistence

The databaseName constructor argument namespaces the database. This allows users to create multiple stores, if needed, which will not conflict with each other. You can use your browser's developer tools to see these databases, usually through the "Storage" tab.

dev-tools

The store interface is intended to be immutable, meaning that it does not provide a general method of wiping a DB. However, an IDB database may be deleted via the standard IDB API.

// creates the database
const store = new BrowserAnalyticsStore({ databaseName: "my-analytics" });
await store.init();

// use the browser API to delete the database
window.indexedDB.deleteDatabase("my-analytics");