Sub topic 3
Choosing an IP match type
This chapter assumes you can open the tag settings of a GA4 web data stream and know that GA4 events can carry parameters. You should also know your organization's public IP addresses. The next chapter covers the full rule definition. Here the focus is the one decision inside a rule that goes wrong most often: which match type to use.
What the match type does
An internal traffic rule has three parts: a rule name, a traffic_type value, and one or more IP conditions. GA4 compares the IP address of each incoming request with those conditions. When one matches, GA4 sets the traffic_type parameter on the event to the value you chose, usually internal. A data filter then acts on that value.
The match type decides how GA4 compares the address in the request with the text you entered. A wrong choice fails in one of two ways:
- Too narrow. Some of your own addresses are not matched, and their visits stay in your reports.
- Too broad. Addresses that belong to real customers are matched and silently dropped once the filter is active.
The second failure is worse, because nothing in the reports shows what is missing.
What address GA4 compares
GA4 sees the public IP address of the connection, not the address of your laptop on the office network. Office networks typically use network address translation (NAT), so every device behind the router appears as one public address, or a small block of them.
A rule for 192.168.1.20 or 10.0.0.5 will never match. Those are private addresses and never reach GA4. Find your public address by visiting a "what is my IP" page from the network you want to exclude. Ask your network administrator whether the organization has a fixed block of public addresses.
GA4 accepts both IPv4 addresses (203.0.113.25) and IPv6 addresses (2001:db8::1). If your network has both, a rule written only for IPv4 misses visits that arrive over IPv6. Add a condition for each family.
The six match types
| Match type | Matches when | Typical use |
|---|---|---|
| IP address equals | The address is identical to your value | One fixed address |
| IP address begins with | The address starts with your text | Prefix such as `203.0.113.` |
| IP address ends with | The address ends with your text | Rarely useful |
| IP address contains | The text appears anywhere in the address | Rarely safe |
| IP address is in range (CIDR notation) | The address falls inside the block | A block of public addresses |
| IP address matches regular expression | The address fits your pattern | Ranges that CIDR cannot express |
Treat the text-based types (begins with, ends with, contains) as string comparisons. They have no notion of numbers or octets, so they match characters, not addresses.
Equals
Use equals for a single fixed address, such as a small office with one static IP. It is the most precise type and cannot match anything you did not list. The cost is maintenance. Ten home offices means ten conditions.
Begins with, ends with, and contains
These compare characters, so the text you enter can match more than you intend. Take the rule "begins with 203.0.113.1". It matches 203.0.113.1, but also 203.0.113.10, 203.0.113.100, and 203.0.113.199. If you meant only the first, you have excluded up to 111 other addresses.
The safer habit is to end a prefix at a dot. "Begins with 203.0.113." matches the whole last octet range, 203.0.113.0 through 203.0.113.255, and nothing in 203.0.1130.x, which is not a valid address anyway. That works only when you want the entire final octet. For anything smaller, use CIDR.
"Contains" is the most dangerous option. The text 10 appears in 203.0.113.10, in 110.45.7.200, and in thousands of other addresses. Avoid it unless the text is long enough to be unique.
CIDR range
CIDR notation writes an address followed by a slash and a number. The number says how many leading bits of the address are fixed. A smaller number means a bigger block. Each step of one bit doubles or halves the block.
| CIDR | Addresses covered | Example span |
|---|---|---|
| /32 | 1 | `203.0.113.25` only |
| /28 | 16 | `203.0.113.0` to `203.0.113.15` |
| /24 | 256 | `203.0.113.0` to `203.0.113.255` |
| /23 | 512 | `203.0.112.0` to `203.0.113.255` |
An office that owns a block of public addresses is the natural CIDR case:
IP address is in range (CIDR notation): 203.0.113.0/24This matches every address from 203.0.113.0 to 203.0.113.255. The block must be yours. If your provider assigned you only 203.0.113.0/28 and you enter /24, you exclude 240 addresses that belong to someone else. Get the exact block from your network administrator or your internet provider.
CIDR is usually the best choice for any block of addresses. It is exact, it works for IPv6 (2001:db8::/32), and it needs no escaping.
Regular expression
Use a regular expression only when the addresses you need to cover do not form a clean CIDR block. For example, the range 203.0.113.1 to 203.0.113.199 is not a power-of-two block. This pattern matches it:
^203\.0\.113\.([1-9]|[1-9][0-9]|1[0-9]{2})$Read it in parts:
^and$anchor the pattern to the start and end of the address. Without them, the pattern can match inside a longer string.203\.0\.113\.matches the fixed first three octets. Each dot is escaped as\., because an unescaped.matches any character.[1-9]matches 1 to 9.[1-9][0-9]matches 10 to 99.1[0-9]{2}matches 100 to 199.
The pattern does not match 203.0.113.0, or anything from 203.0.113.200 up. That is deliberate in this example. Check the boundaries every time you write one.
Two common mistakes make regex rules too broad:
- Forgetting to escape dots.
203.0.113.5also matches203x0y113z5, which is not an address, but it matches real addresses less obviously when combined with other omissions. - Forgetting anchors. An unanchored
203\.0\.113\.5matches203.0.113.5,203.0.113.50, and203.0.113.59.
Several conditions in one rule
A rule can hold more than one IP condition, and GA4 tags the traffic when any of them matches. Use this to combine a CIDR block for the main office, an equals condition for a remote worker with a static address, and a separate IPv6 condition. Keep the conditions of one rule tied to one purpose. If a contractor's address changes, you can then edit one named rule instead of untangling a mixed one.
A rule's traffic_type value can differ between rules. Use internal for staff, and a value such as agency for a partner, if you want separate filters later. Each value needs its own data filter.
Choosing a type
Work through these questions in order.
In short: prefer equals for one address, CIDR for a block, and regex only when nothing simpler is exact. Use "begins with" when you need the entire last octet and nothing else.
Check before you rely on it
A rule that looks right can still be wrong. Before you activate the matching data filter:
- Write down the lowest and highest address each condition should match, then confirm the type covers both ends and nothing outside them.
- Test a few edge values by hand. For the regex above, try
203.0.113.1,203.0.113.199,203.0.113.200, and203.0.113.0. - Visit your site from an address the rule should match, then look for the
traffic_typevalue on that event in DebugView. - Visit from an address it should not match, such as your phone on mobile data, and confirm the event has no internal tag.
A later chapter covers the Testing filter state, which lets you see what a filter would exclude without removing anything.
Summary of the trade-offs
- Equals is exact but needs one condition per address.
- Begins with is short, but it works on characters and can match more than you mean unless the text ends at a dot.
- Ends with and contains match unrelated addresses easily and rarely suit this task.
- CIDR is exact and compact for blocks you own, and it works for IPv6.
- Regex handles odd ranges, but it is easy to get subtly wrong, so anchor it, escape dots, and test the boundaries.
Exercise
Your office owns the public block 203.0.113.0 to 203.0.113.15, and a colleague proposes the condition "IP address begins with 203.0.113.1". Which addresses does that wrongly include or leave out, and which condition would you use instead?
Show answer
"Begins with 203.0.113.1" leaves out 203.0.113.0 and 203.0.113.2 through 203.0.113.9, because they do not start with that text. It also wrongly includes addresses such as 203.0.113.100 through 203.0.113.199, which are outside your block. The block has 16 addresses, which is a /28, so use "IP address is in range (CIDR notation)" with 203.0.113.0/28.
Exercise
A regular expression rule reads 203.0.113.5 with no anchors and unescaped dots. Name two problems and write a corrected pattern that matches only 203.0.113.5.
Show answer
First, the dots are unescaped, so each one matches any character. Second, without ^ and $ the pattern also matches longer addresses that contain the text, such as 203.0.113.50 or 203.0.113.59. A corrected pattern is ^203\.0\.113\.5$. For a single address, "IP address equals 203.0.113.5" is simpler and safer.