How to use calculations in your flow
With calculations you can let Qonfi work out exactly what a user needs, based on the answers they give. Think of the right number of tiles for a terrace, the right amount of paint for a wall, or the right boiler capacity for a household.
There are many ways to use calculations. In this article we'll go through three common use cases:
- Use case 1 – Using calculations in your flow. Show a calculated value in a question or result text, so the user sees what has been calculated.
- Use case 2 – Using calculated values in your matching. Use a variable to determine which products are a fit, for example only tiles that match the selected size.
- Use case 3 – Advising the right quantity of products. Advise the right quantity of products, including the matching title and price.
You can use these separately, or combine all three in the same flow.
Example garden tiles
Throughout this article we'll use one example. Say you are selling garden tiles and beams. We want to calculate the right tile quantity based on their preferred tile size and square meters. When a user says he prefers a 40x40cm tile and needs tiles for his 20 square meters terrace, we can store both inputs (40x40 and 20m2) in two separate variables. We can then use these inputs to calculate the right tile quantity.
Variables can be used to store information that a user provides and show this information later in your flow or use the input in a calculation. You refer to a variable using the vars. prefix, for example {vars.square_meters}.
Steps 1 to 3 are the foundation for all three use cases: you first have to store the right values and create your calculation before you can do anything with the outcome.
1. Enabling variables
We can use variables to create calculations. Variables can be used to store information that a user provide and show this information later in your flow or use the input in a calculation.
To start using variables we'll have to enable them first. You can do this under "Settings -> Configure -> Enable variables.
2. Adding the necessary variables in our flow
Once we have enabled variables (step 1), we can now create variables and store the right values. There are two ways to store values.
Storing a fixed value per answer
Let's start by adding the tile_size variable to each of the tile size answers. In the screenshot below, you can see that we have added a variable tile_size with value 40 for the tile size answer "40x40 CM". We repeat this for each of the other tile size answers. So we create a variable with the same variable name tile_size for the tile size answer "60x60 CM", but with the value 60.
Storing the user's input
We have now set the first variable to store the correct tile size, based on a user choice. We also need to store the square meters of the user in a separate variable, in order to calculate the right tile quantity based on the needed square meters and the preferred tile size.
To do this, edit the (only) answer of the "Square meters" question and add a variable square_meters and check the Use input checkbox. The square meters user input will now be stored in the square_meters variable.
We have now created two variables:
tile_size— the selected tile size answer of the user is stored as value (40, 60 or 100).square_meters— the m2 input of the user is stored as value.
3. Creating calculations based on the added variables
We can now use both variables to calculate the right quantities for each product category. Since the product sizes differ for tiles and beams, the quantity needed for each product category differs given the stated m2 of the user. Therefore, we have to separately calculate the quantities for the tiles and beams based on the user input.
We create two additional variables: quantity_tiles and quantity_beams at the square meters question.
Let's for example look at calculation for the "quantity_tiles" variable in the image above: {ceil((vars.square_meters*100)/vars.tile_size)}
In this calculation, we:
- Take the previously defined "square_meters" variable and multiply it by 100 resulting in the total square centimeters.
- Next, we'll divide the total square centimeters by the tile_size variable defined in the previous "tile size" question.
- Lastly we'll round this calculation up with the
ceilfunction.
For example: let's say a user wants a 60x60cm tile and needs 20 square meters of tiles. In this case the:
- 'square_meters' value is 20 and it is multiplied by 100 to calculate the square centimeters. Resulting in 2000 square centimeters.
- The 2000 square centimeters is then divided by 60 (the selected tile_size). Resulting in 33.33 tiles.
- The result is rounded up with the
ceilfunction. Resulting in 34 tiles.
We do the same for the "quantity_beams" variable, but just using a little bit of a different calculation.
Rounding up is intentional here: with 33 tiles the customer would fall short. For all available calculation and rounding methods (round, floor, roundTo, if, comparisons), see the article on calculating and displaying variables in texts.
Use case 1: Using calculations in your flow
Say you want to confirm to the user what you've calculated, before they get to the advice. For example: "You need 95 tiles for your 57 m² terrace." Showing this makes it clear that the advice is based on their own input, and it gives them a chance to correct themselves if they entered the wrong number.
You can do this anywhere you can enter text: question texts, answer texts and result texts. Simply refer to your variable using the {vars.variable_name} syntax:
In the previous steps we created two variables we can use here: {vars.square_meters}, which stores what the user entered, and {vars.quantity_tiles}, which stores the outcome of our calculation.
You need {vars.quantity_tiles} tiles for your {vars.square_meters} m² terrace.
This results in the following:
Use case 2: Using calculated values in your matching
Say you sell boilers. The capacity a customer needs depends on the size of their household, so you ask how many people live there and calculate the required capacity from that. You then only want to advise boilers that are actually large enough.
You could create a separate matching rule for every household size, but that quickly becomes a lot of work, and you'd have to update it every time you add a new boiler to your feed.
In a matching rule you would normally use a fixed value to match the right products to an answer, but you can also match products based on a (calculated) variable instead. That way, one rule covers every situation.
In this example we calculated the required capacity and stored it in the required_capacity variable. We can now compare that variable to the capacity of each boiler in our feed.
Setting it up
- Go to Matching → Data matching and open the matching rule of the answer you want.
- Select the feed property you want to match on, for example
amount_litres. - Select the operator, for example Greater than or equal to.
- In the next dropdown, switch from Value to Variable.
- Select the variable you want to compare against, for example
required_capacity.
If the calculation results in 200 liters, only boilers of 200 liters or more will remain in the advice. A smaller household automatically gets a smaller model, without you having to create separate rules for each situation.
Use case 3: Advising the right quantity of products
Say a user needs 34 tiles. You don't just want to show them the right tile, you want the advice to show 34x that tile, at the price of 34 tiles. Otherwise they still have to do the math themselves.
Back to our tile example. In the previous steps we calculated the number of tiles the user needs and stored it in quantity_tiles. In this use case we combine that variable with the information from your product feed.
Make sure calculated properties are enabled (see above). Go to Feed → Mapping; you will now see a calculator icon next to your mapping properties.
Mapping the calculated quantity
Let's start with the Quantity property. Set the value to the variable we calculated earlier, using the {vars.variable_name} syntax. For our tiles, that's {vars.quantity_tiles}.
This is the one place where the quantity of a product is determined. Once it's mapped, the outcome becomes available in your other property mappings as {internal.quantity}.
Where vars. refers to something the user provided or something you calculated in your flow, internal.quantity refers to the quantity that was actually mapped for the product being shown. That means you only define the calculation once: in every property after this, you simply refer to the result. If your calculation ever changes, you only update the "Quantity" property and the title and price will follow automatically.
In your mappings you can combine three prefixes:
{feed.property_name}— a property from your product feed, for example{feed.price}{vars.variable_name}— a variable from your flow, for example{vars.quantity_tiles}{internal.quantity}— the quantity that was mapped for the product being shown
Note that {internal.quantity} is only available in Feed → Mapping, since it comes from the mapping itself. In your question and result texts you keep using {vars.variable_name}.
Updating the product title to show the correct quantity of products
First, let's change our product title to show the correct quantity of products. Click the calculator icon next to the Title prefix property and enter:
{internal.quantity}x
Great! Let's check out the result so far. We can now see that the products advised contain the correct quantities in their titles.
Calculating the correct prices
Now that the quantities are correct, let's make sure the prices match them as well.
First, let's make sure that the mapping for our price properties are correctly set to the "Number" type. You can find this under Feed → Properties.
Once that's done, head back to Feed → Mapping and click the calculator icon next to the Price property. Just like the title prefix, use {internal.quantity} in combination with the {feed.price} property to calculate the price.
If you use the sale_price property in your advise, you will need to do the same for the sale_price property.
Awesome! If we preview the flow, we can now see that the calculated prices are also correctly displayed. Every product in the advice now shows both the right quantity and the right total price, based on what the user entered.
Passing the quantity on to your product page
You can also add the calculated quantity to your product link as a query parameter. That way, the right quantity is already preset when the user lands on the product page.
Click the calculator icon next to the Link property and add the quantity to the URL:
{feed.link}?quantity={internal.quantity}
Note that most platforms don't read a quantity parameter on the product page by default. Check with your developer which parameter your webshop expects, or use the add-to-cart URL mapping instead.
That's it! Your flow now calculates the right quantity, shows it to your users, matches the right products and displays the correct titles and prices.
Want to take it one step further and let users add all products with the correct quantities to their cart in one click? See the article on product bundles.
Documentation