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

# Events

> Track events and list event names with the MailGlyph PHP SDK.

Use a **public key** for tracking events, and a **secret key** for listing event names.

## Track an event (`events->track`)

```php theme={null}
<?php

use MailGlyph\MailGlyph;

$tracker = new MailGlyph($_ENV['MAILGLYPH_PUBLIC_KEY']);

$result = $tracker->events->track([
    'email' => 'user@example.com',
    'event' => 'purchase',
    'subscribed' => true,
    'data' => [
        'product' => 'premium',
        'amount' => 99,
    ],
]);

echo $result->contact . PHP_EOL;
echo $result->event . PHP_EOL;
echo $result->timestamp . PHP_EOL;
```

## List event names (`events->listNames`)

```php theme={null}
<?php

use MailGlyph\MailGlyph;

$client = new MailGlyph($_ENV['MAILGLYPH_SECRET_KEY']);

$eventNames = $client->events->listNames();
print_r($eventNames);
```

## List event names with alias (`events->getNames`)

`getNames()` is an alias for `listNames()`:

```php theme={null}
<?php

$eventNames = $client->events->getNames();
print_r($eventNames);
```

## Key-type mismatch example

```php theme={null}
<?php

use MailGlyph\Exceptions\AuthenticationException;
use MailGlyph\MailGlyph;

$client = new MailGlyph($_ENV['MAILGLYPH_SECRET_KEY']);

try {
    $client->events->track([
        'email' => 'user@example.com',
        'event' => 'signup',
    ]);
} catch (AuthenticationException $error) {
    echo $error->getMessage() . PHP_EOL;
}
```

See endpoint details in the [Track event API reference](https://docs.mailglyph.com/api-reference/public-api/trackEvent.md) and [List event names API reference](https://docs.mailglyph.com/api-reference/events/listEventNames.md).


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