The problem with raw data
GA4 records what your site sends it: a page address, a file name, a campaign name. However, these raw values are rarely readable in a report. They need to be sorted into meaningful categories, with a method suited to your volume and your needs.
2 methods can be used to write this classification:
- Rules in Google Tag Manager (GTM): the value is calculated in the visitor's browser, at collection time.
- GA4 Data import: the value is linked later, from a CSV file.
The typical classification case
Most of the time, it means classifying downloaded documents by type (manual, pricing, warranty), pages by section or campaigns by channel. At first, a few rules are enough, then exceptions arrive and the list grows.
Method 1: rules in GTM
GTM offers 2 main variables to write a classification.
RegEx Table
The RegEx Table variable compares an input variable (a URL, a file name, a click text) to a list of regular expressions, that is, expressions that recognise many spelling variants, which is what makes them useful. Each row links an expression to an output value. By default, the expression must match the whole string (Full Match option) and case is ignored. In addition, a default value covers the cases with no match and capture groups can be reused in the output ($1, $2).
The order of the rows sets the priorities: place the most specific rules first.
Lookup Table
The Lookup Table variable links an input value to an output value by exact match. It is easy to read back. However, it accepts no variants: a URL that differs by a single character is not recognised.
The limits common to both variables
- The logic lives in the container. The rules are mixed in with tags and triggers, so a table of 200 rows becomes hard to read in the interface.
- Every change requires a published version. Edit, preview mode, QA, publish: fixing a typo takes the same path as new tracking.
- The calculation runs in the visitor's browser. The variable is evaluated on every event where it is used, so it weighs on the site, in a negligible way if the rules are few.
- The value is frozen at collection. A faulty rule leaves wrong values in the data already collected.
Method 2: GA4 Data import with a CSV file
Data import (type Custom event data) replaces the rule with a mapping table: 1 CSV file, 1 row per key value. GA4 links the file to its data when the report is opened, so the history is reclassified too. It has other strengths:
- The file can hold thousands of rows.
- Nothing runs on the visitor's side and the GTM container stays light.
- An update is just a new upload of the file, manual or scheduled by SFTP, with no version to publish.
In return, Data import has its limits:
- A purely exact match. This means writing many rows before the import.
- A delay. Imported data can take up to 24h to appear.
- GA4 only. Other web analytics solutions (Piano, Adobe) and Ads or Meta tags do not see the file. The value is also unavailable in audiences and in exploration segments.
The same rule written both ways
The rule: classify a downloaded document by type. Pricing aimed at professionals (/pro/ folder) comes before general pricing.
In GTM, with a RegEx Table
Input variable: the click URL (built-in variable Click URL). Full Match enabled, hence the .* on both sides. Default value: Other.
| Order | Expression | Output |
|---|---|---|
| 1 | .*/pro/.*pricing.* | Professional pricing |
| 2 | .*pricing.* | Pricing |
| 3 | .*(manual|guide).* | Manual |
| 4 | .*warranty.* | Warranty |
In Data import
A simple mapping table:
| file_name | document_type |
|---|---|
| pro-pricing-2024.pdf | Professional pricing |
| pricing-2024.pdf | Pricing |
| boiler_manual_v3.pdf | Manual |
| warranty-conditions.pdf | Warranty |
Priority becomes unnecessary, since each file has its own row.
GTM rules or Data import: the comparison
| Criterion | GTM rules (RegEx, Lookup) | GA4 Data import |
|---|---|---|
| Type of condition | Expression or exact match | Exact match on the key |
| Unknown values | Classified by expression or by default | (not set) until a row is added |
| Number of rules | A few dozen | Thousands of rows |
| Update | GTM version to publish | New upload of the file |
| History | Value frozen at collection | Also applied to past events |
| Value usable in GA4 audiences, BigQuery and Ads or Meta tags | Yes | No, limited to GA4 reports and explorations |
| Who maintains it | GTM profile | Marketing or content team |
How to choose between GTM and Data import
- The number of rules. Up to a few dozen rules, a RegEx Table stays readable. Beyond that, the CSV file has the advantage.
- Values never seen. An expression classifies new values, whereas a file requires one row per value.
- How the value is used. A need to reclassify history points to Data import. A value wanted in audiences, in BigQuery or in Ads tags points to GTM.
- The person who maintains it. A rule changed every week by the content team is better kept in a CSV. A stable rule, held by the person who manages the container, stays in GTM.
Finally, the 2 methods can be combined: GTM normalises the key (file name in lowercase, parameters removed) and Data import enriches it with titles, themes and needs.
Tools
- Google Tag Manager: RegEx Table, Lookup Table, preview mode.
- Google Analytics 4: Custom definitions, Data import, explorations.
- Sheets or Excel: maintaining the CSV file.
Choosing where to house a classification rule is part of the tracking expertise.