Segment.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. 'use strict';
  2. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. /**
  5. * Licensed Materials - Property of IBM
  6. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2019, 2020
  7. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. */
  9. define(['underscore'], function (_) {
  10. var TRACK_TYPE_APPLICATION = 'application';
  11. var WIDGET_CATEGORY = 'InProduct';
  12. var WIDGET_DETAILS_PREFIX = 'custom.';
  13. var dataSourceInfoMap = {
  14. uploadedFile: 'custom.counts.uploadedFile',
  15. module: 'custom.counts.dataModule',
  16. package: 'custom.counts.fmPackage'
  17. };
  18. var widgetTypeMap = {
  19. data: 'custom.counts.data_widget',
  20. nondata: 'custom.counts.nondata_widget'
  21. };
  22. return function () {
  23. function Segment(options) {
  24. _classCallCheck(this, Segment);
  25. this.dashboardApi = options.dashboardApi;
  26. this.instrumentationSvc = options.instrumentationSvc;
  27. this.logger = options.logger;
  28. this.infoCallback = undefined;
  29. }
  30. Segment.prototype.registerInfoCallback = function registerInfoCallback(cb) {
  31. this.infoCallback = cb;
  32. };
  33. Segment.prototype.unregisterInfoCallback = function unregisterInfoCallback() {
  34. this.infoCallback = null;
  35. };
  36. Segment.prototype.track = function track(action, getInfoCallback) {
  37. try {
  38. var globalInfo = this.infoCallback ? this.infoCallback() : {};
  39. var suppliedInfo = getInfoCallback ? getInfoCallback() : {};
  40. suppliedInfo = _extends({}, globalInfo, suppliedInfo);
  41. suppliedInfo.action = action;
  42. var trackType = suppliedInfo.trackType || TRACK_TYPE_APPLICATION;
  43. delete suppliedInfo.trackType;
  44. if (this._canTrack(trackType)) {
  45. var payload = this.getInfo(suppliedInfo, trackType);
  46. payload.action = action;
  47. payload.milestoneName = payload.milestoneName ? payload.milestoneName : action + '_' + payload.objectType;
  48. this.instrumentationSvc.track(payload);
  49. }
  50. } catch (e) {
  51. this.logger.error(e);
  52. }
  53. };
  54. Segment.prototype._canTrack = function _canTrack(trackType) {
  55. return trackType === TRACK_TYPE_APPLICATION || this.dashboardApi.getApplicationName() !== 'explore';
  56. };
  57. Segment.prototype.getInfo = function getInfo() {
  58. var suppliedInfo = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  59. var trackType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : TRACK_TYPE_APPLICATION;
  60. return trackType === TRACK_TYPE_APPLICATION ? this._getApplicationInfo(suppliedInfo) : this._getWidgetInfo(suppliedInfo, trackType);
  61. };
  62. Segment.prototype._getApplicationInfo = function _getApplicationInfo() {
  63. var suppliedInfo = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  64. var detailsInfo = this._getApplicationDetailsInfo();
  65. return _extends({
  66. type: suppliedInfo.type,
  67. objectType: this.dashboardApi.getApplicationName(),
  68. object: this._getBoardId()
  69. }, detailsInfo, suppliedInfo);
  70. };
  71. Segment.prototype._getWidgetInfo = function _getWidgetInfo() {
  72. var suppliedInfo = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  73. var trackType = arguments[1];
  74. var details = {};
  75. if (suppliedInfo.details) {
  76. _.each(suppliedInfo.details, function (value, name) {
  77. details[WIDGET_DETAILS_PREFIX + name] = value;
  78. });
  79. }
  80. var payload = _extends({
  81. type: suppliedInfo.type,
  82. objectType: trackType,
  83. category: WIDGET_CATEGORY
  84. }, details);
  85. if (suppliedInfo.object) {
  86. payload.object = suppliedInfo.object;
  87. }
  88. payload.milestoneName = suppliedInfo.action + '_' + trackType + '_in_' + this.dashboardApi.getApplicationName();
  89. return payload;
  90. };
  91. Segment.prototype._getBoardId = function _getBoardId() {
  92. return this.dashboardApi.getDashboardInfo().boardId;
  93. };
  94. Segment.prototype._getApplicationDetailsInfo = function _getApplicationDetailsInfo() {
  95. var dataSourcesInfo = this._getApplicationDataSourcesInfo();
  96. var widgetsInfo = this._getApplicationWidgetsInfo();
  97. return _extends({}, dataSourcesInfo, widgetsInfo, {
  98. 'custom.counts.tabs': this._getApplicationTabsCount()
  99. });
  100. };
  101. Segment.prototype._getApplicationDataSourcesInfo = function _getApplicationDataSourcesInfo() {
  102. var dataSourceList = this.dashboardApi.getFeature('DataSources').getDataSourceList();
  103. return Segment.getDataSourcesTypeCount(dataSourceList);
  104. };
  105. Segment.prototype._getApplicationWidgetsInfo = function _getApplicationWidgetsInfo() {
  106. var widgets = this.dashboardApi.getCanvas().findContent({ type: 'widget' });
  107. return Segment.getWidgetsTypeCount(widgets);
  108. };
  109. Segment.prototype._getApplicationTabsCount = function _getApplicationTabsCount() {
  110. var tabs = this.dashboardApi.getCanvas().findContent({ type: 'container' });
  111. return tabs.length;
  112. };
  113. Segment.prototype.destroy = function destroy() {
  114. this.dashboardApi = null;
  115. this.glassContext = null;
  116. this.instrumentationSvc = null;
  117. };
  118. Segment.getDataSourcesTypeCount = function getDataSourcesTypeCount() {
  119. var dataSourceList = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  120. var info = dataSourceList.reduce(function (acc, dataSource) {
  121. var _extends2;
  122. var type = dataSource.getType();
  123. return _extends({}, acc, (_extends2 = {}, _extends2[dataSourceInfoMap[type]] = (acc[dataSourceInfoMap[type]] || 0) + 1, _extends2));
  124. }, {});
  125. return info;
  126. };
  127. Segment.getWidgetsTypeCount = function getWidgetsTypeCount() {
  128. var widgets = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  129. var info = widgets.reduce(function (acc, widget) {
  130. var _extends3;
  131. var type = widget.getType() === 'live' || widget.getType() === 'widget.live' ? 'data' : 'nondata';
  132. return _extends({}, acc, (_extends3 = {}, _extends3[widgetTypeMap[type]] = (acc[widgetTypeMap[type]] || 0) + 1, _extends3));
  133. }, {});
  134. return info;
  135. };
  136. return Segment;
  137. }();
  138. });
  139. //# sourceMappingURL=Segment.js.map