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 Feedback widgets or 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 your Hotjar settings.
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.
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
Is your REGEX not working?
This REGEX tester can help you build regular expressions. Make sure 'PCRE2 (PHP >=7.3)' is selected in the menu on the left.
Wildcard examples (where * can be anything)
Target:
www.example.com/pages/*
(http|https):\/\/www.example.com\/pages\/.*
Target:
www.example.com/pages/*/article
(http|https):\/\/www.example.com\/pages\/[^\/]+\/article
www.example.com/user/<user_id>/profile/(numeric user_id)
(http|https):\/\/www.example.com\/user\/[0-9]+\/profile\/
Wildcard excluding a certain pattern
Target:
All pages which match the pattern but do not include the word "article" or "post" www.example.com/pages/*/subpage
(http|https):\/\/www.example.com\/pages\/(?!article|post)[^\/]+\/subpage
Excluding certain words or patterns in a URL
Target:
All pages which do not include "/somepage" and "/someotherpage"
^((?!\/somepage|\/someotherpage).)*$
Target:
Pages which include the word "page" and do not include "somepage":
(?=.*page.*)(?!.*somepage).*
Multiple pages which don't follow any pattern
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/
(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)
192.168.0.1 or 192\.168\.0\.1
Both would work since in regex, the "." character means "any character".
IP Ranges
Block the IPs 192.168.0.14 - 192.168.0.62
192\.168\.0\.(1[4-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]|6[0-2])
Block the IPs 192.168.0.0 - 192.168.0.255
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
127.76.111.64/28
127\.76\.111\.(6[4-9]|7[0-9])
Different IPs
Block all these IPs:
192.168.0.1
192.168.0.100
192.168.0.150
192\.168\.0\.(1|100|150)