# Events

> The `Events` class provides methods for tracking all types of user interactions on your e-commerce site. Every event is enriched with device info, session data, referrer, and user context before being sent to the Segmentify API.

## SegmentifyEventManager

Use the `SegmentifyEventManager` singleton to access the shared `Events` instance. This is the recommended approach — it guarantees a single instance across your app:

```typescript
import { SegmentifyEventManager } from '@segmentify/integration';

SegmentifyEventManager().PageView({ category: 'home' });
SegmentifyEventManager().ProductView({ productId: 'PROD-123', price: 99.99 });
```

`SegmentifyEventManager()` creates an `Events` instance on first call and returns the same instance on subsequent calls.

::: tip
You can also create an instance directly with `new Events()`, but `SegmentifyEventManager` ensures all parts of your app share the same queue and config.
:::

## Page View

Track when a user visits a page:

```typescript
await SegmentifyEventManager().PageView({
  category: 'home',
  subCategory: 'main',
  params: { page: 'homepage' },
});
```

## Product View

Track when a user views a product. The library automatically detects the traffic source (social, paid, search, etc.):

```typescript
await SegmentifyEventManager().ProductView({
  productId: 'PROD-123',
  title: 'Amazing Product',
  price: 99.99,
  oldPrice: 129.99,
  category: 'electronics',
  brand: 'TechBrand',
  inStock: true,
  url: 'https://example.com/product/123',
  image: 'https://example.com/images/product-123.jpg',
  colors: ['red', 'blue'],
  sizes: ['S', 'M', 'L'],
});
```

## Basket Operations

### Add to Basket

```typescript
await SegmentifyEventManager().BasketAdd({
  productId: 'PROD-123',
  price: 99.99,
  quantity: 1,
});
```

### Remove from Basket

```typescript
await SegmentifyEventManager().BasketRemove({
  productId: 'PROD-123',
  price: 99.99,
  quantity: 1,
});
```

### View Basket

```typescript
await SegmentifyEventManager().BasketView({
  totalPrice: 299.97,
  productList: [
    { productId: 'PROD-123', price: 99.99, quantity: 2 },
    { productId: 'PROD-456', price: 99.99, quantity: 1 },
  ],
});
```

### Clear Basket

```typescript
await SegmentifyEventManager().BasketClear({});
```

::: tip
`basketId` is automatically generated from the user ID. You don't need to pass it manually.
:::

## Purchase

Track completed purchases:

```typescript
await SegmentifyEventManager().Purchase({
  totalPrice: 299.97,
  orderNo: 'ORDER-789',
  productList: [
    { productId: 'PROD-123', price: 99.99, quantity: 2 },
    { productId: 'PROD-456', price: 99.99, quantity: 1 },
  ],
  paymentType: 'credit_card',
  coupon: 'SAVE10',
  tax: 30.0,
  shipment: 5.99,
});
```

## Checkout Steps

Track checkout funnel progression for drop-off analysis:

```typescript
// Step 1: Customer information
await SegmentifyEventManager().CustomerInformation({
  totalPrice: 199.99,
  productList: [{ productId: 'SKU-100', quantity: 1, price: 199.99 }],
});

// Step 2: Payment information
await SegmentifyEventManager().PaymentInformation({
  totalPrice: 199.99,
  orderNo: 'pending',
  productList: [{ productId: 'SKU-100', quantity: 1, price: 199.99 }],
});

// Step 3: Purchase (see above)
```

## User Operations

### Register

```typescript
await SegmentifyEventManager().Register({
  email: 'newuser@example.com',
  username: 'newuser',
});
```

### Log In

```typescript
await SegmentifyEventManager().LogIn({
  email: 'user@example.com',
  username: 'johndoe',
});
```

### Log Out

```typescript
await SegmentifyEventManager().Logout({});
```

### Identify (Update Profile)

Update user profile data without triggering a login/register event:

```typescript
await SegmentifyEventManager().Identify({
  email: 'user@example.com',
  source: 'email-collection',
  isRegistered: true,
});
```

The `Identify` method also handles special source types:

- `source: 'email-collection'` automatically sets `emailNtf: true`
- `source: 'whatsapp-collection'` automatically sets `whatsappNtf: true`

### IYS Permission (Turkey Compliance)

Record IYS consent for commercial communications:

```typescript
await SegmentifyEventManager().IysPermission({
  email: 'user@example.com',
  phone: '+905551234567',
});
```

## Favorites / Wishlist

### Add to Favorites

```typescript
await SegmentifyEventManager().FavoriteAdd({
  productId: 'SKU-999',
  favoritesList: ['SKU-1', 'SKU-2', 'SKU-999'],
});
```

### Remove from Favorites

```typescript
await SegmentifyEventManager().FavoriteRemove({
  productId: 'SKU-999',
  favoritesList: ['SKU-1', 'SKU-2'],
});
```

### View Favorites

```typescript
await SegmentifyEventManager().FavoriteView({
  favoritesList: ['SKU-1', 'SKU-2'],
});
```

### Favorite Detail

```typescript
await SegmentifyEventManager().FavoriteDetail({
  favoritesList: ['SKU-1', 'SKU-2', 'SKU-3'],
});
```

## Form Tracking

Track form submissions for lead capture and analytics:

```typescript
await SegmentifyEventManager().Form({
  formName: 'contact',
  fields: { name: 'John', message: 'Hello' },
  instanceId: 'contact-form-1',
});
```

## Interaction Tracking

Track clicks, scroll events, or custom UI interactions:

```typescript
await SegmentifyEventManager().Interaction({
  type: 'click',
  instanceId: 'hero-banner-1',
  interactionId: 'cta-button',
});
```

## Gamification

### Wheel of Fortune

```typescript
await SegmentifyEventManager().GamificationWof({
  type: 'win',
  win: '10%_discount',
  instanceId: 'wof-banner-1',
});
```

### Scratch Card

```typescript
await SegmentifyEventManager().GamificationScratch({
  type: 'win',
  win: 'free_shipping',
  instanceId: 'scratch-card-1',
});
```

## Advanced: sendEvent

All methods above are convenience wrappers around `sendEvent`. You can call it directly for custom events:

```typescript
await SegmentifyEventManager().sendEvent({
  name: 'CUSTOM_EVENT',
  type: 'my_custom_type',
  params: { key: 'value' },
});
```

### Delayed Events

Send an event after a delay (e.g., engagement tracking):

```typescript
await SegmentifyEventManager().sendEvent(
  { name: 'CUSTOM_EVENT', type: 'scroll-depth', params: { depth: 75 } },
  { timeout: 3000 } // 3 second delay
);
```

### Deferred Events

Queue an event for the next page load:

```typescript
await SegmentifyEventManager().sendEvent({
  name: 'PRODUCT_VIEW',
  productId: 'SKU-42',
  nextPage: true, // Saved to localStorage, sent on next page
});
```

## Event Methods Summary

| Method                | Internal Event Name | Step             |
| --------------------- | ------------------- | ---------------- |
| `PageView`            | `PAGE_VIEW`         | —                |
| `ProductView`         | `PRODUCT_VIEW`      | —                |
| `BasketView`          | `CHECKOUT`          | `view-basket`    |
| `BasketAdd`           | `BASKET_OPERATIONS` | `add`            |
| `BasketRemove`        | `BASKET_OPERATIONS` | `remove`         |
| `BasketClear`         | `BASKET_OPERATIONS` | `clear`          |
| `Purchase`            | `CHECKOUT`          | `purchase`       |
| `CustomerInformation` | `CHECKOUT`          | `customer-info`  |
| `PaymentInformation`  | `CHECKOUT`          | `payment-info`   |
| `Register`            | `USER_OPERATIONS`   | `signup`         |
| `LogIn`               | `USER_OPERATIONS`   | `signin`         |
| `Logout`              | `USER_OPERATIONS`   | `signout`        |
| `Identify`            | `USER_OPERATIONS`   | `update`         |
| `IysPermission`       | `USER_OPERATIONS`   | `iyspermissions` |
| `FavoriteAdd`         | `WISHLIST`          | `add`            |
| `FavoriteRemove`      | `WISHLIST`          | `remove`         |
| `FavoriteView`        | `WISHLIST`          | `view-favorites` |
| `FavoriteDetail`      | `WISHLIST`          | `detail`         |
| `Form`                | `FORM`              | —                |
| `Interaction`         | `INTERACTION`       | —                |
| `GamificationWof`     | `GAMIFICATION`      | `win`            |
| `GamificationScratch` | `GAMIFICATION`      | `win`            |
| `ErrorHandler`        | `CUSTOM_EVENT`      | —                |

## See Also

- [Queue — How Events Are Processed](/10-queue)
- [Error Handling](/07-error-handling)
