Skip to content

Commit

Permalink
UHF-9378: move user role logic to theme file
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mys committed Feb 8, 2024
1 parent dd0be98 commit 3ad709d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
2 changes: 1 addition & 1 deletion dist/js/matomo.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions hdbt.theme
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,20 @@ function hdbt_preprocess_html(&$variables): void {

$variables['#attached']['library'][] = 'hdbt/cssmenu';

// Add Matomo analytics.
$variables['#attached']['library'][] = 'hdbt/matomo';
// Add Matomo analytics for certain users (now anonymous only)
// Possibly some other roles on future
$user_roles = Drupal::currentUser()->getAccount()->getRoles();
$allowed_roles = ['anonymous']; // To be expanded if needed
$add_matomo = false;
foreach($user_roles as $role) {
if (in_array($role, $allowed_roles)) {
$add_matomo = true;
break;
}
}
if ($add_matomo) {
$variables['#attached']['library'][] = 'hdbt/matomo';
}

// Disable Genesys button if the chat element does not exist.
$variables['#attached']['library'][] = 'hdbt/disable_genesys_button';
Expand Down
17 changes: 1 addition & 16 deletions src/js/matomo.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
// eslint-disable-next-line func-names
(function ($, Drupal) {
function loadMatomoAnalytics() {
// Check if the user qualifies for Matomo
let followUser = false;
// Currently limited to anonymous users, might need some role
// based ID fetching later on
const followedUserIds = [0];

try {
followUser = followedUserIds.includes(drupalSettings.user.uid);
} catch (err) {
console.log('user settings not available');
}

if (typeof Drupal.eu_cookie_compliance === 'undefined') {
return;
}

// Load Matomo only if statistics cookies are allowed.
if (
Drupal.eu_cookie_compliance.hasAgreed('statistics') &&
followUser
) {
if (Drupal.eu_cookie_compliance.hasAgreed('statistics')) {
// Matomo Tag Manager
// eslint-disable-next-line no-multi-assign
const _mtm = (window._mtm = window._mtm || []);
Expand Down

0 comments on commit 3ad709d

Please sign in to comment.