Make js/matomo.js independent from jQuery

Created on 24 August 2018, over 6 years ago
Updated 9 February 2023, almost 2 years ago

I am working in a site where we want to keep jQuery away from ordinary users, however we will still need it on admin pages etc. This is to make the page load and interactions more quick. Especially on mobile devices.

Drupal 8 core follows the same approach: jQuery is only included where it is strictly needed, not on every page (unlike Drupal 7).

The script in the js/matomo.js is needed for tracking mailto links and for colorbox integration. However it will trigger inclusion of jQuery everywhere on the site, even though we do not need the mailto tracking and colorbox integration.

Could we change the module in one of two ways:

1) Leave out this script file (and jQuery) if we do not need it (no mailto / no colorbox)?

- or -

2) Modify the script not to be dependent of jQuery? I tried to convert it here:

/**
 * @file
 * Attaches several event listener to a web page.
 */

(function () {
  "use strict";

  // Polyfill for closest().
  if (!Element.prototype.matches)
    Element.prototype.matches = Element.prototype.msMatchesSelector ||
        Element.prototype.webkitMatchesSelector;

  if (!Element.prototype.closest)
    Element.prototype.closest = function(s) {
      var el = this;
      if (!document.documentElement.contains(el)) return null;
      do {
        if (el.matches(s)) return el;
        el = el.parentElement || el.parentNode;
      } while (el !== null && el.nodeType === 1);
      return null;
    };

  // Callback for events
  function matomoHandleClick(event) {
    var linkElement;

    // Catch the closest surrounding link of a clicked element.
    linkElement = event.target.closest('a,area');

    if (
      linkElement
      && linkElement.href.length
      && linkElement.href.substr(0, 7) === 'mailto:'
      && window.drupalSettings.matomo.trackMailto
    ) {
      // Mailto link clicked.
      _paq.push(['trackEvent', 'Mails', 'Click', linkElement.href.substring(7)]);
    }
  }

  // Main functionality - wait for DOM ready.
  document.addEventListener('DOMContentLoaded', function() {
    // Attach mousedown, keyup, touchstart events to document only and catch
    // clicks on all elements.
    var bodyElement = document.querySelector('body');
    bodyElement.addEventListener('mousedown', matomoHandleClick, true);
    bodyElement.addEventListener('keyup', matomoHandleClick, true);
    bodyElement.addEventListener('touchstart', matomoHandleClick, true);

    // Colorbox: This event triggers when the transition has completed and the
    // newly loaded content has been revealed.
    // Because Colorbox depends on jQuery, we can assume jQuery
    // is present in this case.
    if (typeof jQuery !== 'undefined' && window.drupalSettings.matomo.trackColorbox) {
      jQuery(document).bind('cbox_complete', function () {
        var href = jQuery.colorbox.element().attr('href');
        if (href) {
          _paq.push(['setCustomUrl', href]);
          _paq.push(['trackPageView']);
        }
      });
    }

  });

})();

I can work out a patch if needed.

Feature request
Status

Needs review

Version

1.0

Component

Code

Created by

🇩🇰Denmark bjaxelsen

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024