# Javascript API

## Reading Javascript events from Spara

Spara's JavaScript API exposes three events to the DOM:

* `Spara.onSparaLoad()` fires when Spara's embed script finishes executing
* `Spara.onUserMessageSent()` fires when a lead sends message to Spara
* `Spara.onUserScheduled()` fires when a lead schedules a call

These can be used however you like. For example, by pushing these events to your window's data layer for ingestion by Google Analytics.

To access these Javascript events, you will need to add an additional script to your website's `<head>`:

```html
<script type="text/javascript" src="https://app.spara.co/embed-<app_id>.js"></script>
<script>
  Spara = {
    onSparaLoad: () => {
        console.log("Spara is loaded! 🎬");
    },
    onUserMessageSent: (text) => { 
        console.log("User sent a message to Spara 💬");
    },
    onUserScheduled: () => {
        console.log("User has scheduled a call");
    }
  }
</script>
```

## Writing information to Spara using Javascript

Spara's Javascript API exposes methods to write information to Spara Lead objects. For more information on how leads are represented in our database see our documentation on Lead [Webhooks](/developers/spara-api/webhooks.md#schema).

`SparaActions.setFields` accepts a dictionary of arbitrary key/value pairs assigned to the current website visitor. For example:

```javascript
SparaActions.setFields({
  acme_uuid: '1234567',  // This is an arbitrary field and value
  experiment_group: 'control'
}
```

To specifically send Spara information on page load, you will need to add an additional Javascript snippet immediately after initializing Spara's Javascript embed.

Here is a full example, which sets the current visitor's "ACME uuid" field:

```html
    <script type="text/javascript" src="https://app.spara.co/embed-<app_id>.js"></script>
    <script>
      Spara = {
        fields: {
          acme_uuid: '1234567'  // This is an arbitrary field and value
        }
      }
    </script>
```

#### Note on Query Parameters

A reminder that Spara automatically ingests all query parameters from your webpages, as long as they are mapped in the Spara platform. See [Query Parameters](/developers/spara-api/query-parameters.md) for more information.

## Opening Spara using Javascript

Spara provides JS functions to dynamically open Spara Navigator, Smartbar, and Product Demo.

* `SparaActions.openChat()` - opens Navigator
* `SparaActions.openSmartbarChat()` - opens Smartbar
* `SparaActions.openProductDemo()` - opens Product Demo

`openChat` and `openSmartbarChat` accept the same optional arguments:

* `userMessage` - prefills and sends the lead's (i.e. user's) message
* `aiMessage` - starts the chat with a Spara-generated (i.e. AI) message

For example, `SparaActions.openChat({ userMessage: 'Hi' })` opens Spara Navigator with the user sending a message "Hi." See the [FAQ](#faq) for a full CTA-click walkthrough.

#### How to open Product Demo from a link or email CTA

Use `SparaActions.openProductDemo()` to launch a Product Demo session programmatically. A common use case is linking to your website from an email campaign and having the Product Demo open automatically when the visitor arrives.

To set this up:

1. Add a query parameter to your link (e.g., `?demo` or `?open_product_demo=1`)
2. On your website, include JavaScript that checks for this parameter and calls `openProductDemo()`

```javascript
// Check for ?demo in the URL and open Product Demo if present
if (window.location.search.includes('demo')) {
  SparaActions.openProductDemo();
}
```

For email campaigns, append the query parameter to your landing page URL. For example, linking to `https://yoursite.com/?demo` will open the Product Demo when the visitor arrives.

{% hint style="info" %}
UTM parameters like `utm_source` and `utm_campaign` are automatically captured by Spara when the page loads, so you can combine the demo trigger with your usual campaign tracking (e.g., `https://yoursite.com/?demo&utm_source=newsletter`).
{% endhint %}

### How do I set up persistent ad tracking in Google Tag Manager?

To better attribute Spara conversions to your advertising campaigns (LinkedIn, Google, Facebook), you must capture and store "Click IDs" from the URL when a visitor first arrives.

A Click ID is a unique identifier that tracks a user’s click from an ad to your website.

Because users often browse multiple pages before engaging with the Spara AI agent, we cannot rely on the URL remaining the same. This guide shows you how to use **Google Tag Manager (GTM)** to save these IDs into a First-Party Cookie that follows the user for 90 days.

#### Supported Identifiers

This setup automatically handles the following industry-standard tracking parameters:

* gclid (Google Click ID)
* wbraid / gbraid (Google iOS Privacy-compliant IDs)
* li\_fat\_id (LinkedIn First-Party Tracking)
* fbclid (Facebook Click ID)

***

#### Step 1: The "Universal" Tracking Script

We will create a single tag that handles all major ad networks. This script checks the URL for any of the supported IDs and saves them as a secure cookie on your root domain.

1. Open Google Tag Manager.
2. Go to Tags > New.
3. Tag Type: Select Custom HTML.
4. Name: Script - Universal Ad Tracking.
5. HTML Content: Copy and paste the code block below.
6. Action Required: Edit the config.domain line to match your website's root domain (e.g., .spara.co).

```html
<script>
/**
 * SPARA.CO UNIVERSAL TRACKING SCRIPT
 * Captures ad parameters from URL and stores them in 90-day cookies.
 */
(function() {
    // --- CONFIGURATION ---
    var config = {
        domain: '.yourdomain.com', // REPLACE with your root domain (with leading dot)
        expiryDays: 90,            // Attribution window
        path: '/'
    };


    // Mapping: URL Parameter -> Cookie Name
    var idMap = {
        'gclid':     'gclid',
        'wbraid':    'wbraid',
        'gbraid':    'gbraid',
        'li_fat_id': 'li_fat_id',
        'fbclid':    'fbclid'
    };
    // ---------------------


    // Helper: Get Query Param
    function getParam(name) {
        var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
        return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
    }


    // Helper: Set Cookie
    function setCookie(name, value) {
        var date = new Date();
        date.setTime(date.getTime() + (config.expiryDays * 24 * 60 * 60 * 1000));
        var expires = "expires=" + date.toUTCString();
        document.cookie = name + "=" + value + ";" + expires + ";domain=" + config.domain + ";path=" + config.path;
    }


    // Execution Logic
    // We loop through the map. If a param exists in the URL, we save/overwrite the cookie.
    // If it does NOT exist, we do nothing (preserving any existing cookie).
    for (var paramKey in idMap) {
        var paramValue = getParam(paramKey);
        if (paramValue) {
            setCookie(idMap[paramKey], paramValue);
        }
    }
})();
</script>
```

6. Triggering:
   1. Click Triggering.
   2. Select All Pages (or Initialization - All Pages).
   3. Why? This ensures that if a user lands on any page via an ad, the ID is captured immediately.
7. Save the tag.

***

#### Step 2: Create "Reader" Variables

Now that the cookies are being created, you need GTM variables to "read" them so you can pass the data to Spara or other tools.

1. Go to Variables.
2. Under User-Defined Variables, click New.
3. Variable Type: 1st Party Cookie.
4. Cookie Name: li\_fat\_id (Must match the name in the script map above).
5. URI Decode Cookie: ☑️Check this box.
6. Name: Cookie - li\_fat\_id.
7. Save.

Repeat this process for gclid, wbraid, and any others you need.

| Variable Name        | Cookie Name |
| -------------------- | ----------- |
| Cookie - gclid       | gclid       |
| Cookie - wbraid      | wbraid      |
| Cookie - li\_fat\_id | li\_fat\_id |

***

#### Step 3: Verification (QA)

Before publishing, verify the setup works using the GTM Preview mode or your browser's developer tools.

1. Visit your site with a fake ID:
2. <https://www.yourdomain.com/?li\\_fat\\_id=TEST\\_123\\&gclid=TEST\\_456>
3. Inspect the Cookies:
4. Right-click the page > Inspect.
5. Go to the Application tab (Chrome) or Storage tab (Firefox).
6. Expand Cookies on the left and select your domain.
7. Confirm the Data:
8. You should see li\_fat\_id with value TEST\_123.
9. You should see gclid with value TEST\_456.
10. Domain: Ensure it shows .yourdomain.com (this confirms it will work across subdomains).
11. Expires: Ensure the date is roughly 90 days in the future.

***

#### Step 4: Passing IDs to Spara

Once the variables are created, you can pass them into Spara via Spara's Javascript API.

If you are initializing Spara via GTM, update your Spara Initialization Tag to include these variables:

```javascript
// Example Spara Identification Call
Spara.identify({
  email: 'user@example.com',
  // Pass the GTM Variables we created in Step 2
  gclid: '{{Cookie - gclid}}', 
  li_fat_id: '{{Cookie - li_fat_id}}',
  wbraid: '{{Cookie - wbraid}}'
});
```

## FAQ

### How do I make a CTA button open Spara chat with a specific opening message?

A common pattern is to have a CTA on your page — for example, a "Request a Demo" button — that opens Spara Navigator with a page-specific opening message rather than the generic default. Use `SparaActions.openChat()` together with the `aiMessage` option to inject the opening message at click time.

For example, on a pricing page:

```javascript
// Example assumes CTA button has id="request-demo"
document.getElementById('request-demo')?.addEventListener('click', () => {
  SparaActions.openChat({
    aiMessage: "Happy to walk you through pricing — what plan are you considering?"
  });
});
```

This lets each page provide contextual messaging — a pricing page can lead with a pricing question, a features page can lead with a product question — instead of every CTA opening to the same generic greeting.

To make this work, Navigator should be loaded in [Spara Navigator](/agents/chat-agents/spara-navigator.md#preview-mode-hidden) so the chat only appears when the CTA is clicked.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.spara.com/developers/spara-api/javascript-api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
