You are here

public function ServicePluginBase::getAmpOutput in Analytics 8

Build an <amp-analytics> tag for output on an amp-enabled page request.

Parameters

array $settings: The settings of the amp-analytics tag.

  • type
  • config_json
  • config_url

Return value

array A structured, renderable array.

1 call to ServicePluginBase::getAmpOutput()
AmpAnalytics::getOutput in analytics_amp/src/Plugin/AnalyticsService/AmpAnalytics.php
Returns the output of the analytics service.

File

src/Plugin/ServicePluginBase.php, line 161

Class

ServicePluginBase
Defines a base implementation for analytics service plugins will extend.

Namespace

Drupal\analytics\Plugin

Code

public function getAmpOutput(array $settings) {
  $output = [];
  $element = [
    '#type' => 'html_tag',
    '#tag' => 'amp-analytics',
    '#attached' => [
      'library' => 'amp/amp.analytics',
    ],
  ];
  if (!empty($settings['config_json'])) {
    $json_element = [
      '#type' => 'html_tag',
      '#tag' => 'script',
      '#attributes' => [
        'type' => 'application/ld+json',
      ],
      '#value' => $settings['config_json'],
    ];
    $element['#value'] = \Drupal::service('renderer')
      ->renderPlain($json_element);
  }
  if (!empty($settings['type'])) {
    $element['#attributes']['type'] = $settings['type'];
  }
  if (!empty($settings['config_url'])) {
    $element['#attributes']['config'] = $settings['config_url'];
  }
  $output['analytics_' . $this
    ->getServiceId()] = $element;
  return $output;
}