Using regex tables in Google Tag Manager (GTM) can significantly enhance your ability to manage tags with advanced conditions and mappings. Regex (regular expressions) allows you to create flexible and powerful patterns for matching data. Here’s a detailed guide on how to use regex tables for advanced tag management in GTM:
Table of Contents
Toggle1. Access Google Tag Manager
- Sign in to Google Tag Manager:
- Navigate to Google Tag Manager and sign in with your Google account.
- Select Your Container:
- Choose the container where you want to implement the regex table or create a new one if you don’t have a container set up yet.
2. Enable Built-In Variables
To work effectively with regex tables, you might need certain built-in variables to be enabled.
- Navigate to Variables:
- In the GTM workspace, click on
Variablesin the left-hand menu.
- In the GTM workspace, click on
- Enable Click Variables:
- Click
Configureunder Built-In Variables. - Ensure variables such as
Click URL,Click Text,Page URL, and other relevant variables are checked. These will be used as input values in your regex table.
- Click
3. Create a Regex Table Variable
- Add a New User-Defined Variable:
- Click on
Newunder User-Defined Variables.
- Click on
- Choose Variable Type:
- Click
Variable Configuration. - Select
Lookup Table.
- Click
- Configure the Lookup Table:
- Name your variable (e.g., “Advanced Regex Lookup Table”).
- Define Input Variable:
- Select the input variable (e.g.,
Page URLorClick URL).
- Select the input variable (e.g.,
- Add Lookup Table Rows:
- Add rows for each regex pattern you want to use. In the
Inputcolumn, enter your regex patterns. In theOutputcolumn, enter the values you want to map to.
- Add rows for each regex pattern you want to use. In the
- Enable Regex Matching:
- Check the box that says
Enable regex matching.
- Check the box that says
- Save the Variable:
- Click
Saveto save your regex table variable.
- Click
Example Regex Table Configuration:
| Input (Regex Pattern) | Output |
|---|---|
| ^/products/.* | Products Page |
| ^/services/.* | Services Page |
| facebook.com | Social Media |
| twitter.com | Social Media |
| instagram.com | Social Media |
| ^(?!.yourdomain.com).$ | External Link |
4. Use the Regex Table Variable in a Tag
- Create or Edit a Tag:
- Navigate to
Tagsin the left-hand menu. - Click on
Newto create a new tag or select an existing tag to edit.
- Navigate to
- Set Up Tag Configuration:
- Click on
Tag Configurationand select the type of tag you want to configure (e.g.,Google Analytics: GA4 EventorUniversal Analytics).
- Click on
- Use the Regex Table Variable:
- In the tag configuration fields, use the regex table variable. For example, you can set the event category, action, or label to use the regex table variable by selecting it from the variable list.
Example for Google Analytics Tag (GA4):
- Event Name:
page_category - Event Parameters:
Parameter Name:category_name,Value:{{Advanced Regex Lookup Table}}
5. Test Your Setup
- Enter Preview Mode:
- Click
Previewin the GTM workspace to enter preview mode. - Enter your website’s URL in the prompt to open your site with GTM’s debug pane enabled.
- Click
- Test the Tags:
- Navigate through your website and perform actions that should trigger your tags.
- In the GTM debug pane, check that the regex table variable resolves correctly and that the tag fires with the expected values.
- Verify in Google Analytics:
- Go to Google Analytics to verify that the events are being tracked correctly.
- For GA4, check the
Eventssection underRealtime. - For Universal Analytics, go to
Real-Time>Events.
6. Publish Your Changes
- Submit Your Changes:
- Once you’re satisfied with the setup and have thoroughly tested it, click
Submitin GTM.
- Once you’re satisfied with the setup and have thoroughly tested it, click
- Publish Your Container:
- Add a version name and description, then click
Publishto make your changes live.
- Add a version name and description, then click
Example Use Cases for Regex Tables:
Use Case 1: Tracking Specific Page Categories
You want to track different sections of your site, such as product and service pages.
- Regex Patterns:
/products/.*to match any URL under the products section./services/.*to match any URL under the services section.
- Outputs:
Products PageServices Page
- Implementation:
- Use these patterns in your regex table and map them to meaningful names.
- In your Google Analytics tag, set the event parameter
page_categoryto use the regex table variable.
Use Case 2: Custom Event Tracking for Outbound Links
You want to differentiate between clicks on social media links and other external links.
- Regex Patterns:
facebook\.comtwitter\.cominstagram\.com^(?!.*yourdomain\.com).*$to match any external link not containing your domain.
- Outputs:
Social MediaExternal Link
- Implementation:
- Use these patterns in your regex table.
- In your event tracking tag, set a custom event parameter (e.g.,
link_type) to use the regex table variable.
Tips for Using Regex Tables:
- Testing Patterns:
- Use tools like regex101.com to test and validate your regex patterns before implementing them in GTM. This can help ensure that your patterns are correct and will match the expected input values.
- Complex Mappings:
- If you have complex requirements, consider breaking down your regex patterns into simpler ones and using multiple regex tables. This approach can make your setup easier to manage and debug.
- Performance Considerations:
- Ensure your regex patterns are efficient. Poorly optimized patterns can slow down your tracking setup, especially if they are applied to high-traffic variables like
Page URL.
- Ensure your regex patterns are efficient. Poorly optimized patterns can slow down your tracking setup, especially if they are applied to high-traffic variables like
- Documentation:
- Keep good documentation of your regex patterns and what they are intended to match. This will help you and others understand the purpose of each pattern and make maintenance easier.
By following these detailed steps and tips, you can effectively use regex tables in Google Tag Manager to handle advanced tag management scenarios. This will enable more precise and flexible data collection, allowing you to gain deeper insights into user behavior on your website.
