Add support for other services

Created on 19 July 2023, over 1 year ago
Updated 23 August 2023, over 1 year ago

Problem/Motivation

We use a custom module that is pretty similar to what the tarte_au_citron module does (only it is based on the tacjs module). It detects which modules are installed and enables these services in tarteaucitron.js automatically.

Our module supports some modules that tarte_au_citron does not :

  • google_analytics
  • matomo
  • fb_likebox
  • facebook_pixel

If we could add support for these in tarte_au_citron, we could use it instead of our custom module.

Proposed resolution

I don't have time to submit patches right now, but I can share the logic we use to detect if the module is enabled and to prevent it from loading its scripts directly.

It would of course need to be adapted for tarte_au_citron, but I think most of the conditions (to see if the module is enabled or to detect the script in attachments) could be reused.

google_analytics

  $cache = CacheableMetadata::createFromRenderArray($attachments);

  $gaSettings = Drupal::config('google_analytics.settings');
  $cache->addCacheableDependency($gaSettings);

  if ($ua = $gaSettings->get('account')) {
    $services[] = 'gtag';
    $variables['gtagUa'] = $ua;

    if (isset($attachments['#attached']['html_head'])) {
      foreach ($attachments['#attached']['html_head'] as $i => $attachment) {
        if (in_array($attachment[1], [
          'google_analytics_tracking_file',
          'google_analytics_tracking_script',
        ])) {
          unset($attachments['#attached']['html_head'][$i]);
        }
      }
    }
  }

  $cache->applyTo($attachments);

facebook_pixel

  $cache = CacheableMetadata::createFromRenderArray($attachments);

  if (Drupal::service('update.update_hook_registry')
    ->getInstalledVersion('facebook_pixel') >= 8101) {
    $settings = Drupal::config('facebook_pixel.settings');
  }
  else {
    $settings = Drupal::config('facebook_pixel.facebookpixelconfig');
  }
  $cache->addCacheableDependency($settings);

  if ($id = $settings->get('facebook_id')) {
    $services[] = 'facebookpixel';
    $variables['facebookpixelId'] = $id;

    if (isset($attachments['#attached']['html_head'])) {
      foreach ($attachments['#attached']['html_head'] as $i => $attachment) {
        if ($attachment[1] == 'facebook_tracking_pixel_script') {
          unset($attachments['#attached']['html_head'][$i]);
        }
      }
    }
    if (isset($attachments['#attached']['library'])) {
      foreach ($attachments['#attached']['library'] as $i => $library) {
        if ($library == 'facebook_pixel/facebook_pixel') {
          unset($attachments['#attached']['library'][$i]);
        }
      }
    }
  }

  $cache->applyTo($attachments);

matomo

  $cache = CacheableMetadata::createFromRenderArray($attachments);

  $matomoSettings = Drupal::config('matomo.settings');
  if ($matomoSettings->isNew()) {
    $matomoSettings = Drupal::config('piwik.settings');
  }
  $cache->addCacheableDependency($matomoSettings);

  if ($site_id = $matomoSettings->get('site_id')) {
    if ($matomo_url = $matomoSettings->get('url_http')) {
      if ($matomo_url_https = $matomoSettings->get('url_https')) {
        $matomo_url = $matomo_url_https;
      }

      $services[] = 'matomohightrack';
      $variables['matomoId'] = $site_id;
      $variables['matomoHost'] = $matomo_url;

      if (isset($attachments['#attached']['html_head'])) {
        foreach ($attachments['#attached']['html_head'] as $i => $attachment) {
          if ($attachment[1] == 'matomo_tracking_script') {
            unset($attachments['#attached']['html_head'][$i]);
          }
        }
      }
    }
  }

  $cache->applyTo($attachments);

fb_likebox

  if (\Drupal::entityQuery('block')
    ->condition('plugin', 'fb_likebox_block')
    ->accessCheck()
    ->count()
    ->execute() > 0) {
    $services[] = 'facebooklikebox';
  }

With a preprocess to alter the block:

/**
 * Implements hook_preprocess_HOOK().
 *
 * @phpstan-param array<string, mixed> $variables
 */
function insite_tarteaucitron_preprocess_block(&$variables): void {
  if ($variables['plugin_id'] == 'fb_likebox_block') {
    unset($variables['content']['#attached']['library']);
  }
}
Feature request
Status

Needs work

Version

1.0

Component

Code

Created by

🇫🇷France prudloff Lille

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @prudloff
  • Status changed to Needs work over 1 year ago
  • 🇫🇷France klelostec

    Hello @prudloff
    Thanks for your proposal.
    At this time, I freeze the implementation of new services support. The reason is there is a lot of tarte au citron services and to make easier this module management I plan to move submodules outside this one.
    This way, we'll be able to manage services module releases indenpendently from this one like jquery_ui module does.
    And the most important reason is to manage compatibilities with other contrib module versions. For example, at this time tarte_au_citron_googletagmanager is only compatible with google_tag version 1.6 while a 2.x version is available.

    It will be very helpful if you create the module for the wanted services and I will be very happy to reference them here.

Production build 0.71.5 2024