Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

middleware.ts 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* eslint-disable lines-around-comment */
  2. import { IState } from '../app/types';
  3. import {
  4. CONFERENCE_JOINED,
  5. CONFERENCE_WILL_LEAVE,
  6. SET_ROOM
  7. } from '../base/conference/actionTypes';
  8. import { SET_CONFIG } from '../base/config/actionTypes';
  9. import { SET_NETWORK_INFO } from '../base/net-info/actionTypes';
  10. import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
  11. import {
  12. TRACK_ADDED,
  13. TRACK_REMOVED,
  14. TRACK_UPDATED
  15. } from '../base/tracks/actionTypes';
  16. import {
  17. getLocalAudioTrack,
  18. getLocalVideoTrack
  19. } from '../base/tracks/functions';
  20. import { createLocalTracksDurationEvent, createNetworkInfoEvent } from './AnalyticsEvents';
  21. import { UPDATE_LOCAL_TRACKS_DURATION } from './actionTypes';
  22. import { createHandlers, initAnalytics, resetAnalytics, sendAnalytics } from './functions';
  23. /**
  24. * Calculates the duration of the local tracks.
  25. *
  26. * @param {Object} state - The redux state.
  27. * @returns {Object} - The local tracks duration.
  28. */
  29. function calculateLocalTrackDuration(state: IState) {
  30. const now = Date.now();
  31. const { localTracksDuration } = state['features/analytics'];
  32. const { conference } = state['features/base/conference'];
  33. const { audio, video } = localTracksDuration;
  34. const { camera, desktop } = video;
  35. const tracks = state['features/base/tracks'];
  36. const audioTrack = getLocalAudioTrack(tracks);
  37. const videoTrack = getLocalVideoTrack(tracks);
  38. const newDuration = { ...localTracksDuration };
  39. if (!audioTrack || audioTrack.muted || !conference) {
  40. newDuration.audio = {
  41. startedTime: -1,
  42. value: audio.value + (audio.startedTime === -1 ? 0 : now - audio.startedTime)
  43. };
  44. } else if (audio.startedTime === -1) {
  45. newDuration.audio.startedTime = now;
  46. }
  47. if (!videoTrack || videoTrack.muted || !conference) {
  48. newDuration.video = {
  49. camera: {
  50. startedTime: -1,
  51. value: camera.value + (camera.startedTime === -1 ? 0 : now - camera.startedTime)
  52. },
  53. desktop: {
  54. startedTime: -1,
  55. value: desktop.value + (desktop.startedTime === -1 ? 0 : now - desktop.startedTime)
  56. }
  57. };
  58. } else {
  59. const { videoType } = videoTrack;
  60. if (video[videoType as keyof typeof video].startedTime === -1) {
  61. newDuration.video[videoType as keyof typeof video].startedTime = now;
  62. }
  63. }
  64. return {
  65. ...localTracksDuration,
  66. ...newDuration
  67. };
  68. }
  69. /**
  70. * Middleware which intercepts config actions to handle evaluating analytics
  71. * config based on the config stored in the store.
  72. *
  73. * @param {Store} store - The redux store.
  74. * @returns {Function}
  75. */
  76. MiddlewareRegistry.register(store => next => action => {
  77. switch (action.type) {
  78. case SET_CONFIG:
  79. if (navigator.product === 'ReactNative') {
  80. // Resetting the analytics is currently not needed for web because
  81. // the user will be redirected to another page and new instance of
  82. // Analytics will be created and initialized.
  83. resetAnalytics();
  84. }
  85. break;
  86. case SET_ROOM: {
  87. // createHandlers is called before the SET_ROOM action is executed in order for Amplitude to initialize before
  88. // the deeplinking logic is executed (after the SET_ROOM action) so that the Amplitude device id is available
  89. // if needed.
  90. const createHandlersPromise = createHandlers(store);
  91. const result = next(action);
  92. createHandlersPromise.then(handlers => {
  93. initAnalytics(store, handlers);
  94. });
  95. return result;
  96. }
  97. }
  98. const result = next(action);
  99. switch (action.type) {
  100. case CONFERENCE_JOINED: {
  101. const { dispatch, getState } = store;
  102. const state = getState();
  103. dispatch({
  104. type: UPDATE_LOCAL_TRACKS_DURATION,
  105. localTracksDuration: {
  106. ...calculateLocalTrackDuration(state),
  107. conference: {
  108. startedTime: Date.now(),
  109. value: 0
  110. }
  111. }
  112. });
  113. break;
  114. }
  115. case CONFERENCE_WILL_LEAVE: {
  116. const { dispatch, getState } = store;
  117. const state = getState();
  118. const { localTracksDuration } = state['features/analytics'];
  119. const newLocalTracksDuration = {
  120. ...calculateLocalTrackDuration(state),
  121. conference: {
  122. startedTime: -1,
  123. value: Date.now() - localTracksDuration.conference.startedTime
  124. }
  125. };
  126. sendAnalytics(createLocalTracksDurationEvent(newLocalTracksDuration));
  127. dispatch({
  128. type: UPDATE_LOCAL_TRACKS_DURATION,
  129. localTracksDuration: newLocalTracksDuration
  130. });
  131. break;
  132. }
  133. case SET_NETWORK_INFO:
  134. sendAnalytics(
  135. createNetworkInfoEvent({
  136. isOnline: action.isOnline,
  137. details: action.details,
  138. networkType: action.networkType
  139. }));
  140. break;
  141. case TRACK_ADDED:
  142. case TRACK_REMOVED:
  143. case TRACK_UPDATED: {
  144. const { dispatch, getState } = store;
  145. const state = getState();
  146. const { localTracksDuration } = state['features/analytics'];
  147. if (localTracksDuration.conference.startedTime === -1 || action.mediaType === 'presenter') {
  148. // We don't want to track the media duration if the conference is not joined yet because otherwise we won't
  149. // be able to compare them with the conference duration (from conference join to conference will leave).
  150. // Also, do not track media duration for presenter tracks.
  151. break;
  152. }
  153. dispatch({
  154. type: UPDATE_LOCAL_TRACKS_DURATION,
  155. localTracksDuration: {
  156. ...localTracksDuration,
  157. ...calculateLocalTrackDuration(state)
  158. }
  159. });
  160. break;
  161. }
  162. }
  163. return result;
  164. });