Prerequisites

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.

<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:

  • Minified code

  • Non-minified code

<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>

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 to package the library directly into your project.

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

npm install castled-js-sdk --save

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

Since the module exports the related 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:

   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:

   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

BrowserSupported Versions
Safariv7 or later
IEv10 or later
Edgev15 or later
Mozilla Firefoxv40 or later
Chromev37 or later
Operav23 or later
Yandexv14.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.