Skip to content

Commit

Permalink
Merge pull request #260 from yoonghan/develop
Browse files Browse the repository at this point in the history
More webvital comparison
  • Loading branch information
yoonghan authored Aug 25, 2024
2 parents f486b73 + c3aff1c commit 43c1a6b
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion backstop_data/engine_scripts/cookies.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"path": "/",
"name": "termsRead",
"name": "termsGranted",
"value": "true",
"expirationDate": 1798790400,
"hostOnly": false,
Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/mocks/walcron-microfrontend-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ jest.mock("@yoonghan/walcron-microfrontend-shared", () => {
__esModule: true,
reportWebVitals: (callback: any) => {
callback({
name: "CLP",
name: "CLS",
delta: 1,
id: "test",
value: 2,
})
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { site } from "@/config/site"
import ReactGA from "react-ga4"

describe("Client Cookie", () => {
const expectedCookieValue = "termsRead=true;secure;path=/;SameSite=Lax"
const expectedCookieValue = "termsGranted=true;secure;path=/;SameSite=Lax"

beforeAll(() => {
//allow cookie to be re-written
Expand Down
4 changes: 2 additions & 2 deletions src/components/GoogleAnalytic/ClientCookie/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Image from "next/image"
import ReactGA from "react-ga4"

const cookiePrivacy = "https://policies.google.com/technologies/cookies"
const cookieName = "termsRead"
const cookieName = "termsGranted"

type Props = { ga4Id: string }

Expand All @@ -31,7 +31,7 @@ function ClientCookie({ ga4Id }: Props) {
document.cookie = `${cookieName}=true;secure;path=/;SameSite=Lax`
setCookieRead(true)
acceptGAConsent()
}, [])
}, [acceptGAConsent])

useEffect(() => {
function getCookie(cname: string) {
Expand Down
11 changes: 10 additions & 1 deletion src/components/GoogleAnalytic/GoogleAnalytic.jest.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,24 @@ describe("Google Analytic", () => {
const gaValue = "123"
const initializeMock = jest.fn()
const eventMock = jest.fn()
const gtagMock = jest.fn()
jest.spyOn(ReactGA, "initialize").mockImplementation(initializeMock)
jest.spyOn(ReactGA, "event").mockImplementation(eventMock)
jest.spyOn(ReactGA, "gtag").mockImplementation(gtagMock)

render(<GoogleAnalytic ga4Id={gaValue} />)
expect(initializeMock).toHaveBeenCalledWith(gaValue)
expect(eventMock).toHaveBeenCalledWith("web_vitals", {
cwv_metric: "CLP",
cwv_metric: "CLS",
cwv_value: 1,
cwv_id: "test",
})
//first needs to be consent
expect(gtagMock).toHaveBeenNthCalledWith(2, "event", "CLS", {
metric_delta: 1,
metric_id: "test",
metric_value: 2,
value: 1,
})
})
})
9 changes: 8 additions & 1 deletion src/components/GoogleAnalytic/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@ export function GoogleAnalytic({ ga4Id }: Props) {
ReactGA.initialize(ga4Id)

//https://pilotdigital.com/blog/how-to-monitor-core-web-vitals-in-google-analytics/
//https://github.com/google-marketing-solutions/web-vitals-gtm-template
useEffect(() => {
reportWebVitals(({ name, delta, id }) => {
reportWebVitals(({ name, delta, id, value }) => {
ReactGA.event("web_vitals", {
cwv_metric: name,
cwv_value: delta,
cwv_id: id,
})
ReactGA.gtag("event", name, {
value: delta,
metric_id: id,
metric_value: value,
metric_delta: delta,
})
})
}, [])

Expand Down

0 comments on commit 43c1a6b

Please sign in to comment.