Google Analytics Regex Cheat Sheet

Google Analytics uses regular expressions (regex) for pattern matching in various settings such as filters, goals, and segments. Here’s a cheat sheet for some common use cases:

Basic Patterns

  • Match Any Character:
    • . (dot)
    • Example: ca.e matches “case,” “cake,” “cave,” etc.
  • Match Beginning of Line:
    • ^ (caret)
    • Example: ^home matches “homepage” but not “welcome home.”
  • Match End of Line:
    • $ (dollar sign)
    • Example: blog$ matches “myblog” but not “blogging.”

Character Classes

  • Match Any Single Character in a Set:
    • [abc] matches ‘a’, ‘b’, or ‘c’.
    • Example: [aeiou] matches any vowel.
  • Match Any Single Character Not in a Set:
    • [^abc] matches any character except ‘a’, ‘b’, or ‘c’.
    • Example: [^0-9] matches any non-digit.

Quantifiers

  • Match 0 or More Occurrences:
    • * (asterisk)
    • Example: go*gle matches “ggle,” “gogle,” “google,” etc.
  • Match 1 or More Occurrences:
    • + (plus)
    • Example: cat+ matches “cat,” “catt,” “cattt,” etc.
  • Match 0 or 1 Occurrence:
    • ? (question mark)
    • Example: colou?r matches “color” and “colour.”

Anchors

  • Word Boundary:
    • \b
    • Example: \bword\b matches “word” but not “subword.”

Groups and Ranges

  • Grouping:
    • ( )
    • Example: (go)+ matches “gogo,” “gogogo,” etc.
  • Logical OR:
    • |
    • Example: cat|dog matches “cat” or “dog.”
  • Range of Characters:
    • [a-z] matches any lowercase letter.
    • Example: [0-9] matches any digit.

Escape Characters

  • Escape Special Characters:
    • \
    • Example: \. matches a literal period.

Examples

  • Match URL Paths:
    • ^/blog/[\w-]+/$
      • Matches paths like “/blog/my-post/”.
  • Match Subdomains:
  • Match IP Addresses:
    • ^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$
      • Matches IPv4 addresses.
  • Match Query Parameters:
    • utm_.*
      • Matches query parameters starting with “utm_”.

Notes

  • Always test your regex patterns in a safe environment before applying them to your Google Analytics settings.
  • Google Analytics uses a slightly modified version of regular expressions, so refer to the official documentation for any specific nuances.

This cheat sheet covers some common regex patterns applicable to Google Analytics. Adjust patterns based on your specific use case and requirements.