Skip to content

klimeapp/klime-js

Repository files navigation

Klime JavaScript SDKs

This monorepo contains the official Klime SDKs for JavaScript environments.

Packages

Package Description npm
@klime/browser Browser SDK for vanilla JavaScript apps npm install @klime/browser
@klime/node Node.js SDK for server-side tracking npm install @klime/node

Coming soon: @klime/react and @klime/nextjs

Which SDK should I use?

  • Browser apps: Use @klime/browser - includes automatic browser context (userAgent, locale, timezone) and page unload handling
  • Node.js servers: Use @klime/node - includes graceful shutdown on SIGTERM/SIGINT

Quick Start

Browser

import { KlimeClient } from "@klime/browser";

const client = new KlimeClient({
  writeKey: "your-write-key",
});

// Identify a user
client.identify("user_123", { email: "user@example.com" });

// Track an event
client.track(
  "Button Clicked",
  { buttonName: "Sign up" },
  { userId: "user_123" }
);

// Associate user with a group and set group traits
client.group(
  "org_456",
  { name: "Acme Inc", plan: "enterprise" },
  { userId: "user_123" }
);

// Or just link the user to a group (if traits are already set)
client.group("org_456", null, { userId: "user_123" });

Node.js

const { KlimeClient } = require("@klime/node");

const client = new KlimeClient({
  writeKey: "your-write-key",
});

// Identify a user
client.identify("user_123", { email: "user@example.com" });

// Track an event
client.track("API Request", { endpoint: "/users" }, { userId: "user_123" });

// Associate user with a group and set group traits
client.group(
  "org_456",
  { name: "Acme Inc", plan: "enterprise" },
  { userId: "user_123" }
);

// Or just link the user to a group (if traits are already set)
client.group("org_456", null, { userId: "user_123" });

// Graceful shutdown
process.on("SIGTERM", async () => {
  await client.shutdown();
  process.exit(0);
});

Features

Both SDKs share these core features:

  • Automatic Batching: Events are batched and sent every 2 seconds or when batch reaches 20 events
  • Automatic Retries: Failed requests retry with exponential backoff
  • Zero Dependencies: Uses only native APIs (fetch in browsers, fetch/https in Node.js)
  • TypeScript Support: Full type definitions included
  • Universal Module Support: Works with ESM (import) and CommonJS (require) out of the box

Browser-specific Features

  • Automatic browser context (userAgent, locale, timezone)
  • Page unload handling (beforeunload event)
  • crypto.randomUUID() with fallback

Node.js-specific Features

  • Process exit handling (SIGTERM/SIGINT)
  • Fallback to https/http modules for Node.js < 18

Development

This monorepo uses pnpm workspaces.

# Install dependencies
pnpm install

# Build all packages
pnpm build

# Clean all packages
pnpm clean

API Reference

See the individual package READMEs for detailed API documentation:

License

MIT

About

Klime SDK for JavaScript

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors