Reports
Working with reports in code is done by using a Report
object. A Report
object can be used in a few days:
- Supplied directly to a Chart for visualization:<Chart data={data.reportObject} />
- Report data retrieved via the JS API:const reportData = await ReportData.get(reportObject);
- As data source for report plugins:const forecast = await PluginData.get("plugins/forecast", {data: reportObject,params: ...});
note
Reports cannot be retrieved in parallel at the moment, but this will be fixed in the near future.
If you have multiple <Chart data={"abcd-1234"}>
or <Chart data={reportObject}>
or use Promise.all
, you have to get the data 1-by-1 sequentially instead using the following workaround:
Report bookmarks
The easiest way to work with Reports that involves as little code as possible is to make one from the UI first. Use the Custom Report
option from the Custom
top menu. Once the report results are in, bookmark the report.
Because report bookmarks also work with Template Variables, end-users can change the questions and those changes will be reflected when you retrieve or use the bookmarked report.
To get the bookmark ID for your report, edit your Insight and use the Example Code
dropdown to select the bookmarked report, and then copy the bookmark ID string.
Once you have the bookmark ID you can use it anywhere instead of a Report object:
- Supplied directly to a Chart for visualization:<Chart data="abcd-12345" />
- Get the Report object:const reportObject = await Report.getBookmark("abcd-12345");const reportData = await ReportData.get(reportObject);
- Report data retrieved via the JS API:const reportData = await ReportData.get("abcd-12345");
- As data source for report plugins:const forecast = await PluginData.get("plugins/forecast", {data: "abcd-12345",params: ...});
Report objects
Another way to work with reports is to use Report objects directly. This has the advantage that you can dynamically build your report based on user inputs or report/plugin results:
The reportObject
can then be used as shown in the examples at the top of this page.
For recipes and details working with the report object, such as specific reports, date ranges, custom SQL and more the API-Library for the Report class.
tip
An easy way to construct a Report object is to run a non-standalone Insight on top of any report you've made from the UI. Then use the Debug component show the raw report object like this, which can then be fully copy-pasted as-is:
This is less clean than building up the report object using the API, but is much faster in development.
Report errors
A report error can be checked and handled in a few different ways.
The first option is to let the <Chart>
or <Table>
report visualization components handle any errors.
This is the easiest option in case you don't want the whole Insight to fail, but if it's ok if only this specific report doesn't display:
All other options below make the complete Insight fail. This is best if any code that runs after the report, is dependent on the report results.
Because a failed ReportData.get
returns an error object, we can return that directly. As explained in the adding code page, an error object can be returned directly, so that it gets picked up and the error is shown to the end-user.
To check if the report has an error we first need to use Utils.validResult(reportData)
:
Alternatively we can return a string directly, which is displayed as error to the end-user:
Or we can return an error object with more details:
Similarly an error can happen in other ways as well:
Using questions/variables
As described in the Questions & Variables page, the questions
object contains end-user assignments for things such as choosing an conversion event, user/event property or any simple property such as a date or number.
Questions can be used with the report object in a few ways. Below are a few examples:
Event variables
End-user answers what three events to use to make a funnel report. Because events
of the Report
object is just an array event names, we can assign them as follows:
User/event property variables
End-user picks a user property, such as campaign source, to group a report on:
Similarly event property variables can be used:
User/event item object
A helper method under user/event property variables, questions.event_price_property.item()
, returns an item object which can be used directly when constructing manual report items, such as a calculation:
User property variables can be used in a similar way.
For more details and recipes see the API-Library on the Report.