npm package
For apps built with a bundler such as Vite or webpack, install the SDK from npm instead of loading the script tag
On this page
Install#
The package includes TypeScript types, including the ecommerce event shapes (EcommerceEventMap, Item, PurchaseProperties).
npm install @sfere.io/sfere-sdkLoad the SDK#
load() returns immediately and buffers calls until the SDK is ready. Unlike the script-tag loader, it does not send the first page view for you. Call sfere.page() after loading and on every route change.
import { SfereAnalytics } from "@sfere.io/sfere-sdk";
export const sfere = SfereAnalytics.load({
writeKey: "YOUR_WRITE_KEY",
host: "https://events.sfere.io",
});
sfere.page();On the server, where there is no window, load() returns an inert instance whose methods do nothing, so shared code can import it during server rendering.
Vue Router#
import { SfereAnalytics } from "@sfere.io/sfere-sdk";
import router from "./router";
const sfere = SfereAnalytics.load({ writeKey: "YOUR_WRITE_KEY", host: "https://events.sfere.io" });
router.afterEach((to) => {
sfere.page(String(to.name || to.path), { path: to.path });
});
export default sfere;Options#
| Option | Default | What it does |
|---|---|---|
writeKey |
required | The source’s browser key, keyId:secret. |
host |
required | https://events.sfere.io. Scheme and host only, no path. |
batching |
false |
true sends up to 10 events at a time, or every 5 seconds. Pass { size, timeout } to tune it. |
consent |
not set | Initial consent categories, for example { analytics: true, advertising: false }. See Consent and privacy. |
sessionTimeoutMinutes |
30 |
Minutes of inactivity before a new session starts. |
clickIdStorage |
"session" |
Keep landing-page click IDs for the session, or "local" to keep them across visits. |
carryTraits |
true |
Copy identify traits onto every later event. |
disableClientPersistence |
automatic | Keep IDs and traits in memory only. Turns on by itself when the browser blocks storage. |
debug |
false |
Log every event to the browser console. Use it while testing only. |