Set Up Google Tag Manager: Tags, Triggers, and dataLayer | OpsBlu Docs

Set Up Google Tag Manager: Tags, Triggers, and dataLayer

Step-by-step GTM implementation guide covering container installation, dataLayer syntax, tag/trigger architecture, GA4 events, and debug mode.

What Google Tag Manager Does

Google Tag Manager (GTM) is a tag management system that lets you deploy and update marketing and analytics tags on your website without editing source code. Instead of hardcoding pixels and scripts, you configure them through the GTM web interface and publish changes through versioned containers.

Install the Container Snippet

Every GTM container has two snippet blocks. Both are required.

Head snippet -- place as high in <head> as possible:

<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXXX');</script>
<!-- End Google Tag Manager -->

Body snippet -- place immediately after the opening <body> tag:

<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->

Replace GTM-XXXXXXX with your container ID from the GTM admin panel.

The dataLayer

The dataLayer is a JavaScript array that acts as the communication bridge between your website and GTM. Your site pushes structured data into it; GTM reads that data to decide which tags to fire and what values to pass.

Initializing the dataLayer

Declare the dataLayer before the GTM snippet so early pushes are not lost:

<script>
  window.dataLayer = window.dataLayer || [];
  dataLayer.push({
    'pageType': 'product',
    'userLoggedIn': true,
  });
</script>
<!-- GTM head snippet goes here -->

Push Syntax for Events

Use dataLayer.push() to send events and their associated data:

dataLayer.push({
  'event': 'add_to_cart',
  'ecommerce': {
    'currency': 'USD',
    'value': 29.99,
    'items': [{
      'item_id': 'SKU-456',
      'item_name': 'Running Shoes',
      'price': 29.99,
      'quantity': 1
    }]
  }
});

The event key is what GTM triggers listen for. Everything else becomes available as dataLayer variables inside GTM.

Tag, Trigger, and Variable Architecture

GTM is built on three primitives:

Tags are the scripts you want to execute (GA4 events, Meta Pixel, LinkedIn Insight Tag, custom HTML). A tag does nothing until a trigger fires it.

Triggers define when a tag fires. They listen for specific conditions: page loads, clicks, form submissions, custom events from the dataLayer, timers, or scroll depth thresholds.

Variables supply dynamic values to tags and triggers. They pull data from the dataLayer, URL, cookies, DOM elements, or JavaScript expressions.

Built-in Variables vs. User-Defined Variables

GTM provides built-in variables like Page URL, Click URL, Click Text, and Referrer. Enable them under Variables > Built-In Variables.

User-defined variables are ones you create. The most common types:

Variable Type Use Case
Data Layer Variable Read values from dataLayer.push() calls
1st Party Cookie Read cookie values like _ga or session IDs
JavaScript Variable Read global JS variables like document.title
Custom JavaScript Run a function that returns a computed value
Constant Store static values like measurement IDs
Lookup Table Map one value to another (e.g., hostname to GA4 ID)

Common Triggers

Page View Triggers

  • All Pages -- fires on every page load. Used for base tracking tags like GA4 config.
  • Some Pages -- fires when conditions match, such as Page Path contains /thank-you.
  • DOM Ready -- fires after the DOM is fully parsed. Use this when your tag depends on elements being present.
  • Window Loaded -- fires after all resources (images, scripts) finish loading.

Click and Form Triggers

  • All Clicks / Just Links -- fires on click events. Filter by Click URL, Click Text, or CSS selector.
  • Form Submission -- fires when a form is submitted. Enable "Check Validation" to wait for the form to actually submit successfully.

Custom Event Trigger

Fires when a specific event value appears in the dataLayer:

Trigger type: Custom Event, Event name: add_to_cart. This matches any dataLayer.push({ 'event': 'add_to_cart' }) call.

Setting Up a GA4 Event Tag

  1. Create a new tag. Choose Google Analytics: GA4 Event.
  2. Set the Measurement ID (G-XXXXXXX) or reference a Constant variable.
  3. Enter the Event Name (e.g., purchase).
  4. Add Event Parameters by mapping parameter names to GTM variables (e.g., value mapped to a dataLayer variable pulling ecommerce.value).
  5. Set the trigger to the Custom Event trigger you created.

For the base GA4 pageview, create a GA4 Configuration tag (or a GA4 Event tag with no event name override) triggered on All Pages.

Preview and Debug Mode

Click Preview in the top-right of the GTM interface. This opens Tag Assistant in a new tab connected to your site.

Tag Assistant shows:

  • Which tags fired on each event (and which did not)
  • The dataLayer state at every point in the page lifecycle
  • Variable values resolved at the time each trigger evaluated
  • The order tags executed in

Use this to verify that your triggers match the right conditions and your variables resolve to expected values before publishing.

Publishing and Version Control

When your changes are tested, click Submit to create a new version. Add a version name and description explaining what changed. GTM keeps a full version history, and you can roll back to any previous version instantly.

Workspaces

GTM supports multiple workspaces so different team members can work on changes simultaneously without conflicts. Each workspace is an independent draft. When you publish one workspace, others will see a notification to sync.

Common Pitfalls

  • Missing dataLayer declaration -- if you push events before the dataLayer array exists, those events are lost. Always declare window.dataLayer = window.dataLayer || [] before the GTM snippet.
  • Tag sequencing issues -- if Tag B depends on Tag A loading first, use tag sequencing (Tag A setup tag) rather than relying on trigger order.
  • Too many All Pages triggers -- firing heavy tags on every page load hurts performance. Use specific triggers whenever possible.
  • Not using Preview mode -- publishing untested changes to production is the most common source of tracking outages.