Skip to content

Commit

Permalink
[Issue #1889] Updating GA4 for nextjs v2 (#2002)
Browse files Browse the repository at this point in the history
## Summary
Fixes #1889

### Time to review: __5 mins__

## Changes proposed
> Added manual "search" api call for GA4 on search bar click
> Updated template to call GA4 correctly
> Updated @next/third-party to canary to fix issues

## Context for reviewers
> Context in Eng Slack Thread

## Additional information
> Screenshots, GIF demos, code examples or output to help show the
changes working as expected.
  • Loading branch information
btabaska authored May 13, 2024
1 parent aa4d15f commit bb423dc
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 7 deletions.
8 changes: 4 additions & 4 deletions frontend/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 frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"ts:check": "tsc --noEmit"
},
"dependencies": {
"@next/third-parties": "^14.0.4",
"@next/third-parties": "^14.3.0-canary.59",
"@opentelemetry/api": "^1.8.0",
"@trussworks/react-uswds": "^7.0.0",
"@uswds/uswds": "^3.6.0",
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ export default function RootLayout({ children, params }: LayoutProps) {

return (
<html lang={locale}>
<head>
<GoogleAnalytics gaId={PUBLIC_ENV.GOOGLE_ANALYTICS_ID} />
</head>
<body>
{/* Separate layout component for the inner-body UI elements since Storybook
and tests trip over the fact that this file renders an <html> tag */}

{/* TODO: Add locale="english" prop when ready for i18n */}
<Layout locale={locale}>{children}</Layout>
</body>
<GoogleAnalytics gaId={PUBLIC_ENV.GOOGLE_ANALYTICS_ID} />
</html>
);
}
2 changes: 1 addition & 1 deletion frontend/src/app/template.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default function Template({ children }: { children: React.ReactNode }) {
useEffect(() => {
isProd &&
PUBLIC_ENV.GOOGLE_ANALYTICS_ID &&
sendGAEvent({ event: "page_view" });
sendGAEvent("event", "page_view");
}, [isProd]);

return <div>{children}</div>;
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/components/search/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { Icon } from "@trussworks/react-uswds";
import { useSearchParamUpdater } from "../../hooks/useSearchParamUpdater";
import { useState } from "react";
import { sendGAEvent } from "@next/third-parties/google";

interface SearchBarProps {
initialQueryParams: string;
Expand All @@ -14,6 +15,7 @@ export default function SearchBar({ initialQueryParams }: SearchBarProps) {

const handleSubmit = () => {
updateQueryParams(inputValue, "query");
sendGAEvent("event", "search", { search_term: inputValue });
};

return (
Expand Down

1 comment on commit bb423dc

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report for ./frontend

St.
Category Percentage Covered / Total
🟢 Statements 84.14% 870/1034
🟡 Branches 65.01% 223/343
🟡 Functions 75.58% 164/217
🟢 Lines 84.18% 809/961

Test suite run success

164 tests passing in 56 suites.

Report generated by 🧪jest coverage report action from bb423dc

Please sign in to comment.