JavaScript API

How to leverage JavaScript to read/write data to Spara.

Reading Javascript events from Spara

Spara's JavaScript API exposes two 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>:

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

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

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:

Note on Query Parameters

A reminder that Spara automatically ingests all query parameters from your webpages. So another way to See Query Parametersfor more information

Opening Spara Navigator using Javascript

In addition, Spara provides a JS function to dynamically open Spara Navigator: SparaAction.openChat It has 2 options: aiMessage & userMessage.

For example, SparaActions.openChat({ userMessage: 'Hi' }) opens Spara Navigator with the user sending a message "Hi."

In another example, SparaActions.openChat({ aiMessage: 'How can I help?' }) opens Spara Navigator with Spara's AI saying "How can I help" to start the thread. Note that aiMessage will only create a new message for an empty thread.

How to open Spara Navigator when a user clicks a CTA

A common interaction is for Spara Navigator to load in a "closed" state, but "magically" open when a website visitor clicks a CTA.

To accomplish this:

  • Set Navigator to load in Spara Navigator. Website visitors will only see the bottom right avatar on page load.

  • Write a brief Javascript snippet that opens Spara Navigator when a CTA is clicked.

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

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

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

Last updated