Concepts
Persistence
Storage layer can be utilized to persist effect's internal state and attribute's values across sessions.
The storage layer's logic is implemented by modules that are installed in your application.
Defining a strategy#
Both effects and attributes can optionally define a persistence strategy:
# ...persist: localstorage
Conditional persistence#
It is possible you may need to check for user's consent before persisting the values. In that case, you can do:
# ...persist: conditions: - attribute: gdprConsent operator: equals value: true storage: localstorage
Multiple storages#
You can also have multiple different storage layers for different conditions:
# ...persist: - conditions: - attribute: platform operator: equals value: web storage: localstorage # for web- conditions: - attribute: platform operator: equals value: ios storage: userdefaults # for iOS
Module setup#
We can install the module module-localstorage in our application to give a demo:
$ npm install --save @eventvisor/module-localstorage
And then set it up when initializing the SDK:
import { createInstance } from "@eventvisor/sdk";import { createLocalStorageModule } from "@eventvisor/module-localstorage";const eventvisor = createInstance({ modules: [ createLocalStorageModule(), ],});
Learn more in module-localstorage page.
Usage example#
From any of our effects, we can now reference the module name to persist its data:
# ...persist: localstorage
Creating custom module#
To create or integrate with a custom storage layer, you can create a new custom module with the methods readFromStorage
, writeToStorage
and removeFromStorage
:
Defining module#
export function createCustomModule() { return { name: "custom", readFromStorage: async ({ key }) => { return "some value"; }, writeToStorage: async ({ key, value }) => { // write value to the storage layer here... }, removeFromStorage: async ({ key }) => { // remove value from the storage layer here... }, };};
SDK setup#
And then set it up when initializing the SDK:
import { createInstance } from "@eventvisor/sdk";import { createCustomModule } from "./custom";const eventvisor = createInstance({ modules: [ createCustomModule(), ],});
Because there's a storage layer involved now, and when SDK is first initialized it will attempt to read values if already persisted, you should wait until the SDK is ready before tracking any events or setting attributes:
await eventvisor.onReady();
This is the only use case where the .onReady()
method is expected to be utilized. Otherwise, SDKs are ready to be used as soon as they are initialized.
Storage usage#
Now from the effects, we can use the custom storage layer like this:
# ...persist: custom