# Profiles

Create customer profiles records in your warehouse using Jitsu Profile Builder.

<Screenshot src="/docs/screenshots/profiles.png" />

:::info
Profile builder is an enterprise feature. Please [contact support](mailto:support@jitsu.com) to enable it for your account.
:::


## What are Profiles?

Profiles are customer records stored in your warehouse, based on the events data you send to Jitsu.

Profile Builder generates profiles based on the `traits` object in `identify` events.

You can also define custom logic for profile generation using a JavaScript function,
allowing you to leverage up to a year’s worth of user events data.

## Configuring Profile Builder

To set up Profile Builder, navigate to the `Customers` -> `Profile Builder` section in the Jitsu UI.

Profile builder is an enterprise feature and initially will appear in a LOCKED state.
Please [contact support](mailto:support@jitsu.com) to enable it for your account.
:::info
While in a locked state, Profile Builder will not create profiles in your warehouse. However, you can still configure it, debug your custom profile generation function, and preview profile results based on example events data. 
:::

Profile Builder configuration consists of the following sections:

* **Profile Function** - code of a JavaScript function that generates profiles based on the events data.
* **Transformation** - allows setting up a chain of functions to filter or transform events before they are used for building a profile.
* **Environment Variables** - environment variables that can be used in your profile generation function.
* **Settings** - destination and other settings.

### Events Sources

All sites added to the workspace act as event sources for Profile Builder.

By default, only non-anonymous events—those containing the `userId` field—are used to build profiles.

With [Transformation](#transformation), you can exclude events from profile building or, conversely, assign a profile ID to events that originally lacked a userId field.

### Profile Function

The Code section is where you define the JavaScript function that generates profiles based on the events data.

Profile generation function has the following signature:

```javascript
export default async function(events, user, context) {
  context.log.info("Profile Id: " + user.profileId)
  const profile = {}
  for (const event of events) {
    //count events by type
    profile[event.type] = (profile[event.type] ?? 0) + 1
  }
  profile.anonId = user.anonymousId
  return {
    traits: profile
  }
}
```
where:
* `events` is an Iterable of events. Use the `for (const event of events)` loop to iterate over events.
* `user` is a user object. It contains the following fields:
  * `profileId` - ID of profile
  * `userId` - user ID
  * `anonymousId` - anonymous ID
  * `traits` - user traits collected using built-in profile generation logic
* `context` the function context. I contain various services that can be used in the function
    * `context.log` - [logging service](/docs/functions/runtime#logging)
    * `context.store` - [persistent storage](/docs/functions/runtime#persistent-storage)
    * `context.fetch` - [a standard fetch API](/docs/functions/runtime#fetch-api) to make HTTP requests
    * `context.getWarehouse` - [Warehouse API](/docs/functions/runtime#warehouse-api) to query your data warehouses
    * `context.profileBuilder` - contains meta information about the current Profile Builder: `id`, `version`

**Return value** of the function is a profile object:

```typescript
type ProfileResult = {
  profileId?: string;
  destinationId?: string;
  tableName?: string;
  traits: Record<string, any>;
}
```
where:
* `profileId` - Allows to override the profile ID. If not specified, Jitsu will use profile ID assigned in [Transformation](#transformation) or `userId` by default.
* `destinationId` - Allows to override the default destination. If not specified, Jitsu wil use the Default Destination from the Profile Builder settings.
* `tableName` - Allows to override the default table name. If not specified, Jitsu will use the Default Table Name from the Profile Builder settings.
* `traits` - object with profile properties. All custom profile properties should be placed in the `traits` property.

Traits returned by the function will be merged with the traits collected using built-in profile generation logic.

#### SDK

It is also possible to work on Profile Builder function code in your IDE and then sync it with Jitsu.

[Jitsu SDK](/docs/functions/sdk) supports Profile Builder functions.<br/>
Profile Builder function should be located in the `src/profiles` directory and configured with the `profileBuilderId` config property within the function code.<br/>
Id of Profile Builder can be obtained from the `Settings` tab.

```typescript
import { ProfileFunction } from "@jitsu/protocols/profile";

export const config = {
    profileBuilderId: "[Profile Builder ID]", // Required: id of Profile Builder where this function will be used. Can be found in the Profile Builder Settings UI
    slug: "profile.ts", //id (uniq per workspace) used to identify function in Jitsu
};

const profileExample: ProfileFunction = async (events, user, { log }) => {
  //TODO
}
```

### Transformation

Transformation allows you to filter or transform events before they are used for building a profile.

In the Transformation section you can set up the chain of standard Jitsu [functions](/docs/functions) to process events before they are passed to the Profile Builder function.

Use `return "drop"` to exclude an event from profile building.

#### Custom Profile ID

Using transformation, you can assign a profile ID to events that originally lacked a `userId` field or had a different `userId` field.

To assign a profile ID to an event, add `JITSU_PROFILE_ID` property to the returned event object:

```javascript
export default async function transform(event, { log, props, store }) {
    event.JITSU_PROFILE_ID = event.propeties.internalId;
    return event;
}
```

## Publishing Profile Builder

Collecting of customers events history and profile-building logic starts after the initial version of Profile Builder is published.

To publish the Profile Builder, click the `Publish` button in the top right corner of the Profile Builder editor.

:::tip
You can continue to work on your profile generation function after the initial version is published by utilizing the **Drafts**.

Saved Drafts can be used for debugging and previewing profile results based on example events data and don't affect the production profile generation process.
:::

Each time you publish a new version of the Profile Builder, the following happens:

1. Profile Builder's numeric `version` is incremented.
2. Profile Builder starts rebuilding profiles of all tracked customers based on the new version of the profile generation function.<br/>This process may take some time depending on the number of customers and events and can be monitored on the `Build Progress` tab.
3. After the rebuild is complete, the new version of the profile generation function is used to rebuild profiles in real-time based on new events coming.