Skip to content

Commit

Permalink
Ga4 mc (#332)
Browse files Browse the repository at this point in the history
* ga4

* ga4

* update action

* Fix analytics

* Fix analytics

* Fix analytics

* Fix analytics #3

* Fix analytics #4

* Fix analytics #5

* CSP config update

* sampleRate change

* Title change

* Add event

* Change event

* Title change #2

* Package update

* Package update

* Package update

* Package update

* Package update

* Package.json update

---------

Co-authored-by: Matteo Cancellieri <matteo.cancellieri@open.ac.uk>
  • Loading branch information
valeriibudko and mcancellieri committed Jan 12, 2024
1 parent 49675b6 commit 114cab5
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pull-request-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- uses: actions/checkout@v2.2.0
- uses: actions/setup-node@v1.4.4
with:
node-version: '12'
node-version: '14'
- name: npm install, build, and test
run: |
npm ci
Expand Down
3 changes: 2 additions & 1 deletion csp.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const PRODUCTION = '*.core.ac.uk core.ac.uk'
const config = {
'default-src': [SELF, PRODUCTION],
// PDF.js worker sadly uses unsafe-eval
'script-src': [SELF, '*.google-analytics.com', "'unsafe-eval'"],
'script-src': [SELF, '*.google-analytics.com', "'unsafe-eval'", '*.googletagmanager.com'],
'style-src': [
SELF,
'fonts.googleapis.com',
Expand All @@ -26,6 +26,7 @@ const config = {
// https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#transport
'*.google-analytics.com',
'stats.g.doubleclick.net',
'*.googletagmanager.com',
],
'connect-src': ['*'],
}
Expand Down
23 changes: 9 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"postcss-preset-env": "^7.7.2",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-ga": "^3.3.0",
"react-ga4": "^2.1.0",
"react-to-print": "^2.12.1",
"smoothscroll-polyfill": "^0.4.4",
"webpack": "^4.44.2",
Expand Down
64 changes: 23 additions & 41 deletions utils/analytics.jsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,24 @@
import React, { createElement } from 'react'
import ReactGA from 'react-ga'
import ReactGA from 'react-ga4'

const isProduction = process.env.NODE_ENV === 'production'
const trackers = (process.env.GA_TRACKING_CODE || '').split(',')
let isGAInitialized = false

if (isProduction && trackers[0] !== '') {
if (trackers.length === 2) {
ReactGA.initialize([
{
trackingId: trackers[0],
gaOptions: {
name: 'prod',
siteSpeedSampleRate: 10,
},
},
{
trackingId: trackers[1],
gaOptions: {
name: 'dev',
siteSpeedSampleRate: 10,
},
},
])
isGAInitialized = true
} else {
ReactGA.initialize(trackers[0])
isGAInitialized = true
}
if (isProduction && process.env.GA_TRACKING_CODE) {
ReactGA.initialize(process.env.GA_TRACKING_CODE, {
siteSpeedSampleRate: 2,
})
isGAInitialized = true
}

const logPageView = () => {
if (!isGAInitialized) return
ReactGA.set({ page: window.location.pathname })
ReactGA.pageview(
window.location.pathname,
trackers.length === 2 ? ['prod', 'dev'] : undefined
)

ReactGA.send({
hitType: 'Reader',
page: `Reader. ${window.location.pathname}`,
title: `Reader. ${window.location.pathname}`,
})
}

export const logEvent = ({
Expand All @@ -47,21 +29,21 @@ export const logEvent = ({
}) => {
if (!isGAInitialized) return

ReactGA.event(
{
category,
action,
label: 'reader',
value,
nonInteraction,
},
trackers.length === 2 ? ['prod', 'dev'] : undefined
)
const valueCategory = category ?? 'Default category'
const valueAction = action ?? 'Default action'
ReactGA.event({
category: `Reader. ${valueCategory}`,
action: `Reader. ${valueAction}`,
label: 'Reader',
value: value ?? 99,
nonInteraction: nonInteraction ?? true,
})
}

export const logTiming = (options) => {
if (!isGAInitialized) return
ReactGA.timing(options, trackers.length === 2 ? ['prod', 'dev'] : undefined)
// eslint-disable-next-line no-console
console.log(JSON.stringify(options))
}

export const withGoogleAnalytics = (Page) => {
Expand Down

0 comments on commit 114cab5

Please sign in to comment.