You are here

mautic.module in Mautic Integration 8

Drupal Module: Mautic.

Adds the required Javascript to all your Drupal pages to allow tracking by the Mautic statistics package.

File

mautic.module
View source
<?php

/**
 * @file
 * Drupal Module: Mautic.
 *
 * Adds the required Javascript to all your Drupal pages to allow tracking by
 * the Mautic statistics package.
 *
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;

/**
 * Implements hook_help().
 */
function mautic_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.mautic':
      $output = '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Mautic adds a web statistics tracking system to your website.') . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dt>' . t('Configuring Mautic') . '</dt>';
      $output .= '<dd>' . t('All settings for this module can be found on the <a href=":mautic_settings">Mautic settings</a> page. When entering the Mautic code here, it will automatically add the required JavaScript to every page generated.', [
        ':mautic_settings' => Url::fromRoute('mautic.admin_settings_form')
          ->toString(),
      ]) . '</dd>';
      return $output;
    case 'mautic.admin_settings_form':
      return t('<a href=":mautic_url">Mautic</a> is a free website traffic and marketing effectiveness service.', [
        ':mautic_url' => 'https://www.mautic.org/',
      ]);
  }
}

/**
 * Implements hook_page_attachments().
 *
 * Insert JavaScript to the page.
 */
function mautic_page_attachments(array &$page) {
  $config = \Drupal::config('mautic.settings');
  $request = \Drupal::request();

  // Add Mautic tracking code if needed
  $status = NULL;
  if ($exception = $request->attributes
    ->get('exception')) {
    $status = $exception
      ->getStatusCode();
  }
  $trackable_status_codes = [
    '403',
    '404',
  ];
  if ($config
    ->get('mautic_enable') && _mautic_visibility_pages() && !in_array($status, $trackable_status_codes)) {

    // Integrate mautic.
    $mautic_settings = [
      'base_url' => $config
        ->get('mautic_base_url'),
    ];
    $page['#attached']['drupalSettings']['mautic'] = $mautic_settings;
    if ($config
      ->get('header')) {
      $page['#attached']['library'][] = 'mautic/mautic-header';
    }
    else {
      $page['#attached']['library'][] = 'mautic/mautic-footer';
    }

    // Add custom field and optional Lift integration JS code\
    if ($config
      ->get('lift_enable')) {
      $moduleHandler = \Drupal::service('module_handler');
      if ($moduleHandler
        ->moduleExists('acquia_lift')) {
        $page['#attached']['library'][] = 'mautic/customFields';
      }
    }
  }
}

/**
 * Tracking visibility check for pages.
 *
 * Based on visibility setting this function returns TRUE if JS code should
 * be added to the current page and otherwise FALSE.
 */
function _mautic_visibility_pages() {
  static $page_match;
  if (!isset($page_match)) {
    $config = \Drupal::config('mautic.settings');
    $visibility_request_path_mode = $config
      ->get('visibility.request_path_mode');
    $visibility_request_path_pages = $config
      ->get('visibility.request_path_pages');
    if (!empty($visibility_request_path_pages)) {
      $pages = mb_strtolower($visibility_request_path_pages);
      if ($visibility_request_path_mode < 2) {
        $path = \Drupal::service('path.current')
          ->getPath();
        $path_alias = mb_strtolower(\Drupal::service('path_alias.manager')
          ->getAliasByPath($path));
        $page_match = \Drupal::service('path.matcher')
          ->matchPath($path_alias, $pages) || $path != $path_alias && \Drupal::service('path.matcher')
          ->matchPath($path, $pages);
        $page_match = !($visibility_request_path_mode xor $page_match);
      }
      else {
        $page_match = FALSE;
      }
    }
    else {
      $page_match = TRUE;
    }
  }
  return $page_match;
}

Functions

Namesort descending Description
mautic_help Implements hook_help().
mautic_page_attachments Implements hook_page_attachments().
_mautic_visibility_pages Tracking visibility check for pages.