JavaScript Events

How to leverage JavaScript events exposed by Spara.

Spara's JavaScript embed snippet exposes three events to your DOM:

  • Spara.onSparaLoad() fires when Spara's embed script finishes executing

  • Spara.onUserMessageSent() fires when a lead sends message to Spara

  • Spara.onUserCTAClick() fires when a lead clicks a CTA in the Spara chat window. This event includes the label and URL of the CTA as metadata.

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! 🎬");
    },
    onUserCtaClick: (c) => { 
        console.log("User clicked the Spara CTA 🚀");
        console.log({c.label, c.url})
    },
    onUserMessageSent: () => { 
        console.log("User sent a message to Spara 💬");
    },
  }
</script>

Last updated