Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

MatomoHandler.ts 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* global _paq */
  2. import { getJitsiMeetGlobalNS } from '../../base/util/helpers';
  3. import AbstractHandler, { IEvent } from './AbstractHandler';
  4. /**
  5. * Analytics handler for Matomo.
  6. */
  7. export default class MatomoHandler extends AbstractHandler {
  8. _userProperties: Object;
  9. /**
  10. * Creates new instance of the Matomo handler.
  11. *
  12. * @param {Object} options - The matomo options.
  13. * @param {string} options.matomoEndpoint - The Matomo endpoint.
  14. * @param {string} options.matomoSiteID - The site ID.
  15. */
  16. constructor(options: any) {
  17. super(options);
  18. this._userProperties = {};
  19. if (!options.matomoEndpoint) {
  20. throw new Error(
  21. 'Failed to initialize Matomo handler: no endpoint defined.'
  22. );
  23. }
  24. if (!options.matomoSiteID) {
  25. throw new Error(
  26. 'Failed to initialize Matomo handler: no site ID defined.'
  27. );
  28. }
  29. this._enabled = true;
  30. this._initMatomo(options);
  31. }
  32. /**
  33. * Initializes the _paq object.
  34. *
  35. * @param {Object} options - The matomo options.
  36. * @param {string} options.matomoEndpoint - The Matomo endpoint.
  37. * @param {string} options.matomoSiteID - The site ID.
  38. * @returns {void}
  39. */
  40. _initMatomo(options: any) {
  41. // @ts-ignore
  42. const _paq = window._paq || [];
  43. // @ts-ignore
  44. window._paq = _paq;
  45. _paq.push([ 'trackPageView' ]);
  46. _paq.push([ 'enableLinkTracking' ]);
  47. (function() {
  48. // add trailing slash if needed
  49. const u = options.matomoEndpoint.endsWith('/')
  50. ? options.matomoEndpoint
  51. : `${options.matomoEndpoint}/`;
  52. // configure the tracker
  53. _paq.push([ 'setTrackerUrl', `${u}matomo.php` ]);
  54. _paq.push([ 'setSiteId', options.matomoSiteID ]);
  55. // insert the matomo script
  56. const d = document,
  57. g = d.createElement('script'),
  58. s = d.getElementsByTagName('script')[0];
  59. g.type = 'text/javascript';
  60. g.async = true;
  61. g.defer = true;
  62. g.src = `${u}matomo.js`;
  63. s.parentNode?.insertBefore(g, s);
  64. })();
  65. }
  66. /**
  67. * Extracts the integer to use for a Matomo event's value field
  68. * from a lib-jitsi-meet analytics event.
  69. *
  70. * @param {Object} event - The lib-jitsi-meet analytics event.
  71. * @returns {number} - The integer to use for the 'value' of a Matomo
  72. * event, or NaN if the lib-jitsi-meet event doesn't contain a
  73. * suitable value.
  74. * @private
  75. */
  76. _extractValue(event: IEvent) {
  77. const value = event?.attributes?.value;
  78. // Try to extract an integer from the 'value' attribute.
  79. return Math.round(parseFloat(value ?? ''));
  80. }
  81. /**
  82. * Sets the permanent properties for the current session.
  83. *
  84. * @param {Object} userProps - The permanent properties.
  85. * @returns {void}
  86. */
  87. setUserProperties(userProps: any = {}) {
  88. if (!this._enabled) {
  89. return;
  90. }
  91. const visitScope = [ 'user_agent', 'callstats_name', 'browser_name' ];
  92. // add variables in the 'page' scope
  93. Object.keys(userProps)
  94. .filter(key => visitScope.indexOf(key) === -1)
  95. .forEach((key, index) => {
  96. // @ts-ignore
  97. _paq.push([
  98. 'setCustomVariable',
  99. 1 + index,
  100. key,
  101. userProps[key],
  102. 'page'
  103. ]);
  104. });
  105. // add variables in the 'visit' scope
  106. Object.keys(userProps)
  107. .filter(key => visitScope.indexOf(key) !== -1)
  108. .forEach((key, index) => {
  109. // @ts-ignore
  110. _paq.push([
  111. 'setCustomVariable',
  112. 1 + index,
  113. key,
  114. userProps[key],
  115. 'visit'
  116. ]);
  117. });
  118. }
  119. /**
  120. * This is the entry point of the API. The function sends an event to
  121. * the Matomo endpoint. The format of the event is described in
  122. * analyticsAdapter in lib-jitsi-meet.
  123. *
  124. * @param {Object} event - The event in the format specified by
  125. * lib-jitsi-meet.
  126. * @returns {void}
  127. */
  128. sendEvent(event: IEvent) {
  129. if (this._shouldIgnore(event)) {
  130. return;
  131. }
  132. const value = this._extractValue(event);
  133. const matomoEvent: Array<string | number | undefined> = [
  134. 'trackEvent', 'jitsi-meet', this._extractName(event) ];
  135. if (!isNaN(value)) {
  136. matomoEvent.push(value);
  137. }
  138. // @ts-ignore
  139. _paq.push(matomoEvent);
  140. }
  141. }
  142. const globalNS = getJitsiMeetGlobalNS();
  143. globalNS.analyticsHandlers = globalNS.analyticsHandlers || [];
  144. globalNS.analyticsHandlers.push(MatomoHandler);