Skip to content

JavaScript Snippet

The JavaScript snippet is a convenient way to track client-side events on a web site or web application. For ease of integration, it collects user, session, and device information automatically from the browser.

1. Add JS snippet

Copy-paste this snippet in the <head> section of your site or app:

<script type="text/javascript">
  !function(){var sumatra=window.sumatra=window.sumatra||[];sumatra.methods=["load","track","enrich","ready","personalize","registerAnalyticsSource","addEnrichmentCallback"],sumatra.factory=function(t){return function(){sumatra.push([t].concat(Array.prototype.slice.call(arguments)))}};for(var t=0;t<sumatra.methods.length;t++){var r=sumatra.methods[t];sumatra[r]=sumatra.factory(r)}sumatra.loadJS=function(e,t){var r=document.createElement("script");r.type="text/javascript",r.async=!0,r.src="https://cdn.sumatra.ai/v1/sumatra-sdk.min.js";var a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(r,a)},sumatra.loadJS(),
    sumatra.load("SUMATRA_API_KEY", "https://api.sumatra.ai")
    sumatra.track('page');
  }();
</script>

Important

Replace SUMATRA_API_KEY with your private API key.

2. Start tracking events

The JS snippet will not send events to Sumatra on its own. Instead, you send events and their attributes by adding event-tracking code like the example below:

window.sumatra.track('add_to_cart', {
    my_first_attribute: 'foo@bar.com',
    another_attribute: 9.99
});

The function may be placed anywhere you'd like, but is typically called from a callback or event handler in your application.

3. Add callbacks

When you are ready to use Sumatra to fetch features or take an action in your application, add an asynchronous callback to take action on the response.

The following example logs the output to the console.

window.sumatra.track('purchase', {
    my_first_attribute: 'foo@bar.com',
    another_attribute: 9.99
}, (response) => {
    console.log(response)
})

The response object will contain all of the features computed by your Scowl code. See the API response docs for the full details.