Hotjar Session targeting and tracking settings use the PCRE2 flavor of regex
You can test REGEX code at regex101.com using the "PCRE2 (PHP >=7.3)" flavor in the lefthand menu and enter different URLs in the TEST STRING field to make sure they will match.
If you are using REGEX for your Heatmap URL filter, please see this article: What Are Heatmap URL Filters?
A Regular Expression, REGEX, is a special text string for describing a search pattern. Within Hotjar, you can use Regular Expressions to target pages with Surveys, or to determine when Hotjar should start recording a user's session.
In addition, Regular Expressions can be used to prevent IP addresses from being tracked by Recordings and Heatmaps. The IP block configurations can be found in the How to block your own IP section of our How to Change Session Capture and Edit Site Settings article.
Need help building your Regular Expressions?
If you're unfamiliar with Regular Expressions and would like to learn more, we highly recommend taking a quick crash course!
Page Targeting with Regular Expressions
For page targeting, when setting up your tool, there will be a Page Targeting option in which Regular Expressions can be set as the targeting option.
- Wildcard examples (where * can be anything)
- Wildcard excluding a certain pattern
- Excluding certain words or patterns in a URL
- Multiple pages which don't follow any pattern
Wildcard examples (where * can be anything)
Example:
Target: www.example.com/pages/*
Example: (http|https):\/\/www.example.com\/pages\/.*
Example:
Target: www.example.com/pages/*/article
Example: (http|https):\/\/www.example.com\/pages\/[^\/]+\/article
Example:
Target:www.example.com/user/<user_id>/profile/(numeric user_id)
Example: (http|https):\/\/www.example.com\/user\/[0-9]+\/profile\/
Wildcard excluding a certain pattern
Example:
Target: All pages which match the pattern but do not include the word "article" or "post" www.example.com/pages/*/subpage
Example: (http|https):\/\/www.example.com\/pages\/(?!article|post)[^\/]+\/subpage
Excluding certain words or patterns in a URL
Example: Target: Pages which include the word "page" and do not include "somepage":
Example: (?=.*page.*)(?!.*somepage).*
Please note that while this example uses lookahead, a feature of the PCRE2 flavor of regex, it will not work for our heatmaps and global filters, as they use the RE2 flavor as mentioned in our guide What Are Heatmap URL Filters.
Multiple pages that don't follow any pattern
Example: Target these three pages:
- www.example.com/pages/article-one/details/
- www.example.com/pages/some-random-word/details/
- www.example.com/pages/another-post/details/
Example: (http|https):\/\/www.example.com\/pages\/(article-one|some-random-word|another-post)\/details\/
Blocking IPs with Regular Expressions
IP support
Only IPv4 addresses can be entered at this time. We are unable to support IPv6 or dynamic IP addresses.
Simple IP match (single IP)
Example: Block the IP 192.168.0.1
Example: 192.168.0.1 or 192\.168\.0\.1
Both would work since in regex, the "." character means "any character".
IP Ranges
Example: Block the IPs 192.168.0.14 - 192.168.0.62
Example: 192\.168\.0\.(1[4-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]|6[0-2])
Example: Block the IPs 192.168.0.0 - 192.168.0.255
Example: 192\.168\.0\.\d{1,3}
IP Range Regular Expression Builder
To generate IP ranges in regex format, check out this free tool:
http://www.analyticsmarket.com/freetools/ipregex
Example: 127.76.111.64/28
Example: 127\.76\.111\.(6[4-9]|7[0-9])
Different IPs
Block all these IPs:
Example:
- 192.168.0.1
- 192.168.0.100
- 192.168.0.150
Example: 192\.168\.0\.(1|100|150)
Watch out for common mistakes
- If you include the start and end characters (^ and $) then any URL that includes text before or after the pattern will not be matched. Avoid using them. It's very common for URLs to include query strings at the end, such as the UTM parameter that are added to URLs for tracking purposes. An example would be:
https://www.hotjar.com/?utm_campaign=ads - A forward-slash (/) at the end of the URL is generally optional. If your REGEX includes that character at the end, then a visit to the same URL but without the forward-slash wouldn't match. It is better not to include that final forward slash character.
- There is a limit of 750 characters for all of our REGEX targeting rules. If you go over this limit, an error message stating that There was a problem saving session targeting and tracking settings. Please retry later will alert you of this issue.