posthog-1.0.0-alpha5/modules/posthog_js/posthog_js.module
modules/posthog_js/posthog_js.module
<?php
/**
* @file
* Primary module hooks for the Posthog JS module.
*/
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Html;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_page_attachments().
*/
function posthog_js_page_attachments(array &$attachments) {
$posthogJsSettings = \Drupal::config('posthog_js.settings');
/** @var \Drupal\posthog_js\ConditionsCheckerInterface $conditionsChecker */
$conditionsChecker = Drupal::service('posthog_js.conditions_checker');
$bubbleable_metadata = new BubbleableMetadata();
// If js conditions don't apply, or the module is disabled, return early:
if (!$posthogJsSettings->get('enabled') || !$conditionsChecker->apply($bubbleable_metadata)) {
return;
}
// Initiate cacheable metadata:
$cache = CacheableMetadata::createFromRenderArray($attachments);
// We want to parse the json string to an array, so we have an object
// inside javascript to work with:
$initConfigJsonString = trim($posthogJsSettings->get('init_config_json'));
$initConfigJson = [];
if (!empty($initConfigJsonString)) {
$initConfigJson = Json::decode($initConfigJsonString);
}
// Add Posthog JS settings to the page:
$posthogSettings = \Drupal::config('posthog.settings');
/** @var \Drupal\posthog\UserAttributesProvider $posthogPropertyProvider */
$posthogPropertyProvider = \Drupal::service('posthog.user_attributes_provider');
$attachments['#attached']['drupalSettings']['posthog_js'] = [
'host' => $posthogSettings->get('host') ?? '',
'api_key' => $posthogSettings->get('api_key') ?? '',
'init_config_json' => $initConfigJson,
'cdn' => $posthogJsSettings->get('cdn'),
// Note, this is is the only place outside of "UserAttributesProvider" where
// we want to use this method:
'distinct_id' => $posthogPropertyProvider->getUserDistinctIdBySetting(),
'identify_anonymous' => $posthogSettings->get('identify_anonymous'),
'user_properties' => $posthogPropertyProvider->getCurrentUserProperties(),
];
$attachments['#attached']['library'][] = 'posthog_js/init';
// Add cacheable metadata:
$cache->addCacheableDependency($bubbleable_metadata);
$cache->applyTo($attachments);
}
/**
* Implements hook_library_info_alter().
*/
function posthog_js_library_info_alter(&$libraries, $module) {
// If the module is not "posthog_js" or the "cdn" setting is disabled, return
// early:
if ($module !== 'posthog_js' || \Drupal::config('posthog_js.settings')->get('cdn')) {
return;
}
// Find the "cdn" library dependency and remove it:
$key = array_search('posthog_js/cdn', $libraries['init']['dependencies']);
if ($key !== FALSE) {
unset($libraries['init']['dependencies'][$key]);
}
// Add the "local" library dependency:
$libraries['init']['dependencies'][] = 'posthog_js/local';
}
/**
* Implements hook_help().
*/
function posthog_js_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.posthog_js':
$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;
}
