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

# User event

> Detailed `user` event guide

The `user` event is a fundamental aspect of user analytics, enabling consistent tracking and reference across their lifetime.
Whether you’re dealing with registered users or anonymous visitors, the `user` event helps you associate meaningful data with each individual.\
This documentation provides detailed guidance on utilizing the `user` event to enrich
your user data for more personalized and insightful analytics.

First of all, you don't need to fire an `user` event for every user action. Instead, you should fire it when
you first learn about a user, and then update the user's information as it changes (for example, when they update their profile, or when they first register).

## How It Works

When you fire a `user` event using the Edgee SDK or Data Layer, we securely process the user data at the edge and make sure all subsequent events are bound to that user.
Here's how it works across different platforms:

* **Browser**: Edgee stores the user data in an encrypted first-party cookie. This ensures privacy and compliance while persisting the data across sessions.
* **Edge Processing**: Unlike traditional client-side solutions, Edgee processes the user data at the network edge, eliminating the need for heavy local storage and enhancing data accuracy.

This architecture ensures that all subsequent `page` or `track` events are seamlessly enriched with the user's data. As a result, actions can be properly attributed, providing a complete and accurate view of user behavior.

In addition to that, some components support the user event natively, such as [Amplitude](/docs/proxy/components/data-collection/amplitude) and [Segment](/docs/proxy/components/data-collection/segment).
For these components - in addition to enriching the `page` and `track` events for them as well - every time you make a `user` call, Edgee will send the appropriate event to the component (e.g. `identify` for Segment).

## When to use the `user` event

The `user` event is particularly useful in scenarios where you collect information about a user without having a definitive `userId`, such as:

* Capturing an email address during a newsletter sign-up.
* Associating actions with an anonymous user before they create an account.
* Updating user traits after a profile update or preference change.

## Event fields

The `user` event collects the following fields about the user:

| Field          | Type   | Description                                                                                                                      |
| -------------- | ------ | -------------------------------------------------------------------------------------------------------------------------------- |
| `user_id`      | String | A unique identifier for the user, typically from your own user database.                                                         |
| `anonymous_id` | String | An alternative identifier used when a `user_id` is not available, ensuring that user actions can still be collected anonymously. |
| `properties`   | Object | Free-form dictionary of properties of the user                                                                                   |

<Warning>
  **Privacy Notice**:
  This method and all the fields it collects are subject to data privacy regulations.
  When collecting user data, always prioritize user privacy and data protection.
  Ensure that you are compliant with relevant data privacy regulations and obtain user consent before collecting any personal information.
</Warning>

## How to collect the `user` event?

The `user` event can be collected manually by calling the `edgee.user()` method via the Edgee SDK.

Here's a breakdown of how to structure an `user` call:

```javascript theme={"dark"}
// Syntax: edgee.user(user_obj, components);
edgee.user({
    "user_id": "abcde",
    "anonymous_id": "12345",
    "properties": {
      "email": "me@example.com",
      "name": "John Doe"
    }
}, {
    "all": false,
    "edgee/google-analytics": true
});
```

As usual, you can optionally specify the components where the event should be sent. If the components parameter is not
specified, the event will be sent to all the components you've set up in your Edgee project.

The `edgee.user()` method expect two optionals parameters:

| field                     | type           | description                                                                                                                                                                                                                                          |
| ------------------------- | -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `user_obj` (*optional*)   | object\|String | A free-form dictionary object containing details of the `user` event. This object can include all the following optional fields: `user_id`, `anonymoudId`, and `properties`. You can also provide a string that will be used as the `user_id` field. |
| `components` (*optional*) | object         | Specifies which analytics components should receive the event data. This allows for targeted data sending based on your configured components within the Edgee platform.                                                                             |

## Best Practices for `user` Events

* **Consistent Identification**: Use consistent identifiers across your application to ensure accurate user tracking. Decide on a standard for when to use `user_id` versus `anonymous_id` and stick to it.
* **Relevant Properties**: Only include properties that offer actionable insights. Avoid collecting sensitive or unnecessary information to maintain data integrity.
* **Selective Destination Sending**: Utilize the components parameter to fine-tune where your data is sent, optimizing for relevance and efficiency.
* **Privacy Compliance**: Ensure that you are compliant with relevant data privacy regulations when collecting user data. Always prioritize user consent and data protection.
