diff --git a/src/common/components/SocialShare.js b/src/common/components/SocialShare.js index 8bd4c7b8c..d5bcfa437 100644 --- a/src/common/components/SocialShare.js +++ b/src/common/components/SocialShare.js @@ -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'; @@ -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); @@ -280,10 +280,6 @@ const SocialShare = props => { } }; - const onShare = method => { - trackEvent('share', { props: { url: pageUrl.toString(), method } }); - }; - if (!name) { return null; } diff --git a/src/monitoring/events.js b/src/monitoring/events.js new file mode 100644 index 000000000..23a003bc1 --- /dev/null +++ b/src/monitoring/events.js @@ -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 }; +}; diff --git a/src/monitoring/events.test.js b/src/monitoring/events.test.js new file mode 100644 index 000000000..cad6e25e5 --- /dev/null +++ b/src/monitoring/events.test.js @@ -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
; +}; + +test('Events: onShare', async () => { + const trackEvent = jest.fn(); + useAnalytics.mockReturnValue({ + trackEvent, + }); + + const expectedUrl = window.location.toString(); + + await render(