# Leanbase SDK

Lightweight browser SDK for Leanbase - event tracking, autocapture, and session replay.

## Installation

```bash
npm install @leanbase.com/js
# or
pnpm add @leanbase.com/js
```

## Quick Start

```javascript
import { Leanbase } from '@leanbase.com/js'

// Initialize with your API key
const leanbase = new Leanbase('your-api-key')

// Track events
leanbase.capture('button_clicked', {
  button_name: 'signup',
  page: 'homepage'
})

// Identify users
leanbase.identify('user-123', {
  email: 'user@example.com',
  name: 'John Doe'
})
```

## CDN / Script Tag

Include the prebuilt IIFE bundle from a CDN and use the global `leanbase` object. Call `leanbase.init('<apiKey>', options)` once, then use `leanbase.capture(...)` and friends.

```html
<script src="https://unpkg.com/@leanbase.com/js/dist/leanbase.iife.js"></script>
<script>
  // Initialize with your API key
  leanbase.init('your-api-key', { host: 'https://i.leanbase.co' })

  // Track an event
  leanbase.capture('pageview')
</script>
```

Alternatively via jsDelivr:

```html
<script src="https://cdn.jsdelivr.net/npm/@leanbase.com/js/dist/leanbase.iife.js"></script>
```

## Configuration

```javascript
const leanbase = new Leanbase('your-api-key', {
  host: 'https://i.leanbase.co', // default
  autocapture: true, // default - automatically capture clicks and form interactions
  preloadFeatureFlags: true, // default - fetch feature flags on initialization
})
```

## API Reference

### `leanbase.capture(event, properties?, options?)`

Track custom events.

```javascript
leanbase.capture('purchase_completed', {
  amount: 99.99,
  currency: 'USD',
  product_id: 'abc-123'
})
```

### `leanbase.identify(distinctId?, properties?, options?)`

Identify users and set user properties.

```javascript
leanbase.identify('user-456', {
  email: 'jane@example.com',
  plan: 'premium'
})
```

### `leanbase.group(groupType, groupKey, properties?)`

Associate users with groups (teams, organizations, etc).

```javascript
leanbase.group('company', 'acme-corp', {
  name: 'Acme Corporation',
  plan: 'enterprise'
})
```

### `leanbase.alias(alias)`

Create an alias for the current user.

```javascript
leanbase.alias('user-123')
```


## Publishing

### Version Bumping
This monorepo uses [Changesets](https://github.com/changesets/changesets) for versioning.

To bump the version:
1. Create a changeset: `pnpm changeset add`
   - Select `@leanbase.com/js` package
   - Choose version bump type (patch, minor, major)
   - Add a description of changes
2. Commit the changeset file (`.changeset/*.md`)
3. Run `pnpm changeset version` to update versions
4. Commit the version changes

Example changeset file (`.changeset/example-changes.md`):
```markdown
---
"@leanbase.com/js": patch
---

Added IIFE build for script-tag usage and improved documentation.
```

### Publishing to NPM
1. Ensure you have an NPM token with publish permissions
2. Set the token: `export NPM_TOKEN=your_token`
3. Publish: `pnpm --filter=@leanbase.com/js publish`
4. Or test first: `pnpm --filter=@leanbase.com/js exec npm publish --dry-run`

The package will be published to https://www.npmjs.com/package/@leanbase.com/js

## License

Copyrighted by Leanflag Limited
