Custom Tealium Event Tracking in Drupal – Behind the Code

As a certified Drupal developer with 8+ years of experience, I’ve worked on many complex integrations. But one recent challenge stood out: implementing custom Tealium event tracking in Drupal in a flexible and scalable way.

This post is a deep dive into how we made it dynamic, configurable, and production-ready — a behind-the-scenes look for developers, architects, and digital marketers who live in the intersection of data and code.


🎯 The Problem

Most tracking scripts are added statically or via GTM. But we needed something more granular:

  • Track clicks on specific hyperlinks or buttons
  • Only fire in production
  • Be configurable by content editors or site builders (no deploys for every tracking change)
  • Allow dynamic events like CTA clicks, outbound links, form submissions


🧠 Our Approach

✅ Step 1: Create a Custom Drupal Module

We built a core module tealium_event, with a submodule gson_tealium_event for project-specific configs.

✅ Step 2: Add a YAML-Based Configuration System

We stored trigger conditions and event data in a YAML file like:

cta_download:
  url_pattern: "/resources/*"
  link_text_contains: "Download Now"
  event_data:
    event_name: "download_cta_click"
    category: "CTA"
    label: "Download Resource"
        

This config made the solution fully extensible — just update YAML, no code changes!


💻 Step 3: Inject JavaScript via Drupal Library

The JS file reads the YAML config (converted to JSON via drupalSettings) and:

  • Listens for click events
  • Matches anchor href & inner text against config
  • Fires the appropriate Tealium utag.link() call with event data

Example:

if (link.textContent.includes(config.link_text_contains)) {
  utag.link({
    event_name: config.event_data.event_name,
    category: config.event_data.category,
    label: config.event_data.label
  });
}
        

🔐 Step 4: Add an Admin Config Form

We added a Drupal config form under Configuration > Development > Tealium Events, allowing admins to:

  • Enable/disable tracking
  • Restrict to production only
  • Preview parsed event data


🌐 Step 5: Restrict to Production via Environment Variable

We used environment-specific settings:

$config['tealium_event.settings']['enabled'] = getenv('TEALIUM_ENABLED') ?: FALSE;
        

This gave us control per environment (dev, stage, prod) without risking dirty data.


🚀 Results

✅ Fully dynamic Tealium tracking ✅ Configurable via YAML + UI ✅ Zero deployments for new event tags ✅ Clean separation between logic and data ✅ Marketing team happy, dev team happier 😊


🧩 Learnings

  • YAML + JavaScript is powerful when used well in Drupal
  • Always decouple logic and configuration
  • Tealium is flexible, but integration needs solid planning
  • UI toggles and environment flags are a must-have for production-ready tracking


🗣️ Over to You

Have you implemented something similar? Would love to hear how you’re handling tracking in Drupal or other frameworks.

Let’s geek out on clean code + data + digital experience! Feel free to drop your thoughts or questions below 👇

#Drupal #Tealium #WebAnalytics #JavaScript #DrupalDeveloper #TagManagement #DigitalMarketing #WebDevelopment #OpenSource #Drupal10

To view or add a comment, sign in

More articles by Pradip Mehta

Insights from the community

Others also viewed

Explore topics