Skip to main content
Drupal is a registered trademark of Dries Buytaert
Release: OpenID Connect / OAuth client 3.0.0-alpha9 New alpha version released for module openid_connect (3.0.0-alpha9). Usage Milestone: Google Analytics Module google_analytics crossed 1,000 active installs. Release: Timelinr 1.0.1 Minor update available for module timelinr (1.0.1). Release: GraphQL Compose Codegen 1.1.2 Minor update available for module graphql_compose_codegen (1.1.2). Release: Mapy.com 1.1.3 Minor update available for module mapycom (1.1.3). Release: Ckeditor5 entity browser 3.0.3 Minor update available for module ckeditor5_entity_browser (3.0.3). Release: Ckeditor5 entity browser 3.0.1 Minor update available for module ckeditor5_entity_browser (3.0.1). Release: Ckeditor5 entity browser 3.0.2 Minor update available for module ckeditor5_entity_browser (3.0.2). Release: Teamleader Integration 4.0.2 Minor update available for module teamleader (4.0.2). Module Revived: Entityqueue Buttons 1.1.2 Module entityqueue_buttons updated after 8 months of inactivity (1.1.2).

Google Tag Manager: Events

283 sites Security covered Drupal 9–11
View on drupal.org

This module provides an API to push events to your Google Tag Manager data layer directly from PHP. It allows you to trigger events with custom data after a page loads, enabling richer tracking of user interactions.

INTRODUCTION

The Google Tag Manager: Events module provides API to push events
to GTM Datalayer from PHP.

REQUIREMENTS

This module requires Google Tag module.

2.x supports Google Tag 8.x-1.x only.
3.x supports Google Tag 2.0.x only.

INSTALLATION

Install as you would normally install a contributed Drupal module. Visit:
https://www.drupal.org/docs/extending-drupal/installing-modules
for further information.

CONFIGURATION

To push the events via Google Tag Manager: Events module you must
configure at least one GTM container.

FOR DEVELOPERS

You can see gtm_events_test module for usage examples.

The goal

You can push an event directly from PHP code. It means that event will
be pushed after page loading in browser.

How to push event

To push event you can use 'google_tag_events' service.

google_tag_events_service()->setEvent(
  'some_event_name',
  [
    'event' => 'some_event_name'
    'foo' => 'bar'
  ]
);

After this code executing will be pushed event like

dataLayer.push({
  'event': 'some_event_name',
  'foo': 'bar'
});

Debug mode

You can test GTM events pushing without any configured GTM containers.
Just enable debug mode on config page /admin/config/system/google-tag/events/settings.

How to check

You can use recommended browser extensions to check datalayer:

Event plugin

You can incapsulate the event data preparation code to event plugin.
Plugin must be placed into
tests/modules/gtm_events_test/src/Plugin/google_tag_event directory.

For example:

/**
 * Node node page visit GTM event plugin.
 *
 * @Plugin(
 *   id = "example_event_node_view",
 *   label = @Translation("Node page visit event")
 * )
 */
class ExampleEventNodeView extends GoogleTagEventsPluginBase {

  /**
   * {@inheritdoc}
   */
  public function process(array $data = NULL) {
    $data = $data ?? $this->data;

    $event_data = [
      'event' => 'node_view',
      'title' => $data['node']->getTitle(),
    ];

    return $event_data;
  }

}

Then call the service

/**
 * Implements hook_entity_view().
 */
function comment_entity_view(
    array &$build,
    EntityInterface $entity,
    EntityViewDisplayInterface $display,
    $view_mode
  ) {
  if ($entity instanceof NodeInterface) {
    google_tag_events_service()->setEvent(
      'example_event_node_view',
      ['node' => $entity]
    );
  }
}

The result of this code will be

dataLayer.push({
  'event': 'node_view',
  'title': 'Some node title'
});

A plugin name must match with event name to call plugin's process method.

MAINTAINERS

Depends on

Dependencies of the latest stable release

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
3
Tracked since
Jun 2025
Latest release
1 month ago
Releases (12 mo)
2 ▲ from 1
Maintenance
Active

Release Timeline

Releases

Version Type Core Release date
4.x-dev Dev 11 Jul 1, 2026
3.2.0 Stable 9–11 Jul 1, 2026
3.1.0 Stable 9–11 Jun 4, 2025