InstrumentationUtil.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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(['../../../features/dashboard/segment/api/impl/Segment'], function (Segment) {
  10. return function () {
  11. function InstrumentationUtil() {
  12. _classCallCheck(this, InstrumentationUtil);
  13. }
  14. /**
  15. * Calls the glass service directly to track a user action
  16. *
  17. * @param {object} The glass context
  18. * @param {string} The action to track
  19. * @param {object} Contains the context to track
  20. */
  21. InstrumentationUtil.track = function track(glassContext, action) {
  22. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  23. var instrumentationSvc = glassContext.getCoreSvc('.Instrumentation');
  24. if (instrumentationSvc && instrumentationSvc.enabled) {
  25. var widgetsCount = {};
  26. var datasourcesCount = {};
  27. if (options.sources && options.sources.length > 0) {
  28. var dataSourceList = options.sources.map(function (source) {
  29. if (source.getType) {
  30. return source;
  31. } else {
  32. return {
  33. getType: function getType() {
  34. return source.type;
  35. }
  36. };
  37. }
  38. });
  39. datasourcesCount = Segment.getDataSourcesTypeCount(dataSourceList);
  40. }
  41. if (options.widgets && options.widgets.length > 0) {
  42. var widgetList = options.widgets.map(function (widget) {
  43. if (widget.getType) {
  44. return widget;
  45. } else {
  46. return {
  47. getType: function getType() {
  48. return widget.type;
  49. }
  50. };
  51. }
  52. });
  53. widgetsCount = Segment.getWidgetsTypeCount(widgetList);
  54. }
  55. var payload = _extends({
  56. type: options.type,
  57. objectType: options.objectType,
  58. action: action,
  59. milestoneName: action + '_' + options.objectType
  60. }, datasourcesCount, widgetsCount);
  61. if (options.object) {
  62. payload.object = options.object;
  63. }
  64. instrumentationSvc.track(payload);
  65. }
  66. };
  67. InstrumentationUtil.trackWidget = function trackWidget(action, dashboardApi) {
  68. var widgetSpec = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  69. var widgetId = arguments[3];
  70. var widgetType = void 0;
  71. var trackType = void 0;
  72. var widgetSource = void 0;
  73. if (action === 'deleted') {
  74. var canvasAPI = dashboardApi.getCanvas();
  75. var contentAPI = canvasAPI.getContent(widgetId);
  76. if (contentAPI) {
  77. if (contentAPI.getType() === 'widget.live') {
  78. trackType = 'data_widget';
  79. var stringResources = dashboardApi.getDashboardCoreSvc('.StringResources');
  80. var visualization = contentAPI.getFeature('Visualization');
  81. var error = contentAPI.getFeature('state').getError() || null;
  82. var params = error ? error.getParams() : null;
  83. var errorInfo = params ? params.errorInfo : null;
  84. widgetType = errorInfo ? errorInfo.id : visualization.getDefinition() ? visualization.getDefinition().getLabel() : stringResources.get('one_unknown_live');
  85. } else {
  86. trackType = 'nonData_widget';
  87. widgetType = contentAPI.getType();
  88. }
  89. }
  90. } else {
  91. var getWidgetSource = function getWidgetSource() {
  92. return widgetSpec && widgetSpec.data && widgetSpec.data.dataViews && widgetSpec.data.dataViews.length ? 'fromRecommendation' : 'manual';
  93. };
  94. widgetSource = getWidgetSource();
  95. //Recommendation is being tracked from another component
  96. if (widgetSource !== 'fromRecommendation') {
  97. trackType = widgetSpec.type === 'live' ? 'data_widget' : 'nonData_widget';
  98. widgetType = widgetSpec.name || widgetSpec.type;
  99. }
  100. }
  101. if (widgetType) {
  102. var callback = function callback() {
  103. var payload = {
  104. details: {
  105. widgetType: widgetType,
  106. assetType: dashboardApi.getApplicationName()
  107. },
  108. type: action === 'deleted' ? 'Deleted Object' : 'Created Object',
  109. trackType: trackType,
  110. widgetType: widgetType
  111. };
  112. if (widgetId) {
  113. payload.object = widgetId;
  114. }
  115. var assetId = dashboardApi.getDashboardInfo().boardId;
  116. if (assetId) {
  117. payload.details['assetId'] = assetId;
  118. }
  119. if (widgetSource) {
  120. payload.widgetSource = widgetSource; //'created'
  121. payload.details.widgetSource = widgetSource; //'created'
  122. }
  123. return payload;
  124. };
  125. var segment = dashboardApi.getFeature('segment');
  126. segment.track(action, callback);
  127. }
  128. };
  129. return InstrumentationUtil;
  130. }();
  131. });
  132. //# sourceMappingURL=InstrumentationUtil.js.map