# Web SDK Integration

{% hint style="success" %}
The latest version of the Web SDK: <mark style="color:red;">**3.0**</mark>

Please see the [**changelog**](https://docs.devtodev.com/integration/integration-of-sdk-v2/sdk-integration/web/web-sdk-releases) if you are using an outdated version.
{% endhint %}

## Integration

### Installation via NPM package

```bash
npm install @dev-2-dev/websdk
```

### CDN installation

To integrate SDK, add the following line to the tag of your page:

```html
<script src="https://cdn.devtodev.com/sdk/web/v3/devtodevsdk.js"></script>
```

The SDK will be available globally as `window.devtodev`.

## Initialization

[Add the application](https://docs.devtodev.com/getting-started/adding-an-app-to-the-space) to the Space using the wizard for adding applications.

In order for SDK for WEB to start working, it is necessary to perform initialization right after the page is loaded and you have a basic user identifier at your disposal.

#### Using NPM package&#x20;

```javascript
import DTDAnalytics from '@dev-2-dev/websdk';

// Create an instance
const analytics = new DTDAnalytics();

// Initialize with your app ID
analytics.initialize('App ID', config);
```

#### Using CDN&#x20;

```html
<!DOCTYPE html>
<html>
  <head>
    <script src="https://cdn.devtodev.com/sdk/web/v3/devtodevsdk.js"></script>
  </head>
  <body>
    <script>
      const analytics = window.devtodev;

      // Initialize with your app ID
      analytics.initialize('App ID', config);
    </script>
  </body>
</html>
```

You can find the `App ID` in the settings of the respective app in devtodev (Settings → SDK → Integration → [Credentials](https://docs.devtodev.com/reports-and-functionality/project-related-reports-and-fuctionality/settings#integration)).&#x20;

For [Cross-platform type projects](https://docs.devtodev.com/getting-started/cross-platform-application) use `App ID + Platform ID` .

**`config`** – is an object that is used for specifying additional properties during initialization.

{% hint style="info" %}
Since there’s no option to get any consistent identifier in web browsers, we recommend using as a User ID either a social network ID with your app or an ID that your server assigns to a user. It’s best to assign a User ID and specify it in the **`config`** object during the SDK initialization instead of using a [**`setUserId`**](https://docs.devtodev.com/integration/setting-up-events/user-profile#user-id) method after the initialization.

If you have a game app, we recommend specifying the current player’s level either in the **`config`** or at the earliest possible moment after the initialization via the [**`setCurrentLevel`**](https://docs.devtodev.com/integration/setting-up-events/user-profile#current-user-level) method.
{% endhint %}

### **`Config`**

<table><thead><tr><th width="273.45508982035926">Parameter</th><th width="150">Type</th><th>Description</th></tr></thead><tbody><tr><td><strong><code>userId</code></strong></td><td>string</td><td><p>Unique user identifier. For example, user’s ID in a social network, or a unique account name used for user identification on your server. If at the time of initialization this identifier is not yet available, specify the identifier later using </p><p> the <strong><code>setUserId</code></strong> method.</p></td></tr><tr><td><strong><code>currentLevel</code></strong></td><td>integer</td><td>The player level at the moment of devtodev SDK initialization. Must be greater than 0. It’s optional but we recommend using it for improving data accuracy.</td></tr><tr><td><strong><code>trackingAvailability</code></strong></td><td>boolean</td><td>The property allows or disallows devtodev tracking of the user. By default, it is set to <em><strong><code>true</code></strong></em>. SDK stores the previously assigned value. Pass false if the user opted out of tracking in line with GDPR.</td></tr><tr><td><strong><code>logLevel</code></strong></td><td>string</td><td>The level of logging the SDK activity. The "<em><strong><code>No</code></strong></em>" value is used by default. For troubleshooting during integration, it is recommended to set it to "<em><strong><code>Debug</code></strong></em>", and either switch it "<em><strong><code>No</code></strong></em>" or use it only for error handling "<em><strong><code>Error</code></strong></em>" in the release version.</td></tr><tr><td><strong><code>applicationVersion</code></strong></td><td>String</td><td>The app version. Cannot be empty.</td></tr></tbody></table>

Example:

```javascript
const config = {};
config.userId = "Unique user identifier";
config.currentLevel = 2;
config.trackingAvailability = true;
config.logLevel = "Error";
config.applicationVersion = "1";
analytics.initialize("App ID", config);
```

## Session recording&#x20;

A powerful session recording plugin that automatically captures user interactions on your website using advanced screen recording technology. This plugin integrates seamlessly with the DevToDev WebSDK to provide comprehensive session replay capabilities.&#x20;

{% content-ref url="../../../../reports-and-functionality/project-related-reports-and-fuctionality/users/session-replays" %}
[session-replays](https://docs.devtodev.com/reports-and-functionality/project-related-reports-and-fuctionality/users/session-replays)
{% endcontent-ref %}

### Installation&#x20;

#### NPM

```bash
npm install @dev-2-dev/websdk-plugin-session-tracker
```

#### Yarn&#x20;

```bash
yarn add @dev-2-dev/websdk-plugin-session-tracker
```

### Enabling the plugin&#x20;

#### NPM

```javascript
import DTDAnalytics from '@dev-2-dev/websdk';
import DTDWebSessionTracker from '@dev-2-dev/websdk-plugin-session-tracker';

const sessionTracker = new DTDWebSessionTracker();
const analytics = new DTDAnalytics();

analytics.initialize('App ID');
analytics.registerPlugin(sessionTracker);
```

#### CDN&#x20;

```html
<script src="https://cdn.devtodev.com/sdk/web/v3/devtodevsdk.js"></script>
<script src="https://cdn.devtodev.com/sdk/web/v3/plugins/session-tracker/websdk-plugin-session-tracker.js"></script>

<script>
  const analytics = window.devtodev;
  const sessionTracker = new DTDWebSessionTracker();

  // Initialize with your app ID
  analytics.initialize('App ID', config);
  analytics.registerPlugin(sessionTracker);
</script>
```

#### Disabling from SDK Config

You can disable session recording through the SDK's `webSessions` configuration:

```javascript
import { DTDAnalytics } from '@dev-2-dev/websdk';

const analytics = new DTDAnalytics({
  appId: 'App ID',
  webSessions: {
    enabled: false, // Disable session recording
  },
});
```

### Session recording configuration&#x20;

#### Recording Options (Constructor Config)

Configure privacy and recording behavior when creating the tracker instance:

```javascript
import { DTDWebSessionTracker } from '@dev-2-dev/websdk-plugin-session-tracker';

const sessionRecordingConfig = {
  recordCanvas: true, // Record canvas elements
  maskAllInputs: false, // Mask all input fields
  maskInputOptions: {
    password: true, // Always mask password fields (default: true)
    email: true, // Mask email fields (default: true)
    text: false, // Don't mask text inputs (default: false)
  },
  blockClass: 'sensitive-field', // Block elements with this class
};

const sessionTracker = new DTDWebSessionTracker(sessionRecordingConfig);
```

#### Configuration Options

These options are passed to the `DTDWebSessionTracker` constructor:

| Option             | Type    | Default             | Description                                     |
| ------------------ | ------- | ------------------- | ----------------------------------------------- |
| `recordCanvas`     | boolean | `true`              | Record canvas elements                          |
| `maskAllInputs`    | boolean | `false`             | Mask all input fields                           |
| `maskInputOptions` | object  | See below           | Selective input masking options                 |
| `blockClass`       | string  | `'sensitive-field'` | CSS class name to block elements from recording |

**Mask Input Options:**

| Option     | Type    | Default | Description                 |
| ---------- | ------- | ------- | --------------------------- |
| `password` | boolean | `true`  | Always mask password fields |
| `email`    | boolean | `true`  | Mask email fields           |
| `text`     | boolean | `false` | Mask text inputs            |

### Privacy configuration examples&#x20;

1. Mask all inputs:

```javascript
const sessionTracker = new DTDWebSessionTracker({
  maskAllInputs: true,
});
```

2. Selective masking:&#x20;

```javascript
const sessionTracker = new DTDWebSessionTracker({
  maskAllInputs: false,
  maskInputOptions: {
    password: true,  // Mask passwords
    email: true,     // Mask emails
    text: false,     // Don't mask text inputs
  },
});
```

3. Block specific elements. Add the `'sensitive-field'` class to exclude elements from recording:&#x20;

```html
<div class="sensitive-field">
  <p>This content will not be recorded</p>
</div>

<input type="password" class="sensitive-field" />
<input type="email" class="sensitive-field" />
```
