Skip to content

Add PostHog Analytics #952

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

Open
wants to merge 9 commits into
base: STAG
Choose a base branch
from
6 changes: 3 additions & 3 deletions frontend/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ export default ({ history, location }) => {
useEffect(() => {
if (getCookieConsentValue() === 'true') {
AnalyticsService.init()
AnalyticsService.pageView(window.location)
const unlisten = history.listen(AnalyticsService.pageView)
AnalyticsService.pageView(window.location) // Initial page view tracking
const unlisten = history.listen(AnalyticsService.pageView) // Listen for route changes
return () => {
unlisten()
unlisten() // Cleanup listener on component unmount
}
}
}, [location, history])
Expand Down
8 changes: 8 additions & 0 deletions frontend/src/constants/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ const settings = {
required: false,
value: process.env.REACT_APP_GOOGLE_ANALYTICS_ID,
},
POSTHOG_API_KEY: {
required: false,
value: process.env.REACT_APP_POSTHOG_PROJECT_API_KEY,
},
POSTHOG_API_HOST: {
required: false,
value: process.env.REACT_APP_POSTHOG_API_HOST || 'https://us.i.posthog.com',
},
ID_TOKEN_NAMESPACE: {
required: true,
value:
Expand Down
19 changes: 17 additions & 2 deletions frontend/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import ReactDOM from 'react-dom'
import { Router } from 'react-router-dom'
import App from './App'
import * as serviceWorker from './serviceWorker'
import './i18n'
Expand All @@ -16,6 +17,8 @@ import configureStore, { history } from 'redux/configureStore'
import config from 'constants/config'
// import theme from './material-ui-theme'
import theme from './junctionTheme'
import posthog from 'posthog-js'
import { PostHogProvider } from 'posthog-js/react'

import { library } from '@fortawesome/fontawesome-svg-core'
import { fab } from '@fortawesome/free-brands-svg-icons'
Expand All @@ -25,7 +28,7 @@ library.add(fab)
const { store, persistor } = configureStore()

/** Disable log statements in production */
function noop() {}
function noop() { }

if (!config.IS_DEBUG) {
console.log = noop
Expand All @@ -48,6 +51,14 @@ WebFont.load({
},
})


// Add PostHog
posthog.init(config.POSTHOG_API_KEY, {
api_host: config.POSTHOG_API_HOST,
person_profiles: 'identified_only',
capture_pageview: false
})

ReactDOM.render(
<Provider store={store}>
<PersistGate
Expand All @@ -70,7 +81,11 @@ ReactDOM.render(
>
<Notifier />
<CssBaseline />
<App history={history} />
<PostHogProvider client={posthog}>
<Router history={history}>
<App history={history} location={window.location} />
</Router>
</PostHogProvider>
</SnackbarProvider>
</ThemeProvider>
</StylesProvider>
Expand Down
28 changes: 9 additions & 19 deletions frontend/src/services/analytics.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,39 @@
import ReactGA from 'react-ga'
import ReactPixel from 'react-facebook-pixel'
import config from 'constants/config'
import posthog from 'posthog-js'

const AnalyticsService = {}

AnalyticsService.init = () => {
if (config.GOOGLE_ANALYTICS_ID) {
ReactGA.initialize(config.GOOGLE_ANALYTICS_ID)
}

if (config.FACEBOOK_PIXEL_ID) {
ReactPixel.init(
config.FACEBOOK_PIXEL_ID,
{
autoConfig: true,
debug: false,
},
{},
)
if (config.POSTHOG_API_KEY) {
posthog.init(config.POSTHOG_API_KEY, { api_host: config.POSTHOG_API_HOST })
}
}

AnalyticsService.pageView = location => {
if (config.GOOGLE_ANALYTICS_ID) {
ReactGA.pageview(location.pathname)
if (config.POSTHOG_API_KEY) {
posthog.capture('$pageview', { path: location.pathname })
}
}

AnalyticsService.events = {
LOG_IN: () => {
ReactPixel.track('CompleteRegistration', {
posthog.capture('CompleteRegistration', {
value: 0.1,
currency: 'EUR',
})
},
VIEW_EVENT: slug => {
ReactPixel.track('ViewContent', {
posthog.capture('ViewContent', {
value: 0.1,
currency: 'EUR',
content_ids: slug,
content_type: 'product',
})
},
BEGIN_REGISTRATION: slug => {
ReactPixel.track('AddToCart', {
posthog.capture('AddToCart', {
value: 0.5,
currency: 'EUR',
contents: [
Expand All @@ -56,7 +46,7 @@ AnalyticsService.events = {
})
},
COMPLETE_REGISTRATION: slug => {
ReactPixel.track('Purchase', {
posthog.capture('Purchase', {
value: 1,
currency: 'EUR',
contents: [
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"next-auth": "^4.22.1",
"pino": "^6.3.1",
"pino-pretty": "^4.0.0",
"posthog-js": "^1.154.5",
"react-helmet": "^6.1.0",
"react-pdf": "^4.1.0",
"uuid": "^8.3.2",
Expand Down