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

Page stats cassée #849

Merged
merged 16 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 73 additions & 0 deletions netlify/functions/get-stats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
let { MATOMO_TOKEN } = process.env

const authorizedMethods = [
'VisitsSummary.getVisits',
'VisitsSummary.getVisits',
'VisitorInterest.getNumberOfVisitsPerVisitDuration',
'VisitFrequency.get',
'Actions.getPageUrl',
'Referrers.getWebsites',
'Referrers.getSocials',
'Referrers.getKeywords',
'Actions.getEntryPageUrls',
'Actions.getPageUrls',
'Events.getAction',
]

// This function authorizes requests made from our front-end to fetch stats properties
// Our full stats data are now private, since they could expose sensitive informations
exports.handler = async (event, context) => {
const requestParams = decodeURIComponent(
event.queryStringParameters.requestParams
)

const matomoMethod = new URLSearchParams(requestParams).get('method'),
authorizedMethod = authorizedMethods.includes(matomoMethod)

const authorizedSiteId = idSite === '153'

if (!authorizedMethod || !authorizedSiteId)
return {
statusCode: 401,
}

const response = await fetch(
'https://stats.data.gouv.fr/?' +
requestParams +
'&token_auth=' +
MATOMO_TOKEN
)
const json = await response.json()

// Remove secret pages that would reveal groupe names that should stay private
if (requestParams.includes('Page')) {
return success(
json.filter(
(el) =>
!isPrivate(el.label) &&
!(el.subtable && el.subtable.find((t) => isPrivate(t.url)))
)
)
}

return success(json)
}

const success = (data) => ({
statusCode: 200,
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
})

const privateURLs = [ 'conférence/', 'conference/', 'sondage/' ]
laem marked this conversation as resolved.
Show resolved Hide resolved

const isPrivate = (rawString) => {
const uriComponents = decodeURIComponent(rawString)

return privateURLs.string?.
uriComponents != undefined &&
privateURLs.some((url) => uriComponents.includes(url))
)
}
1 change: 1 addition & 0 deletions source/components/stats/StatsContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
useVisitsDuration,
useWebsites,
} from './matomo'

import Section from './utils/Section'

const Wrapper = styled.div`
Expand Down
6 changes: 4 additions & 2 deletions source/components/stats/content/IframeFigures.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,16 @@ const Text = styled.p`
`

export default function IframeFigures(props) {
const { i18n } = useTranslation()
const currentLangInfos = getCurrentLangInfos(i18n)
if (!props.pages.length || !props.activePages.length) return
const [iframes, activeIframes] =
props.pages &&
props.activePages &&
getIframeRate(props.pages, props.activePages)

const [iframePages, totalIframe] =
props.pages && getIdentifiedIframes(props.pages)
const { i18n } = useTranslation()
const currentLangInfos = getCurrentLangInfos(i18n)

return (
<div>
Expand Down
1 change: 1 addition & 0 deletions source/components/stats/content/ScoreFromURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const Text = styled.p`
`

export default function ScoreFromURL(props) {
if (!props.pages.length) return
const scores = props.pages && getScores(props.pages)
// we exclude high number of visits on same urls (corresponds to average test score ?)
// pb : if a user goes to end page, come back to test, change test score, come back to end page, 2 score values are taken into account instead of one.
Expand Down
3 changes: 2 additions & 1 deletion source/components/stats/content/sources/Table.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import styled from 'styled-components'
import { Trans } from 'react-i18next'
import styled from 'styled-components'

import Tile from '../../utils/Tile'

Expand Down Expand Up @@ -75,6 +75,7 @@ export default function Table(props) {
<th>%</th>
</tr>
{props.data &&
props.data.length > 0 &&
props.data.map(
(line, index) =>
(!props.limit || index < props.limit) && (
Expand Down
47 changes: 20 additions & 27 deletions source/components/stats/matomo.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,36 @@
import { useQuery } from 'react-query'
import axios from 'axios'
import { useQuery } from 'react-query'

const idSite = 153

export const useChart = ({ chartPeriod, chartDate }) =>
export const useX = (queryName, urlQuery, transformResult, keepPreviousData) =>
useQuery(
['chart', chartPeriod, chartDate],
queryName,
() =>
axios
.get(
`https://stats.data.gouv.fr/?module=API&date=last${chartDate}&period=${chartPeriod}&format=json&idSite=${idSite}&method=VisitsSummary.getVisits`
'/.netlify/functions/get-stats?requestParams=' +
encodeURIComponent(urlQuery)
)
.then((res) => res.data),
{
keepPreviousData: true,
}
.then((res) => transformResult(res)),
{ keepPreviousData }
)

export const useSimulationsTerminees = () =>
useQuery(
['SimulationsTerminees'],
() =>
axios
.get(
`https://stats.data.gouv.fr/?module=API&method=Events.getAction&idSite=${idSite}&period=range&date=last6000&format=JSON`
)
.then((res) =>
res.data.find((action) => action.label === 'A terminé la simulation')
),
{
keepPreviousData: true,
}
export const useChart = ({ chartPeriod, chartDate }) =>
useX(
['chart', chartPeriod, chartDate],
`module=API&date=last${chartDate}&period=${chartPeriod}&format=json&idSite=${idSite}&method=VisitsSummary.getVisits`,
(res) => res.data,
true
)

export const useX = (queryName, urlQuery, transformResult) =>
useQuery(queryName, () =>
axios
.get('https://stats.data.gouv.fr/?' + urlQuery)
.then((res) => transformResult(res))
export const useSimulationsTerminees = () =>
useX(
['SimulationsTerminees'],
`module=API&method=Events.getAction&idSite=${idSite}&period=range&date=last6000&format=JSON`,
(res) =>
res.data.find((action) => action.label === 'A terminé la simulation'),
true
)

export const useVisitsDuration = () =>
Expand Down