Javascript API

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

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

<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, as long as they are mapped in the Spara platform. See Query Parameters 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 for a full CTA-click walkthrough.

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

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.

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

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:

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:

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 Preview Mode: Hidden so the chat only appears when the CTA is clicked.

Last updated