Adding Code
JS and JSX code
Code for Insights are specified in both a JS and JSX section. Code can be entered directly when you create or edit an Insight on Stormly.
When an Insight runs it first evaluates the js
code and will be mostly used for logic, calculations and data retrieval. After that the jsx
is evaluated and used to display the visualizations and results, mostly using ready visualizations and helpers the Stormly API provides.
A return statement at the end of the js
code can return any object, which will be available in the jsx
code under the global variable data
.
Note that there cannot be any commented code after the return statement.
Most code generally looks something like this:
It will output the following on the Insight results page:
note
The spread operator is currently not supported. A workaround is to use Object.assign, so instead of:
Use this:
Global variables
All global variables listed below are available in both the js
and jsx
code.
data
— Contains whatever is returned from thejs
code. This variable is an exception in that it is only available in thejsx
code.questions
— an object with all answers to Template and Insight questions as answered by end-users. For more details see Adding Variables.standalone
— if this Insight is currently being run on top of a report or plugin this will befalse
. Useful if the same Insight can be both stand-alone and non-standalone.now
— a JSDate
object with the date set to the time when the Insight was first run. Because Insights have an archive history this can be a date from the past, or the current date if a new run. Useful where you need to access to the current date, such as current-time sensitive API helper functions that check if there was enough data for the last X days.development
— When an Insight is created with the check[✅] Run in development mode
, this variable will betrue
. Useful for example to show debugging information when in development mode, or re-use cached results without having to wait.
For non-standalone Insights the following global variables are available:
results
— the current ReportData object if this Insight is run on top of a report, or the PluginData when run on top of plugin results. Useful for example if you want to make a different visualization of a specific report or plugin.report
— the current Report object if this Insight is run on top of a report. Useful for example if you want to make a forecast of any report with a timeseries, or create an Insight that automatically segments any Report based on the clusters of users found.
Displaying errors
When a string or error object is returned in the JS code, it will be picked up and displayed as an error to the end-user.
Or we can return an error object with more details. All fields are optional except for "code": "error"
:
This will be displayed to the end-user of the Insight as:
As explained later in the chapter on working with data and plugins, the ReportData.get
and PluginData.get
return a detailed error object in case they have an error. The error object can be returned as-is:
Checking data availability
Often you want to check before you start running your Insight if there is enough data at all, such as: is there data for the last 30 days, with a minimum of 20 users per day?
The Utils.amountOfDataFor
helper is what you need. Note that this helper's first argument is the now
object — this makes sure that when this Insight is being opened from the Archive it checks using the historical date range.
The example below checks if there have been at least 20 users per day, for the last 30 days:
The dataCheck
object also contains information on how when the first and last data was seen in that period, how many days ago was the first data seen and more.
In addition, Utils.hasEnoughDataFor
can be used with the same arguments and returns a boolean directly.
For more details and arguments, such as supplying different report objects and working with custom date ranges, see amountOfDataFor in the api-library.
Helpers and components
More helper and visualization components can be found in the Helpers and Components api-library documentation.