Skip to main content
userTourKit
@tour-kit/surveysHooks

useSurveyScoring

Access NPS, CSAT, and CES scoring utilities plus a getSurveyScore helper that automatically selects the right algorithm for a completed survey

domidex01Published Updated

useSurveyScoring returns scoring functions bound to the current surveys context. The getSurveyScore helper reads a completed survey's responses and applies the correct algorithm based on SurveyConfig.type.

Import

import { useSurveyScoring } from '@tour-kit/surveys';

Usage

import { useSurveyScoring } from '@tour-kit/surveys';

function NPSResults() {
  const scoring = useSurveyScoring();

  // Auto-select algorithm from survey type
  const result = scoring.getSurveyScore('nps-q1');

  if (!result) return <p>No completed responses yet.</p>;

  // result is NPSResult when the survey type is 'nps'
  const nps = result as import('@tour-kit/surveys').NPSResult;

  return (
    <div>
      <p>NPS Score: {nps.score}</p>
      <p>Promoters: {nps.promoters} ({nps.promoterPct.toFixed(1)}%)</p>
      <p>Passives: {nps.passives}</p>
      <p>Detractors: {nps.detractors}</p>
    </div>
  );
}

Manual scoring

function CustomScoring() {
  const { calculateNPS, calculateCSAT, calculateCES } = useSurveyScoring();

  const ratings = [8, 9, 10, 6, 7, 9, 10];

  const nps = calculateNPS(ratings);
  const csat = calculateCSAT([4, 5, 3, 5, 4], 4);   // threshold: 4 of 5
  const ces = calculateCES([5, 6, 4, 7, 5]);          // 1–7 scale

  return (
    <dl>
      <dt>NPS</dt><dd>{nps.score}</dd>
      <dt>CSAT</dt><dd>{csat.score}%</dd>
      <dt>CES</dt><dd>{ces.score} avg</dd>
    </dl>
  );
}

Returns

Prop

Type

getSurveyScore returns null for type: 'custom' surveys — custom surveys have no standard scoring algorithm. Use calculateNPS, calculateCSAT, or calculateCES directly with manually extracted response values if you need scoring for a custom survey.

Standalone scoring functions

The same algorithms are exported as standalone functions that do not require a provider:

import { calculateNPS, calculateCSAT, calculateCES } from '@tour-kit/surveys';

// Use in server actions, API routes, or analytics pipelines
const nps = calculateNPS([9, 10, 6, 8, 10]);

See the Utilities section for detailed documentation.