AnalyticsService.php in Analytics 7
File
lib/AnalyticsService.php
View source
<?php
abstract class AnalyticsService implements AnalyticsServiceInterface {
private $machine_name;
private $label;
private $description;
private $service;
private $options;
protected $hasMultiple;
public function __construct(array $data) {
foreach ($data as $key => $value) {
$this->{$key} = $value;
}
$this->options += $this
->defaultConfiguration();
}
public function getMachineName() {
return $this->machine_name;
}
public function getLabel() {
return $this->label;
}
public function getDescription() {
return $this->label;
}
public function getService() {
return $this->service;
}
public function defaultConfiguration() {
return array();
}
public function getConfiguration() {
return $this->options;
}
public function buildConfigurationForm($form, &$form_state) {
return array();
}
public function canTrack(array $context) {
return !path_is_admin($_GET['q']);
}
public function hasMultipleInstances() {
if (!isset($this->hasMultiple)) {
ctools_include('export');
$services = ctools_export_crud_load_all('analytics_service');
$count = 0;
foreach ($services as $service) {
if ($service->service == $this
->getService()) {
$count++;
}
}
$this->hasMultiple = $count >= 2;
}
return $this->hasMultiple;
}
public function getCacheableUrls() {
return array();
}
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;
}
}