Commit 35ca5f5e authored by Dave Reid's avatar Dave Reid
Browse files

Issue #3300064 by Dave Reid: Add a Google Optimize service plugin.

parent 8f8863b2
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -40,6 +40,20 @@ analytics.service_configuration:
  type: mapping
  label: 'Analytics service configuration'

analytics.service_configuration.google_optimize:
  type: analytics.service_configuration
  label: 'Google Optimize service settings'
  mapping:
    container_id:
      type: string
      label: 'Container ID'
    async:
      type: boolean
      label: 'Use async'
    anti_flicker:
      type: boolean
      label: 'Add the Optimize anti-flickr snippet'

analytics.service_configuration.google_tag_manager:
  type: analytics.service_configuration
  label: 'Google Tag Manager service settings'
+108 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\analytics\Plugin\AnalyticsService;

use Drupal\analytics\Plugin\ServicePluginBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;

/**
 * Google Optimize service plugin.
 *
 * @AnalyticsService(
 *   id = "google_optimize",
 *   label = @Translation("Google Optimize"),
 *   multiple = true,
 * )
 */
class GoogleOptimize extends ServicePluginBase {

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'container_id' => '',
      'async' => FALSE,
      'anti_flicker' => FALSE,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['container_id'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Container ID'),
      '#default_value' => $this->configuration['container_id'],
      '#required' => TRUE,
      '#size' => 15,
      '#placeholder' => 'GTM-XXXX',
    ];
    $form['async'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Async'),
      '#default_value' => $this->configuration['async'],
    ];
    $form['anti_flicker'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Add the <a href="@url">Optimize anti-flicker snippet</a>', ['@url' => 'https://support.google.com/optimize/answer/7100284']),
      '#description' => $this->t('If you notice page flicker, adding this snippet can help.'),
      '#default_value' => $this->configuration['anti_flicker'],
      '#return_value' => TRUE,
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function getOutput() {
    $output = [];

    if ($this->configuration['anti_flicker']) {
      // This only needs to be output once so it has the same ID every time.
      $output['#attached']['html_head'][] = [
        [
          '#type' => 'html_tag',
          '#tag' => 'style',
          '#value' => Markup::create('.async-hide { opacity: 0 !important}'),
        ],
        'google_analytics_optimize_anti_flicker_css',
      ];

      $optimize_js = <<<END
(function(a,s,y,n,c,h,i,d,e){s.className+=' '+y;h.start=1*new Date;
h.end=i=function(){s.className=s.className.replace(RegExp(' ?'+y),'')};
(a[n]=a[n]||[]).hide=h;setTimeout(function(){i();h.end=null},c);h.timeout=c;
})(window,document.documentElement,'async-hide','dataLayer',4000,
{'{$this->configuration['container_id']}':true});
END;
      $output['#attached']['html_head'][] = [
        [
          '#type' => 'html_tag',
          '#tag' => 'script',
          '#value' => Markup::create($optimize_js),
        ],
        'analytics_' . $this->getServiceId() . '_optimize_anti_flicker_js',
      ];
    }

    $output['#attached']['html_head'][] = [
      [
        '#type' => 'html_tag',
        '#tag' => 'script',
        '#attributes' => [
          'src' => 'https://www.googleoptimize.com/optimize.js?id=' . $this->configuration['container_id'],
          'async' => $this->configuration['async'],
        ],
      ],
      'analytics_' . $this->getServiceId() . '_optimize',
    ];

    return $output;
  }

}
+3 −3
Original line number Diff line number Diff line
@@ -41,7 +41,7 @@ class GoogleTagManager extends ServicePluginBase {
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['container_id'] = [
      '#type' => 'textfield',
      '#title' => t('Container ID'),
      '#title' => $this->t('Container ID'),
      '#default_value' => $this->configuration['container_id'],
      '#required' => TRUE,
      '#size' => 15,
@@ -54,14 +54,14 @@ class GoogleTagManager extends ServicePluginBase {
    ];
    $form['data_layer']['name'] = [
      '#type' => 'textfield',
      '#title' => t('Data Layer variable'),
      '#title' => $this->t('Data Layer variable'),
      '#default_value' => $this->configuration['data_layer']['name'],
      '#size' => 15,
      '#required' => TRUE,
    ];
    $form['data_layer']['value'] = [
      '#type' => 'textarea',
      '#title' => t('Data Layer value JSON'),
      '#title' => $this->t('Data Layer value JSON'),
      '#default_value' => $this->configuration['data_layer']['value'],
      '#element_validate' => [
        [get_class($this), 'validateJson'],