Skip to content

Commit 39e8446

Browse files
log to fast api (#931)
* log to fast api * refactor * text * dont log api calls * move to internal api
1 parent 486910b commit 39e8446

File tree

5 files changed

+92
-3
lines changed

5 files changed

+92
-3
lines changed

Diff for: src/app/_common/fastApiTracking.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"use client";
2+
3+
import { useEffect } from "react";
4+
import { CookieBannerUtils } from "@navikt/arbeidsplassen-react";
5+
import { usePathname, useSearchParams } from "next/navigation";
6+
7+
export function FastApiTracker(): null {
8+
const pathname = usePathname();
9+
const searchParams = useSearchParams();
10+
11+
useEffect(() => {
12+
const trackPageView = async () => {
13+
try {
14+
const userActionTaken: boolean = CookieBannerUtils.getUserActionTakenValue(document.cookie);
15+
const hasCookieConsent: { analyticsConsent: boolean } = CookieBannerUtils.getConsentValues(
16+
document.cookie,
17+
);
18+
const eventName: string = userActionTaken
19+
? hasCookieConsent.analyticsConsent
20+
? "accepted"
21+
: "not-accepted"
22+
: "no-action";
23+
24+
await fetch("/stillinger/api/internal/fastapi", {
25+
method: "POST",
26+
headers: { "Content-Type": "application/json" },
27+
body: JSON.stringify({
28+
url_host: window.location.host,
29+
url_path: pathname,
30+
url_query: searchParams.toString(),
31+
event_name: eventName,
32+
}),
33+
});
34+
} catch (err) {
35+
console.error("An error occurred sending event to API route:", err);
36+
}
37+
};
38+
39+
trackPageView();
40+
}, [pathname, searchParams]);
41+
42+
return null;
43+
}

Diff for: src/app/layout.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { defaultMetadataDescription, defaultOpenGraphImage, getMetadataTitle } f
1717
import App from "./App";
1818
import Providers from "./Providers";
1919
import { CookieBannerUtils } from "@navikt/arbeidsplassen-react";
20+
import { FastApiTracker } from "@/app/_common/fastApiTracking";
2021

2122
export async function generateMetadata(): Promise<Metadata> {
2223
return {
@@ -55,6 +56,7 @@ export default async function RootLayout({ children }: RootLayoutProps): Promise
5556
<body data-theme="arbeidsplassen" className={localFont.className}>
5657
<Providers userPreferences={await actions.getUserPreferences()}>
5758
<App userActionTaken={userActionTaken}>{children}</App>
59+
<FastApiTracker />
5860
</Providers>
5961
</body>
6062
</html>

Diff for: src/app/stillinger/api/internal/fastapi/route.ts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { NextRequest, NextResponse } from "next/server";
2+
3+
const FAST_API_APP_ID_PROD = "41fb84fd-4ff3-43f4-b7e0-d84444fb2f91";
4+
const FAST_API_APP_ID_DEV = "501ec40e-4010-4cb4-ad13-61ab529dd765";
5+
6+
export async function POST(req: NextRequest) {
7+
const allowedOrigins = ["https://arbeidsplassen.intern.dev.nav.no", "https://arbeidsplassen.nav.no"];
8+
9+
const origin = req.headers.get("origin");
10+
if (!origin || !allowedOrigins.includes(origin)) {
11+
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
12+
}
13+
14+
try {
15+
const { url_host, url_path, url_query, event_name } = await req.json();
16+
17+
const appId = process.env.NODE_ENV === "production" ? FAST_API_APP_ID_PROD : FAST_API_APP_ID_DEV;
18+
19+
if (!appId) {
20+
return NextResponse.json({ error: "Missing API ID" }, { status: 500 });
21+
}
22+
23+
const response = await fetch("https://fastapi.nav.no/api/send", {
24+
method: "POST",
25+
headers: { "Content-Type": "application/json" },
26+
body: JSON.stringify({
27+
app_id: appId,
28+
url_host,
29+
url_path,
30+
url_query,
31+
event_name,
32+
}),
33+
});
34+
35+
if (!response.ok) {
36+
throw new Error("Failed to send event to Fast API");
37+
}
38+
39+
return NextResponse.json({ success: true });
40+
} catch (err) {
41+
console.error("Fast API Server Error:", err);
42+
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
43+
}
44+
}

Diff for: src/app/stillinger/stilling/[id]/_components/icons/TwitterIcon.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ function TwitterIcon(): ReactElement {
1313
fill="none"
1414
>
1515
<path
16-
fill-rule="evenodd"
17-
clip-rule="evenodd"
16+
fillRule="evenodd"
17+
clipRule="evenodd"
1818
d="M18.6204 3.20461C18.7346 3.07456 18.8992 3 19.0722 3C19.5892 3 19.8652 3.60931 19.5241 3.99786L13.7239 10.6056L19.8986 19.4265C20.3625 20.0893 19.8884 21 19.0793 21H16.1207C15.7943 21 15.4886 20.8408 15.3014 20.5735L10.7197 14.0282L4.7796 20.7954C4.66544 20.9254 4.50079 21 4.32775 21C3.81075 21 3.53484 20.3907 3.87589 20.0021L10.0099 13.0141L4.10142 4.57346C3.63748 3.91069 4.11163 3 4.92066 3H7.87934C8.20566 3 8.51145 3.15921 8.69858 3.42654L13.0141 9.59155L18.6204 3.20461ZM16.2248 19.8L5.30479 4.2H7.77521L18.6952 19.8H16.2248Z"
1919
fill="currentColor"
2020
/>

Diff for: src/middleware.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function addCspHeaders(requestHeaders: Headers, responseHeaders: Headers) {
3535
frame-src 'self';
3636
block-all-mixed-content;
3737
${process.env.NODE_ENV === "production" ? "upgrade-insecure-requests;" : ""};
38-
connect-src 'self' https://sentry.gc.nav.no umami.nav.no;
38+
connect-src 'self' https://sentry.gc.nav.no umami.nav.no https://fastapi.nav.no;
3939
`;
4040

4141
// Replace newline characters and spaces

0 commit comments

Comments
 (0)