How to Test Context with Answers End-to-End

If you’re familiar with our query rules feature, you know that it’s helpful to be able to test that your context rules are working end-to-end!

Here’s a quick and easy way to do that:

  1. Set up a code sandbox that adds the search bar and sets context; you can fork this one as an example: Context + Query Rules - CodeSandbox. Replace the [[REPLACE_ME]] values with your own business id, api key, etc.

  2. Define your context in the constant on line 17. The sandbox has the following example:

      const userData = {
        name: "ava"
      }; 

Update this value to be the context you’re expecting (to align with your query rule).

  1. Save the file and experiment with conducting searches, to make sure the experience reflects the query rule.

Here’s an example of us using this test on one of our demo accounts: Context + Query Rules - DTC - CodeSandbox. In this sandbox, we’re testing the following query rule:

{
      "criteria": {
        "contextContainsKey": "$.name"
      },
      "actions": [
        {
          "actionType": "ADD_FILTER",
          "filter": {
            "name": {
              "$eq": "{{$.name}}"
            }
          },
          "verticals": [
            "products"
          ]
        }
      ],
      "name": "Filter By Product"
    }

Let us know how this works for you!

3 Likes

This works great! We used the code sandbox to text context + query rules for a client who wanted to show different content for prospects than for existing customers.

We set the context in the sandbox like this:

<script>
      const context = {
        is_prospect: false
      };
    </script>

And then toggled “is_prospect” from “false” to “true” to test the rules. Here is an example of two query rules we set up to show different content when a prospect searches “basics” than when an existing customer searches “basics”:

{
  "criteria": {
    "contextMatches": {
      "$.is_prospect": {
        "$eq": true
      }
    },
    "searchTermContains": "basics"
  },
  "actions": [
    {
      "actionType": "BOOST_ENTITIES",
      "entityIds": [
        "promotiongetstarted"
      ],
      "verticalKey": "Promotion"
    }
  ]
},
{
  "criteria": {
    "contextMatches": {
      "$.is_prospect": {
        "$eq": false
      }
    },
    "searchTermContains": "basics"
  },
  "actions": [
    {
      "actionType": "BOOST_ENTITIES",
      "entityIds": [
        "promotiongetstartedwebinar"
      ],
      "verticalKey": "Promotion"
    }
  ]
},