Skip to content

Commit

Permalink
feat: standardize event tracking for link sharing
Browse files Browse the repository at this point in the history
add a common analytics event for sharing and reuse it in both copy-link share button usages in the application. add a test for the new common code.
  • Loading branch information
tm-ruxandra committed Mar 12, 2024
1 parent 73035a2 commit d6bb94c
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 8 deletions.
8 changes: 2 additions & 6 deletions src/common/components/SocialShare.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import useMediaQuery from '@mui/material/useMediaQuery';

import { useCopy } from 'custom-hooks';

import { useAnalytics } from 'monitoring/analytics';
import { useShareEvent } from 'monitoring/events';

import CopyLink from './CopyLink';

Expand Down Expand Up @@ -261,7 +261,7 @@ const PostToService = props => {
const SocialShare = props => {
const { buttonProps } = props;
const { t } = useTranslation();
const { trackEvent } = useAnalytics();
const { onShare } = useShareEvent();
const { socialShareProps } = useContext(SocialShareContext);
const { name } = socialShareProps;
const [open, setOpen] = useState(false);
Expand All @@ -280,10 +280,6 @@ const SocialShare = props => {
}
};

const onShare = method => {
trackEvent('share', { props: { url: pageUrl.toString(), method } });
};

if (!name) {
return null;
}
Expand Down
31 changes: 31 additions & 0 deletions src/monitoring/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright © 2021-2024 Technology Matters
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

import { useMemo } from 'react';

import { useAnalytics } from 'monitoring/analytics';

export const useShareEvent = () => {
const { trackEvent } = useAnalytics();
const pageUrl = useMemo(() => window.location, []);

const onShare = method => {
trackEvent('share', { props: { url: pageUrl.toString(), method } });
};

return { onShare };
};
52 changes: 52 additions & 0 deletions src/monitoring/events.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright © 2021-2023 Technology Matters
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
import { render } from 'tests/utils';

import { useAnalytics } from 'monitoring/analytics';
import { useShareEvent } from 'monitoring/events';

jest.mock('monitoring/analytics', () => ({
...jest.requireActual('monitoring/analytics'),
useAnalytics: jest.fn(),
}));

const Component = () => {
const { onShare } = useShareEvent();

onShare('test');
return <div></div>;
};

test('Events: onShare', async () => {
const trackEvent = jest.fn();
useAnalytics.mockReturnValue({
trackEvent,
});

const expectedUrl = window.location.toString();

await render(<Component />);

const eventCall = trackEvent.mock.calls[0];
expect(eventCall[0]).toStrictEqual('share');
expect(eventCall[1]).toStrictEqual({
props: {
method: 'test',
url: expectedUrl,
},
});
});
5 changes: 4 additions & 1 deletion src/sharedData/components/SharedDataEntryFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import { useCollaborationContext } from 'collaboration/collaborationContext';
import CopyLink from 'common/components/CopyLink';
import RouterLink from 'common/components/RouterLink';
import { formatDate } from 'localization/utils';
import { useShareEvent } from 'monitoring/events';
import {
SHARE_ACCESS_ALL,
SHARE_ACCESS_TYPES,
Expand Down Expand Up @@ -162,6 +163,8 @@ const ShareDialog = props => {
}
};

const { onShare } = useShareEvent();

const onChange = useCallback(
event => {
setShowUpdateSuccess(false);
Expand Down Expand Up @@ -280,7 +283,7 @@ const ShareDialog = props => {
</Typography>
</Stack>
)}
<CopyLink pageUrl={sharedResource.shareUrl} />
<CopyLink pageUrl={sharedResource.shareUrl} onShare={onShare} />
</DialogContent>
<DialogActions
sx={{
Expand Down
4 changes: 3 additions & 1 deletion src/sharedData/sharedDataHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export const useSharedData = () => {
const downloadFile = useCallback(
file => {
trackEvent('dataEntry.file.download', {
props: { [entityType]: owner.slug },
props: {
[entityType]: owner.slug,
},
});
window.open(file.url, '_blank');
},
Expand Down

0 comments on commit d6bb94c

Please sign in to comment.