Insights Canvas: Run Calculations on Your Survey Data

The Calculation data element allows you to reference survey data, run calculations on the data, and output the results to your Insights Canvas (all using SurveyGizmo's Custom Scripting library).

Learn about adding other Data elements here.


1. Click anywhere on your canvas or click the green Insert button to access the add-element menu. Select Calculation from the available options.

2. On the Settings tab, provide a Title for the Calculation element and input your PHP script into the Calculation Workspace field. 

The following function can be used to pull results from a specific survey and question:

sgapiGetResults(%%sid, %%qid);

%%sid refers to the Survey ID of the survey containing the question data that you want to reference.

%%qid refers to the Question ID of the question that contains the data that you want to reference.

The above function allows you to fetch specific data which you can then manipulate further using PHP scripting.

3. Use the Layout & Options tab to customize several visual aspects of the Calculation element. The following settings are available:

Display Options:

  • Width & Height - Set the width and height of your question chart (in pixels).
  • Rotation - Use this field to rotate the shape element clockwise. Enter a value between 0-360.
  • Order (z-index) - This feature is particularly important if you want to place your shape behind another element i.e. Text. In order to ensure that the shape is behind the text, you must input a higher z-index value for the text element than the z-index value for the shape. For example, a text element with a z-index of '2' would show in-front of a shape with a z-index of '1'.
  • Hide Element - Check the Hide this element checkbox to remove this specific question chart from view on your canvas.

Class Name

Use the class name provided to reference the calculation element in custom CSS via Canvas Style > Custom Code > Custom CSS. For more details regarding Insights Canvas styling options, visit the tutorial.

Tap Event

Learn about triggering Tap Events from your Calculation Element.

Example 1

You might be wondering when you would use the Calculation element. 

In the below example script, we pull the results to NPS questions from two separate surveys. We then add up the Promoters, Passives, and Detractors from both surveys and last, calculate a combined NPS Score for the two surveys. The combined data is then output directly to your Insights Canvas via the Calculation element.

The items highlighted in yellow should be replaced with your survey & question IDs.

%%sid1 = "1234567"; //Survey ID of first survey
%%qid1 = "10"; //Question ID of NPS Question in first survey
%%sid2 = "7654321"; //Survey ID of the second survey
%%qid2 = "10"; //Question ID of NPS Question in second survey

%%arr1 = sgapiGetResults(%%sid1, %%qid1);
%%arr2 = sgapiGetResults(%%sid2, %%qid2);

%%detractors1 = %%arr1['result']['detractor']['count'];
%%passives1 = %%arr1['result']['passive']['count'];
%%promoters1 = %%arr1['result']['promoter']['count'];

%%detractors2 = %%arr2['result']['detractor']['count'];
%%passives2 = %%arr2['result']['passive']['count'];
%%promoters2 = %%arr2['result']['promoter']['count'];

%%total_det = %%detractors1 + %%detractors2;
%%total_pas = %%passives1 + %%passives2;
%%total_pro = %%promoters1 + %%promoters2;

%%total = %%total_det + %%total_pas + %%total_pro;
%%nps = sgapiRound((%%total_pro / %%total * 100) - (%%total_det / %%total * 100), 1);

%%output = "Total Promoters: " . %%total_pro . "<br/> Total Passives: " . %%total_pas . "<br/>Total Detractors: " . %%total_det;
%%output .= "<br/>Combined NPS Score: " . %%nps;

The resulting output is as follows (example):

Total Promoters: 43
Total Passives: 19
Total Detractors: 79
Combined NPS Score: -26

Example 2

This calculation can be used with any numeric value to calculate the average value for a Hidden Value Action from all responses. In the below example script, a numeric quiz score value was calculated and stored in a Hidden Value Action. The averaged data is then output directly to your Insights Canvas via the Calculation element.

The items highlighted in yellow should be replaced with your survey & question IDs.

%%output .= "<h4 class='meta'>Quiz Score Results</h4>";
%%sid = 1234567;
%%qid = 52;
%%quizscore = sgapiGetResults(%%sid, %%qid);
%%total_responses = %%quizscore['result']['total'];
%%average = 'N/A';
if (%%total_responses) {
	%%sum = 0;
    foreach ( %%quizscore['result']['values'] as %%val => %%num ) {
    	%%sum += %%val * %%num;
    %%average = sgapiRound(%%sum / %%total_responses * 100, 3);
%%output .= "<h4 class='meta'>" . %%average . "%</h4>";
// %%output .= sgapiPrint_R( %%quizscore );

The resulting output is as follows (example):

Quiz Score Results


You can use css to style the way items with the meta class or h4 tag display in your Insights Canvas

Scripting and Other Out-of-the-Box Customizations

We’re always happy to help you debug any documented script that is used as is. That said, we do not have the resources to write scripts on demand or to debug a customized script.

If you have customization ideas that you haven't figured out how to tackle, we're happy to be a sounding board for SurveyGizmo features and functionality ideas that might meet your customization. Beyond this, you might want to consult with someone on our Programming Services Team; these folks might have the scripting chops to help you to achieve what you are looking for!

Basic Standard Market Research HR Professional Full Access Reporting
Free Individual Team & Enterprise
Feature Included In