Sub topic 4
Defining an internal traffic rule
This chapter assumes you have a GA4 property with a web data stream, Editor or Administrator access, and a basic grasp of events and sessions. You also need to know the public IP addresses your office or home network uses to reach the internet. By the end you will be able to define a rule that tags internal events with a traffic_type value, and you will know what the rule does and does not do on its own.
What an internal traffic rule is
An internal traffic rule is a setting on a web data stream. It says: "When an event arrives from an IP address that matches this condition, add the parameter traffic_type to that event with this value."
The rule does not remove anything. It only labels events. Removal is a separate step, done by a data filter at the property level. The next chapter covers that filter. Until you create one and make it active, tagged events still appear in your reports.
The two pieces fit together like this:
- The rule marks the event (
traffic_type = internal). - The data filter reads the mark and decides whether to exclude the event.
The two values must match exactly, including case. If the rule writes internal and the filter looks for Internal, nothing is excluded.
Where to find the setting
The rule lives in the data stream's tag settings, not in the property's data filter page.
- Open Admin and select the correct property.
- Under Data collection and modification, choose Data streams, then select your web stream.
- Under Google tag, choose Configure tag settings.
- Under Settings, choose Show all, then Define internal traffic.
- Select Create.
If you manage several web streams, repeat the rule on each one. Rules belong to a stream, so a rule on one stream does not cover another.
The fields of a rule
A rule has four inputs.
| Field | Meaning | Typical value |
|---|---|---|
| Rule name | A label for people. GA4 does not use it for matching. | `Head office` |
| traffic_type value | The string written into the `traffic_type` parameter. | `internal` |
| Match type | How the IP address is compared with your value. | IP address is in range (CIDR notation) |
| Value | The address, range, or pattern to compare against. | `203.0.113.0/24` |
You can add several conditions to one rule. A rule matches an event when any of its conditions match, so you can list a head office and a branch office under one rule name. Alternatively, keep one rule per location, which makes later audits easier to read.
Choosing the traffic_type value
The default value is internal. Keep it unless you have a reason to change it. A different value is useful when you want to separate groups of traffic. For example, you could use office for staff and agency for a contractor, and then build a filter for each.
Two details cause errors here:
- The value is case sensitive.
internalandInternalare different values. - Filters compare the whole value. A filter for
internaldoes not matchinternal_office.
Choosing the match type
GA4 offers several match types for the IP address. The main ones are listed below.
| Match type | Use it when | Example |
|---|---|---|
| IP address equals | You have one fixed address | `203.0.113.25` |
| IP address begins with | Your addresses share a leading part | `203.0.113.` |
| IP address ends with | Rarely useful, only for a shared tail | `.25` |
| IP address contains | Rarely useful, easy to over-match | `0.113` |
| IP address is in range (CIDR notation) | You own a block of consecutive addresses | `203.0.113.0/24` |
| IP address matches regular expression | The addresses do not form a clean block | see below |
Prefer equals for one address and CIDR for a block. They are the easiest to read and the hardest to get wrong. The broad text matches (begins with, ends with, contains) are tempting, but they can sweep in addresses that belong to real visitors.
A CIDR range
CIDR notation describes a block of addresses by its first address and the number of fixed leading bits. Here is a rule value for an office that owns 256 consecutive addresses:
IP address is in range (CIDR notation): 203.0.113.0/24The /24 means the first 24 bits are fixed, so the rule matches every address from 203.0.113.0 through 203.0.113.255. A /32 matches exactly one address. A smaller number such as /16 matches far more, so check the size before you save it.
A regular expression
Use a regular expression when the addresses you want do not form a clean CIDR block. This example matches 203.0.113.1 through 203.0.113.199, and it excludes 203.0.113.0 and everything from .200 upward:
^203\.0\.113\.([1-9]|[1-9][0-9]|1[0-9]{2})$Three habits keep a pattern safe:
- Escape the dots as
\.. An unescaped.matches any character, so203.0.113.5could be matched by a pattern meant for a different address. - Anchor the pattern with
^at the start and$at the end. Without anchors,203\.0\.113\.5also matches203.0.113.50. - Test the pattern against sample addresses before you save it.
Setting traffic_type in code
The rule in tag settings is the normal method, but you can also set the parameter yourself. This is useful when you identify internal users by something other than their IP address, such as a logged-in staff account or a special cookie. Set it on the configuration call:
gtag('config', 'G-XXXXXXXXXX', {
'traffic_type': 'internal'
});Every event from that page load now carries traffic_type = internal. An internal traffic data filter with the same value can then exclude them. Replace G-XXXXXXXXXX with your own measurement ID.
If you use Google Tag Manager, set the same parameter as a field on your Google tag configuration instead of editing page code.
Worked example
Suppose a company has two offices. The head office owns the block 203.0.113.0/24. A small branch office has one address, 198.51.100.7.
You decide on one rule per location.
- Create the rule Head office. Set the
traffic_typevalue tointernal. Choose IP address is in range (CIDR notation) and enter203.0.113.0/24. - Create the rule Branch office. Set the
traffic_typevalue tointernal. Choose IP address equals and enter198.51.100.7. - Save both rules.
Both rules write the same value, so one data filter that looks for internal will cover both offices.
To check the result, open Admin, then DebugView, while you browse the site from the office network. Events should show traffic_type with the value internal. Events from a phone on mobile data should not show it. Keep in mind that DebugView only lists devices that are in debug mode, so enable debug mode in your browser or tag before you look. Later chapters cover the full verification process.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Tagged events never appear as internal | The rule is on a different stream than the one receiving data | Add the rule to the correct stream |
| Filter excludes nothing | The filter value differs from the rule value, for example by case | Make both values identical |
| A colleague at home is not excluded | Their home IP is not in the rule | Use a non-IP method, covered in a later chapter |
| Real visitors vanish from reports | The match type is too broad, such as `contains` or a short CIDR prefix | Narrow the value and re-check the range size |
| Your own IP changed and tagging stopped | The office has a dynamic address | Update the rule, or use a code-based method |
| The regex rule never matches | An unescaped dot, a missing anchor, or a typo | Test the pattern with sample addresses |
Limits to remember
- A rule affects only events collected after you save it. It does not change data GA4 has already processed.
- A rule depends on the IP address GA4 sees. A visitor on a VPN shows the VPN's address, not the office address.
- IPv6 addresses need their own condition. An IPv4 rule never matches them, so add an IPv6 condition if your network uses one.
- The rule alone excludes nothing. You need an active data filter.
Exercise
Your company owns the addresses 192.0.2.0 through 192.0.2.255. You create a rule with the value internal, match type CIDR, and the range 192.0.2.0/24. A week later the reports still include office visits. You then open the data filter page and find a filter that looks for Internal. What is wrong, and how do you fix it?
Show answer
The rule writes internal (lowercase) and the filter looks for Internal. The comparison is case sensitive, so no event matches the filter and nothing is excluded. Change the filter value, or the rule value, so both are identical, for example internal. Also confirm that the filter is not still in Testing state, since a filter in Testing only labels events and does not exclude them.
Exercise
An office has the addresses 203.0.113.1 through 203.0.113.199 and no others. Why might the regular expression 203.0.113.[0-9]+ be a poor choice, and what would you use instead?
Show answer
The dots are not escaped, so each one matches any character. There are no anchors, so the pattern also matches longer strings that merely contain a match, and [0-9]+ accepts any number, including 200 to 255 and values beyond that. Use the anchored, escaped pattern ^203\.0\.113\.([1-9]|[1-9][0-9]|1[0-9]{2})$, or use a CIDR range if the block is clean.