npm package: @rebutiq/sdk
Description: Rebutiq SDK — Automated dispute defense tracking for SaaS apps
Latest version: 1.0.0

# @rebutiq/sdk

Automated dispute defense tracking for SaaS apps. Collect behavioral evidence that wins chargebacks.

## Install

```bash
npm install @rebutiq/sdk
```

## Browser / Frontend

```js
import rebutiq from '@rebutiq/sdk';

rebutiq.init({ apiKey: 'rbq_live_...' });

// Identify user (links to Stripe customer)
rebutiq.identify({
  userId: 'user_123',
  stripeCustomerId: 'cus_xxx',
  email: 'john@example.com',
  name: 'John Doe',
});

// Track events
rebutiq.track('terms.accepted', { tosVersion: 'v2.1' });
rebutiq.track('feature.used', { feature: 'export_report' });
rebutiq.track('payment.success', { amount: 9900 });
```

Auto-captures: page views, clicks, session duration, SPA navigation.

## React

```jsx
import { RebutiqProvider, useRebutiq, useRebutiqIdentify, useRebutiqPage } from '@rebutiq/sdk/react';

// Wrap your app
function App() {
  return (
    <RebutiqProvider apiKey="rbq_live_...">
      <Dashboard />
    </RebutiqProvider>
  );
}

// In components
function Dashboard() {
  const rbq = useRebutiq();

  // Auto-identify when user loads
  useRebutiqIdentify({
    userId: user.id,
    email: user.email,
    stripeCustomerId: user.stripeId,
  });

  // Track page views on route change
  useRebutiqPage(location.pathname);

  return (
    <button onClick={() => rbq.track('button.clicked', { label: 'Export' })}>
      Export
    </button>
  );
}
```

## Vue

```js
import rebutiq from '@rebutiq/sdk';

// In main.js
rebutiq.init({ apiKey: 'rbq_live_...' });

// In a component
rebutiq.identify({ userId: user.id, stripeCustomerId: user.stripeId });
rebutiq.track('feature.used', { feature: 'dashboard_view' });

// Track route changes
router.afterEach((to) => {
  rebutiq.page(to.name, { path: to.path });
});
```

## Next.js

```jsx
// app/providers.jsx
'use client';
import { RebutiqProvider } from '@rebutiq/sdk/react';

export function Providers({ children }) {
  return (
    <RebutiqProvider apiKey={process.env.NEXT_PUBLIC_REBUTIQ_KEY}>
      {children}
    </RebutiqProvider>
  );
}
```

## Node.js (Server-side)

```js
import createRebutiq from '@rebutiq/sdk/node';

const rbq = createRebutiq('rbq_live_...', {
  endpoint: 'https://api.rebutiq.com/api',
});

// Track server-side events
rbq.track('payment.success', {
  amount: 9900,
  invoiceId: 'in_xxx',
}, {
  userId: 'user_123',
  ip: req.ip,
  userAgent: req.headers['user-agent'],
});

// Identify
await rbq.identify({
  userId: 'user_123',
  stripeCustomerId: 'cus_xxx',
  email: 'john@example.com',
});

// Express middleware (auto-tracks all requests)
app.use(rbq.middleware({
  getUserId: (req) => req.user?.id,
  ignorePaths: ['/health', '/favicon.ico'],
}));

// Flush on shutdown
process.on('SIGTERM', () => rbq.shutdown());
```

## API

### Core

| Method | Description |
|--------|-------------|
| `init(config)` | Initialize with API key |
| `identify(userData)` | Link user to Stripe customer |
| `track(event, properties)` | Track a custom event |
| `page(name, properties)` | Track page view |
| `flush()` | Force-send queued events |
| `reset()` | Clear user/session |
| `shutdown()` | Flush and stop |
| `use(fn)` | Add event middleware |

### React Hooks

| Hook | Description |
|------|-------------|
| `useRebutiq()` | Get SDK instance |
| `useRebutiqIdentify(data)` | Auto-identify on change |
| `useRebutiqPage(pathname)` | Track page on route change |
| `useRebutiqTrack()` | Stable track function |

### Config

```js
{
  apiKey: 'rbq_live_...',      // Required
  endpoint: 'https://...',     // Custom API URL
  debug: false,                // Console logging
  autoCapture: true,           // Auto page views, clicks, session
  clickSelector: 'a, button',  // CSS selector for click tracking
  onError: (err) => {},        // Error callback
}
```

## License

MIT