Skip to content

Commit

Permalink
feat: Added file size and type to plausible event (#1274)
Browse files Browse the repository at this point in the history
  • Loading branch information
josebui authored Nov 14, 2023
1 parent eef7c72 commit 3f9221a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 20 deletions.
24 changes: 24 additions & 0 deletions src/group/components/GroupSharedDataUploadFiles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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(
Expand All @@ -56,6 +63,9 @@ beforeEach(() => {
slug: 'slug-1',
});
useNavigate.mockReturnValue(() => {});
useAnalytics.mockReturnValue({
trackEvent: jest.fn(),
});
});

const dropFiles = async files => {
Expand Down Expand Up @@ -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({});
Expand All @@ -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 () => {
Expand Down
43 changes: 23 additions & 20 deletions src/sharedData/components/SharedDataUpload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down

0 comments on commit 3f9221a

Please sign in to comment.