diff --git a/modules/persistent_visitor_parameters_user_registration/config/install/persistent_visitor_parameters_user_registration.settings.yml b/modules/persistent_visitor_parameters_user_registration/config/install/persistent_visitor_parameters_user_registration.settings.yml new file mode 100644 index 0000000000000000000000000000000000000000..27bc8e302c1f856cb24c0aa67a01e119171f452e --- /dev/null +++ b/modules/persistent_visitor_parameters_user_registration/config/install/persistent_visitor_parameters_user_registration.settings.yml @@ -0,0 +1 @@ +enable_utm_user_logging: TRUE diff --git a/modules/persistent_visitor_parameters_user_registration/config/schema/persistent_visitor_parameters_user_registration.schema.yml b/modules/persistent_visitor_parameters_user_registration/config/schema/persistent_visitor_parameters_user_registration.schema.yml new file mode 100644 index 0000000000000000000000000000000000000000..5720f4ca94fbf9c48fa53256f03e75b422992ead --- /dev/null +++ b/modules/persistent_visitor_parameters_user_registration/config/schema/persistent_visitor_parameters_user_registration.schema.yml @@ -0,0 +1,8 @@ +persistent_visitor_parameters_user_registration.settings: + type: config_object + label: 'Persistent visitor parameters user registration settings' + mapping: + enable_utm_user_logging: + type: boolean + label: 'Enable UTM user registration logging' + description: 'Check this to enable logging of UTM parameters for new user registrations.' diff --git a/modules/persistent_visitor_parameters_user_registration/persistent_visitor_parameters_user_registration.info.yml b/modules/persistent_visitor_parameters_user_registration/persistent_visitor_parameters_user_registration.info.yml new file mode 100644 index 0000000000000000000000000000000000000000..0de41e14b4ae9729918ce4d1a8b2f7a85f6f11f9 --- /dev/null +++ b/modules/persistent_visitor_parameters_user_registration/persistent_visitor_parameters_user_registration.info.yml @@ -0,0 +1,8 @@ +name: 'Persistent Visitor Parameters: User Registration' +type: module +description: 'Saves the UTM parameters & referrer on user registration in a dedicated table with views support to allow statistics on user registration sources.' +package: Custom +core_version_requirement: ^10 || ^11 +dependencies: + - persistent_visitor_parameters:persistent_visitor_parameters + - drupal:user diff --git a/modules/persistent_visitor_parameters_user_registration/persistent_visitor_parameters_user_registration.install b/modules/persistent_visitor_parameters_user_registration/persistent_visitor_parameters_user_registration.install new file mode 100644 index 0000000000000000000000000000000000000000..0a34eb627aff5fe20c33a964f1ff3102df3660c2 --- /dev/null +++ b/modules/persistent_visitor_parameters_user_registration/persistent_visitor_parameters_user_registration.install @@ -0,0 +1,62 @@ +<?php + +/** + * @file + * Install, update and uninstall functions for the Persistent Visitor Parameters User Registration module. + */ + +/** + * Implements hook_schema(). + */ +function persistent_visitor_parameters_user_registration_schema() { + $schema['persistent_visitor_parameters_user_registration'] = [ + 'description' => 'Stores UTM parameters for user registrations.', + 'fields' => [ + 'uid' => [ + 'description' => 'The user ID.', + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + ], + 'utm_source' => [ + 'description' => 'The UTM source parameter.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => FALSE, + ], + 'utm_medium' => [ + 'description' => 'The UTM medium parameter.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => FALSE, + ], + 'utm_campaign' => [ + 'description' => 'The UTM campaign parameter.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => FALSE, + ], + 'utm_term' => [ + 'description' => 'The UTM term parameter.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => FALSE, + ], + 'utm_content' => [ + 'description' => 'The UTM content parameter.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => FALSE, + ], + 'HTTP_REFERER' => [ + 'description' => 'The referrer parameter.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => FALSE, + ], + ], + 'primary key' => ['uid'], + ]; + + return $schema; +} diff --git a/modules/persistent_visitor_parameters_user_registration/persistent_visitor_parameters_user_registration.links.menu.yml b/modules/persistent_visitor_parameters_user_registration/persistent_visitor_parameters_user_registration.links.menu.yml new file mode 100644 index 0000000000000000000000000000000000000000..6e5bccfe1f5707ebf876c20a9bc947cc225bcfe3 --- /dev/null +++ b/modules/persistent_visitor_parameters_user_registration/persistent_visitor_parameters_user_registration.links.menu.yml @@ -0,0 +1,5 @@ +persistent_visitor_parameters_user_registration.settings: + title: Persistent Visitor Parameters User Registration Settings + description: Configuration for the Persistent Visitor Parameter User Registration Settings + parent: persistent_visitor_parameters.overview + route_name: persistent_visitor_parameters_user_registration.settings diff --git a/modules/persistent_visitor_parameters_user_registration/persistent_visitor_parameters_user_registration.links.task.yml b/modules/persistent_visitor_parameters_user_registration/persistent_visitor_parameters_user_registration.links.task.yml new file mode 100644 index 0000000000000000000000000000000000000000..3708dfb4878fde956a74ce449753cbfe0565a50c --- /dev/null +++ b/modules/persistent_visitor_parameters_user_registration/persistent_visitor_parameters_user_registration.links.task.yml @@ -0,0 +1,4 @@ +persistent_visitor_parameters_user_registration.settings: + title: User Registration Settings + route_name: persistent_visitor_parameters_user_registration.settings + base_route: persistent_visitor_parameters.settings diff --git a/modules/persistent_visitor_parameters_user_registration/persistent_visitor_parameters_user_registration.module b/modules/persistent_visitor_parameters_user_registration/persistent_visitor_parameters_user_registration.module new file mode 100644 index 0000000000000000000000000000000000000000..af9ca3e23e23ae35a071005f66523c33f56f7504 --- /dev/null +++ b/modules/persistent_visitor_parameters_user_registration/persistent_visitor_parameters_user_registration.module @@ -0,0 +1,138 @@ +<?php + +/** + * @file + * Module hooks for Persistent Visitor Parameters User Registration module. + */ + +use Drupal\Core\Database\Database; + +/** + * Implements hook_views_data(). + */ +function persistent_visitor_parameters_user_registration_views_data() { + $data = []; + + // Define the table for Views integration. + $data['persistent_visitor_parameters_user_registration'] = [ + 'table' => [ + 'group' => t('Persistent Visitor Parameters User Registration'), + 'provider' => 'persistent_visitor_parameters_user_registration', + 'join' => [ + 'users_field_data' => [ + 'left_field' => 'uid', + 'field' => 'uid', + ], + ], + 'base' => [ + 'field' => 'uid', + 'title' => t('User Registration UTM Parameters'), + 'help' => t('Contains User related UTM parameters.'), + ], + ], + 'uid' => [ + 'title' => t('User ID'), + 'help' => t('The user id.'), + 'field' => [ + 'id' => 'standard', + ], + 'filter' => [ + 'id' => 'numeric', + ], + 'sort' => [ + 'id' => 'standard', + ], + 'argument' => [ + 'id' => 'numeric', + ], + 'relationship' => [ + 'title' => t('User'), + 'help' => t('The user.'), + 'base' => 'users_field_data', + 'base field' => 'uid', + 'id' => 'standard', + ], + ], + ]; + + // Add fields to the table. + $fields = [ + 'utm_source' => t('UTM Source'), + 'utm_medium' => t('UTM Medium'), + 'utm_campaign' => t('UTM Campaign'), + 'utm_term' => t('UTM Term'), + 'utm_content' => t('UTM Content'), + 'HTTP_REFERER' => t('HTTP REFERER'), + ]; + + foreach ($fields as $field_name => $field_label) { + $data['persistent_visitor_parameters_user_registration'][$field_name] = [ + 'title' => $field_label, + 'help' => t('The @field_label parameter.', ['@field_label' => strtolower($field_label)]), + 'field' => [ + 'id' => 'standard', + ], + 'filter' => [ + 'id' => 'string', + ], + 'sort' => [ + 'id' => 'standard', + ], + 'argument' => [ + 'id' => 'string', + ], + ]; + } + return $data; +} + +/** + * Implements hook_user_insert(). + */ +function persistent_visitor_parameters_user_registration_user_insert($account) { + // If the logging is disabled, return early: + $enableUserLogging = \Drupal::config('persistent_visitor_parameters_user_registration.settings')->get('enable_utm_user_logging'); + if (!(bool) $enableUserLogging) { + return; + } + + // Only proceed if the user is not an admin user ($account->isNew() will + // return FALSE here, so we don't check it): + if (in_array('administrator', $account->getRoles())) { + return; + } + // Retrieve the cookie manager service. + $cookieManager = \Drupal::service('persistent_visitor_parameters.cookie_manager'); + + // Get cookies using the service. + $cookieContent = $cookieManager->getCookie(); + + // Check if the cookie content is empty (no UTM parameters found): + if (empty($cookieContent)) { + return; + } + + $utmKeys = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'HTTP_REFERER']; + $fieldsArray = array_filter($cookieContent, fn($key) => in_array($key, $utmKeys, TRUE), ARRAY_FILTER_USE_KEY); + + // No UTM parameters found. Return early: + if (empty($fieldsArray)) { + return; + } + + // Add the user ID to the array of fields: + $fieldsArray['uid'] = $account->id(); + + // Insert UTM parameters into the custom database table. + try { + $connection = Database::getConnection(); + $connection + ->insert('persistent_visitor_parameters_user_registration') + ->fields($fieldsArray) + ->execute(); + } + catch (\Exception $e) { + // Log the error. + \Drupal::logger('persistent_visitor_parameters_user_registration')->error($e->getMessage()); + } +} diff --git a/modules/persistent_visitor_parameters_user_registration/persistent_visitor_parameters_user_registration.routing.yml b/modules/persistent_visitor_parameters_user_registration/persistent_visitor_parameters_user_registration.routing.yml new file mode 100644 index 0000000000000000000000000000000000000000..7787b9f17716afbbee66a48d459ef8453555c405 --- /dev/null +++ b/modules/persistent_visitor_parameters_user_registration/persistent_visitor_parameters_user_registration.routing.yml @@ -0,0 +1,7 @@ +persistent_visitor_parameters_user_registration.settings: + path: '/admin/config/people/persistent-visitor-parameters/user-registration-settings' + defaults: + _title: 'User Registration Settings' + _form: 'Drupal\persistent_visitor_parameters_user_registration\Form\SettingsForm' + requirements: + _permission: 'administer site configuration' diff --git a/modules/persistent_visitor_parameters_user_registration/src/Form/SettingsForm.php b/modules/persistent_visitor_parameters_user_registration/src/Form/SettingsForm.php new file mode 100644 index 0000000000000000000000000000000000000000..7762909eba23243fc52b70fae6ecd21fef8a012d --- /dev/null +++ b/modules/persistent_visitor_parameters_user_registration/src/Form/SettingsForm.php @@ -0,0 +1,53 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\persistent_visitor_parameters_user_registration\Form; + +use Drupal\Core\Form\ConfigFormBase; +use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Url; + +/** + * Configure Persistent visitor parameters user registration settings. + */ +final class SettingsForm extends ConfigFormBase { + + /** + * {@inheritdoc} + */ + public function getFormId(): string { + return 'persistent_visitor_parameters_user_registration_settings'; + } + + /** + * {@inheritdoc} + */ + protected function getEditableConfigNames(): array { + return ['persistent_visitor_parameters_user_registration.settings']; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state): array { + $form['enable_utm_user_logging'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Enable UTM User Logging'), + '#default_value' => $this->config('persistent_visitor_parameters_user_registration.settings')->get('enable_utm_user_logging'), + '#description' => $this->t('Check this to enable logging of UTM parameters for new user registrations. ONLY the following parameters are supported: <ul><li>utm_source</li><li>utm_medium</li><li>utm_campaign</li><li>utm_term</li><li>utm_content</li><li>HTTP_REFERER</li></ul><br>Note, that you have to specify the following parameters <a href=":url" target="_blank">here</a> first:<br><strong>Get Parameters:</strong> "utm_source|utm_medium|utm_campaign|utm_term|utm_content"<br><strong>Server Parameters:</strong> "HTTP_REFERER"', [':url' => Url::fromRoute('persistent_visitor_parameters.settings')->toString()]), + ]; + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state): void { + $this->config('persistent_visitor_parameters_user_registration.settings') + ->set('enable_utm_user_logging', $form_state->getValue('enable_utm_user_logging')) + ->save(); + parent::submitForm($form, $form_state); + } + +} diff --git a/persistent_visitor_parameters.info.yml b/persistent_visitor_parameters.info.yml index d4ba3ea7ffc47228dc83d242c959a769323646b2..193d848b25e9bddb34382457d7e5197fcd265ea2 100644 --- a/persistent_visitor_parameters.info.yml +++ b/persistent_visitor_parameters.info.yml @@ -1,6 +1,6 @@ name: Persistent Visitor Parameters type: module description: 'Checks GET and HTTP Request parameters from anonymous visitors, and saves those in a cookie for further processing' -core_version_requirement: ^8.8 || ^9 || ^10 +core_version_requirement: ^8.8 || ^9 || ^10 || ^11 package: Custom -configure: persistent_visitor_parameters.settings \ No newline at end of file +configure: persistent_visitor_parameters.settings diff --git a/persistent_visitor_parameters.links.menu.yml b/persistent_visitor_parameters.links.menu.yml index 943da855dfa9340f485a9b7fa7773db208d371f7..ada260f697df05a3bea78d344ed28592e6fb3465 100644 --- a/persistent_visitor_parameters.links.menu.yml +++ b/persistent_visitor_parameters.links.menu.yml @@ -1,6 +1,10 @@ +persistent_visitor_parameters.overview: + title: 'Persistent Visitor Parameters' + parent: 'user.admin_index' + route_name: persistent_visitor_parameters.overview + description: 'Overview of the Persistent Visitor Parameters' persistent_visitor_parameters.settings: title: 'Persistent Visitor Parameters Settings' - parent: 'system.admin_config_content' + parent: 'persistent_visitor_parameters.overview' route_name: persistent_visitor_parameters.settings description: 'Configuration for the Persistent Visitor Parameters' - weight: 99 \ No newline at end of file diff --git a/persistent_visitor_parameters.links.task.yml b/persistent_visitor_parameters.links.task.yml new file mode 100644 index 0000000000000000000000000000000000000000..3617df305f2c71f6275d4e0446d45915665df618 --- /dev/null +++ b/persistent_visitor_parameters.links.task.yml @@ -0,0 +1,4 @@ +persistent_visitor_parameters.settings: + title: Settings + route_name: persistent_visitor_parameters.settings + base_route: persistent_visitor_parameters.settings diff --git a/persistent_visitor_parameters.routing.yml b/persistent_visitor_parameters.routing.yml index b3b5e7168d5fab5e98e12443e0f79a3467e4b8e2..ff3b9e8afe9612685e6b31cc6ea361bed2a143e2 100644 --- a/persistent_visitor_parameters.routing.yml +++ b/persistent_visitor_parameters.routing.yml @@ -1,5 +1,12 @@ +persistent_visitor_parameters.overview: + path: '/admin/config/people/persistent-visitor-parameters' + defaults: + _controller: '\Drupal\system\Controller\SystemController::systemAdminMenuBlockPage' + _title: 'Persistent Visitor Parameters' + requirements: + _permission: 'access administration pages' persistent_visitor_parameters.settings: - path: '/admin/config/persistent-visitor-parameters' + path: '/admin/config/people/persistent-visitor-parameters/settings' defaults: _form: 'Drupal\persistent_visitor_parameters\Form\SettingsForm' _title: 'Persistent Visitor Parameters Settings' diff --git a/src/CookieManager.php b/src/CookieManager.php index a6d674d7a14a919d05e9c2e8793ea30b244949c4..01273f8262e979b73505086d42271b22d4c0d22a 100644 --- a/src/CookieManager.php +++ b/src/CookieManager.php @@ -55,7 +55,8 @@ class CookieManager { $config = $this->configFactory->get('persistent_visitor_parameters.settings'); $mode = $config->get('mode'); - // if first-touch mode activated, and cookie already exists... skip the new one. Otherwise - replace it with a new one + // If first-touch mode activated, and cookie already exists... skip the new + // one. Otherwise - replace it with a new one: if (empty($mode) && !empty($this->getCookie())) { return; } @@ -82,7 +83,7 @@ class CookieManager { */ public function neededGetParams() { $config = $this->configFactory->get('persistent_visitor_parameters.settings'); - $getParamsConfig = array_filter(explode('|', $config->get('get_parameters'))); + $getParamsConfig = array_filter(array_map('trim', explode('|', $config->get('get_parameters')))); $queryParams = $this->requestStack->getCurrentRequest()->query->all(); return array_intersect_key($queryParams, array_flip($getParamsConfig)); } @@ -92,7 +93,7 @@ class CookieManager { */ public function neededServerParams() { $config = $this->configFactory->get('persistent_visitor_parameters.settings'); - $serverParamsConfig = array_filter(explode('|', $config->get('server_parameters'))); + $serverParamsConfig = array_filter(array_map('trim', explode('|', $config->get('server_parameters')))); $serverParams = $this->requestStack->getCurrentRequest()->server->all(); return array_intersect_key($serverParams, array_flip($serverParamsConfig)); } diff --git a/src/EventSubscriber/ResponseSubscriber.php b/src/EventSubscriber/ResponseSubscriber.php index d53d0219d2334f5f09344e29adcacf494399f296..c84731e3d3f6812cff7d380fbfe7c04fe57967bc 100644 --- a/src/EventSubscriber/ResponseSubscriber.php +++ b/src/EventSubscriber/ResponseSubscriber.php @@ -54,8 +54,7 @@ class ResponseSubscriber implements EventSubscriberInterface { return; } } - - // only non-logged users + // Set the cookie only for anonymous users: if (!$this->currentUser->isAnonymous()) { return; } diff --git a/src/Form/SettingsForm.php b/src/Form/SettingsForm.php index 9ede766d072b0628ae1017a3226dd2e6886f83a5..83b1cd17c632a6632a6dc44e989ab35808803dc4 100644 --- a/src/Form/SettingsForm.php +++ b/src/Form/SettingsForm.php @@ -106,4 +106,4 @@ class SettingsForm extends ConfigFormBase { ->save(); } -} \ No newline at end of file +}