Skip to content

Commit

Permalink
Feature : ajout du mode cookieless si l'utilisateur n'accepte pas les…
Browse files Browse the repository at this point in the history
… cookies posthog + envoi de l'opportunityId quand on envoi un formulaire (#1303)

- modification de l'api opportunity pour renvoyer l'opportunityId dans
la réponse
- initialisation de posthog avec persistence: 'memory' (mode cookieless)
- si acceptation des cookies > persistence: 'localStorage+cookie'
- si refus des cookies > persistence: 'memory'
- plus de vérification de l'acceptation des cookies pour l'envoi des
formulaires
- ajout de l'opportunityId dans les properties de la capture de l'event 

Pour tester en local bien modifier l'initialisation de posthog (qui
normalement n'est possible qu'en prod)
  • Loading branch information
oumeimaelisbihani authored Nov 18, 2024
1 parent 833c21b commit 25db8ec
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 20 deletions.
5 changes: 3 additions & 2 deletions apps/web/src/components/element/form/TeeForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,10 @@ const saveForm = async () => {
isLoading.value = true
const opportunity = new OpportunityApi(localForm.value, props.dataId, props.dataSlug || props.dataId, props.formType)
requestResponse.value = await opportunity.fetch()
// analytics / send event
Analytics.sendEvent(TrackId.Results, getEventName())
if (requestResponse.value.id) {
Analytics.sendEvent(TrackId.Results, getEventName(), { opportunityId: requestResponse.value.id })
}
} finally {
isLoading.value = false
formIsSent.value = true
Expand Down
4 changes: 4 additions & 0 deletions apps/web/src/service/api/opportunityApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default class OpportunityApi extends RequestApi {
async fetch() {
let resp: ReqResp = {}
try {
const payload: OpportunityBody = this.payload()
const response = await fetch(this.url, {
method: 'POST',
headers: this._headers,
Expand All @@ -53,6 +54,9 @@ export default class OpportunityApi extends RequestApi {
resp.status = response.status
resp.statusText = response.statusText
resp.url = response.url
if (payload.opportunity.id) {
resp.id = payload.opportunity.id
}
} catch (error: unknown) {
resp.ok = false
resp.status = 500
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/types/otherTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface ReqResp extends ReqError {
code?: string
message?: string
data?: any
id?: string
resultsMapping?: ResultsMapping[]
url?: string
}
Expand Down
10 changes: 2 additions & 8 deletions apps/web/src/utils/analytic/analytics.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import Posthog from '@/utils/analytic/posthog'
import Cookie from '@/utils/cookies'
import { CookieValue } from '@/types/cookies'
import Matomo from './matomo'

export default class Analytics {
static sendEvent(action: string, name: string | null = null, value?: string | number | object | Record<string, string | number>) {
const posthogCookie = Cookie.getCookieByValue(CookieValue.Posthog)
if (posthogCookie?.accepted) {
Posthog.captureEvent(action, name ? name : 'unnamed event', value)
}

static sendEvent(action: string, name: string | null = null, value?: object) {
Posthog.captureEvent(name ? name : 'unnamed event', value)
Matomo.sendEvent(action, name, JSON.stringify(value))
}
}
16 changes: 6 additions & 10 deletions apps/web/src/utils/analytic/posthog.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import posthog, { PostHog } from 'posthog-js'
import Config from '@/config'
import Cookie from '@/utils/cookies'
import { CookieValue } from '@/types/cookies'
import { RouteLocationNormalized } from 'vue-router'

export default class Posthog {
Expand All @@ -13,29 +11,27 @@ export default class Posthog {
api_host: 'https://eu.i.posthog.com',
capture_pageview: false,
capture_pageleave: false,
persistence: 'memory',
person_profiles: 'always'
})
}
}

static activatePosthogCookie() {
if (this._posthog) {
this._posthog.opt_in_capturing()
this._posthog.set_config({ persistence: 'localStorage+cookie' })
}
}

static deactivatePosthogCookie() {
if (this._posthog) {
this._posthog.opt_out_capturing()
this._posthog.set_config({ persistence: 'memory' })
}
}

static capturePageView(to: RouteLocationNormalized) {
if (this._posthog) {
const posthogCookie = Cookie.getCookieByValue(CookieValue.Posthog)
if (posthogCookie?.accepted) {
this._posthog.capture('$pageview', { path: to.fullPath })
}
this._posthog.capture('$pageview', { path: to.fullPath })
}
}

Expand All @@ -45,9 +41,9 @@ export default class Posthog {
}
}

static captureEvent(action: string, name: string | null = null, value?: string | number | object | Record<string, string | number>) {
static captureEvent(name: string | null = null, value?: object) {
if (this._posthog) {
this._posthog.capture(name ? name : 'unnamed event', { action: action, value: value })
this._posthog.capture(name ? name : 'unnamed event', value)
}
}
}

0 comments on commit 25db8ec

Please sign in to comment.