Skip to content

TypeScript SDK

If you are building something with TypeScript (or plain JS), this is your go-to. Works in Node.js, browser, and React Native - though React Native needs a small polyfill (more on that below).

Terminal window
npm install @codeserk/forge-stats

The easiest way to get started. Initialize once - usually at app startup - and call track from anywhere without passing a client around.

import { init, track } from '@codeserk/forge-stats'
init({ baseUrl: 'https://api-events.forge.codeserk.es', sdk: 'YOUR_SDK_KEY' })
// fire-and-forget - errors are swallowed and logged
track({ content: [{ type: 'View', name: '/home' }], referrer: document.referrer })

If you need to await the result or handle errors yourself, use sendEvent instead:

import { sendEvent } from '@codeserk/forge-stats'
await sendEvent({ content: [{ type: 'View', name: '/home' }] })

Prefer to manage the client yourself? Fair enough.

import { Client } from '@codeserk/forge-stats'
const client = new Client({ baseUrl: 'https://api-events.forge.codeserk.es', sdk: 'YOUR_SDK_KEY' })
client.track({ content: [{ type: 'View', name: '/home' }] })

By default, errors go to console. Pass your own logger if you want them somewhere else:

init({
baseUrl: 'https://api-events.forge.codeserk.es',
sdk: 'YOUR_SDK_KEY',
logger: { error: (...args) => myLogger.error(...args) },
})

You will need a crypto.subtle polyfill - react-native-quick-crypto works well for this.

Initializes the singleton. Call this before track or sendEvent.

OptionTypeRequiredDescription
baseUrlstringyesBase URL of the Forge Stats API
sdkstringyesBase64-encoded SDK key from the Forge dashboard
loggerLoggernoDefaults to console

Fire-and-forget. Swallows errors and logs them via the logger.

Same as track but returns a Promise - use this when you need to await or catch errors.

Returns the singleton Client instance. Throws if init() has not been called yet.