A Coveo Trigger for Every Need

April 12, 2021

By David Austin

What Are Triggers?

In the case of Coveo, Triggers are a feature in the query pipeline. They define a function or action that will be taken (within the client-side search interface), when a certain search query meets a query pipeline condition. For example, you type in "Coveo" into the search and something happens because that search query was the trigger.

Before you setup triggers you need to understand your audience and have reviewed your analytics to see what type of queries people are making. Otherwise you could be creating a trigger than will never be hit.

So What's Possible?

There are four actions that make up the Trigger feature:

  • notify - will display a message in the search interface to the user
  • query - will run a new query that may or may not include text from the one entered by the user
  • redirect - will redirect the user to a specific URL based upon a set of conditions
  • execute - will execute some custom JavaScript that you define based upon the query entered

What's Required?

Before a Trigger will operate, two areas need to be setup.

First, a Trigger (with a Condition) needs to be added to the query pipeline in the Platform Admin.

Second, the component, if not already done so, needs to be added to the search UI. Let's have a look at each to know how to set them up properly.

Setup Trigger In Query Pipeline

In order to add a Trigger, via the Platform Admin, login and go to Query Pipelines, then select the pipeline in use. Once there, click on Triggers as shown below.

Setting up a trigger in a Coveo query pipeline

Once there, click Add Rule. You'll then be presented with selecting a trigger type on the left, and a condition on the right. The trigger type is one of the four options listed above. The conditions, which typically would be based on a type of query (i.e. $query) but are anything available within the Advanced Query, are added on the right side either by creating a new condition or selecting a global condition.

Adding a trigger in a Coveo query pipeline

Conditions

It's important to understand what "problem" or "challenge" you're trying to solve when it comes to implementing a condition.

This might be as simple as directing users to particular pages based upon queries.

It might be that your company has identified an issue and improving the search itself, getting users to better results reduces calls or emails to your support staff. Whatever it is, while creating a condition that is based upon information readily available is easy, be sure to create a condition that makes sense and is focused, testable and repeatable.

Conditions are also one of those things that, the fewer in number, the better.

If you've created conditions via the rules in the Query Pipeline it might seem impossible to know all the ones you've created. While you can add conditions via any aspect of a Query Pipeline setup, they are also found in their own area, aptly titled, Conditions shown below.

Managing conditions in Coveo

Syntax For Conditions

Just as queries have their own syntax, so do conditions. The syntax is fairly similar, which you can find here.

You can, like the majority of options in Coveo Cloud, create a condition via point and click measures, but perhaps your condition needs fine tuning.

In that case, like many options, you can also enter "developer mode" and construct your condition manually using the proper syntax of course.

Once a condition has been created, go to the Conditions area in Coveo Cloud, select said condition, and then click ... More followed by Edit code. A modal will pop-up displaying the condition in the code form.

Constructing a condition manually in Coveo using syntax

Add Component To Search UI

Now that we've got a Trigger type selected and a condition for which it will occur; next steps are to add it to the Search UI. Which is remarkably easy. How easy? Just copy this code into the DOM where you want a trigger to appear (if you're doing a notify).


<div class="CoveoTriggers"></div>

With that in place, you'll really just need to style it according to your liking. As query, execute, and redirect all perform an action that will result in either the search page refreshing or redirecting to another page or doing something else entirely you won't need to do much else.

See Them In Action

Now that we've got a Trigger setup, let's see each of them in action, shall we?

Notify Trigger

As discussed, the notify trigger is intended to inform users of something based upon a query and an optional condition. In this example we wanted to post a message to users if they entered the word "Coveo".

Animation of the Coveo notify trigger

Query Trigger

With the query trigger we can, based upon a query string or other condition, cause the search to perform an a different query. In this case, if we entered in "McDonald's" we want it to search for "burger".

Execute Trigger

The execute trigger is perhaps the most complex of the bunch and the most powerful.

If you have a JS function that you want to run if a specific query is entered (or other criteria are met) then this is what you'd want to use.

In this case, if the user enters the word " function"="" i="" programmatically="" add="" a="" div="" to="" the="" dom="" and="" display="" some="" text="" inside="" of="" />

While it resembles a notify trigger in this case, it's more flexible in what you can do. A few things to note:

  • The name of the function is all you need to enter within the Function field. e.g. myFunction("abc",123,...)
  • The JavaScript function needs to exist PRIOR to all Coveo logic. i.e. before the search is initialized.

Animation of the Coveo execute trigger

Redirect Trigger

Perhaps the most useful of the set, redirect allows you to simply redirect the user based upon a set of conditions.

In my example, if the user enters the word "Search" I'm redirecting the browser to coveo.com.

This is useful if you have specific products you're launching, or you want to redirect users to a solution if they enter in a specific error perhaps.

Animation of the Coveo redirect trigger

Debugging Triggers

Determining if your Trigger is working or not can sometimes be cumbersome. Luckily you can find it within the api call as shown below when a successful trigger is made.

Go to your Developer Tools and open up the Network tab and locate the query submission.

Seeing this it tells me the trigger is setup properly on the Coveo side. But if I never saw my function run, then I know the issue is on the client side.

Debugging Coveo triggers

And With That...

I hope you're better prepared to work with Triggers. The possibilities are endless. However, use as with all things in Coveo, use sparingly.

I highly doubt users want to be redirect on a common phrase or notified of messages based upon every other word. The nice thing is they can streamline workflow, add some nice features, and in the end improve a visitors experience.

Image of Fishtank employee David Austin

David Austin

Development Team Lead | Sitecore Technology MVP x 3

David is a decorated Development Team Lead with Sitecore Technology MVP and Coveo MVP awards, as well as Sitecore CDP & Personalize Certified. He's worked in IT for 25 years; everything ranging from Developer to Business Analyst to Group Lead helping manage everything from Intranet and Internet sites to facility management and application support. David is a dedicated family man who loves to spend time with his girls. He's also an avid photographer and loves to explore new places.