# Managing your audience's identities

## How it works[](#how-it-works)

Our JavaScript client manages the identities of your audience, first as an `anonymousId`. Then, when you identify them, you know them by their `userId`. Then we remove those values when a person logs out and you call `cioanalytics.reset();`.

We store user identity information in cookies and local storage. You can retrieve or override this information as necessary to support your integrations. This page provides information about the values we store, *where* we store them, and how to get or override them.

## ID Persistence[](#id-persistence)

We write the user’s IDs to their browser’s local storage, and use that as the user ID on cookies whenever possible.

If a user returns to your site after the cookie expires, we’ll look for an old ID in the user’s `localStorage`. If we find an ID, we’ll set it as the user’s ID again in the new cookie. If a person clears their cookies *and* `localstorage`, they’ll remove all IDs, and they’ll get a completely new `anonymousID` the next time they visit your site.

## Anonymous IDs[](#anonymous-ids)

We generate a [universally unique ID (UUID)](https://en.wikipedia.org/wiki/Universally_unique_identifier) for website visitors when our JavaScript initializes, and we set this value as the `anonymousId` for each new visitor to your site. This happens before we load direct-connection integrations, so they don’t generate their own user IDs.

Example:

```javascript
ajs_anonymous_id=%2239ee7ea5-b6d8-4174-b612-04e1ef3fa952
```

You can override the auto-generated anonymousID in code using the methods described below:

*   [Set anonymousId](#override-the-anonymous-id) (before the `ready` method returns)
*   [Use a call to override the anonymousID](#override-the-default-anonymous-id)
*   [Set `anonymousId` in the `options` object of a call](#override-anonymous-ids-using-the-options-object)

### Retrieve the Anonymous ID[](#retrieve-the-anonymous-id)

You can get the user’s current `anonymousId` with the `user` method.

```javascript
cioanalytics.user().anonymousId();
```

If the user doesn’t have an `anonymousId` (it’s `null`) this call will automatically generate and sets a new `anonymousId` for the user.

### Refreshing the Anonymous ID[](#refreshing-the-anonymous-id)

A user’s `anonymousId` changes in any of the following situations:

*   They clear their cookies *and* `localstorage`.
*   Your site or app calls `cioanalytics.reset()` during a user’s browser session.
*   Your site or app calls `cioanalytics.identify()` with a userId that is different from the current userId.

### Override the Anonymous ID[](#override-the-anonymous-id)

You can also set the `anonymousId` immediately, even before the `ready` method returns.

```javascript
 cioanalytics.load('writekey');
 cioanalytics.page();
 cioanalytics.setAnonymousId('ABC-123-XYZ');
```

You might use this method if you queue calls before `ready` returns and those methods require a custom `anonymousId`. Keep in mind that setting the `anonymousId` in Analytics.js does not overwrite the anonymous tracking IDs for any other integrations you use.

 Direct-connection integrations might have their own anonymous IDs

In some rare cases, integrations might not use Customer.io’s `anonymousId`. Read the documentation for your integration to find out if it sets its own ID.

### Override the default Anonymous ID[](#override-the-default-anonymous-id)

If the default anonymous UUID doesn’t meet your needs, you can override `anonymousId` for the current user using either of the following methods.

```javascript
cioanalytics.user().anonymousId('ABC-123-XYZ');
```

```javascript
cioanalytics.setAnonymousId('ABC-123-XYZ')
```

### Override Anonymous IDs using the options object[](#override-anonymous-ids-using-the-options-object)

You can override an `anonymousId` in the `options` object of [`identify`](/integrations/api/cdp/#operations/identify/), [`page`](/integrations/api/cdp/#operations/page/), or [`track`](/integrations/api/cdp/#operations/track/) calls. The custom `anonymousId` persists when you use the methods below, even if you don’t specify the anonymousId in the calls.

 Identify

#### Identify[](#Identify)

```javascript
cioanalytics.identify('user_123', {
  name: 'Jane Kim'
}, {
  anonymousId: 'ABC-123-XYZ'
});
```

 Page

#### Page[](#Page)

```javascript
cioanalytics.page({}, { anonymousId: 'ABC-123-XYZ' });
```

 Track

#### Track[](#Track)

```javascript
cioanalytics.track('Email Clicked', {
  callToAction: 'Signup'
}, {
  anonymousId: 'ABC-123-XYZ'
});
```

## Saving traits to the context object[](#saving-traits-to-the-context-object)

Traits are things that you know about a user or a group, and which can change over time—like [attributesA key-value pair that you associate with a person or an object—like a person’s name, the date they were created in your workspace, or a company’s billing date etc. Use attributes to target people and personalize messages.](/journeys/attributes/) in Customer.io Journeys.

The `options` object contains a child object called `context` that automatically captures data depending on the event and the SDK or library you use. See our [the context object](/integrations/data-in/source-spec/common-fields/#the-context-object) to learn more.

The `context` object contains an optional `traits` dictionary. This dictionary contains traits about the current user. You can use this to retrieve information about a user that you set or stored as a result of previous `identify` calls. This might be useful if you also want to send traits as properties in `track` or `page` calls.

The information you pass in `traits` *does not* appear in your downstream tools (like Salesforce, Mixpanel, or Google Analytics). But this data *does* appear in warehouses and storage integrations.

Imagine that you sent this `identify` call.

```javascript
cioanalytics.identify('12091906-01011992', {
    plan_id: 'Paid, Tier 2',
    email: 'cool.person@example.com'
});
```

You can pass the `plan_id` into `context.traits`, so you can use them in `track` and `page` events that the user triggers later, as shown below.

```javascript
cioanalytics.track('Clicked Email', {
  	emailCampaign: 'First Touch'
  },
  {
    traits: {
      plan_id: 'Paid, Tier 2'
    }
  }
);
```

This appends the `plan_id` trait to the `track` event. This does *not* add the name or email, since those traits were not in the `context` object. You must do this for every event that you want these traits to appear on, because `context` does not persist between calls.

## Clearing Traits[](#clearing-traits)

You can pass an empty object to the `traits` object to clear *all* cached traits for a User or Group.

Traits are cached by default when you call the Identify and Group methods. You can clear the `traits` object for the user or group by passing `traits` an empty object:

```javascript
cioanalytics.user().traits({});
```

```javascript
cioanalytics.group().traits({});
```

## Using cioanalytics.user() and cioanalytics.group()[](#using-cioanalyticsuser-and-cioanalyticsgroup)

You can use the `user` or `group` method as soon as the Analytics.js library loads, to return information about the currently identified user or group. This information is retrieved from the user’s cookie.

 Use the `ready` function

You can wrap any reference to `user()` or `group()` in a [ready function block](/integrations/data-in/connections/javascript/utility-methods/#ready) to make sure that cioanalytics.js fully loads and these methods are available.

 Get the current user

#### Get the current user[](#Get the current user)

```javascript
cioanalytics.ready(function() {

  var user = cioanalytics.user();
  var id = user.id();
  var traits = user.traits();

});
```

 Get a group

#### Get a group[](#Get a group)

```javascript
cioanalytics.ready(function() {

  var group = cioanalytics.group();
  var id = group.id();
  var traits = group.traits();

});
```

## Anonymizing IP addresses[](#anonymizing-ip-addresses)

We automatically collect the user’s IP address for device-based events.

You can pass a value for `options.context.ip` to prevent us from recording IP addresses like this:

```javascript
  cioanalytics.track("Order Completed", {}, { context: { ip: "0.0.0.0" }});
```

You must add this override to *every* `track` call to explicitly override IP collection. If you reset this trait in the context object, we’ll default to the normal IP collection behavior.