Skip to content

Commit

Permalink
EPMRPP-87528 || Code Review fix - 2
Browse files Browse the repository at this point in the history
  • Loading branch information
BlazarQSO committed Dec 4, 2023
1 parent 0428ed3 commit a7e6e79
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 46 deletions.
32 changes: 7 additions & 25 deletions app/src/components/main/analytics/AnalyticsWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,18 @@
import { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { instanceIdSelector, apiBuildVersionSelector } from 'controllers/appInfo';
import { baseEventParametersSelector } from 'controllers/appInfo';
import track from 'react-tracking';
import GA4 from 'react-ga4';
import { idSelector, isAdminSelector } from 'controllers/user/selectors';
import {
autoAnalysisEnabledSelector,
patternAnalysisEnabledSelector,
projectInfoIdSelector,
} from 'controllers/project/selectors';
import { omit } from 'common/utils';
import { gaMeasurementIdSelector } from 'controllers/appInfo/selectors';
import ReactObserver from 'react-event-observer';
import { normalizeDimensionValue, getAppVersion } from './utils';
import { normalizeDimensionValue, getAppVersion, baseEventParametersShape } from './utils';

export const analyticsEventObserver = ReactObserver();

@connect((state) => ({
instanceId: instanceIdSelector(state),
buildVersion: apiBuildVersionSelector(state),
userId: idSelector(state),
isAutoAnalyzerEnabled: autoAnalysisEnabledSelector(state),
isPatternAnalyzerEnabled: patternAnalysisEnabledSelector(state),
projectId: projectInfoIdSelector(state),
isAdmin: isAdminSelector(state),
...baseEventParametersSelector(state),
gaMeasurementId: gaMeasurementIdSelector(state),
}))
@track(({ children, dispatch, ...additionalData }) => additionalData, {
Expand All @@ -53,7 +41,7 @@ export const analyticsEventObserver = ReactObserver();
userId,
isAutoAnalyzerEnabled,
isPatternAnalyzerEnabled,
projectId,
projectInfoId,
isAdmin,
gaMeasurementId,
...data
Expand All @@ -66,7 +54,7 @@ export const analyticsEventObserver = ReactObserver();
auto_analysis: normalizeDimensionValue(isAutoAnalyzerEnabled),
pattern_analysis: normalizeDimensionValue(isPatternAnalyzerEnabled),
timestamp: Date.now(),
...(!isAdmin && { project_id: `${projectId}|${instanceId}` }),
...(!isAdmin && { project_id: `${projectInfoId}|${instanceId}` }),
...omit(data, data.place ? ['action'] : ['action', 'place']),
};
GA4.event(data.action, eventParameters);
Expand All @@ -76,15 +64,9 @@ export const analyticsEventObserver = ReactObserver();
})
export class AnalyticsWrapper extends Component {
static propTypes = {
instanceId: PropTypes.string.isRequired,
buildVersion: PropTypes.string.isRequired,
children: PropTypes.node,
userId: PropTypes.number.isRequired,
isAutoAnalyzerEnabled: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]).isRequired,
isPatternAnalyzerEnabled: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]).isRequired,
projectId: PropTypes.number.isRequired,
isAdmin: PropTypes.bool.isRequired,
baseEventParameters: baseEventParametersShape,
gaMeasurementId: PropTypes.string,
children: PropTypes.node,
};

static defaultProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export const formatEcDashboardData = (dashboard) => {
const sortedWidgets = sortDashboardWidgets([...dashboard.widgets]);
return sortedWidgets.map((widget, index) =>
getEcWidget({
itemListName: dashboard.id,
itemId: widget.widgetId,
index: index + 1,
itemName: widgetTypesMessages[widget.widgetType].defaultMessage,
Expand Down
2 changes: 1 addition & 1 deletion app/src/components/main/analytics/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
export { AnalyticsWrapper } from './AnalyticsWrapper';
export { EmptyAnalyticsWrapper } from './EmptyAnalyticsWrapper';
export { pageEventsMap } from './constants';
export { provideEcGA } from './utils';
export { provideEcGA, baseEventParametersShape } from './utils';
export { normalizeEventString } from './utils';
11 changes: 11 additions & 0 deletions app/src/components/main/analytics/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import GA4 from 'react-ga4';
import PropTypes from 'prop-types';

export const normalizeDimensionValue = (value) => {
return value !== undefined ? value.toString() : undefined;
Expand Down Expand Up @@ -56,3 +57,13 @@ export const provideEcGA = ({ eventName, baseEventParameters, additionalParamete

GA4.event(eventName, eventParameters);
};

export const baseEventParametersShape = PropTypes.shape({
instanceId: PropTypes.string.isRequired,
buildVersion: PropTypes.string.isRequired,
userId: PropTypes.number.isRequired,
isAutoAnalyzerEnabled: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]).isRequired,
isPatternAnalyzerEnabled: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]).isRequired,
projectInfoId: PropTypes.number.isRequired,
isAdmin: PropTypes.bool.isRequired,
}).isRequired;
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { activeProjectSelector } from 'controllers/user';
import { fetchDashboardsAction } from 'controllers/dashboard';
import { analyticsEnabledSelector, baseEventParametersSelector } from 'controllers/appInfo';
import { getWidgets } from 'pages/inside/dashboardItemPage/modals/common/widgets';
import { provideEcGA } from 'components/main/analytics';
import { provideEcGA, baseEventParametersShape } from 'components/main/analytics';
import { activeDashboardIdSelector, pageSelector } from 'controllers/pages';
import {
getWidgetModeValuesString,
Expand Down Expand Up @@ -83,15 +83,7 @@ export class WidgetWizardContent extends Component {
fetchDashboards: PropTypes.func,
activeDashboardId: PropTypes.number,
currentPage: PropTypes.string,
baseEventParameters: PropTypes.shape({
instanceId: PropTypes.string.isRequired,
buildVersion: PropTypes.string.isRequired,
userId: PropTypes.number.isRequired,
isAutoAnalyzerEnabled: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]).isRequired,
isPatternAnalyzerEnabled: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]).isRequired,
projectInfoId: PropTypes.number.isRequired,
isAdmin: PropTypes.bool.isRequired,
}).isRequired,
baseEventParameters: baseEventParametersShape,
};
static defaultProps = {
formValues: {
Expand Down Expand Up @@ -138,6 +130,7 @@ export class WidgetWizardContent extends Component {
getEcWidget({
itemName: widgetTypesMessages[formValues.widgetType].defaultMessage,
itemVariant: this.props.currentPage,
itemListName: activeDashboardId || 'noID',
}),
],
},
Expand Down Expand Up @@ -201,6 +194,7 @@ export class WidgetWizardContent extends Component {
itemId: id,
itemName: widgetTypesMessages[data.widgetType].defaultMessage,
itemVariant: this.props.currentPage,
itemListName: selectedDashboard.id,
}),
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { CHARTS, MULTI_LEVEL_WIDGETS_MAP, NoDataAvailable } from 'components/wid
import { activeDashboardIdSelector } from 'controllers/pages';
import { WIDGETS_EVENTS } from 'analyticsEvents/dashbordsPageEvents';
import { getEcWidget } from 'components/main/analytics/events/common/widgetPages/utils';
import { provideEcGA } from 'components/main/analytics/utils';
import { provideEcGA, baseEventParametersShape } from 'components/main/analytics/utils';
import { widgetTypesMessages } from 'pages/inside/dashboardItemPage/modals/common/messages';
import { isWidgetDataAvailable } from '../../modals/common/utils';
import { WidgetHeader } from './widgetHeader';
Expand Down Expand Up @@ -91,15 +91,7 @@ export class SimpleWidget extends Component {
}).isRequired,
activeDashboardId: PropTypes.number.isRequired,
isAnalyticsEnabled: PropTypes.bool.isRequired,
baseEventParameters: PropTypes.shape({
instanceId: PropTypes.string.isRequired,
buildVersion: PropTypes.string.isRequired,
userId: PropTypes.number.isRequired,
isAutoAnalyzerEnabled: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]).isRequired,
isPatternAnalyzerEnabled: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]).isRequired,
projectInfoId: PropTypes.number.isRequired,
isAdmin: PropTypes.bool.isRequired,
}).isRequired,
baseEventParameters: baseEventParametersShape,
};

static defaultProps = {
Expand Down Expand Up @@ -364,6 +356,7 @@ export class SimpleWidget extends Component {
eventName: 'remove_from_cart',
baseEventParameters,
additionalParameters: {
item_list_name: activeDashboardId,
items: [
getEcWidget({
itemId: id,
Expand Down

0 comments on commit a7e6e79

Please sign in to comment.