Query evaluation
Learn how to evaluate queries in real-time.
This guide provides practical examples of using the Nuxt SDK to evaluate CQL queries from your application.
Basic usage
To evaluate queries, you can use either the useEvaluation composable or the <Personalization> component, depending on whether you prefer an imperative or declarative approach. Both use Nuxt's useAsyncData under the hood, so the query is evaluated on the server during SSR and the result is hydrated on the client.
For client-side rendering, you can use the composables and components from @croct/plug-nuxt/csr, which follow the same patterns as the Vue SDK.
Here is an example:
<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>Note that the result of the evaluation is not limited to boolean values. For example, you can use the following query to find out the location from which the user is accessing your application:
<script setup>const {data: location} = await useEvaluation("location");</script>
<template> <address v-if="location"> {{ location.cityName }}, {{ location.stateName }} - {{ location.countryName }} </address></template>In this case, the result of the evaluation is a geographic location, and the output would look something like this:
<address>San Francisco, California - United States</address>Fault tolerance
You should always provide a fallback value when evaluating queries to make your application resilient to unexpected errors, downtime, and network failures.
All you have to do is specify the result you want to use as a fallback:
<script setup>const {data: returning} = await useEvaluation("user is returning", { fallback: false,});</script>
<template> <p v-if="returning">Welcome back!</p> <p v-else>Welcome!</p></template>In this example, the result is set to false if the evaluation fails, ensuring that the application will work even if the evaluation fails.
Context variables
In some cases, you may want to pass additional information that can be used by the query in the evaluation process.
For example, let's say you want to check whether the user is accessing your application from one of a list of countries. The SDK allows you to pass this information as an attribute to the query:
<script setup>const {data: enabled} = await useEvaluation( "context's countries include location's countryName", { context: { attributes: { countries: ['United States', 'Canada', 'Mexico'], }, }, });</script>
<template> <span v-if="enabled">Available</span> <span v-else>Unavailable</span></template>Any attribute passed in the attributes option will be available in the query as a context variable.