Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

202411 consentratio lp 135 #495

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from

Conversation

anthonylindsay
Copy link
Collaborator

Adds a custom module to track cookie consent choices to the database without GA.
Loads Javascript to listen for EU Cookie choice selection, logs choice and timestamp to the DB.
Tracking can be turned on or off via settings screen.
https://eccservicetransformation.atlassian.net/browse/LP-135

label: 'ECC Cookie Consent Tracker settings'
mapping:
enable:
type: string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not boolean?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exact why is lost in the mists of history, but I'm guessing it's an artifact of drush generate:module. It really doesn't matter one way or the other. It's stored as a string, it's read as a string, it works. I don't see it as having much impact either way? I mean, we can change it, but it'll be change for change's sake.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generate may be quick but it's no guarantee of good code.

@@ -0,0 +1,11 @@
# Essex Co Co cookie consent statistics.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, what am I missing? I don't see it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The abbreviation is confusing. Change to Essex County Council or ECC, like you have in other places.

// several times per page load.
count++;
if (count === 2) {
console.log(response);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the console logs or add a debug mode so we can control if they are written.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

console.log removed, PR updated.

];

/** @var \Drupal\Core\Database\Connection $connection */
$connection = \Drupal::service('database');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use a content entity instead of directly accessing the database table. It would make CRUD much easier.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah. The idea is to make this as minimalistic as possible, with the least overhead. Entities come with overhead. The DB write here couldn't be simpler, and the data extraction is a single SQL query. Entities and all of Drupal's APIs would be needless expense. In fact, we only ever want to do create and read, never update nor delete. We're doing just enough, and no more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Don't call services like this. Use create() for dependency injection.

web/modules/custom/ecc_cct/ecc_cct.install Show resolved Hide resolved
@@ -0,0 +1,11 @@
# Essex Co Co cookie consent statistics.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The abbreviation is confusing. Change to Essex County Council or ECC, like you have in other places.

label: 'ECC Cookie Consent Tracker settings'
mapping:
enable:
type: string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generate may be quick but it's no guarantee of good code.


async function ecc_cct_log(choice) {
const response = await fetch('/ecc-cct/js/log/' + choice).catch(error => {
console.error('Error sending integer:', error);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do users need to see anything in the console?

0 => $this->t('Disabled'),
1 => $this->t('Enabled'),
],
'#default_value' => $this->config('ecc_cct.settings')->get('enable'),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add #config_target and you should be able to remove default_value and the submitForm method.

*/
public function log(int $choice, Request $request): Response {
// If this isn't turned on, bail.
if (!\Drupal::config('ecc_cct.settings')->get('enable')) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would probably be faster (one database read?) to use a State setting instead of config. Either way use the methods state() or config() in ControllerBase.

];

/** @var \Drupal\Core\Database\Connection $connection */
$connection = \Drupal::service('database');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Don't call services like this. Use create() for dependency injection.

* Implements hook_preprocess_html().
*/
function ecc_cct_preprocess_html(&$vars) {
if ($vars['root_path'] !== 'admin') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to exclude the same paths as EU Cookie Compliance

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes as follows:

  1. using eu cookie settings for excluded paths
  2. using dependency injection for db service access
  3. using config() from controllerbase to access config
  4. using #config_target instead of default_value
  5. js is printing nothing to console now
  6. changed config schema to use integer as integer is what's actually used (boolean makes radio button choke)

@Polynya
Copy link
Collaborator

Polynya commented Nov 27, 2024

/start

Copy link

Starting Review App.

Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants