# @oxvoinsights/types

**TypeScript types for the OXVO Insights browser SDK.**
Use this package when your page loads OXVO Insights with a script tag and you want `window.oxvoinsights` to be typed.

## Do you need it?

- **Yes**, if OXVO Insights reaches your page through a `<script>` tag and your TypeScript code calls `window.oxvoinsights`.
- **No**, if you install an SDK from npm. These packages already include their types:
    - `@oxvoinsights/browser` for websites and web apps
    - `@oxvoinsights/react` for React apps
    - `@oxvoinsights/node` for Node.js servers

## Install

```bash
npm install --save-dev @oxvoinsights/types
```

The package contains only types, so it adds nothing to your bundle.

## Type `window.oxvoinsights`

Add a declaration file to your project:

```ts
// oxvoinsights.d.ts
import type { OxvoInsights } from '@oxvoinsights/types'

declare global {
    interface Window {
        oxvoinsights?: OxvoInsights
    }
}

export {}
```

Calls are then checked by TypeScript:

```ts
window.oxvoinsights?.track('plan_selected', { plan: 'pro' })
window.oxvoinsights?.identify('user_12345', { plan: 'pro' })

if (window.oxvoinsights?.flags.isEnabled('new-checkout')) {
    showNewCheckout()
}
```

## Type options, events and properties

```ts
import type { InsightsInitOptions, OxvoInsights, Properties } from '@oxvoinsights/types'

const options: InsightsInitOptions = {
    token: '<your-project-token>',
    host: '<your-api-host>',
    capturePageview: 'history_change',
}

const properties: Properties = { plan: 'pro' }

type Events = {
    plan_selected: { plan: 'free' | 'pro' }
}

function trackPlan(client: OxvoInsights<Events>) {
    client.track('plan_selected', { plan: 'pro' })
}
```

`OxvoInsights<Events>` checks event names and their properties in `track` calls.

## Versions

This package is released alongside `@oxvoinsights/browser` and describes its current API.
Use the same major version as the SDK your page loads.

## Learn more

- **OXVO Insights:** [oxvo.com](https://oxvo.com)
- **Support:** [oxvo.com/support](https://oxvo.com/support)

