> ## Documentation Index
> Fetch the complete documentation index at: https://docs.castled.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Getting Started with JavaScript SDK

> [!NOTE]
>
> **Prerequisites**
>
> * [Castled CEP account is already setup](https://docs.castled.io/account-management/user-signup.md)
> * Make sure you have conset from from the user before sending user attributes or events to Castled.

The Castled JS SDK enables you to send user attributes and events from your website to Castled. This data can serve as triggers for campaigns or workflows, or be synchronized with your data warehouse for advanced customer segmentation.
The following steps will guide you through the process of integrating SDK with your web application.

## SDK Initialization

You can use one of the following approaches to add SDK to your website

### Add SDK as a code snippet

To integrate the JavaScript SDK with your website, place the following code snippet in the `<head>` section of your website.

```javascript
<script type="text/javascript">
!function(){var e=window.castledanalytics=window.castledanalytics||[];e.methods=["ready","load","track","identify"],e.factory=function(t){return function(){e.push([t].concat(Array.prototype.slice.call(arguments)))}};for(var t=0;t<e.methods.length;t++){var r=e.methods[t];e[r]=e.factory(r)}e.loadJS=function(e,t){var r=document.createElement("script");r.type="text/javascript",r.async=!0,r.src="https://cdn.castled.io/js/latest/events.min.js";var a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(r,a)},e.loadJS(),
e.load(<APP_ID>)}();
</script>
```

The above snippet lets you integrate the SDK with your website and load it asynchronously to keep your page load time unaffected.

To load the events SDK synchronously, you can refer to the minified or non-minified versions of the code in the following sections:

> [tab] Minified code
>
> ```html
> <script>
> castledanalytics=window.castledanalytics=[];for(var methods=["ready","load","track","identify"],i=0;i<methods.length;i++){var method=methods[i];castledanalytics[method]=function(a){return function(){castledanalytics.push([a].concat(Array.prototype.slice.call(arguments)))}}(method)}castledanalytics.load(<APP_ID>);
> </script>
> <script src="https://cdn.castled.io/js/latest/events.min.js"></script>
> ```

> [tab] Non-minified code
>
> ```html
>    <script>
>    castledanalytics = window.castledanalytics = [];
>
>    var methods = [
>    'ready',
>    'load',
>    'track',
>    'identify'
>    ];
>
>    for (var i = 0; i < methods.length; i++) {
>    var method = methods[i];
>    castledanalytics[method] = (function (methodName) {
>    return function () {
>    castledanalytics.push([methodName].concat(Array.prototype.slice.call(arguments)));
>    };
>    })(method);
>    }
>    castledanalytics.load(APP_ID);
>    </script>
>    <script src="https://cdn.castled.io/js/latest/events.min.js"></script>
> ```

\-- `APP-ID` is a unique id associated with your Castled workspace. It can be found in the Castled dashboard at **Settings > Api Keys**.

### Install using npm package manager

you can also use this [**NPM module**](https://www.npmjs.com/package/castled-js-sdk) to package the library directly into your project.

To install the SDK via npm, run the following command:

```bash
npm install castled-js-sdk --save
```

**Note that this NPM module is only meant to be used for a browser installation**.

> [!NOTE]
>
> Since the module exports the [**related APIs**](#exported-apis) on an
> already-defined object combined with the Node.js module caching, you should
> run the following code snippet only once and use the exported object
> throughout your project:

```javascript
   import * as castledanalytics from "castled-js-sdk";
   castledanalytics.ready(() => {
   console.log("Analytics module is ready!");
   });
   castledanalytics.load(<APP_ID>);
   export { castledanalytics };
```

You can also do this with **ES5** using the `require` method, as shown:

```javascript
   var castledanalytics = require("castled-js-sdk");
   castledanalytics.load(<APP_ID>);
   exports.castledanalytics = castledanalytics;
```

### Exported APIs

The APIs exported by the module are:

* `load`
* `ready`
* `identify`
* `track`

### Supported browser versions

| **Browser** | **Supported Versions** |
| :- | :- |
| Safari | v7 or later |
| IE | v10 or later |
| Edge | v15 or later |
| Mozilla Firefox | v40 or later |
| Chrome | v37 or later |
| Opera | v23 or later |
| Yandex | v14.12 or later |

> If the SDK does not work on the browser versions that you are targeting, verify if adding the browser polyfills to your application solves the issue.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.