Query evaluation
Learn how to evaluate queries in real-time.
This guide provides practical examples of using the Vue SDK to evaluate CQL queries from your application.
Basic usage
To evaluate queries in real-time, you can use either the useEvaluation composable or the <Personalization> component, depending on whether you prefer an imperative or declarative approach.
Here is an example:
<script setup>import {useEvaluation} from '@croct/plug-vue';
const {data: isDeveloper} = 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>If you are using a server-side rendering framework, you should also provide an initial value for pre-rendering, which will then be personalized on the client.
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>import {useEvaluation} from '@croct/plug-vue';
const {data: location} = 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>import {useEvaluation} from '@croct/plug-vue';
const {data: returning} = 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>import {useEvaluation} from '@croct/plug-vue';
const {data: enabled} = useEvaluation( "context's countries include location's countryName", { 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.