You are here

public function AnalyticsService::getAmpOutput in Analytics 7

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

array $context: An array of context about the current page request.

Return value

array A structured, renderable array.

2 calls to AnalyticsService::getAmpOutput()
AmpAnalyticsService::getOutput in analytics_amp/src/AmpAnalyticsService.php
Returns the output of the analytics service.
GoogleUniversalAnalyticsService::getOutput in analytics_google/src/GoogleUniversalAnalyticsService.php
Returns the output of the analytics service.

File

lib/AnalyticsService.php, line 112

Class

AnalyticsService

Code

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