Skip to content
Snippets Groups Projects
Commit 79465650 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

inject matomo when user accepts cookie

parent e20d72ed
No related branches found
No related tags found
1 merge request!275Resolve "cookie banner for matomo"
Pipeline #96842 passed
...@@ -5,10 +5,14 @@ import { useAppSelector } from '@/redux/hooks/useAppSelector'; ...@@ -5,10 +5,14 @@ import { useAppSelector } from '@/redux/hooks/useAppSelector';
import { Button } from '@/shared/Button'; import { Button } from '@/shared/Button';
import Link from 'next/link'; import Link from 'next/link';
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
import { cookiePolicyUrlSelector } from '@/redux/configuration/configuration.selectors'; import {
cookiePolicyUrlSelector,
matomoUrlSelector,
} from '@/redux/configuration/configuration.selectors';
import { OptionInput } from '@/components/Map/Drawer/ExportDrawer/CheckboxFilter/OptionInput'; import { OptionInput } from '@/components/Map/Drawer/ExportDrawer/CheckboxFilter/OptionInput';
import { CheckboxItem } from '@/components/Map/Drawer/ExportDrawer/CheckboxFilter/CheckboxFilter.types'; import { CheckboxItem } from '@/components/Map/Drawer/ExportDrawer/CheckboxFilter/CheckboxFilter.types';
import { ZERO } from '@/constants/common'; import { ZERO } from '@/constants/common';
import { injectMatomoTracking } from '@/utils/injectMatomoTracking';
import { import {
USER_ACCEPTED_COOKIES_COOKIE_NAME, USER_ACCEPTED_COOKIES_COOKIE_NAME,
USER_ACCEPTED_COOKIES_COOKIE_VALUE, USER_ACCEPTED_COOKIES_COOKIE_VALUE,
...@@ -24,6 +28,7 @@ export const CookieBanner = (): React.ReactNode => { ...@@ -24,6 +28,7 @@ export const CookieBanner = (): React.ReactNode => {
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const { visible, accepted } = useAppSelector(selectCookieBanner); const { visible, accepted } = useAppSelector(selectCookieBanner);
const cookiePolicyUrl = useAppSelector(cookiePolicyUrlSelector); const cookiePolicyUrl = useAppSelector(cookiePolicyUrlSelector);
const matomoUrl = useAppSelector(matomoUrlSelector);
const [options, setOptions] = useState([selectMatomoOption]); const [options, setOptions] = useState([selectMatomoOption]);
...@@ -51,6 +56,7 @@ export const CookieBanner = (): React.ReactNode => { ...@@ -51,6 +56,7 @@ export const CookieBanner = (): React.ReactNode => {
USER_ACCEPTED_MATOMO_COOKIES_COOKIE_NAME, USER_ACCEPTED_MATOMO_COOKIES_COOKIE_NAME,
USER_ACCEPTED_COOKIES_COOKIE_VALUE.ACCEPTED, USER_ACCEPTED_COOKIES_COOKIE_VALUE.ACCEPTED,
); );
injectMatomoTracking(matomoUrl);
} else { } else {
localStorage.setItem( localStorage.setItem(
USER_ACCEPTED_MATOMO_COOKIES_COOKIE_NAME, USER_ACCEPTED_MATOMO_COOKIES_COOKIE_NAME,
......
...@@ -20,6 +20,7 @@ import { ...@@ -20,6 +20,7 @@ import {
REQUEST_ACCOUNT_EMAIL, REQUEST_ACCOUNT_EMAIL,
TERMS_OF_SERVICE_ID, TERMS_OF_SERVICE_ID,
COOKIE_POLICY_URL, COOKIE_POLICY_URL,
MATOMO_URL,
} from './configuration.constants'; } from './configuration.constants';
import { ConfigurationHandlersIds, ConfigurationImageHandlersIds } from './configuration.types'; import { ConfigurationHandlersIds, ConfigurationImageHandlersIds } from './configuration.types';
...@@ -60,6 +61,11 @@ export const searchDistanceValSelector = createSelector( ...@@ -60,6 +61,11 @@ export const searchDistanceValSelector = createSelector(
state => configurationAdapterSelectors.selectById(state, SEARCH_DISTANCE_NAME_ID)?.value, state => configurationAdapterSelectors.selectById(state, SEARCH_DISTANCE_NAME_ID)?.value,
); );
export const matomoUrlSelector = createSelector(
configurationOptionsSelector,
state => configurationAdapterSelectors.selectById(state, MATOMO_URL)?.value,
);
export const adminEmailValSelector = createSelector( export const adminEmailValSelector = createSelector(
configurationOptionsSelector, configurationOptionsSelector,
state => configurationAdapterSelectors.selectById(state, REQUEST_ACCOUNT_EMAIL)?.value, state => configurationAdapterSelectors.selectById(state, REQUEST_ACCOUNT_EMAIL)?.value,
......
...@@ -18,6 +18,11 @@ import { getProjects } from '@/redux/projects/projects.thunks'; ...@@ -18,6 +18,11 @@ import { getProjects } from '@/redux/projects/projects.thunks';
import { getSubmapConnectionsBioEntity } from '@/redux/bioEntity/thunks/getSubmapConnectionsBioEntity'; import { getSubmapConnectionsBioEntity } from '@/redux/bioEntity/thunks/getSubmapConnectionsBioEntity';
import { getArrowTypes, getLineTypes, getShapes } from '@/redux/shapes/shapes.thunks'; import { getArrowTypes, getLineTypes, getShapes } from '@/redux/shapes/shapes.thunks';
import { MATOMO_URL } from '@/redux/configuration/configuration.constants'; import { MATOMO_URL } from '@/redux/configuration/configuration.constants';
import {
USER_ACCEPTED_COOKIES_COOKIE_VALUE,
USER_ACCEPTED_MATOMO_COOKIES_COOKIE_NAME,
} from '@/components/FunctionalArea/CookieBanner/CookieBanner.constants';
import { injectMatomoTracking } from '@/utils/injectMatomoTracking';
import { getAllBackgroundsByProjectId } from '../backgrounds/backgrounds.thunks'; import { getAllBackgroundsByProjectId } from '../backgrounds/backgrounds.thunks';
import { getConfiguration, getConfigurationOptions } from '../configuration/configuration.thunks'; import { getConfiguration, getConfigurationOptions } from '../configuration/configuration.thunks';
import { import {
...@@ -69,7 +74,11 @@ export const fetchInitialAppData = createAsyncThunk< ...@@ -69,7 +74,11 @@ export const fetchInitialAppData = createAsyncThunk<
try { try {
const configuration = store.getState().configuration.main.data; const configuration = store.getState().configuration.main.data;
if (configuration) { const userAcceptedMatomo =
localStorage.getItem(USER_ACCEPTED_MATOMO_COOKIES_COOKIE_NAME) ===
USER_ACCEPTED_COOKIES_COOKIE_VALUE.ACCEPTED;
if (configuration && userAcceptedMatomo) {
const options = configuration.options.filter(option => option.type === MATOMO_URL); const options = configuration.options.filter(option => option.type === MATOMO_URL);
let url = ''; let url = '';
if (options.length > ZERO) { if (options.length > ZERO) {
...@@ -78,21 +87,7 @@ export const fetchInitialAppData = createAsyncThunk< ...@@ -78,21 +87,7 @@ export const fetchInitialAppData = createAsyncThunk<
if (!url.startsWith('http')) { if (!url.startsWith('http')) {
url = ''; url = '';
} }
if (url !== '') { injectMatomoTracking(url);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
// eslint-disable-next-line no-underscore-dangle,no-multi-assign
const _mtm = (window._mtm = window._mtm || []);
_mtm.push({ 'mtm.startTime': new Date().getTime(), event: 'mtm.Start' });
const d = document;
const g = d.createElement('script');
const s = d.getElementsByTagName('script')[ZERO];
g.async = true;
g.src = url;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
s.parentNode.insertBefore(g, s);
}
} }
} catch (e) { } catch (e) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
......
import { ZERO } from '@/constants/common';
export const injectMatomoTracking = (url: string | undefined): void => {
if (url !== '' && url !== undefined) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
// eslint-disable-next-line no-underscore-dangle,no-multi-assign
const _mtm = (window._mtm = window._mtm || []);
_mtm.push({ 'mtm.startTime': new Date().getTime(), event: 'mtm.Start' });
const d = document;
const g = d.createElement('script');
const s = d.getElementsByTagName('script')[ZERO];
g.async = true;
g.src = url;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
s.parentNode.insertBefore(g, s);
}
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment