Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ RUN npm ci --include=dev --legacy-peer-deps || npm install --include=dev --legac
ENV NODE_ENV=$NODE_ENV
ENV NODE_OPTIONS="--openssl-legacy-provider"

RUN npm run build
RUN npm run build && \
mkdir -p public/static/search/_next && \
cp -r .next/static public/static/search/_next/

EXPOSE 8080
CMD ["node_modules/next/dist/bin/next", "start", "-p", "8080"]
Expand Down
3 changes: 3 additions & 0 deletions csp.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const config = {
'*.doubleclick.net',
'*.googletagmanager.com',
'*.cloudflareinsights.com',
'tracker.core.ac.uk',
LOGROCKET,
'blob:',
'data:',
Expand Down Expand Up @@ -50,6 +51,8 @@ const config = {
SELF,
PRODUCTION,
'sentry.io',
'plausible.io',
'tracker.core.ac.uk',
'*.google-analytics.com',
'*.doubleclick.net',
'*.googletagmanager.com',
Expand Down
2 changes: 1 addition & 1 deletion data/.formap.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion data/.members.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions data/data-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const getApiFromFileName = function (filepath) {
}

const retrieveStats = async (cacheFilePath) => {
if (process.env.NODE_ENV === 'development') return null
const errorMsg =
'Fail. ==> Statistics retrieval failed due to API Core instability.'

Expand Down
55 changes: 50 additions & 5 deletions hooks/use-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,62 @@ import ReactGA from 'react-ga4'
import { useCookie } from '@oacore/design'

const useAnalytics = () => {
const PLAUSIBLE_DOMAIN = 'core-frontend-stage-search.thankfulplant-67ea1df5.uksouth.azurecontainerapps.io'
const analyticsAllowed = useCookie('analytics_cookies_allowed')

const router = useRouter()
const reportPageview = useCallback((url, title, type = 'search') => {
const search = typeof window !== 'undefined' ? window.location.search.substring(1) : ''

ReactGA.send({
hitType: type,
page: url,
title: `Search | ${window.location.search.substring(1)}`,
title: `Search | ${search}`,
})
}, [])

useEffect(() => {
console.log('init useAnalytics plausible')
if (typeof window === 'undefined') return undefined

import('@plausible-analytics/tracker').then(({ init }) => {
console.log('Init plausible-analytics/tracker')
init({
domain: PLAUSIBLE_DOMAIN,
endpoint: 'https://tracker.core.ac.uk/api/event',
outboundLinks: true,
fileDownloads: true,
formSubmissions: true,
autoCapturePageviews: false,
})
console.log('Plausible initialized, window.plausible:', !!window.plausible)
if (window.plausible) {
window.plausible('pageview')
console.log('Plausible pageview sent')
}
}).catch((error) => {
console.log('Error plausible-analytics/tracker')
console.log(error)
})

return undefined
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])

useEffect(() => {
console.log('init useAnalytics GA')
if (typeof window === 'undefined') return undefined

if (analyticsAllowed && process.env.NODE_ENV === 'production') {
// Initialise production Google Analytics
console.log(process.env.GA_TRACKING_CODE)
console.log("GA")
console.log("init GA")

ReactGA.initialize(process.env.GA_TRACKING_CODE)
} else if (analyticsAllowed) {
console.log(process.env.GA_TRACKING_CODE)
console.log("init analyticsAllowed")

window.ga = (...args) =>
// We want to have logging in the development environment
// eslint-disable-next-line no-console
Expand All @@ -36,13 +73,21 @@ const useAnalytics = () => {
return () => {
window.ga = null
}
}, [])
}, [analyticsAllowed, reportPageview, router.asPath])

useEffect(() => {
router.events.on('routeChangeComplete', reportPageview)
const handleRouteChange = (url) => {
reportPageview(url)
if (typeof window !== 'undefined' && window.plausible) {
window.plausible('pageview')
console.log('Plausible pageview on route change:', url)
}
}

router.events.on('routeChangeComplete', handleRouteChange)

return () => {
router.events.off('routeChangeComplete', reportPageview)
router.events.off('routeChangeComplete', handleRouteChange)
}
}, [])
}
Expand Down
1 change: 1 addition & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const nextConfig = {
},
{
source: `/static/:path*`,
// source: `/:path*`,
destination: '/:path*',
},
]
Expand Down
Loading