The tag() function lets you attach metadata to survey questions. Tags make it easier to group, filter, and analyse responses later by associating contextual information (e.g., brand, category, market) with each question.
You can apply tags in two ways:
Temporarily, using a context manager
Directly, by passing them as a parameter to a question
Function Definition
Arguments
**kwargs: One or more key-value pairs representing the tags to apply.
Example: brand="Ford", category="SUV"
Returns
A dictionary of tags that can be passed to the tags argument on a question.
Example Usage
1. Using as a Context Manager
Apply tags to all questions within a defined block:
from survey import Survey
s = Survey(**globals())
with s.tag(brand="Ford", category="SUV"):
s.text_question("What do you like about {brand} {category}s?")
s.numeric_question("How many {brand} {category}s have you owned?")
All questions inside the with block automatically include the specified tags (brand and category).
2. Passing Tags Directly to a Question
You can also apply tags to a single question:
s.text_question(
"Why is {brand} your favorite?",
tags=s.tag(brand=brand_name), )
This method is useful when tags vary per question rather than per group.
When to Use Tags
To link questions to external variables like brand, region, or segment.
To simplify filtering and reporting in downstream analysis.
To dynamically format question text using placeholders (e.g., {brand}, {category}).
Related Concepts
For a deeper explanation of tagging and metadata management, see:
