Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Honore la gestion du consentement de l'utilisateur·ice #499

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion front/src/components/Footer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function Footer () {
<li><Link to="/privacy">Privacy</Link></li>
{ import.meta.env.SNOWPACK_MATOMO_URL && (<li>
<label className={styles.consentLabel}>
<input type="checkbox" checked={userHasConsent} onChange={toggleConsent} disabled={true} />
<input type="checkbox" checked={userHasConsent} onChange={toggleConsent} />
I accept to share my navigation stats
</label>
</li>) }
Expand Down
14 changes: 12 additions & 2 deletions front/src/createReduxStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ const initialState = {
charCountPlusSpace: 0,
citationNb: 0,
},
userPreferences: {
trackingConsent: true /* default value should be false */
userPreferences: localStorage.getItem('userPreferences') ? JSON.parse(localStorage.getItem('userPreferences')) : {
trackingConsent: true,
}
}

Expand Down Expand Up @@ -140,6 +140,16 @@ function persistStateIntoLocalStorage ({ getState }) {

return
}
else if (action.type === 'USER_PREFERENCES_TOGGLE') {
// we run the reducer first
next(action)
// we fetch the updated state
const { userPreferences } = getState()
// we persist it for a later page reload
localStorage.setItem('userPreferences', JSON.stringify(userPreferences))

return
}

return next(action)
}
Expand Down
45 changes: 30 additions & 15 deletions front/src/index.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import './wdyr.js'
import React, { lazy } from 'react'
import React, { lazy, useCallback } from 'react'
import { render } from 'react-dom'
import { BrowserRouter as Router, Route, Switch, useParams, useHistory } from 'react-router-dom'
import { Provider } from 'react-redux'
import { BrowserRouter as Router, Route, Switch, useHistory } from 'react-router-dom'
import { Provider, useSelector } from 'react-redux'
import 'whatwg-fetch'

import './styles/general.scss'
Expand Down Expand Up @@ -42,20 +42,35 @@ const store = createStore()
)
})()

const TrackPageViews = () => {
let unlisten

function TrackPageViews () {
const history = useHistory()
const userHasConsent = useSelector(state => state.userPreferences.trackingConsent)

const listen = useCallback((hasContent) => {
if (unlisten) {
unlisten()
unlisten = null
}

return hasContent && history.listen(({ pathname, search, state }, action) => {
/* global _paq */
const _paq = window._paq = window._paq || [];
console.log('history.listen', pathname)

if (hasContent) {
_paq.push(['setConsentGiven'])
_paq.push(['setCustomUrl', pathname])
_paq.push(['trackPageView'])
}
else {
_paq.push(['forgetConsentGiven'])
}
})
}, [userHasConsent])

history.listen(({ pathname, search, state }, action) => {
/* global _paq */
const _paq = window._paq = window._paq || [];

//@todo do this dynamically, based on a subscription to the store
//otherwise, we should use _paq.push(['forgetConsentGiven'])
_paq.push(['setConsentGiven'])
_paq.push(['setCustomUrl', '/' + pathname])
//_paq.push(['setDocumentTitle', 'My New Title'])
_paq.push(['trackPageView'])
})
unlisten = listen(userHasConsent)

return null
}
Expand Down