> ## Documentation Index
> Fetch the complete documentation index at: https://developer.cashbackhive.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Events

> Cashback Hive SDK events reference: subscribe to ready, click and auth:change with on() and off(), and listen for the window-level cbh:ready event.

The SDK has two event mechanisms: a DOM event on `window` for readiness, and an internal emitter for everything after that.

## Window event: `cbh:ready`

Dispatched on `window` once, after initialisation. A `CustomEvent` whose `detail.sdk` is the SDK instance.

```ts theme={null}
window.addEventListener("cbh:ready", (event: CustomEvent<{ sdk: CashbackHiveSdk }>) => {
  const sdk = event.detail.sdk;
});
```

Because it fires once, a listener added after initialisation never receives it. Check `window.CashbackHive?.initialized` first. See [Lifecycle](https://developer.cashbackhive.com/concepts/lifecycle.md).

## Emitter methods

### `on()`

```ts theme={null}
on<K extends keyof EventMap>(event: K, handler: (payload: EventMap[K]) => void): () => void
```

Subscribe to an event. **Returns an unsubscribe function**, which is convenient in frameworks with effect cleanup:

```ts theme={null}
useEffect(() => sdk.on("auth:change", ({ member }) => setMember(member)), [sdk]);
```

Handlers that throw are caught and logged with `console.error`; they don't affect other handlers or the SDK.

### `off()`

```ts theme={null}
off<K extends keyof EventMap>(event: K, handler: (payload: EventMap[K]) => void): void
```

Unsubscribe a specific handler. Equivalent to calling the function returned by `on()`.

## Emitter events

### `ready`

Emitted synchronously inside `init()`, immediately after initialisation completes.

**version** `string`

The SDK version.

Because it's emitted during `init()`, you can only observe it if you registered a handler **before** initialisation — which is only possible with [manual initialisation](https://developer.cashbackhive.com/reference/configuration.md#manual-initialisation). For auto-initialised installs, use the `cbh:ready` window event instead.

### `click`

Emitted by [`openOffer()`](https://developer.cashbackhive.com/reference/tracking#openoffer) before the URL is opened. Not emitted by `trackingUrl()`.

**slug** `string`

The offer's slug.

**url** `string`

The fully built tracked URL that will be opened.

```ts theme={null}
sdk.on("click", ({ slug, url }) => {
  analytics.track("cashback_offer_click", { slug });
});
```

### `auth:change`

Emitted whenever the cached member changes identity: a member signs in, signs out, or a token restored from storage resolves to a member on load. It is **not** emitted when the same member's profile is refreshed (same `id`).

**member** `Member | null`

The new member, or `null` after sign-out.

```ts theme={null}
sdk.on("auth:change", ({ member }) => {
  if (member) showBalances(member);
  else showSignInButton();
});
```

Triggers for `auth:change`:

| Cause | `member` |
| - | - |
| `auth.signIn()` succeeds | the member |
| `auth.signOut()` | `null` |
| Token found on load and `member.get()` succeeds | the member |
| Token found on load but `member.get()` fails (SDK signs out) | `null` — only if a member was previously cached |
| Token arrives via redirect fragment (`#cbh_auth=…`) and member loads | the member |

## Type map

```ts theme={null}
interface EventMap {
  ready: { version: string };
  click: { slug: string; url: string };
  "auth:change": { member: Member | null };
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.