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

# PHP SDK

> Hightouch's PHP SDK for collecting events

The PHP SDK makes it easy to track events from PHP applications.

## Installation

The PHP SDK can be installed via [Composer](https://packagist.org/packages/ht-sdks/events-sdk-php):

```sh theme={null}
composer require ht-sdks/events-sdk-php
```

To initialize the PHP SDK in your application:

```php theme={null}
use Hightouch\Hightouch;

Hightouch::init('WRITE_KEY', [
  'host' => 'https://us-east-1.hightouch-events.com',
]);
```

## API

### Identify

The `identify` method sends an `identify` event.

Unlike the [Browser SDK](https://docs.hightouch.com/events/sdks/browser.md), it does **not** persist the user ID and
traits locally, so user IDs must be explicitly added to other events. This is
because server side events are usually servicing many different users at once.

**Example usage**:

```php theme={null}
Hightouch::identify([
    'userId' => '123',
    'traits' => [
        'location' => 'San Francisco',
    ],
]);
```

**Method parameters**:

| Parameter | Type | Description |
| - | - | - |
| `userId` | string | The user's persistent ID |
| `anonymousId` | string | The user's anonymous ID |
| `traits` | array | Additional traits about the user, such as `email` and `name`. |
| `context` | array | Overrides to values in the event `context`. By default, `context` contains information autocollected by the SDK. |
| `timestamp` | int | The UNIX timestamp (seconds) of the message. When backfilling, this can be a time in the past. By default it uses `time()`. |

### Track

The `track` method sends a `track` event.

**Example usage**:

```php theme={null}
Hightouch::track([
    'userId' => '123',
    'event' => 'Order Completed',
    'properties' => [
        'total' => 29.99,
    ],
]);
```

**Method parameters**:

| Parameter | Type | Description |
| - | - | - |
| `userId` | string | The user's persistent ID |
| `anonymousId` | string | The user's anonymous ID |
| `event` | string | The name of the event. |
| `properties` | array | Additional properties about the event, such as `product_id`. |
| `context` | array | Overrides to values in the event `context`. By default, `context` contains information autocollected by the SDK. |
| `timestamp` | int | The UNIX timestamp (seconds) of the message. When backfilling, this can be a time in the past. By default it uses `time()`. |

### Page

The `page` method sends a `page` event.

**Example usage**:

```php theme={null}
Hightouch::page([
    'anonymousId' => 'anonymous-id',
    'name'        => 'events-sdk-php',
    'category'    => 'docs',
    'properties'  => [
        'path' => '/docs/libraries/php/',
    ],
]);
```

**Method parameters**:

| Parameter | Type | Description |
| - | - | - |
| `userId` | string | The user's persistent ID |
| `anonymousId` | string | The user's anonymous ID |
| `name` | string | The page's name. For example "Getting started" |
| `properties` | array | Additional properties about the event, such as `url`. |
| `context` | array | Overrides to values in the event `context`. By default, `context` contains information autocollected by the SDK. |
| `timestamp` | int | The UNIX timestamp (seconds) of the message. When backfilling, this can be a time in the past. By default it uses `time()`. |

### Screen

The `screen` method sends a `screen` event.

**Example usage**:

```php theme={null}
Hightouch::screen([
    'userId'     => '123',
    'name'       => 'Getting started',
    'properties' => [
        'path' => '/getting-started',
    ],
]);
```

**Method parameters**:

| Parameter | Type | Description |
| - | - | - |
| `userId` | string | The user's persistent ID |
| `anonymousId` | string | The user's anonymous ID |
| `name` | string | The page's name. For example "Getting started" |
| `properties` | array | Additional properties about the event, such as `url`. |
| `context` | array | Overrides to values in the event `context`. By default, `context` contains information autocollected by the SDK. |
| `timestamp` | int | The UNIX timestamp (seconds) of the message. When backfilling, this can be a time in the past. By default it uses `time()`. |

### Group

The `group` method sends a `group` event.

**Example usage**:

```php theme={null}
Hightouch::group([
    'userId'  => '123',
    'groupId' => 'G-1',
    'traits'  => [
        'company_location' => 'San Francisco',
    ],
]);
```

**Method parameters**:

| Parameter | Type | Description |
| - | - | - |
| `userId` | string | The user's persistent ID |
| `anonymousId` | string | The user's anonymous ID |
| `groupId` | string | The id for the group. |
| `traits` | array | Additional traits about the group, such as `company_name`. |
| `context` | array | Overrides to values in the event `context`. By default, `context` contains information autocollected by the SDK. |
| `timestamp` | int | The UNIX timestamp (seconds) of the message. When backfilling, this can be a time in the past. By default it uses `time()`. |

### Flush

The PHP SDK buffers events locally before sending them to Hightouch's servers.
This minimizes the number of requests made by the SDK and improves performance.

To force the local buffer to be sent to Hightouch immediately call the `flush`
method. `flush` should be called when shutting down your app to make sure no events are lost.

**Example usage**:

```php theme={null}
Hightouch::flush();
```
