You are here

AnalyticsService.php in Analytics 7

File

lib/AnalyticsService.php
View source
<?php

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;
  }

}

Classes

Namesort descending Description
AnalyticsService