convertcontacts-8.x-1.0/convertcontacts.module

convertcontacts.module
<?php

/**
 * @file
 * Drupal Module: ConvertContacts.
 *
 * Adds the required Javascript to all your Drupal pages to allow tracking by
 * the ConvertContacts package.
 *
 * @author: ReachLocal <https://www.drupal.org/user/3405578>
 */

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Component\Utility\Unicode;

/**
 * Define default path exclusion list to remove tracking from admin pages.
 */
define('CONVERTCONTACTS_PAGES', "/admin\n/admin/*\n/batch\n/node/*\n/node/*/*\n/user/*\n/user/*/*");
/**
 * Implements hook_help().
 */
function convertcontacts_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the facebook_pixel module.
    case 'help.page.convertcontacts':
      return t('<a href="@convertcontacts">ConvertContacts</a> offers lead &amp; call tracking, lead notifications & nurturing, ROI reports, analytics &amp; insights, and mobile app &amp; alerts.', array('@convertcontacts' => 'http://www.reachlocal.com'));
    default:
  }
}

/**
 * Convert site_id from 'fc62c28f-3f38-4812-85c3-b3fe1329dba8' to '555/6e6/569/cfc4c23ac7e7ab663b58748.js';
 * Return '//cdn.rlets.com/capture_configs/fc6/2c2/8f3/f38481285c3b3fe1329dba8.js'
 */
function convertcontacts_code_snippet_src($reachlocal_tracking_id) {
  $site_id = array();
  array_push($site_id, (substr($reachlocal_tracking_id, 0, 8)));
  array_push($site_id, (substr($reachlocal_tracking_id, 9, 4)));
  array_push($site_id, (substr($reachlocal_tracking_id, 14, 4)));
  array_push($site_id, (substr($reachlocal_tracking_id, 19, 4)));
  array_push($site_id, (substr($reachlocal_tracking_id, 24, 12)));
  $flattened_site_id = implode("",$site_id);
  $snippet_src = array();
  array_push($snippet_src, '//cdn.rlets.com/capture_configs/');
  array_push($snippet_src, (substr($flattened_site_id, 0, 3)));
  array_push($snippet_src, '/');
  array_push($snippet_src, (substr($flattened_site_id, 3, 3)));
  array_push($snippet_src, '/');
  array_push($snippet_src, (substr($flattened_site_id, 6, 3)));
  array_push($snippet_src, '/');
  array_push($snippet_src, (substr($flattened_site_id, 9, 23)));
  array_push($snippet_src, '.js');
  return implode('', $snippet_src);
}

/**
 * Implements hook_page_attachments().
 */
function convertcontacts_page_attachments(array &$page){
  $config = \Drupal::config('convertcontacts.convertcontactsconfig');
  $id = $config->get('convertcontacts_id');
  if (preg_match('/^[A-Z0-9]{8}(-[A-Z0-9]{4}){3}-[A-Z0-9]{12}$/i', $id) && _convertcontacts_visibility_pages()) {

    //  Attach tracker code.
    $page['#attached']['library'][] = 'convertcontacts/mms';
  }
}



/**
 * Determine which pages should run the Capture Code.
 *
 * Based on visibility setting this function returns TRUE if Capture
 * code should be added to the current page, otherwise FALSE.
 */

function _convertcontacts_visibility_pages() {

  static $page_match;
    $config = \Drupal::config('convertcontacts.convertcontactsconfig');
    $visibility = $config->get('convertcontacts_visibility_pages');
    $setting_pages = CONVERTCONTACTS_PAGES; 

    // Match path if necessary.
    if (!empty($setting_pages)) {
      // Convert path to lowercase. This allows comparison of the same path
      // with different case. Ex: /Page, /page, /PAGE.
      $pages = Unicode::strtolower($setting_pages);
      if ($visibility < 2) {
        // Convert the Drupal path to lowercase.
        $path = Unicode::strtolower(\Drupal::service('path.alias_manager')->getAliasByPath(\Drupal::service('path.current')->getPath()));
        // Compare the lowercase internal and lowercase path alias (if any).
        $page_match = \Drupal::service('path.matcher')->matchPath($path, $pages);     
        if ($path != \Drupal::service('path.current')->getPath()) {
          $page_match = $page_match || \Drupal::service('path.matcher')->matchPath($_GET['q'], $pages);
        }
        // When $visibility has a value of 0, the tracking code is displayed on
        // all pages except those listed in $pages. When set to 1, it
        // is displayed only on those pages listed in $pages.
        $page_match = !($visibility xor $page_match);        
      }
      elseif (\Drupal::moduleHandler()->moduleExists('php')) {
        $page_match = php_eval($setting_pages);
      }
      else {
        $page_match = FALSE;
      }
    }
    else {
      $page_match = TRUE;
    }

  return $page_match;
}


/**
 * Delete cached files and directory.
 */
function convertcontacts_clear_js_cache() {
  $path = 'public://convertcontacts';
  if (file_prepare_directory($path)) {
    file_scan_directory($path, '/.*/', array('callback' => 'file_unmanaged_delete'));
    drupal_rmdir($path);

    // Change query-strings on css/js files to enforce reload for all users.
    _drupal_flush_css_js();

    watchdog('convertcontacts', 'Local cache has been purged.', array(), WATCHDOG_INFO);
  }
}

/**
 * Implements hook_library_info_alter().
  *
  * Override the script js in convertcontacts.libraries.info with the site id for the reachcode snippet. 
  */
function convertcontacts_library_info_alter(array &$libraries, $module) {

  $config = \Drupal::config('convertcontacts.convertcontactsconfig');
  $id = $config->get('convertcontacts_id');
  $snippet = convertcontacts_code_snippet_src($id);
  $libraries['mms']['js'] = array($snippet=>array('type'=>'external', 'attributes'=>array('async'=>'async')));
}

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

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