[GA4] Modify and generate events using regular expressions

For web data streams, you can modify and generate a new event in Google Analytics using a regular expression (regex).

To avoid using malformed regex that may impact your site performance, follow these best practices:

  • Before you use regex, use other operators where possible (e.g., exact matches, contains).
  • Test your regex against expected inputs and ensure that it's matching what you expect.
  • If case sensitivity is important, specify your regex to be case sensitive. Use the option that doesn't say "(ignore case)".

Google Analytics 4 executes Event Creation and Modification rules directly on the browser. As a result, certain types of invalid regex run the risk of decreased website performance.

The browser’s implementation of JavaScript regex uses backtracking to try all possible execution paths when evaluating a string against a regular expression. Complex or poorly written regex can cause significant issues when run in certain browsers, and lead to site breakage and computer crashes. This type of outcome can be caused by either the regex in your Event Create/Modify rule or the strings that your rule is matching against.

Best practices to avoid catastrophic backtracking

  • Avoid unbounded or nested repetitions where matching time can increase exponentially. Define a maximum number of expected repetitions.

Was this helpful?

How can we improve it?
Search
Clear search
Close search
Google apps
Main menu