From 3f9221a49716e6cdd99a6eadcb79f9035b6f8d54 Mon Sep 17 00:00:00 2001 From: Jose Buitron Date: Tue, 14 Nov 2023 18:03:03 -0500 Subject: [PATCH] feat: Added file size and type to plausible event (#1274) --- .../GroupSharedDataUploadFiles.test.js | 24 +++++++++++ .../components/SharedDataUpload/index.js | 43 ++++++++++--------- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/src/group/components/GroupSharedDataUploadFiles.test.js b/src/group/components/GroupSharedDataUploadFiles.test.js index c0fa1a943..684b7eeaa 100644 --- a/src/group/components/GroupSharedDataUploadFiles.test.js +++ b/src/group/components/GroupSharedDataUploadFiles.test.js @@ -21,6 +21,8 @@ import { useNavigate, useParams } from 'react-router-dom'; import * as terrasoApi from 'terraso-client-shared/terrasoApi/api'; import { GROUP_TYPES_WITH_REDIRECTS } from 'tests/constants'; +import { useAnalytics } from 'monitoring/analytics'; + import GroupSharedDataUpload from './GroupSharedDataUpload'; jest.mock('terraso-client-shared/terrasoApi/api'); @@ -31,6 +33,11 @@ jest.mock('react-router-dom', () => ({ useNavigate: jest.fn(), })); +jest.mock('monitoring/analytics', () => ({ + ...jest.requireActual('monitoring/analytics'), + useAnalytics: jest.fn(), +})); + const setup = async (membershipType = 'OPEN', userRole = 'MEMBER') => { terrasoApi.requestGraphQL.mockResolvedValue( _.set( @@ -56,6 +63,9 @@ beforeEach(() => { slug: 'slug-1', }); useNavigate.mockReturnValue(() => {}); + useAnalytics.mockReturnValue({ + trackEvent: jest.fn(), + }); }); const dropFiles = async files => { @@ -197,6 +207,10 @@ test('GroupSharedDataUpload: Partial Success', async () => { }); test('GroupSharedDataUpload: Complete Success', async () => { + const trackEvent = jest.fn(); + useAnalytics.mockReturnValue({ + trackEvent, + }); const navigate = jest.fn(); useNavigate.mockReturnValue(navigate); terrasoApi.request.mockResolvedValueOnce({}); @@ -219,6 +233,16 @@ test('GroupSharedDataUpload: Complete Success', async () => { const saveCall = terrasoApi.request.mock.calls[0]; expect(saveCall[0].body.get('target_type')).toBe('group'); expect(saveCall[0].body.get('target_slug')).toBe('slug-1'); + + expect(trackEvent).toHaveBeenCalledWith('dataEntry.file.upload', { + props: { + 'ILM Output': 'Results and analysis of impact', + group: 'slug-1', + size: 7, + type: 'text/csv', + success: true, + }, + }); }); test('GroupSharedDataUpload: PDF Success', async () => { diff --git a/src/sharedData/components/SharedDataUpload/index.js b/src/sharedData/components/SharedDataUpload/index.js index 94de342e7..4c26df767 100644 --- a/src/sharedData/components/SharedDataUpload/index.js +++ b/src/sharedData/components/SharedDataUpload/index.js @@ -111,27 +111,30 @@ const SharedDataUpload = props => { const allPromises = [...filesPromises, ...linksPromises]; Promise.allSettled(allPromises).then(results => { - results - .filter( - result => _.get('value.meta.requestStatus', result) === 'fulfilled' - ) - .forEach(result => { - const isLink = _.has('value.meta.arg.link', result); - if (isLink) { - trackEvent('dataEntry.link.create', { - props: { - [entityType]: owner.slug, - }, - }); - } else { - trackEvent('dataEntry.file.upload', { - props: { - [entityType]: owner.slug, - [ILM_OUTPUT_PROP]: RESULTS_ANALYSIS_IMPACT, - }, - }); - } + const byType = _.groupBy( + result => (_.has('value.meta.arg.link', result) ? 'links' : 'files'), + results + ); + byType.files?.forEach(result => { + const file = _.get('value.meta.arg.file.file', result); + trackEvent('dataEntry.file.upload', { + props: { + [entityType]: owner.slug, + [ILM_OUTPUT_PROP]: RESULTS_ANALYSIS_IMPACT, + size: file.size, + type: file.type, + success: result.status === 'fulfilled', + }, + }); + }); + byType.links?.forEach(result => { + trackEvent('dataEntry.link.create', { + props: { + [entityType]: owner.slug, + success: result.status === 'fulfilled', + }, }); + }); setShowSummary(true); }); }, [entityType, owner.slug, toUpload, targetInput, dispatch, trackEvent]);