Skip to content
Learni
View all tutorials
Outils Web

How to Set Up Google Tag Manager in 2026

Lire en français

Introduction

Google Tag Manager (GTM) is the essential tool in 2026 for centrally managing all your tracking tags, ad pixels, and analytics scripts without editing your site's source code every time. Imagine: instead of manually adding Google Analytics, Facebook Pixel, or Hotjar to every page, you deploy them through an intuitive interface. This speeds up rollouts, reduces errors, and simplifies A/B testing.

Why is it crucial now? With GDPR updates and the end of third-party cookies, GTM enables server-side consent mode and fine-grained first-party data management via dataLayer. This beginner tutorial guides you from A to Z: creating a container, installing on a simple HTML site, setting up a GA4 tag, triggers, and variables. By the end, you'll master preview/debug for live validation. Estimated time: 30 minutes. Ready to supercharge your analytics? (128 words)

Prerequisites

  • A free Google account (Gmail).
  • A test website (static HTML or CMS like WordPress).
  • Basic HTML/JS knowledge (copy-paste is enough).
  • Chrome browser with Developer Tools.
  • Tools: code editor (VS Code) and local server (Live Server extension).

Create a local test project

terminal.sh
mkdir gtm-test-site
cd gtm-test-site
npm init -y
npm install -D live-server
npx live-server --port=3000

This script sets up a project folder, installs Live Server to simulate a local HTTPS/HTTP site, and launches it on port 3000. Why? GTM requires a domain for preview testing. Avoid CORS errors by using a local server instead of file://.

Step 1: Create your GTM container

Head to tagmanager.google.com and sign in with your Google account. Click Create Account > name it (e.g., "My Test Site"), then Continue. Choose your container: name "GTM-SiteTest", type Web, and Create. GTM generates two JS snippets: one for and one for . Copy them carefully—they contain your Container ID (format GTM-XXXXXX). Analogy: the container is like a garage where you park your tags; without it, nothing works.

Install GTM in the head

index-head.html
<!DOCTYPE html>
<html lang="fr">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Site Test GTM</title>
  <!-- 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-XXXXXX');</script>
  <!-- End Google Tag Manager -->
</head>

This full snippet goes in : it asynchronously loads GTM and initializes dataLayer. Replace GTM-XXXXXX with your real ID. Pitfall: don't place it before other critical scripts, as dataLayer must be ready for pushes.

Install GTM in the body

index-body.html
<body>
  <h1>Bienvenue sur mon site test GTM !</h1>
  <button id="bouton-clic">Cliquez-moi</button>

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

  <script src="script.js"></script>
</body>
</html>

Add this block right after for users with JS disabled (noscript fallback). The button will trigger a custom event. Complete with . Avoid placing it too low: GTM needs to initialize early.

Step 2: Enable Preview and Debug mode

In GTM, click Preview (triangle icon). Enter your local URL http://127.0.0.1:3000. A new tab opens with a debug bar at the bottom. Reload the page: you'll see tags "Initializing". Click the button: check events in the GTM console. Tip: if nothing shows, verify the container ID and snippets. It's your best friend for debugging without publishing.

Push data to dataLayer

script.js
document.getElementById('bouton-clic').addEventListener('click', function() {
  window.dataLayer = window.dataLayer || [];
  window.dataLayer.push({
    'event': 'bouton_clique',
    'bouton_id': 'bouton-clic',
    'user_id': 'user123',
    'valeur': 10
  });
  console.log('Événement pushé dans dataLayer');
});

This script listens for the click and pushes a custom event to dataLayer, accessible to all GTM tags. Check in Preview: the event appears. Common pitfall: forgetting window.dataLayer = window.dataLayer || [] before push, causing errors if not initialized.

Step 3: Create your first variable

Variables > New > User-Defined Data Layer Variable. Name: "Button ID". Data Layer Variable Name: bouton_id. Save. This pulls the value from the dataLayer push. Analogy: a variable is a "shortcut" to reuse dynamic data everywhere (tags, triggers).

Page View trigger configuration

trigger-pageview.json
{
  "name": "All Pages",
  "type": "pageview",
  "filter": [
    {
      "parameter": [
        {
          "key": "pagePath",
          "type": "template",
          "value": "{{Page Path}}"
        }
      ]
    }
  ]
}

This JSON represents a standard "All Pages" trigger (copied from GTM export). It fires on every pageview. Use it for global tags like GA4. Pitfall: misconfiguring filters, which blocks the trigger.

Step 4: Add a Google Analytics 4 tag

Tags > New > Google Analytics: GA4 Event. Config: Measurement ID (G-XXXXXX from GA4), Event Name page_view. Trigger: All Pages. Test in Preview: tag fires on each page. For the custom event: new tag, Event Name bouton_clique, parameter bouton_id via variable.

Custom HTML tag for testing

tag-custom.html
<script>
  console.log('Tag custom GTM exécuté !');
  console.log('Données dataLayer:', {{bouton_id}});
</script>

<!-- Pixel de test -->
<img src="https://www.google-analytics.com/collect?v=1&tid=GA_TEST&t=pageview&dl={{Page URL}}" width="1" height="1" style="display:none;">

Create a Custom HTML tag, paste this code. Trigger on bouton_clique. {{bouton_id}} injects the variable. Perfect for testing pixels without real GA. Avoid infinite loops by not pushing dataLayer from the tag.

Step 5: Publish your container

Submit > Publish. Confirm: live version deployed! Check on your site without Preview. In GA4 Realtime, see the page_view hits. Congratulations, GTM is live.

Best practices

  • Always use Preview before publishing to avoid broken tags in production.
  • Version control: name workspaces (e.g., "V1-GA4") for easy rollbacks.
  • Consent Mode: integrate a CMP like Cookiebot for GDPR.
  • DataLayer first: push all custom data before tags.
  • Limit tags: max 20 per page for performance (Core Web Vitals).

Common errors to avoid

  • Wrong container ID: double-check snippets; Preview won't connect.
  • Uninitialized dataLayer: add the || [] check to all pushes.
  • Overly broad triggers: use variables to filter (e.g., {{Page Path}} contains /blog).
  • Forgot noscript: 1-5% of users with JS off miss tags.

Next steps