Skip to content

growthbook/gtm-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GrowthBook Feature Flags and Experimentation - GTM Template

Official Google Tag Manager template for GrowthBook, the open-source feature flagging and A/B testing platform.

Features

  • 🚀 Load the GrowthBook SDK without writing code
  • 📊 Automatic experiment tracking to dataLayer (GA4 compatible)
  • 🔒 Support for encrypted SDK payloads
  • 🏠 Works with GrowthBook Cloud and self-hosted instances
  • 🪣 Optional sticky bucketing for advanced use cases

Installation

From the Community Template Gallery

  1. In Google Tag Manager, go to TemplatesSearch Gallery
  2. Search for "GrowthBook"
  3. Click Add to workspace

Manual Installation

  1. Download template.tpl from this repository
  2. In GTM, go to TemplatesNew
  3. Click the three-dot menu (⋮) → Import
  4. Select the downloaded file

Configuration

Required Fields

Field Description
Client Key Your GrowthBook SDK client key (starts with sdk-). Find this in GrowthBook under SDK ConfigurationSDK Connections.

Optional Fields

Field Description
API Host For self-hosted GrowthBook instances. Leave blank for GrowthBook Cloud.
Decryption Key Required only if you enabled encryption in your SDK Connection settings.
Sticky Bucketing Advanced feature to preserve variation assignments when modifying experiments mid-flight or using multi-arm bandits. Options: Disabled, Cookie, or Local Storage.

Usage

  1. Create a new tag using this template
  2. Enter your Client Key
  3. Set the trigger to All Pages (or your preferred pages)
  4. Save and publish

Experiment Tracking

The GrowthBook SDK automatically pushes experiment_viewed events to the dataLayer:

{
  "event": "experiment_viewed",
  "experiment_id": "your-experiment-key",
  "variation_id": "control"
}

Forwarding to GA4

Quick Setup (Recommended)

Import our pre-built configuration to automatically create the required variables, trigger, and GA4 tag:

  1. Download ga4-tracking-config.json from this repository
  2. In GTM, go to AdminImport Container
  3. Select the JSON file
  4. Choose MergeRename conflicting tags, triggers, and variables
  5. Click Confirm
  6. Go to Tags and open "GA4 Event - GrowthBook Experiment Viewed"
  7. Replace G-XXXXXXXXXX with your GA4 Measurement ID
  8. Save and publish

Note: The GrowthBook tag template must be installed separately from the Community Template Gallery — container imports cannot include custom templates.

Manual Setup

If you prefer to set it up manually:

  1. Create a Data Layer Variable for experiment_id
  2. Create a Data Layer Variable for variation_id
  3. Create a Custom Event Trigger for experiment_viewed
  4. Create a GA4 Event Tag that fires on this trigger with the experiment parameters

See our full GTM + GA4 setup guide for detailed instructions.

Using Feature Flags

To use feature flags, create a separate Custom HTML tag with your logic. The growthbook_queue ensures your code runs after the SDK loads:

<script>
window.growthbook_queue = window.growthbook_queue || [];
window.growthbook_queue.push(function(gb) {
  
  function applyFeatureFlags() {
    // Example: Add a class when feature is enabled
    if (gb.isOn("show-discount")) {
      var el = document.getElementById("price");
      if (el) el.classList.add("show-discount");
    }

    // Example: Use a string feature value
    var buttonColor = gb.getFeatureValue("button-color", "blue");
    var btn = document.getElementById("cta");
    if (btn) btn.style.backgroundColor = buttonColor;
  }

  // Apply on initial load
  applyFeatureFlags();

  // Re-apply when features update via real-time streaming (SSE)
  document.addEventListener("growthbookdata", applyFeatureFlags);
});
</script>

Resources

Support

License

Apache 2.0

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages