Skip to content

Commit

Permalink
Merge pull request #911 from betagouv/main
Browse files Browse the repository at this point in the history
MEP Track companyKind in Matomo as a custom dimension
  • Loading branch information
eletallbetagouv authored Sep 19, 2024
2 parents 0ce3609 + 3fa7376 commit bc25d62
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
2 changes: 2 additions & 0 deletions website/src/analytic/AnalyticContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {Analytic} from './analytic'
export interface AnalyticContextProps {
trackEvent: Analytic['trackEvent']
trackSearch: Analytic['trackSearch']
setTrackedCompanyKind: Analytic['setTrackedCompanyKind']
}

interface Props {
Expand All @@ -23,6 +24,7 @@ export const AnalyticProvider = ({analytic, children}: Props) => {
// analytics are not available server-side
trackEvent: analytic?.trackEvent ?? (() => {}),
trackSearch: analytic?.trackSearch ?? (() => {}),
setTrackedCompanyKind: analytic?.setTrackedCompanyKind ?? (() => {}),
}}
>
{children}
Expand Down
18 changes: 18 additions & 0 deletions website/src/analytic/analytic.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {appConfig} from '@/core/appConfig'
import {usePathname, useSearchParams} from 'next/navigation'
import {useEffect} from 'react'
import {CompanyKind} from 'shared/anomalies/Anomaly'
import {Eularian} from '../plugins/eularian'
import {Matomo} from '../plugins/matomo'

Expand Down Expand Up @@ -57,6 +58,23 @@ export class Analytic {
}
}
}

readonly setTrackedCompanyKind = (companyKind: CompanyKind) => {
const customDimensionId = 1
const args = ['setCustomDimension', customDimensionId, companyKind]
this.log(...args)
try {
// https://developer.matomo.org/guides/tracking-javascript-guide#custom-dimensions
// This doesn't send anything
// but should set the custom dimension "companykind" for the events after it
this.matomo?.push(args)
} catch (e: any) {
console.error('[Analytic]', e)
if (!(e instanceof ReferenceError)) {
throw e
}
}
}
}

export function PageChangesListener({analytic}: {analytic: Analytic}) {
Expand Down
16 changes: 10 additions & 6 deletions website/src/components_feature/reportFlow/Problem/Problem.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {useAnalyticContext} from '@/analytic/AnalyticContext'
import {AnalyticContextProps, useAnalyticContext} from '@/analytic/AnalyticContext'
import {EventCategories, ReportEventActions} from '@/analytic/analytic'
import {NextStepButton} from '@/components_feature/reportFlow/reportFlowStepper/NextStepButton'
import {StepNavigation} from '@/components_feature/reportFlow/reportFlowStepper/ReportFlowStepper'
import {OpenFfWelcomeText, useOpenFfSetupLoaded as useHandleOpenFfSetupLoaded, useOpenFfSetup} from '@/feature/openFoodFacts'
import {RappelConsoWelcome, useHandleRcSetupLoaded, useRappelConsoSetup} from '@/feature/rappelConso'
import {hasStep0, hasStep1Full} from '@/feature/reportUtils'
import {getCompanyKind, hasStep0, hasStep1Full} from '@/feature/reportUtils'
import {initiateReport} from '@/feature/reportUtils2'
import {useI18n} from '@/i18n/I18n'
import {Step2Model} from '@/model/Step2Model'
Expand Down Expand Up @@ -41,6 +41,7 @@ export function Problem({anomaly, isWebView, stepNavigation}: Props) {
}

function ProblemInner({anomaly, isWebView, stepNavigation}: Props) {
const _analytic = useAnalyticContext()
const {report, setReport, sendReportEvent} = useReportFlowContext()
if (!hasStep0(report)) {
throw new Error('Report should have a lang and a category already (in Problem)')
Expand All @@ -50,7 +51,7 @@ function ProblemInner({anomaly, isWebView, stepNavigation}: Props) {
useHandleOpenFfSetupLoaded(openFfSetup, setReport)
useHandleRcSetupLoaded(rappelConsoSetup, setReport)
const specialCategoriesNotLoading = openFfSetup.status !== 'loading' && rappelConsoSetup.status !== 'loading'
const onNext = buildOnNext({sendReportEvent, setReport, stepNavigation})
const onNext = buildOnNext({sendReportEvent, setReport, stepNavigation, _analytic})
return (
<>
<OpenFfWelcomeText setup={openFfSetup} />
Expand All @@ -74,16 +75,19 @@ function buildOnNext({
setReport: setReport,
stepNavigation,
sendReportEvent,
_analytic,
}: {
setReport: SetReport
stepNavigation: StepNavigation
sendReportEvent: SendReportEvent
_analytic: AnalyticContextProps
}) {
return function onNext(next: () => void): void {
return function (next: () => void): void {
setReport(draft => {
if (!hasStep1Full(draft)) {
throw new Error(`Report is not ready to go to next step, step1 is not full`)
if (!hasStep0(draft) || !hasStep1Full(draft)) {
throw new Error(`Report is not ready to go to step2`)
}
_analytic.setTrackedCompanyKind(getCompanyKind(draft))
// In the openFf scenario
// Only if we got all the data, then we build the company/product from it.
// If we only have partial data, then we will build it in step 2.
Expand Down

0 comments on commit bc25d62

Please sign in to comment.