Skip to content
This repository has been archived by the owner on Sep 11, 2020. It is now read-only.

Commit

Permalink
feat(flux): taggue l'utilisation des flux geojson sur Matomo (#11)
Browse files Browse the repository at this point in the history
* feat(flux): taggue l'utilisation des flux geojson sur Matomo

* refactor: refactorise

Co-authored-by: Francois Romain <francoisromain@gmail.com>
  • Loading branch information
NeoBahamut and francoisromain committed Apr 15, 2020
1 parent e5a986f commit 38ba886
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 44 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ URL=

# certificat ssh (docker-compose)
LETSENCRYPT_EMAIL=

# stats
MATOMO_SITE_ID=
MATOMO_HOST=
6 changes: 6 additions & 0 deletions lib/stats/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const MatomoTracker = require('matomo-tracker')

module.exports =
process.env.MATOMO_SITE_ID && process.env.MATOMO_HOST
? new MatomoTracker(process.env.MATOMO_SITE_ID, process.env.MATOMO_HOST)
: null
26 changes: 19 additions & 7 deletions lib/titres/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,30 @@ const basicAuth = require('basic-auth')
const apiGet = require('../api/index')
const geojsonFormat = require('../utils/geojson-format')

const matomo = require('../stats/index')

// importe les requêtes graphQL
const {
titresQuery,
domainesQuery,
typesQuery,
statutsQuery,
tokenQuery
tokenQuery,
} = require('../api/queries')

const params = {
arrays: ['typesIds', 'domainesIds', 'statutsIds'],
strings: ['entreprises', 'noms', 'references', 'substances', 'territoires']
strings: ['entreprises', 'noms', 'references', 'substances', 'territoires'],
}

// pour chaque definition (domainesIds, typesIds, statutsIds)
// retourne un tableau avec les noms correspondant aux ids
// - domaines: []
// - types: []
// - statuts: []
const metasFormat = metas =>
const metasFormat = (metas) =>
Object.keys(metas).reduce((metasObj, metaName) => {
metasObj[metaName] = metas[metaName].map(m => m.nom || m.type.nom)
metasObj[metaName] = metas[metaName].map((m) => m.nom || m.type.nom)

return metasObj
}, {})
Expand All @@ -35,7 +37,7 @@ const tokenGet = async (email, motDePasse) => {

const data = await apiGet('utilisateurTokenCreer', {
query: tokenQuery,
variables
variables,
})

return (data && data.token) || null
Expand All @@ -60,7 +62,7 @@ const auth = async (req, res, next) => {
next()
}

const queryParse = query =>
const queryParse = (query) =>
Object.keys(query).reduce((variables, key) => {
if (params.arrays.includes(key)) {
variables[key] = query[key].split(',')
Expand Down Expand Up @@ -104,9 +106,19 @@ const titresGet = async (req, res) => {

// construit le flux geojson
const geojson = geojsonFormat(titres, {
...metasFormat(metas)
...metasFormat(metas),
})

if (matomo) {
const url = req.protocol + '://' + req.get('host') + req.originalUrl

// track Matomo
matomo.track({
url,
action_name: 'flux / titres',
})
}

res.send(geojson)
} catch (e) {
console.error(e)
Expand Down
60 changes: 24 additions & 36 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"cron": "^1.8.2",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"matomo-tracker": "^2.2.1",
"node-fetch": "^2.6.0",
"npm-check-updates": "^4.1.0",
"rimraf": "^3.0.2",
Expand All @@ -38,7 +39,7 @@
"@types/node": "^13.9.5",
"@types/node-fetch": "^2.5.5",
"@types/serve-index": "^1.7.30",
"commitizen": "^4.0.3",
"commitizen": "^4.0.4",
"conventional-github-releaser": "^3.1.3",
"cz-conventional-changelog": "^3.1.0",
"eslint": "^6.8.0",
Expand Down

0 comments on commit 38ba886

Please sign in to comment.