3 minutes read | Level: Intermediate | Last Updated: October 2025 |
Create a Counter Variable in a Flow
In some cases, a user may need to keep track of how many times an action has been performed within a flow. This can be useful for scenarios such as recording the number of reminders sent, limiting attempts in a registration process, or keeping a running score in a quiz. A counter variable serves this purpose by incrementing its value whenever a specified action occurs.
Users can create a variable (refer to Flow variables vs Contact variables document to know more about how variables are created in a flow) and increment it with expression.
Why use a counter variable?
- To track repeated actions (e.g., number of nudges/reminders sent).
- To limit actions after a certain number of attempts (e.g., stop after 3 failed tries).
- To measure engagement (e.g., how many times a user participates in an activity).
- To track quiz scores or activity points (e.g., increasing score when a user answers correctly).
How to Create a Counter Variable
1. Initialize the counter
- Add an
Update Contact
node. - Select
counter
from the dropdown in what to update field.(Users can also create a contact variable with a different name for the counter.) - Enter the value as Zero(This ensures that if no value exists yet, it starts from 0), this may vary depending on the requirements.
2. Increment the counter
- Use case: Nudging the user when no response is received. If the nudge has already been sent thrice, we stop further nudges. To track how many times a nudge has been sent, we use a counter.
- Add an Update Contact node after a nudge has been sent.
- Use the function -
<%= @contact.fields.counter + 1 %>
A variable counter incremented each time the user reaches this node.
3. Fetch the counter value
- The value of the variable counter can be fetched anywhere with syntax
@contact.fieds.counter
4. Use Split by Contact Field
-
Add a
Split by Contact
Field node to split the flow based on the counter incremented. -
Example: If counter ≥ 3, stop nudging the user.
Advanced Example: Adding Two Counters
In cases where different counters are maintained, their values can be combined:
<%= (@contact.fields.counter || 0) + (@contact.fields.add_counter || 0) %>