You are here

class GoogleAnalyticsCounterMessageManager in Google Analytics Counter 8.3

Defines the Google Analytics Counter message manager.

@package Drupal\google_analytics_counter

Hierarchy

Expanded class hierarchy of GoogleAnalyticsCounterMessageManager

1 string reference to 'GoogleAnalyticsCounterMessageManager'
google_analytics_counter.services.yml in ./google_analytics_counter.services.yml
google_analytics_counter.services.yml
1 service uses GoogleAnalyticsCounterMessageManager
google_analytics_counter.message_manager in ./google_analytics_counter.services.yml
Drupal\google_analytics_counter\GoogleAnalyticsCounterMessageManager

File

src/GoogleAnalyticsCounterMessageManager.php, line 19

Namespace

Drupal\google_analytics_counter
View source
class GoogleAnalyticsCounterMessageManager implements GoogleAnalyticsCounterMessageManagerInterface {
  use StringTranslationTrait;

  /**
   * The google_analytics_counter.settings config object.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $config;

  /**
   * The database connection service.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * The state where all the tokens are saved.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatter
   */
  protected $dateFormatter;

  /**
   * A logger instance.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * The Messenger service.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   *
   * Constructs a GoogleAnalyticsCounterMessageManager object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Database\Connection $connection
   *   A database connection.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state keyvalue collection to use.
   * @param \Drupal\Core\Datetime\DateFormatter $date_formatter
   *   The date formatter service.
   * @param \Psr\Log\LoggerInterface $logger
   *   A logger instance.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, Connection $connection, StateInterface $state, DateFormatter $date_formatter, LoggerInterface $logger, MessengerInterface $messenger) {
    $this->config = $config_factory
      ->get('google_analytics_counter.settings');
    $this->connection = $connection;
    $this->dateFormatter = $date_formatter;
    $this->state = $state;
    $this->logger = $logger;
    $this->messenger = $messenger;
  }

  /**
   * Prints a warning message when not authenticated.
   *
   * @param $build
   *
   */
  public function notAuthenticatedMessage($build = []) {
    $t_arg = [
      ':href' => Url::fromRoute('google_analytics_counter.admin_auth_form', [], [
        'absolute' => TRUE,
      ])
        ->toString(),
      '@href' => 'Authentication',
    ];
    $this->messenger
      ->addWarning(t('Google Analytics have not been authenticated! Google Analytics Counter cannot fetch any new data. Please authenticate with Google from the <a href=:href>@href</a> page.', $t_arg));

    // Revoke Google authentication.
    $this
      ->revokeAuthenticationMessage($build);
  }

  /**
   * Revoke Google Authentication Message.
   *
   * @param $build
   * @return mixed
   */
  public function revokeAuthenticationMessage($build) {
    $t_args = [
      ':href' => Url::fromRoute('google_analytics_counter.admin_auth_revoke', [], [
        'absolute' => TRUE,
      ])
        ->toString(),
      '@href' => 'revoking Google authentication',
    ];
    $build['revoke_authentication'] = [
      '#type' => 'html_tag',
      '#tag' => 'p',
      '#value' => $this
        ->t("If there's a problem with OAUTH authentication, try <a href=:href>@href</a>.", $t_args),
    ];
    return $build;
  }

  /**
   * Returns the link with the Google project name if it is available.
   *
   * @return string
   *   Project name.
   */
  public function googleProjectName() {
    $config = $this->config;
    $project_name = !empty($config
      ->get('general_settings.project_name')) ? Url::fromUri('https://console.developers.google.com/apis/api/analytics.googleapis.com/quotas?project=' . $config
      ->get('general_settings.project_name'))
      ->toString() : Url::fromUri('https://console.developers.google.com/apis/api/analytics.googleapis.com/quotas')
      ->toString();
    return $project_name;
  }

  /**
   * Get the Profile name of the Google view from Drupal.
   *
   * @param string $profile_id
   *   The profile id used in the google query.
   *
   * @return string mixed
   */
  public function getProfileName($profile_id) {
    $profile_id = $this->state
      ->get('google_analytics_counter.total_pageviews_' . $profile_id);
    if (!empty($profile_id)) {
      $profile_name = '<strong>' . $profile_id[key($profile_id)] . '</strong>';
    }
    else {
      $profile_name = '<strong>' . $this
        ->t('(Profile name to come)') . '</strong>';
    }
    return $profile_name;
  }

  /**
   * Get the the top twenty results for pageviews and pageview_totals.
   *
   * @param string $table
   *   The table from which the results are selected.
   *
   * @return mixed
   */
  public function getTopTwentyResults($table) {
    $query = $this->connection
      ->select($table, 't');
    $query
      ->range(0, 20);
    $rows = [];
    switch ($table) {
      case 'google_analytics_counter':
        $query
          ->fields('t', [
          'pagepath',
          'pageviews',
        ]);
        $query
          ->orderBy('pageviews', 'DESC');
        $result = $query
          ->execute()
          ->fetchAll();
        $rows = [];
        foreach ($result as $value) {
          $rows[] = [
            $value->pagepath,
            $value->pageviews,
          ];
        }
        break;
      case 'google_analytics_counter_storage':
        $query
          ->fields('t', [
          'nid',
          'pageview_total',
        ]);
        $query
          ->orderBy('pageview_total', 'DESC');
        $result = $query
          ->execute()
          ->fetchAll();
        foreach ($result as $value) {
          $rows[] = [
            $value->nid,
            $value->pageview_total,
          ];
        }
        break;
      default:
        break;
    }
    return $rows;
  }

  /**
   * Voluminous on screen instructions about authentication.
   *
   * @param $web_properties
   *
   * @return string
   */
  public function authenticationInstructions($web_properties) {
    $t_arg = [
      ':href' => Url::fromRoute('google_analytics_counter.admin_dashboard_form', [], [
        'absolute' => TRUE,
      ])
        ->toString(),
      '@href' => 'Dashboard',
    ];
    $markup_description = $web_properties === 'unauthenticated' ? '<ol><li>' . $this
      ->t('Fill in the Client ID, Client Secret, Authorized Redirect URI, and optionally Google Project Name in the fields below.') . '</li><ul><li>' . $this
      ->t('If you don\'t have a Client ID, a Client Secret, an Authorized Redirect URI, and optionally a Google Project Name, follow the instructions in the README.md included with this module or read the <a href="https://www.drupal.org/docs/8/modules/google-analytics-counter" target="_blank">online documentation</a>.') . '</li></ul><li>' . $this
      ->t('Save configuration.') . '</li><li>' . $this
      ->t('Click Authenticate in "Authenticate with Google Analytics" above.') . '</li><li>' . $this
      ->t('If authentication with Google is successful, the ') . '<strong>' . $this
      ->t(' Google View ') . '</strong>' . $this
      ->t('field will list your analytics profiles.') . '</li><li>' . $this
      ->t('Select an analytics profile to collect analytics from and click Save configuration.') . '</li><ul><li>' . $this
      ->t('If you are not authenticated or if the project you are authenticating to does not have Analytics, no options are available in the') . '<strong>' . $this
      ->t(' Google View.') . '</strong>' . '</strong></li></ul></ol></p>' : '<p>' . $this
      ->t('Client ID, Client Secret, and Authorized redirect URI can only be changed when not authenticated.') . '<ol><li>' . $this
      ->t('Now that you are authenticated with Google Analytics, you MUST select the') . '<strong>' . $this
      ->t(' Google View ') . '</strong>' . $this
      ->t('to collect analytics from.') . '</li><li>' . $this
      ->t('Save configuration.') . '</li><li>' . $this
      ->t('On the next cron job, analytics from the selected') . '<strong>' . $this
      ->t(' Google View ') . '</strong>' . $this
      ->t('will be saved to Drupal.') . '</li><ul><li>' . $this
      ->t('Information on the <a href=:href>@href</a> page is from the', $t_arg) . '<strong>' . $this
      ->t(' Google View') . '</strong>' . $this
      ->t('.') . '</li><li>' . $this
      ->t('After cron runs, compare pagepaths and pageview totals on the <a href=:href>@href</a> in the Top Twenty Results section with your Google Analytics.', $t_arg) . '</li><li>' . $this
      ->t('If date range is set to one of Google\'s predefined time intervals, the Pageviews in Drupal should match Google exactly.', $t_arg) . '</li></ul></ol></p>';
    return $markup_description;
  }

  /**
   * Sets the start and end dates in configuration.
   *
   * @return array
   *   Start and end dates.
   */
  public function setStartDateEndDate() {
    $config = $this->config;
    if ($config
      ->get('general_settings.custom_start_date') != '' & $config
      ->get('general_settings.custom_end_date') != '') {
      $t_args = [
        '%start_date' => $this->dateFormatter
          ->format(strtotime($config
          ->get('general_settings.custom_start_date')), 'custom', 'M j, Y'),
        '%end_date' => $this->dateFormatter
          ->format(strtotime($config
          ->get('general_settings.custom_end_date')), 'custom', 'M j, Y'),
      ];
      return $t_args;
    }
    else {
      $t_args = [];
      switch ($config
        ->get('general_settings.start_date')) {
        case 'today':
          $t_args = [
            '%start_date' => date('M j, Y'),
            '%end_date' => date('M j, Y'),
          ];
          break;
        case 'yesterday':
          $t_args = [
            '%start_date' => date('M j, Y', time() - 60 * 60 * 24),
            '%end_date' => date('M j, Y', time() - 60 * 60 * 24),
          ];
          break;
        case '-1 week last sunday midnight':
          $previous_week = strtotime('-1 week +1 day');
          $start_week = strtotime('last sunday midnight', $previous_week);
          $end_week = strtotime('next saturday', $start_week);
          $start_week = date('M j, Y', $start_week);
          $end_week = date('M j, Y', $end_week);
          $t_args = [
            '%start_date' => $start_week,
            '%end_date' => $end_week,
          ];
          break;
        case 'first day of previous month':
          $t_args = [
            '%start_date' => date('M j, Y', strtotime('first day of previous month')),
            '%end_date' => date('M j, Y', strtotime('last day of previous month')),
          ];
          break;
        case '7 days ago':
          $t_args = [
            '%start_date' => date('M j, Y', strtotime('7 days ago')),
            '%end_date' => date('M j, Y', time() - 60 * 60 * 24),
          ];
          break;
        case '30 days ago':
          $t_args = [
            '%start_date' => date('M j, Y', strtotime('30 days ago')),
            '%end_date' => date('M j, Y', time() - 60 * 60 * 24),
          ];
          break;
        case '3 months ago':
          $t_args = [
            '%start_date' => date('M j, Y', strtotime('3 months ago')),
            '%end_date' => date('M j, Y', time() - 60 * 60 * 24),
          ];
          break;
        case '6 months ago':
          $t_args = [
            '%start_date' => date('M j, Y', strtotime('6 months ago')),
            '%end_date' => date('M j, Y', time() - 60 * 60 * 24),
          ];
          break;
        case 'first day of last year':
          $t_args = [
            '%start_date' => date('M j, Y', strtotime('first day of last year')),
            '%end_date' => date('M j, Y', strtotime('last day of last year')),
          ];
          break;
        case '14 November 2005':
          $t_args = [
            '%start_date' => date('M j, Y', strtotime('14 November 2005')),
            '%end_date' => date('M j, Y', time() - 60 * 60 * 24),
          ];
          break;
        default:
          $t_args = [
            '%start_date' => 'N/A',
            '%end_date' => 'N/A',
          ];
          break;
      }
      return $t_args;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GoogleAnalyticsCounterMessageManager::$config protected property The google_analytics_counter.settings config object.
GoogleAnalyticsCounterMessageManager::$connection protected property The database connection service.
GoogleAnalyticsCounterMessageManager::$dateFormatter protected property The date formatter service.
GoogleAnalyticsCounterMessageManager::$logger protected property A logger instance.
GoogleAnalyticsCounterMessageManager::$messenger protected property The Messenger service.
GoogleAnalyticsCounterMessageManager::$state protected property The state where all the tokens are saved.
GoogleAnalyticsCounterMessageManager::authenticationInstructions public function Voluminous on screen instructions about authentication. Overrides GoogleAnalyticsCounterMessageManagerInterface::authenticationInstructions
GoogleAnalyticsCounterMessageManager::getProfileName public function Get the Profile name of the Google view from Drupal. Overrides GoogleAnalyticsCounterMessageManagerInterface::getProfileName
GoogleAnalyticsCounterMessageManager::getTopTwentyResults public function Get the the top twenty results for pageviews and pageview_totals. Overrides GoogleAnalyticsCounterMessageManagerInterface::getTopTwentyResults
GoogleAnalyticsCounterMessageManager::googleProjectName public function Returns the link with the Google project name if it is available. Overrides GoogleAnalyticsCounterMessageManagerInterface::googleProjectName
GoogleAnalyticsCounterMessageManager::notAuthenticatedMessage public function Prints a warning message when not authenticated. Overrides GoogleAnalyticsCounterMessageManagerInterface::notAuthenticatedMessage
GoogleAnalyticsCounterMessageManager::revokeAuthenticationMessage public function Revoke Google Authentication Message. Overrides GoogleAnalyticsCounterMessageManagerInterface::revokeAuthenticationMessage
GoogleAnalyticsCounterMessageManager::setStartDateEndDate public function Sets the start and end dates in configuration. Overrides GoogleAnalyticsCounterMessageManagerInterface::setStartDateEndDate
GoogleAnalyticsCounterMessageManager::__construct public function Constructs a GoogleAnalyticsCounterMessageManager object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.