google_tag_events-8.x-1.x-dev/tests/modules/gtm_events_test/gtm_events_test.module

tests/modules/gtm_events_test/gtm_events_test.module
<?php

/**
 * @file
 * Provides GTM events application.
 */

use Drupal\Component\Serialization\Json;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\Entity\Node;

/**
 * Implements hook_help().
 */
function gtm_events_test_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the gtm_events_test module.
    case 'help.page.gtm_events_test':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Tests and examples for google_tag_events module.') . '</p>';

      return $output;
  }
}

/**
 * Implements hook_page_bottom().
 */
function gtm_events_test_page_bottom(array &$page_bottom) {
  $current_uri = trim(\Drupal::request()->getRequestUri(), '/');

  switch ($current_uri) {
    case '':
      google_tag_events_service()->setEvent('gtm_events_test_homepage');
      break;

    default:
      break;
  }

  $current_path = \Drupal::service('path.current')->getPath();

  if (preg_match('/\/node\/(\d+)/i', $current_path, $matches)) {
    /** @var \Drupal\node\Entity\Node $node */
    $node = Node::load($matches[1]);
    $node_type = $node ? $node->getType() : NULL;

    switch ($node_type) {
      case 'article':
        google_tag_events_service()->setEvent('gtm_events_test_article_view');
        break;

      default:
        break;
    }
  }

  $route_name = \Drupal::routeMatch()->getRouteName();

  if ($route_name === 'system.403') {
    google_tag_events_service()->setEvent('gtm_events_test_403');
  }
  elseif ($route_name === 'system.404') {
    google_tag_events_service()->setEvent('gtm_events_test_404');
  }

  if (\Drupal::service('path.matcher')->isFrontPage()) {
    // Push the same event twice.
    google_tag_events_service()->setEvent('gtm_events_test_front', ['event' => 'event_1']);
    google_tag_events_service()->setEvent('gtm_events_test_front', ['event' => 'event_2']);
  }

  $events = google_tag_events_service()->getEvents();

  if (empty($events)) {
    return;
  }

  $page_bottom['gtm_events_test'] = [
    '#type' => 'html_tag',
    '#tag' => 'script',
    '#attributes' => [
      'type' => 'application/json',
      'data-selector' => 'google_tag_events',
    ],
    '#value' => Json::encode($events),
  ];

  google_tag_events_service()->flushEvents();
}

/**
 * Implements hook_page_attachments().
 */
function gtm_events_test_page_attachments(array &$attachments) {
  $attachments['#attached']['library'][] = 'gtm_events_test/tracking';
}

/**
 * Implements hook_preprocess_views_view().
 */
function gtm_events_test_preprocess_views_view(&$variables) {
  if (!isset($variables['id']) || !isset($variables['view'])) {
    return;
  }

  /** @var \Drupal\views\ViewExecutable $view */
  $view = $variables['view'];
  $data = ['view' => $view];

  switch ($variables['id']) {
    case 'test_view':
      google_tag_events_service()->setEvent('gtm_events_test_views_display', $data);
      break;

    default:
      break;
  }

  // Render events to view body to call it after views caching.
  $events = google_tag_events_service()->getEvents();

  if (empty($events)) {
    return;
  }

  $variables['rows'][] = [
    '#type' => 'html_tag',
    '#tag' => 'script',
    '#attributes' => [
      'type' => 'application/json',
      'data-selector' => 'google_tag_events',
    ],
    '#value' => Json::encode($events),
  ];

  // Flush GTM events to exclude handling on page bottom.
  google_tag_events_service()->flushEvents();
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function gtm_events_test_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'test_form') {
    $form['#submit'][] = 'gtm_events_test_form_submit';
    $form['actions']['submit']['#submit'][] = 'gtm_events_test_form_submit';
  }
}

/**
 * Form submit callback.
 *
 * @param array $form
 *   Form array.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   Form state.
 *
 * @throws \Drupal\Component\Plugin\Exception\PluginException
 */
function gtm_events_test_form_submit(array &$form, FormStateInterface $form_state) {
  // Save event data to temp store coz page will be reloaded/redirected
  // after form submit.
  google_tag_events_service()->setEvent('gtm_events_test_form_submit', NULL, TRUE);
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc