JavaScript Events
How to leverage JavaScript to read/write data to Spara.
Reading Javascript events from Spara
Spara's JavaScript embed snippet exposes two events to the DOM:
Spara.onSparaLoad()
fires when Spara's embed script finishes executingSpara.onUserMessageSent()
fires when a lead sends message to Spara
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: () => {
console.log("User sent a message to Spara 💬");
},
}
</script>
Writing information to Spara using Javascript
Spara's Javascript embed enables you to send Spara information at the time of initialization. This may only be done once and only on page load.
To send Spara information on page load, you will need to add an additional Javascript snippet immediately after initializing Spara's Javascript embed.
Any information may be added to the Lead object, which represents the current visitor on your website. All fields must be added to the fields
dictionary.
Here is a full example, which sets the current visitor's "ACME uuid" field:
<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>
For more information on how leads are represented in our database see our documentation on Lead Schema.
Last updated