You are here

GoogleAnalyticsGaService.php in Analytics 7

File

analytics_google/src/GoogleAnalyticsGaService.php
View source
<?php

class GoogleAnalyticsGaService extends AnalyticsService {

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return array(
      'id' => NULL,
    );
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm($form, &$form_state) {
    $options['id'] = array(
      '#title' => t('Tracking ID'),
      '#type' => 'textfield',
      '#default_value' => $this
        ->getConfiguration()['id'],
      '#element_validate' => array(
        '_analytics_google_validate_id',
      ),
      '#required' => TRUE,
      '#size' => 15,
    );
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function getOutput(array $context) {
    $actions = array();
    $actions[] = array(
      $this
        ->getMachineName() . '._setAccount',
      $this
        ->getConfiguration()['id'],
    );
    if (variable_get('analytics_privacy_anonymize_ip', FALSE)) {
      $actions[] = array(
        $this
          ->getMachineName() . '._anonymizeIp',
      );
    }
    $actions[] = array(
      $this
        ->getMachineName() . '._trackPageview',
    );
    $actions = array_merge($actions, module_invoke_all('analytics_google_ga_actions', $this, $context));
    drupal_alter('analytics_google_ga_actions', $actions, $this, $context);
    $output = array();
    $actions_code = theme('analytics_google_ga_actions_js', array(
      'actions' => $actions,
      'service' => $this,
    ));
    $output['#attached']['js'][] = array(
      'data' => $actions_code,
      'type' => 'inline',
      'preprocess' => FALSE,
      'scope' => 'footer',
    );
    static $added = FALSE;
    if (!$added) {
      $added = TRUE;
      $code = theme('analytics_google_ga_js', array(
        'service' => $this,
      ));
      $output['#attached']['js'][] = array(
        'data' => $code,
        'type' => 'inline',
        'weight' => 100000,
        'preprocess' => FALSE,
        'scope' => 'footer',
      );
    }
    return $output;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheableUrls() {
    $urls = array();
    $urls[] = 'https://ssl.google-analytics.com/ga.js';
    return $urls;
  }

}

Classes