Optimize can check query parameters and use them in targeting rules.
In this article:When to use query parameter targeting
Query parameters often contain information that you want to use in targeting conditions. For example:
To target users who | include user in the experiment when |
---|---|
searched for “pixel” on your site | the search query parameter includes a value of “pixel” |
were referred by an email campaign | the utm_medium parameter has a value of “email” |
were referred by the “fall” campaign | the utm_campaign parameter has a value of “fall” |
Query parameter targeting explicitly targets values that occur in the query string of a URL. Query parameters are found between the question mark (?
) and hash mark (#
), for example:
https://www.example.com/store/landing?utm_campaign=fall#fragment
https://www.example.com/store/search?q=pixel#fragment
Query parameters in the above examples include:
- Google Analytics campaign parameters (
?utm_campaign=fall
), and - Search queries (
?q=pixel
).
URLs can contain multiple query parameters, called query components. The first occurs after the question mark (?
), and subsequent components occur after the ampersand (&
). The following URL includes three query components from the Google Analytics URL builder:
http://www.example.com?utm_source=google&utm_medium=email&utm_campaign=fall
Benefits over URL targeting
Query parameter targeting has the following benefits over URL targeting:
- Query parameter rules only look at a portion of the URL between the question mark (
?
) and hash mark (#
). - You can assign human-readable variable names (e.g.: “q” becomes “search”).
- You can save, edit and reuse commonly used variables in other experiments.
- You can target the unescaped value (e.g.: "a=b") even when the raw URL contains escaped strings (e.g.: "a%3Db”)
Example: Target visitors who searched for a specific product
You want to target an experiment at visitors who search for pixel on your website. Searches generate the following URL structure:
https://www.example.com/store/search?q=pixel
To create a rule targeting users searching your website for a specific term, you need to create a custom variable, then build a condition with it.
Step 1: Create a custom variable
- Go to the experiment detail page.
- In the Targeting and site changes section, click audience targeting rules.
- Select Query parameter.
- Click Variable > Create new... or select an existing variable.
- Enter a query key, which is the query component you want to target. For example,
q
. - Enter a name for your variable. For example,
search query
. - Click Create variable.
Step 2: Build a condition with your custom variable
After creating your custom variable, Optimize populates it in a new targeting condition, which you can complete by adding a match type and value. For this example, build the following condition and click Add.
Variable | Match type | Value |
search query | contains | pixel |
This condition will evaluate:
- true if the value of the first matching query component (unescaped) contains
pixel
- undefined if there are no query components containing
pixel
Empty vs. undefined parameters
Example 1: An empty parameter
When the value of param1 is empty, param1 can be matched using matches regex and ^$
http://www.example.com/path?param1=¶m2=x
Example 2: Undefined parameter
When the value of param1 is undefined, param1 can be matched with Equals against the string value: undefined.
http://www.example.com/path?param2=x
Match types
The following match types are available in query parameter targeting:
- Equals / does not equal
- Contains / does not contain
- Starts with / does not start with
- Ends with / does not end with
- Regex matches / does not regex match
- Less than
- Less than or equal
- Greater than
- Greater than or equal
Equals / does not equal
Every character in your query parameter, from beginning to end, must be an exact match of the entered value for the condition to evaluate as true. A condition using does not equal will evaluate as true when the query parameter does not equal any of the entered values.
Example:
Variable | Match type | Value |
search query | equals | pixel |
Will evaluate true for:
https://www.example.com/store/Search?keywords=pixel
Variable | Match type | Value |
search query | does not equal | iPhone |
Will evaluate true for:
https://www.example.com/store/Search?keywords=pixel
Contains / does not contain
The contains match type (also known as a "substring match") allows you to target any occurrence of a substring with a longer string. Contains is useful when targeting a unique query string parameter that appears in multiple URLs.
Example:
Variable | Match type | Value |
page | contains | 4 |
Will evaluate true for:
http://www.example.com/member.cgi?id=9&page=4
Variable | Match type | Value |
page | does not contain | page=4 |
Will evaluate true for:
http://www.example.com/member.cgi?id=9&page=2
Starts with / does not start with
The starts with match type matches identical characters starting from the beginning of the query string up to and including the last character in the string you specify. Use the starts with match type when your query parameters are generally unvarying but can include strings at the end that you want to exclude.
Example:
Variable | Match type | Value |
page | starts with | 4 |
Will evaluate true for:
http://www.example.com/member.cgi?id=9&page=4
Variable | Match type | Value |
page | does not start with | 4 |
Will evaluate true for:
http://www.example.com/member.cgi?id=9&page=2
Ends with / doesn't end with
An exact match of the entered value with the end of the URL. You can target shopping cart pages that use /thankyou.html
at the end of their URLs.
Example:
Variable | Match type | Value |
page | ends with | 4 |
Will evaluate true for:
http://www.example.com/member.cgi?id=9&page=24
Variable | Match type | Value |
page | does not end with | 4 |
Will evaluate true for:
http://www.example.com/member.cgi?id=9&page=42
Regex matches / does not regex match
A regular expression uses special characters to enable wildcard and flexible matching. Regex matches are useful when the stem, trailing parameters, or both, can vary in the URLs for the same webpage. If a user could be coming from one of many subdomains, and your URLs use session identifiers, you could use a regular expression to define the constant element of your URL.
Example:
Variable | Match type | Value |
page | regex matches | checkout.cgi\?page=1 |
Will evaluate true for:
http://sports.example.com/checkout.cgi?page=1&id=123
http://fishing.example.com/checkout.cgi?page=1&lang=en&id=123
Variable | Match type | Value |
page | does not regex match | checkout.cgi\?page=1 |
Will evaluate true for:
http://sports.example.com/checkout.cgi?page=2&id=123
Operators
AND
The AND operator is useful when you wish to target a variation based on multiple rules that all need to be true. Conditions using the AND operator will only evaluate as true when all of the values are met.
Example:
To target users searching for pixel
, while browsing from a tablet, create two rules joined by the AND operator.
A query parameter targeting rule:
Variable | Match type | Value |
search query | equals | pixel |
...and a technology targeting rule:
Variable | Match type | Value |
Device Category | equals | tablet |
OR
The OR operator is useful for targeting one kind of page with multiple URL configurations. Conditions using the OR operator will evaluate as true when any of the values are met. You can use OR by adding additional values in a URL targeting rule. When targeting URLs, OR is automatically appended to your first URL after pressing return. Simply type additional URLs (followed by return) to continue building conditions with the OR operator.
Example:
To target searches on your website for either pixel
or chromecast
, create a rule with two site search in the Value field. You'll notice that OR is automatically added after you press enter.
Variable | Match type | Value |
search query | equals | pixel or... chromecast |
The OR operator is supported within individual conditions but you can't use it to combine multiple conditions. For example, you can build a condition like this: "path equals /store
OR /shop
" but you can't use OR across multiple conditions like this: "path equals /store
OR host equals example.com"
Related targeting articles
- Introduction to Optimize targeting
- URL targeting
- Query Parameter targeting
- Audience targeting
- Behavior targeting
- Geo targeting
- Technology targeting
- First-party cookie targeting
- Data Layer variable targeting
- JavaScript variable targeting
- Custom JavaScript targeting
- Google Analytics Cookie Usage on Websites – Analytics Developers