Skip to content

Commit

Permalink
Merge pull request #913 from betagouv/main
Browse files Browse the repository at this point in the history
Revert Matomo stuff
  • Loading branch information
eletallbetagouv authored Sep 19, 2024
2 parents bc25d62 + f2f0ac7 commit 696965c
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 82 deletions.
10 changes: 4 additions & 6 deletions website/src/analytic/AnalyticContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {Analytic} from './analytic'

export interface AnalyticContextProps {
trackEvent: Analytic['trackEvent']
trackSearch: Analytic['trackSearch']
setTrackedCompanyKind: Analytic['setTrackedCompanyKind']
}

interface Props {
Expand All @@ -21,10 +19,10 @@ export const AnalyticProvider = ({analytic, children}: Props) => {
return (
<AnalyticContext.Provider
value={{
// analytics are not available server-side
trackEvent: analytic?.trackEvent ?? (() => {}),
trackSearch: analytic?.trackSearch ?? (() => {}),
setTrackedCompanyKind: analytic?.setTrackedCompanyKind ?? (() => {}),
trackEvent:
analytic?.trackEvent ??
// analytics are not available server-side
((...args: any[]) => {}),
}}
>
{children}
Expand Down
40 changes: 1 addition & 39 deletions website/src/analytic/analytic.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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 All @@ -12,7 +11,7 @@ export class Analytic {
return new Analytic(matomo, eularian)
}

private log = (...args: unknown[]) => {
private log = (...args: (string | undefined)[]) => {
console.debug('[Analytic]', ...args)
}

Expand All @@ -38,43 +37,6 @@ export class Analytic {
}
}
}

readonly trackSearch = (
inputs: {q: string; postalCode?: string; departmentCode?: string},
searchCategory: 'companysearch_smart' | 'companysearch_nameandpostalcode' | 'companysearch_name' | 'companysearch_siret',
nbResults: number,
) => {
const {q, postalCode, departmentCode} = inputs
const trackedSearch = `${postalCode ? `[${postalCode}] ` : ''}${departmentCode ? `[${departmentCode}] ` : ''}${q}`
const args = ['[trackSiteSearch]', trackedSearch, searchCategory, nbResults]
this.log(...args)
try {
// https://developer.matomo.org/guides/tracking-javascript-guide#internal-search-tracking
this.matomo?.push(args)
} catch (e: any) {
console.error('[Analytic]', e)
if (!(e instanceof ReferenceError)) {
throw e
}
}
}

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
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ export const CompanySearchByIdentifier = ({children}: Props) => {
const [submittedIdentity, setSubmittedIdentity] = useState<string | undefined>(undefined)
const _searchByIdentity = useQuery({
queryKey: ['searchCompaniesByIdentity', submittedIdentity],
queryFn: async () => {
queryFn: () => {
if (submittedIdentity) {
const res = await companyApiClient.searchCompaniesByIdentity(submittedIdentity, false, currentLang)
_analytic.trackSearch({q: submittedIdentity}, 'companysearch_siret', res.length)
return res
return companyApiClient.searchCompaniesByIdentity(submittedIdentity, false, currentLang)
}
return null
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import {CompanySearchResult} from '@/model/Company'
import {ReactNode, useState} from 'react'
import {useI18n} from '@/i18n/I18n'
import {useApiClients} from '@/context/ApiClientsContext'
import {useQuery} from '@tanstack/react-query'
import {useToastOnQueryError} from '@/clients/apiHooks'
import {useAnalyticContext} from '@/analytic/AnalyticContext'
import {useForm} from 'react-hook-form'
import {CompanySearchEventActions, EventCategories} from '@/analytic/analytic'
import {useToastOnQueryError} from '@/clients/apiHooks'
import {Animate} from '@/components_simple/Animate'
import {RequiredFieldsLegend} from '@/components_simple/RequiredFieldsLegend'
import {ButtonWithLoader} from '@/components_simple/buttons/Buttons'
import {ScTextInput} from '@/components_simple/formInputs/ScTextInput'
import {useApiClients} from '@/context/ApiClientsContext'
import {useI18n} from '@/i18n/I18n'
import {CompanySearchResult} from '@/model/Company'
import {ButtonWithLoader} from '@/components_simple/buttons/Buttons'
import {ifDefined} from '@/utils/utils'
import {useQuery} from '@tanstack/react-query'
import {ReactNode, useState} from 'react'
import {useForm} from 'react-hook-form'

interface Form {
name: string
Expand All @@ -27,12 +27,9 @@ export const CompanySearchByName = ({children}: Props) => {
const [submittedForm, setSubmittedForm] = useState<Form | undefined>()
const _search = useQuery({
queryKey: ['searchHeadOfficesByName', submittedForm?.name],
queryFn: async () => {
queryFn: () => {
if (submittedForm) {
const {name} = submittedForm
const res = await companyApiClient.searchHeadOfficesByName(name, currentLang)
_analytic.trackSearch({q: name}, 'companysearch_name', res.length)
return res
return companyApiClient.searchHeadOfficesByName(submittedForm.name, currentLang)
}
return null
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@ export const CompanySearchByNameAndPostalCode = ({children}: Props) => {
const [submittedForm, setSubmittedForm] = useState<Form | undefined>()
const _search = useQuery({
queryKey: ['searchCompaniesByNameAndPostalCode', submittedForm?.name, submittedForm?.postalCode],
queryFn: async () => {
queryFn: () => {
if (submittedForm) {
const {name, postalCode} = submittedForm
const res = await companyApiClient.searchCompaniesByNameAndPostalCode(name, postalCode, currentLang)
_analytic.trackSearch({q: name, postalCode}, 'companysearch_nameandpostalcode', res.length)
return res
return companyApiClient.searchCompaniesByNameAndPostalCode(submittedForm.name, submittedForm.postalCode, currentLang)
}
return null
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {useAnalyticContext} from '@/analytic/AnalyticContext'
import {useToastOnQueryError} from '@/clients/apiHooks'
import {Animate} from '@/components_simple/Animate'
import {useApiClients} from '@/context/ApiClientsContext'
Expand Down Expand Up @@ -140,17 +139,14 @@ export function CompanySmartIdentification({
function useCompanySearchSmartQuery(searchInputs: CompanySearchInputs | undefined) {
const {companyApiClient} = useApiClients()
const {currentLang} = useI18n()
const _analytic = useAnalyticContext()
const _search = useQuery({
queryKey: ['searchCompany', searchInputs],
queryFn: async () => {
if (searchInputs) {
const {input, geoArea} = searchInputs
const postalCode = geoArea && geoArea.kind === 'postcode' ? geoArea.postalCode : undefined
const departmentCode = geoArea && geoArea.kind === 'department' ? geoArea.dpt.code : undefined
const res = await companyApiClient.searchSmart(input, postalCode, departmentCode, currentLang)
_analytic.trackSearch({q: input, postalCode, departmentCode}, 'companysearch_smart', res.length)
return res
return companyApiClient.searchSmart(input, postalCode, departmentCode, currentLang)
}
return null
},
Expand Down
16 changes: 6 additions & 10 deletions website/src/components_feature/reportFlow/Problem/Problem.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {AnalyticContextProps, useAnalyticContext} from '@/analytic/AnalyticContext'
import {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 {getCompanyKind, hasStep0, hasStep1Full} from '@/feature/reportUtils'
import {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,7 +41,6 @@ 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 @@ -51,7 +50,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, _analytic})
const onNext = buildOnNext({sendReportEvent, setReport, stepNavigation})
return (
<>
<OpenFfWelcomeText setup={openFfSetup} />
Expand All @@ -75,19 +74,16 @@ function buildOnNext({
setReport: setReport,
stepNavigation,
sendReportEvent,
_analytic,
}: {
setReport: SetReport
stepNavigation: StepNavigation
sendReportEvent: SendReportEvent
_analytic: AnalyticContextProps
}) {
return function (next: () => void): void {
return function onNext(next: () => void): void {
setReport(draft => {
if (!hasStep0(draft) || !hasStep1Full(draft)) {
throw new Error(`Report is not ready to go to step2`)
if (!hasStep1Full(draft)) {
throw new Error(`Report is not ready to go to next step, step1 is not full`)
}
_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 696965c

Please sign in to comment.