useEvaluation

Learn how to evaluate queries using composables.

This composable evaluates a CQL query with full server-side rendering support. It uses Nuxt's useAsyncData under the hood, so the query is evaluated on the server during SSR and the result is hydrated on the client.

Signature

This composable has the following signature:

function useEvaluation<T extends JsonValue>(    query: string,    options?: UseEvaluationOptions,): AsyncData<T>;

Example

Here is an example of how to use this composable:

components/DocsLink.vue
12345678
<script setup>const {data: isDeveloper} = await useEvaluation("user's persona is 'developer'");</script>
<template>  <a v-if="isDeveloper" href="/docs">View docs</a>  <a v-else href="/share">Share with your developer</a></template>

Parameters

query
string

The CQL query to evaluate, with a maximum length of 500 characters.

options(optional)
object

The evaluation options.

fallback(optional)
JSON

A fallback value to use in case of an error.

If not specified, the error ref will contain the error and data will remain null.

timeout(optional)
number

The maximum fetch time in milliseconds.

Once reached, the SDK will abort the request and reject the promise with a timeout error.

attributes(optional)
object

The map of attributes to inject in the evaluation context.

The attributes can be referenced in audience conditions using the context variable. For example, suppose you pass the following attributes:

{cities: ["New York", "San Francisco"]}

You can then reference them in queries like:

context's cities include location's cityName

For more information, see Context variables.

The following restrictions apply to the attributes:

  • Up to 30 entries and 5 levels deep
  • Keys can be either numbers or non-empty strings with a maximum length of 50 characters
  • Values can be null, numbers, booleans, strings (up to 50 characters), or nested maps
  • Nested maps follow the same constraints for keys and values

Return

The return is a Nuxt AsyncData object with the following properties:

data
Ref<T|null>

A ref containing the evaluation result, or null while loading.

pending
Ref<boolean>

A ref indicating whether the query is currently being evaluated.

error
Ref<Error|null>

A ref containing the error if the evaluation failed, or null otherwise.