Google Tag Manager API for Drupal
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.


Google Tag Manager API Module for Drupal by In the Loft Studios



  1. Download and unzip this module into your modules directory.
  2. Goto Administer > Site Building > Modules and enable this module.
  3. Install the chrome extension for testing.


  1. Make sure you have created your account at
  2. Visit admin/config/search/loft-gtm to configure.
  3. Read the quick start guide

Background Learning

Learn more about Google Tag Manager with these links.


By default the code will be spit out using $page['content']['loft_gtm']. This may not place the code where you want it, just after the body tag.

As a second option you can adjust the settings $conf['loft_gtm_theme_support'] and then do the following where $loft_gtm is provided to html.tpl.php.

You will want to do something like this to html.tpl.php

<body class="<?php print $classes; ?>" <?php print $attributes; ?>>
<?php print render($loft_gtm) ?>
<div id="skip-link">
    <a href="#main-content"...


The point of Google Tag Manager is to give away control to the Google UI, rather than the Drupal UI, however some tasks remain for the developer to do. For these tasks read on.


  1. Drupal.loftGTM.dataLayer.push (use this over dataLayer.push for drupal integration)
  2. Drupal.loftGTM.dataLayer.event
  3. loft_gtm_queue_add()

For form submission events

You will add to the queue in the submission hook of a form; the queue will then be emptied on the next page load and the events thus fired.

Here is some example code to send an event based on the contact id of the contact form:

 * Implements hook_form_alter().
function my_module_form_contact_site_form_alter(&$form, $form_state) {
  // Google Tag Manager
  $form['#submit'][] = '_my_module_form_contact_site_form_submit';

 * Form submission handler
function _my_module_form_contact_site_form_submit($form, &$form_state) {
  // Google Tag Manager
  $cid = $form_state['values']['cid'];
  $event_label = $form['cid']['#options'][$cid];
  $event = array (
    '#method' => 'push',
    '#params' =>
    array (
      'event' => 'eventTracker',
      'eventCat' => 'Contact',
      'eventAct' => 'Request',
      'eventLbl' => $event_label,

Usage via Javascript

  1. Simply call one function:

     Drupal.loftGTM.dataLayer.event(event, category, action, label, value);

Usage via PHP

  1. Anytime you want to output a GTM event in HTML you will use loft_gtm_queue_add() to add the event. All events added during processing will be written in html on the next output.
  2. Important to note that the queue is cleared via ajax, so repeatedly viewing source will show you a growing queue. That is inaccurate and only occurs because the ajax is not clearing the queue.

Development servers

It's probably a good idea to do something like this in a dev only settings file:

$conf['loft_gtm_enabled'] = false;
$conf['loft_gtm_logging'] = true;
//$conf['loft_gtm_code'] = '<!--Google Tag Manager Disabled for Dev-->';


  • In the Loft Studios
  • Aaron Klump - Developer
  • PO Box 29294 Bellingham, WA 98228-1294
  • aim: theloft101
  • skype: intheloftstudios
  • d.o: aklump