You are here

class AmpAnalytics in Analytics 8

Provides AMP analytics.

Plugin annotation


@AnalyticsService(
  id = "amp_analytics",
  label = @Translation("AMP Analytics"),
  multiple = true,
)

Hierarchy

Expanded class hierarchy of AmpAnalytics

File

analytics_amp/src/Plugin/AnalyticsService/AmpAnalytics.php, line 18

Namespace

Drupal\analytics_amp\Plugin\AnalyticsService
View source
class AmpAnalytics extends ServicePluginBase {

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'type' => NULL,
      'config_url' => '',
      'config_json' => [],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['type'] = [
      '#title' => t('Type'),
      '#type' => 'select',
      '#default_value' => $this->configuration['type'],
      '#options' => [
        // @todo Add support for all options.
        'adobeanalytics' => t('Adobe Analytics'),
        'googleanalytics' => t('Google Analytics'),
      ],
      '#required' => TRUE,
    ];
    $form['config_url'] = [
      '#title' => t('Remote configuration JSON URL'),
      '#type' => 'textfield',
      '#default_value' => $this->configuration['config_url'],
      '#placeholder' => 'https://example.com/analytics.config.json',
    ];
    $form['config_json'] = [
      '#title' => t('Inline configuration JSON'),
      '#type' => 'textarea',
      '#default_value' => $this->configuration['config_json'],
      '#description' => t('See the <a href="https://www.ampproject.org/docs/reference/extended/amp-analytics.html">amp-analytics documentation</a> for example configuration values.'),
      '#element_validate' => [
        [
          get_class($this),
          'validateJson',
        ],
      ],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function getOutput() {
    if (\Drupal::service('router.amp_context')
      ->isAmpRoute()) {
      return $this
        ->getAmpOutput($this->configuration);
    }
  }
  protected function elementValidateConfigUrl(array &$element, FormStateInterface $form_state) {
    $value = $element['#value'];
    if ($value == '') {
      return;
    }
    elseif (\Drupal::service('stream_wrapper_manager')
      ->isValidUri($value)) {

      // Allow file URIs like public:://config.json.
      return;
    }
    elseif (!UrlHelper::isExternal($value)) {
      $form_state
        ->setError($element, $this
        ->t('Not a valid URL.'));
    }
  }
  protected function elementValidateConfigJson(array &$element, FormStateInterface $form_state) {
    $value = $element['#value'];
    if ($value == '') {
      return;
    }
    elseif (is_string($value)) {

      // Otherwise attempt to convert the value to JSON.
      $data = json_decode($value, TRUE);
      if (json_last_error()) {
        $form_state
          ->setError($element, $this
          ->t('%name is not valid JSON.', [
          '%name' => $element['#title'],
        ]));
      }
      elseif ($element['#required'] && empty($data)) {
        $form_state
          ->setError($element, $this
          ->t('%name is required.', [
          '%name' => $element['#title'],
        ]));
      }
      else {

        // @todo This should attempt to validate the top-level keys.
        $form_state
          ->setValueForElement($element, $data);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AmpAnalytics::buildConfigurationForm public function Form constructor. Overrides ServicePluginBase::buildConfigurationForm
AmpAnalytics::defaultConfiguration public function Gets default configuration for this plugin. Overrides ServicePluginBase::defaultConfiguration
AmpAnalytics::elementValidateConfigJson protected function
AmpAnalytics::elementValidateConfigUrl protected function
AmpAnalytics::getOutput public function Returns the output of the analytics service. Overrides ServicePluginBase::getOutput
DependencyTrait::$dependencies protected property The object's dependencies.
DependencyTrait::addDependencies protected function Adds multiple dependencies.
DependencyTrait::addDependency protected function Adds a dependency.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginDependencyTrait::calculatePluginDependencies protected function Calculates and adds dependencies of a specific plugin instance. 1
PluginDependencyTrait::getPluginDependencies protected function Calculates and returns dependencies of a specific plugin instance.
PluginDependencyTrait::moduleHandler protected function Wraps the module handler. 1
PluginDependencyTrait::themeHandler protected function Wraps the theme handler. 1
ServicePluginBase::$hasMultiple protected property
ServicePluginBase::$serviceId protected property The ID of the service config entity using this plugin.
ServicePluginBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
ServicePluginBase::canTrack public function Determines if the current service can track the current request. Overrides ServicePluginInterface::canTrack
ServicePluginBase::getAmpOutput public function Build an <amp-analytics> tag for output on an amp-enabled page request.
ServicePluginBase::getCacheableUrls public function Overrides ServicePluginInterface::getCacheableUrls
ServicePluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
ServicePluginBase::getLabel public function Returns the label of the analytics service. Overrides ServicePluginInterface::getLabel
ServicePluginBase::getServiceId public function Gets the current service config entity ID that is using this plugin. Overrides ServicePluginInterface::getServiceId
ServicePluginBase::hasMultipleInstances public function
ServicePluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
ServicePluginBase::setServiceId public function Sets the current service config entity ID that is using this plugin. Overrides ServicePluginInterface::setServiceId
ServicePluginBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
ServicePluginBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
ServicePluginBase::validateJson public static function Form element validation callback for a JSON textarea field.
ServicePluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.