You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

analytics-ga.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* global ga */
  2. (function(ctx) {
  3. /**
  4. *
  5. */
  6. function Analytics() {
  7. /* eslint-disable */
  8. /**
  9. * Google Analytics
  10. */
  11. (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  12. (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  13. })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  14. ga('create', 'UA-319188-14', 'jit.si');
  15. ga('send', 'pageview');
  16. /* eslint-enable */
  17. }
  18. /**
  19. * Extracts the integer to use for a Google Analytics event's value field
  20. * from a lib-jitsi-meet analytics event.
  21. * @param {Object} event - The lib-jitsi-meet analytics event.
  22. * @returns {Object} - The integer to use for the 'value' of a Google
  23. * Analytics event.
  24. * @private
  25. */
  26. Analytics.prototype._extractAction = function(event) {
  27. // Page events have a single 'name' field.
  28. if (event.type === 'page') {
  29. return event.name;
  30. }
  31. // All other events have action, actionSubject, and source fields. All
  32. // three fields are required, and the often jitsi-meet and
  33. // lib-jitsi-meet use the same value when separate values are not
  34. // necessary (i.e. event.action == event.actionSubject).
  35. // Here we concatenate these three fields, but avoid adding the same
  36. // value twice, because it would only make the GA event's action harder
  37. // to read.
  38. let action = event.action;
  39. if (event.actionSubject && event.actionSubject !== event.action) {
  40. // Intentionally use string concatenation as analytics needs to
  41. // work on IE but this file does not go through babel. For some
  42. // reason disabling this globally for the file does not have an
  43. // effect.
  44. // eslint-disable-next-line prefer-template
  45. action = event.actionSubject + '.' + action;
  46. }
  47. if (event.source && event.source !== event.action
  48. && event.source !== event.action) {
  49. // eslint-disable-next-line prefer-template
  50. action = event.source + '.' + action;
  51. }
  52. return action;
  53. };
  54. /**
  55. * Extracts the integer to use for a Google Analytics event's value field
  56. * from a lib-jitsi-meet analytics event.
  57. * @param {Object} event - The lib-jitsi-meet analytics event.
  58. * @returns {Object} - The integer to use for the 'value' of a Google
  59. * Analytics event, or NaN if the lib-jitsi-meet event doesn't contain a
  60. * suitable value.
  61. * @private
  62. */
  63. Analytics.prototype._extractValue = function(event) {
  64. let value = event && event.attributes && event.attributes.value;
  65. // Try to extract an integer from the "value" attribute.
  66. value = Math.round(parseFloat(value));
  67. return value;
  68. };
  69. /**
  70. * Extracts the string to use for a Google Analytics event's label field
  71. * from a lib-jitsi-meet analytics event.
  72. * @param {Object} event - The lib-jitsi-meet analytics event.
  73. * @returns {string} - The string to use for the 'label' of a Google
  74. * Analytics event.
  75. * @private
  76. */
  77. Analytics.prototype._extractLabel = function(event) {
  78. let label = '';
  79. // The label field is limited to 500B. We will concatenate all
  80. // attributes of the event, except the user agent because it may be
  81. // lengthy and is probably included from elsewhere.
  82. for (const property in event.attributes) {
  83. if (property !== 'permanent_user_agent'
  84. && event.attributes.hasOwnProperty(property)) {
  85. // eslint-disable-next-line prefer-template
  86. label += property + '=' + event.attributes[property] + '&';
  87. }
  88. }
  89. if (label.length > 0) {
  90. label = label.slice(0, -1);
  91. }
  92. return label;
  93. };
  94. /**
  95. * This is the entry point of the API. The function sends an event to
  96. * google analytics. The format of the event is described in
  97. * AnalyticsAdapter in lib-jitsi-meet.
  98. * @param {Object} event - the event in the format specified by
  99. * lib-jitsi-meet.
  100. */
  101. Analytics.prototype.sendEvent = function(event) {
  102. if (!event) {
  103. return;
  104. }
  105. const gaEvent = {
  106. 'eventCategory': 'jitsi-meet',
  107. 'eventAction': this._extractAction(event),
  108. 'eventLabel': this._extractLabel(event)
  109. };
  110. const value = this._extractValue(event);
  111. if (!isNaN(value)) {
  112. gaEvent.eventValue = value;
  113. }
  114. ga('send', 'event', gaEvent);
  115. };
  116. if (typeof ctx.JitsiMeetJS === 'undefined') {
  117. ctx.JitsiMeetJS = {};
  118. }
  119. if (typeof ctx.JitsiMeetJS.app === 'undefined') {
  120. ctx.JitsiMeetJS.app = {};
  121. }
  122. if (typeof ctx.JitsiMeetJS.app.analyticsHandlers === 'undefined') {
  123. ctx.JitsiMeetJS.app.analyticsHandlers = [];
  124. }
  125. ctx.JitsiMeetJS.app.analyticsHandlers.push(Analytics);
  126. })(window);
  127. /* eslint-enable prefer-template */