AmpTrackingPixel.php in Analytics 8
File
analytics_amp/src/Plugin/AnalyticsService/AmpTrackingPixel.php
View source
<?php
namespace Drupal\analytics_amp\Plugin\AnalyticsService;
use Drupal\analytics\Plugin\ServicePluginBase;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
class AmpTrackingPixel extends ServicePluginBase {
public function defaultConfiguration() {
return [
'url' => NULL,
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['url'] = [
'#title' => t('URL'),
'#type' => 'textfield',
'#default_value' => $this->configuration['url'],
'#description' => $this
->t('See the <a href="@url">substitutions guide</a> to see what variables can be included in the URL.', [
'@url' => 'https://github.com/ampproject/amphtml/blob/master/spec/amp-var-substitutions.md',
]),
'#required' => TRUE,
'#placeholder' => 'https://foo.com/pixel?RANDOM',
];
return $form;
}
public function getOutput() {
if (\Drupal::service('router.amp_context')
->isAmpRoute()) {
$output['analytics_' . $this
->getServiceId()] = [
'#type' => 'html_tag',
'#tag' => 'amp-pixel',
'#attributes' => [
'id' => Html::getUniqueId('analytics_' . $this
->getServiceId()),
'src' => $this->configuration['url'],
],
];
return $output;
}
}
}