Skip to content

Commit

Permalink
add GA
Browse files Browse the repository at this point in the history
  • Loading branch information
fpgmaas committed Jun 23, 2024
1 parent 1656ad3 commit ad429e8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ jobs:
tags: pypiscoutacr.azurecr.io/pypi-scout-frontend:latest
build-args: |
NEXT_PUBLIC_API_URL=https://pypiscout.com/api
NEXT_PUBLIC_GA_TRACKING_ID=${{ secrets.NEXT_PUBLIC_GA_TRACKING_ID }}
6 changes: 3 additions & 3 deletions frontend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ RUN npm install
# Copy the rest of the application code to the container
COPY . .

# Build argument to accept the API URL during build time
# Add build arguments to environment
ARG NEXT_PUBLIC_API_URL

# Set environment variable within the container
ARG NEXT_PUBLIC_GA_TRACKING_ID
ENV NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL}
ENV NEXT_PUBLIC_GA_TRACKING_ID=${NEXT_PUBLIC_GA_TRACKING_ID}

# Build the Next.js application
RUN npm run build
Expand Down
29 changes: 29 additions & 0 deletions frontend/app/components/GoogleAnalytics.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// app/components/GoogleAnalytics.tsx
"use client";

import { useEffect } from "react";

const GoogleAnalytics = () => {
useEffect(() => {
const trackingId = process.env.NEXT_PUBLIC_GA_TRACKING_ID;
if (trackingId) {
const script1 = document.createElement("script");
script1.async = true;
script1.src = `https://www.googletagmanager.com/gtag/js?id=${trackingId}`;
document.head.appendChild(script1);

const script2 = document.createElement("script");
script2.innerHTML = `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${trackingId}');
`;
document.head.appendChild(script2);
}
}, []);

return null;
};

export default GoogleAnalytics;
6 changes: 5 additions & 1 deletion frontend/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Metadata } from "next";
import { Inter } from "next/font/google";
import "./globals.css";
import GoogleAnalytics from "./components/GoogleAnalytics";

const inter = Inter({ subsets: ["latin"] });

Expand All @@ -16,7 +17,10 @@ export default function RootLayout({
}>) {
return (
<html lang="en">
<body className={inter.className}>{children}</body>
<body className={inter.className}>
<GoogleAnalytics />
{children}
</body>
</html>
);
}

0 comments on commit ad429e8

Please sign in to comment.