# Initialization

The initialization process sets up the core Interactions SDK, configuring its identity management, consent status, and loading the sitemap. This process must complete successfully before the SDK can begin collecting data.

The recommended flow follows a general pattern: first, initialize the SDK with necessary configurations (using `init()`), and then, load the sitemap (using `initSitemap()`).

:::note
**Critical**: Please ensure that you have obtained the appropriate consumer consent required before initializing the Salesforce Interactions Web SDK (Data 360). If you have questions regarding what consent is required, please contact your legal representatives. Salesforce can only process consumer consent choices that have been entered by the Customer.
:::

```javascript
SalesforceInteractions.init({
  consents: [...],
  cookieDomain: '...'
}).then(() => {
  SalesforceInteractions.initSitemap({
    global: { ... },
    pageTypeDefault: { ... },
    pageTypes: [...]
  })
})
```

## Initialization Methods

:::note
When you access the SDK outside of the sitemap, use `window.getSalesforceInteractions()` instead of referencing `SalesforceInteractions` directly. This method resolves the correct SDK instance dynamically, even when an advanced SDK feature customizes the global `SalesforceInteractions` namespace.
:::

### init()

The `init()` method initializes the Interactions SDK, accepting a configuration object (`sdkConfig`) and returning a `Promise` that resolves when the SDK is successfully initialized and ready.

```javascript
init(sdkConfig: SdkConfig): Promise<void>
```

This table shows the fields that the required `sdkConfig` object accepts for initializing the SDK.

| Field Name     | Field Type                          | Description                                                                                                                                                         |
| -------------- | ----------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `consents`     | `Consent[]` or `Promise<Consent[]>` | **Required**. A list of consent data values or a `Promise` that resolves a list of consent data values. The SDK waits for the `Promise` to resolve before tracking. |
| `cookieDomain` | string                              | The domain used for the first-party cookies that store identity data. Defaults to the current site's domain.                                                        |

You can initialize the SDK based on how and when you receive consent.

- Asynchronously with a Promise

  - : Use a `Promise` when waiting for a user action (like clicking an opt-in button) or for a third-party Consent Management Platform (CMP) to load.

    ```javascript
    // Initialize with consent provided by a user interaction
    SalesforceInteractions.init({
      cookieDomain: "domain.com",
      consents: new Promise((resolve) => {
        // user clicks button that grants consent
        document.getElementById("opt-in").addEventListener(
          "click",
          () => {
            resolve([
              {
                provider: "Test Provider",
                purpose: "Tracking",
                status: SalesforceInteractions.ConsentStatus.OptIn,
              },
            ]);
          },
          { once: true },
        );

        // User clicks button that revokes consent
        document.getElementById("opt-out").addEventListener(
          "click",
          () => {
            resolve([
              {
                provider: "Test Provider",
                purpose: "Tracking",
                status: SalesforceInteractions.ConsentStatus.OptOut,
              },
            ]);
          },
          { once: true },
        );
      }),
    });
    ```

- Synchronously with Pre-Configured Consent

  - : If consent is available immediately (for example, when consent is loaded from a session cookie), you can pass the array directly.

    ```javascript
    // Initialize with immediate, pre-configured Opt In status
    SalesforceInteractions.init({
      cookieDomain: "domain.com",
      consents: [
        {
          provider: "Test Provider",
          purpose: "Tracking",
          status: SalesforceInteractions.ConsentStatus.OptIn,
        },
      ],
    });
    ```

- Initializing with No Consent

  - : If the user has not made a decision, pass an empty array. The SDK will not track until consent is updated later via `updateConsents()`.

    ```javascript
    // Initialize with tracking disabled until consent is provided later
    SalesforceInteractions.init({
      cookieDomain: "domain.com",
      consents: [],
    });
    ```

### reinit()

Use the `reinit()` method forces the SDK to reinitialize its state and re-run sitemap evaluation. This is useful for Single Page Applications (SPAs) where the content and URL change without a full page reload. Calling `reinit()` ensures the sitemap is re-evaluated to match the new virtual page.

```nolang
reinit(): void
```

Here's an example of using the `reinit()` method to reinitialize the SDK.

```javascript
SalesforceInteractions.init({...}).then(() => {
  let currentUrl = window.location.href

  // Monitor the URL for changes (e.g., using a framework hook or interval)
   setInterval(() => {
    if (currentUrl !== window.location.href) {
      currentUrl = window.location.href
       // Reinitialize the SDK when the virtual page URL changes
       SalesforceInteractions.reinit()
     }
   }, 200)

  SalesforceInteractions.initSitemap({...})
})
```
