Code Monkey home page Code Monkey logo

google_tag_events's Introduction

Google Tag Manager: Events

INTRODUCTION

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

REQUIREMENTS

This module requires GoogleTagManager module.

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.