You are here

abstract class AnalyticsService in Analytics 7

Hierarchy

Expanded class hierarchy of AnalyticsService

File

lib/AnalyticsService.php, line 3

View source
abstract class AnalyticsService implements AnalyticsServiceInterface {
  private $machine_name;
  private $label;
  private $description;
  private $service;
  private $options;
  protected $hasMultiple;
  public function __construct(array $data) {
    foreach ($data as $key => $value) {
      $this->{$key} = $value;
    }
    $this->options += $this
      ->defaultConfiguration();
  }

  /**
   * {@inheritdoc}
   */
  public function getMachineName() {
    return $this->machine_name;
  }

  /**
   * {@inheritdoc}
   */
  public function getLabel() {
    return $this->label;
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return $this->label;
  }

  /**
   * {@inheritdoc}
   */
  public function getService() {
    return $this->service;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return array();
  }

  /**
   * {@inheritdoc}
   */
  public function getConfiguration() {
    return $this->options;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm($form, &$form_state) {
    return array();
  }

  /**
   * {@inheritdoc}
   */
  public function canTrack(array $context) {
    return !path_is_admin($_GET['q']);
  }
  public function hasMultipleInstances() {
    if (!isset($this->hasMultiple)) {
      ctools_include('export');
      $services = ctools_export_crud_load_all('analytics_service');
      $count = 0;
      foreach ($services as $service) {
        if ($service->service == $this
          ->getService()) {
          $count++;
        }
      }
      $this->hasMultiple = $count >= 2;
    }
    return $this->hasMultiple;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheableUrls() {
    return array();
  }

  /**
   * Build an <amp-analytics> tag for output on an amp-enabled page request.
   *
   * @param array $settings
   *   The settings of the amp-analytics tag.
   *   - type
   *   - config_json
   *   - config_url
   * @param array $context
   *   An array of context about the current page request.
   *
   * @return array
   *   A structured, renderable array.
   */
  public function getAmpOutput(array $settings, array $context) {
    $header_js = array(
      '#tag' => 'script',
      '#attributes' => array(
        'src' => 'https://cdn.ampproject.org/v0/amp-analytics-0.1.js',
        'async' => TRUE,
        'custom-element' => 'amp-analytics',
      ),
    );
    $element = array(
      '#theme' => 'html_tag',
      '#tag' => 'amp-analytics',
      '#attributes' => array(
        'id' => drupal_html_id('analytics_' . $this
          ->getMachineName()),
      ),
      '#attached' => array(
        'drupal_add_html_head' => array(
          array(
            $header_js,
            'amp-analytics',
          ),
        ),
      ),
    );
    if (!empty($settings['type'])) {
      $element['#attributes']['type'] = $settings['type'];
    }
    if (!empty($settings['config_json'])) {
      $json = drupal_array_merge_deep($settings['config_json'], module_invoke_all('analytics_amp_json', $this, $context));
      drupal_alter('analytics_amp_json', $json, $this, $context);
      $element['#value'] = '<script type="application/json">' . json_encode($json, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT) . '</script>';
    }
    if (!empty($settings['config_url'])) {
      $element['#attributes']['config'] = file_create_url($settings['config_url']);
    }
    return $element;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AnalyticsService::$description private property
AnalyticsService::$hasMultiple protected property
AnalyticsService::$label private property
AnalyticsService::$machine_name private property
AnalyticsService::$options private property
AnalyticsService::$service private property
AnalyticsService::buildConfigurationForm public function Form constructor. Overrides AnalyticsServiceInterface::buildConfigurationForm 5
AnalyticsService::canTrack public function Determines if the current service can track the current request. Overrides AnalyticsServiceInterface::canTrack
AnalyticsService::defaultConfiguration public function Overrides AnalyticsServiceInterface::defaultConfiguration 5
AnalyticsService::getAmpOutput public function Build an <amp-analytics> tag for output on an amp-enabled page request.
AnalyticsService::getCacheableUrls public function Overrides AnalyticsServiceInterface::getCacheableUrls 2
AnalyticsService::getConfiguration public function Overrides AnalyticsServiceInterface::getConfiguration
AnalyticsService::getDescription public function Returns the description of the plugin instance. Overrides AnalyticsServiceInterface::getDescription
AnalyticsService::getLabel public function Returns the label of the plugin instance. Overrides AnalyticsServiceInterface::getLabel
AnalyticsService::getMachineName public function Returns the machine name of the analytics plugin instance. Overrides AnalyticsServiceInterface::getMachineName
AnalyticsService::getService public function Returns the type of the plugin instance. Overrides AnalyticsServiceInterface::getService
AnalyticsService::hasMultipleInstances public function
AnalyticsService::__construct public function
AnalyticsServiceInterface::getOutput public function Returns the output of the analytics service. 5