# JavaScript Reference

Whether you used [HTML snippet](/docs/sending-data/html),  [JavaScript library](/docs/sending-data/npm), or [React](/docs/sending-data/react) you'll have access to the same API
to send events. The API is based on [Analytics.js](https://getanalytics.io/api/), and is compatible with it. 

## Configuration

### Basic Configuration

| Name           | Script Attribute     | Description                                                                                                                                                                                                                                                                       |
|----------------|----------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `writeKey`     | `data-write-key`     | Browser Write Key configured on Jitsu Site entity. If no Browser Write Key is added for Site entity, Site ID value can be used a Write Key. On Jitsu.Cloud can be omitted if Site has explicitly mapped domain name                                                               |
| `host`         | -                    | Jitsu installation domain, e.g. `your-jitsu-domain.com`. For HTML snippet value is assumed from script URL.                                                                                                                                                                       |
| `debug`        | `data-debug`         | Enables debug log messages in Browser: `true` or `false`. Default `false`                                                                                                                                                                                                         |
| -              | `data-onload`        | Function to call after the script has loaded. Function should be previously defined in `window`                                                                                                                                                                                   |
| -              | `data-init-only`     | By default, the script will send a `page` event. Set this to `true` to just initialize the library. You still will be able to send events manually by setting `data-onload` hook                                                                                                  |

### Advanced Configuration

| Name                    | Script Attribute                                                    | Description                                                                                                                                                                                                                                                                       |
|-------------------------|---------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `idEndpoint`            | `data-id-endpoint`                                                  | Endpoint that makes sure that Jitsu anonymousId cookie is set as server (httpOnly) cookie. Endpoint must be hosted on the same domain as the site where Jitsu code is installed. Required to overcome [Safari ITP restrictions](/docs/sending-data/js-reference/itp).                                             |
| `cookieDomain`          | `data-cookie-domain`                                                | Explicitly specify cookie domain. If not set, cookie domain will be set to top level of the current domain. Example: if JS lives on "app.example.com", cookie domain will be set to ".example.com". If it lives on "example.com", cookie domain will be set to ".example.com" too |
| `fetch`                 | -                                                                   | Custom implementation of Fetch API `fetch` method                                                                                                                                                                                                                                 |
| `fetchTimeoutMs`        | `data-fetch-timeout-ms`                                             | Timeout for fetch requests. Default value: `5000`                                                                                                                                                                                                                                 |
| `echoEvents`            | `data-echo-events`                                                  | If `true`, Jitsu will output events in console. In this case you don't need to set writeKey / host. It's useful for debugging development environment                                                                                                                             |
| `defaultPayloadContext` | `data-default-payload-context`<br/>*expects stringified json value* | Default context object that will be merged with the `context` of every event.                                                                                                                                                                                                     |
| `cookieNames`           | `data-cookie-names`<br/>*expects stringified json value*            | Map of alternative names for standard cookies. Format: `{"anonymousId":"my_eventn_id"}`<br/>Default values:<br/>`anonymousId`: `__eventn_id`<br/>`userId`: `__eventn_uid`<br/>`userTraits`: `__eventn_id_usr`<br/>`groupId`: `__group_id`<br/>`groupTraits`: `__group_traits`     |
| `cookieCapture`         | `data-cookie-capture`<br/>*expects stringified json value*          | Map of cookies to capture in addition to Facebook's `_fbc`,`_fbp` and Google's `_ga` ids. Format: `{"id":"cookie-name"}`. Captured cookies will be added to `context.clientIds` object of event payload.                                                                          |

### Privacy Settings

:::info
Available since Jitsu v2.8.0 and npm packages v1.9.7
:::

Privacy settings are nested under `privacy` object of Jitsu Options:

```javascript
const analytics = jitsuAnalytics({
   host: "https://your-jitsu-domain.com",
   privacy: {
     dontSend: false,
     disableUserIds: true,
     ipPolicy: "stripLastOctet",
     consentCategories: {
       "Analytics": true,
       "Marketing": false
     }
   }
});
```

| Name                | Script Attribute         | Description                                                                                                                                                                                  |
|---------------------|--------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `dontSend`          | `data-privacy-dont-send` | If `true`, disables storing anything in cookies and sending events to Jitsu servers.                                                                                                         |
| `disableUserIds`    | `data-privacy-user-ids`  | If `true`, disables storing in cookies and sending to Jitsu servers any user identifiers (including anonymousId). In this mode `identify` and `group` calls will have no effect.             |
| `ipPolicy`          | `data-privacy-ip-policy` | Controls how Jitsu collects information about user's IP: `keep` - collect full IP, `stripLastOctet` - collect only first 3 octets, e.g: `104.154.19.0` , `remove` - do not collect user's IP |
| `consentCategories` | -                        | Object containing user's consent state by category name. Provided value will be passed to Jitsu in every event in `context.categoryPreferences` object.                                      |



## Methods

### `.page()`

Trigger page view. 

```javascript
//trigger page view with a custom name
jitsu.page("Page Name");

//trigger page view with a custom properties
jitsu.page({  propName: "propVal" });

//trigger page view with a name AND custom properties
jitsu.page("Page Name", {  propName: "propVal" });
```

:::tip
For [HTML Snippet](/docs/sending-data/html) page view is triggered automatically, unless `data-init-only` attribute is set to `true`.
:::

### `.identify()`

Identify a user.

```javascript

// Identify user: `xyz` as a userId, and additional properties (traits)
jitsu.identify('xyz', {
  name: 'Michael Scott',
  company: 'Dunder Mifflin',
})
//or just set a userId
jitsu.identify('xyz')
```

`$doNotSend` is a special property that tells Jitsu to save user and it's properties to local storage, but not to send it to the server. 

This is useful to avoid sending excessive volumes of events to Jitsu

```javascript
jitsu.identify('xyz', {
  name: 'Michael Scott',
  company: 'Dunder Mifflin',
  $doNotSend: true
})
```

:::tip
Use `.identify()` for permanent user identifiers such as a database id of registered user. Use `.setAnonymousId()` for temporary identifiers such as cookies
:::

### `.track()`

Send a custom event.

```javascript

// Event name only
jitsu.track("buttonClick");

// Event with properties
jitsu.track("itemPurchased", { price: 99 });
```

### `.group()`

Assign user to a group, usually a company or organization.

```javascript

// Group ID. Use permanent identifiers such as a database id of company
jitsu.group("g-xyz");

//Group with properties
jitsu.group("g-xyz", { name: "Dunder Mifflin" });
```

### `.setAnonymousId()`

Jitsu automatically detects anonymous id for each visitor. You can override it with `.setAnonymousId()`

```javascript
//Set anonymous id of a user, such as ID of the visitor based on cookie
jitsu.setAnonymousId("xyz");
```

:::warning
Jitsu automatically detects anonymous id for each visitor, you rarely need to use `.setAnonymousId()`.
Use at your own risk.
:::

### `.setContextProperty()`

:::info
Available since Jitsu v2.11.0 and npm packages v1.10.1
:::

Allows to set a `context` property that will be sent with every event.
Effectively, it sets property of `defaultPayloadContext` object that Jitsu merges with a standard `context` for every event.

```javascript
jitsu.setContextProperty("pageViewId", "12345");
```

### `.getContextProperty()`

:::info
Available since Jitsu v2.11.0 and npm packages v1.10.1
:::

Get a property of `defaultPayloadContext` object if available.

```javascript
const pageViewId = jitsu.getContextProperty("pageViewId");  
```

### `.configure()`

:::info
Available since Jitsu v2.8.0 and npm packages v1.9.7
:::

Change Jitsu configuration on the fly.
Only `privacy`, `debug` and `echoEvents` settings can be changed on the fly.

```javascript
//Change Jitsu configuration on the fly
jitsu.configure({
  debug: true,
  privacy: {
    dontSend: true,
    disableUserIds: true,
    ipPolicy: "stripLastOctet"
  }
});
```

### `.getConfiguration()`

:::info
Available since Jitsu v2.11.0 and npm packages v1.10.3
:::

Return current Jitsu configuration.