posthog-1.0.0-alpha5/modules/posthog_php_events/posthog_php_events.module

modules/posthog_php_events/posthog_php_events.module
<?php

/**
 * @file
 * Primary module hooks for Posthog: Events module.
 */

use Drupal\Component\Utility\Html;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;

/**
 * Implements hook_user_login().
 *
 * Note, for some reason, "google_tag" hooks into the login FORM instead of
 * using the login hook. If this implementation causes issues, we should do
 * that instead.
 *
 * @see https://git.drupalcode.org/project/google_tag/-/blob/2.0.x/google_tag.module?ref_type=heads#L170
 */
function posthog_php_events_user_login(AccountInterface $user) {
  /** @var \Drupal\posthog_php\SdkDecoratorInterface $posthogSdk */
  $posthogSdk = \Drupal::service('posthog_php.sdk_decorator');
  /** @var \Drupal\posthog\UserAttributesProvider $userAttributesProvider */
  $userAttributesProvider = \Drupal::service('posthog.user_attributes_provider');

  $trackDrupalUserLogin = \Drupal::config('posthog_php_events.settings')->get('track_drupal_user_login');
  if ($trackDrupalUserLogin) {
    $posthogSdk->captureUserEvent('User login', [
      'user' => $user,
    ]);
  }

  $trackDrupalUserFirstTimeLogin = \Drupal::config('posthog_php_events.settings')->get('track_drupal_user_first_time_login');
  if ($trackDrupalUserFirstTimeLogin) {
    if (!$user->getLastAccessedTime()) {
      $posthogSdk->captureUserEvent('User first time login', [
        'user' => $user,
      ]);
    }
  }
}

/**
 * Implements hook_user_logout().
 */
function posthog_php_events_user_logout(AccountInterface $user) {
  $trackDrupalUserLogout = \Drupal::config('posthog_php_events.settings')->get('track_drupal_user_logout');
  if ($trackDrupalUserLogout) {
    /** @var \Drupal\posthog_php\SdkDecoratorInterface $posthogSdk */
    $posthogSdk = \Drupal::service('posthog_php.sdk_decorator');
    /** @var \Drupal\posthog\UserAttributesProvider $userAttributesProvider */
    $userAttributesProvider = \Drupal::service('posthog.user_attributes_provider');
    $posthogSdk->captureUserEvent('User logout', [
      'user' => $user,
    ]);
  }
}

/**
 * Implements hook_ENTITY_TYPE_insert().
 */
function posthog_php_events_user_insert(AccountInterface $user) {
  $config = \Drupal::config('posthog_php_events.settings');
  /** @var \Drupal\posthog_php\SdkDecoratorInterface $posthogSdk */
  $posthogSdk = \Drupal::service('posthog_php.sdk_decorator');
  /** @var \Drupal\posthog\UserAttributesProvider $userAttributesProvider */
  $userAttributesProvider = \Drupal::service('posthog.user_attributes_provider');

  $trackRegistration = $config->get('track_drupal_user_registration');
  if ($trackRegistration && (\Drupal::currentUser()->isAnonymous() || \Drupal::currentUser()->id() == $user->id())) {
    $posthogSdk->captureUserEvent('User registration', [
      'user' => $user,
    ]);
  }
}

/**
 * Implements hook_ENTITY_TYPE_update().
 */
function posthog_php_events_user_update(AccountInterface $user) {
  /** @var \Drupal\posthog_php\SdkDecoratorInterface $posthogSdk */
  $posthogSdk = \Drupal::service('posthog_php.sdk_decorator');
  /** @var \Drupal\posthog\UserAttributesProvider $userAttributesProvider */
  $userAttributesProvider = \Drupal::service('posthog.user_attributes_provider');

  $trackDrupalUserBlock = \Drupal::config('posthog_php_events.settings')->get('track_drupal_user_block');
  if ($trackDrupalUserBlock) {
    if ($user->original->status->value != $user->status->value && (bool) $user->status->value == FALSE) {
      $posthogSdk->captureUserEvent('User blocked', [
        'user' => $user,
      ]);
    }
  }

  $trackDrupalUserUnblock = \Drupal::config('posthog_php_events.settings')->get('track_drupal_user_unblock');
  if ($trackDrupalUserUnblock) {
    if ($user->original->status->value != $user->status->value && (bool) $user->status->value == TRUE) {
      $posthogSdk->captureUserEvent('User unblocked', [
        'user' => $user,
      ]);
    }
  }
}

/**
 * Implements hook_ENTITY_TYPE_delete().
 */
function posthog_php_events_user_delete(AccountInterface $user) {
  $trackDrupalUserDelete = \Drupal::config('posthog_php_events.settings')->get('track_drupal_user_delete');
  if ($trackDrupalUserDelete) {
    /** @var \Drupal\posthog_php\SdkDecoratorInterface $posthogSdk */
    $posthogSdk = \Drupal::service('posthog_php.sdk_decorator');
    /** @var \Drupal\posthog\UserAttributesProvider $userAttributesProvider */
    $userAttributesProvider = \Drupal::service('posthog.user_attributes_provider');
    $posthogSdk->captureUserEvent('User deleted', [
      'user' => $user,
    ]);
  }
}

/**
 * Implements hook_help().
 */
function posthog_php_events_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.posthog':
      $text = file_get_contents(__DIR__ . '/README.md');
      if (!\Drupal::moduleHandler()->moduleExists('markdown')) {
        return '<pre>' . Html::escape($text) . '</pre>';
      }
      else {
        // Use the Markdown filter to render the README.
        $filter_manager = \Drupal::service('plugin.manager.filter');
        $settings = \Drupal::configFactory()->get('markdown.settings')->getRawData();
        $config = ['settings' => $settings];
        $filter = $filter_manager->createInstance('markdown', $config);
        return $filter->process($text, 'en');
      }
  }
  return NULL;
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc