{"id":24580209,"url":"https://github.com/tomilola-ng/nextjs-ga","last_synced_at":"2026-05-20T06:06:34.411Z","repository":{"id":273659340,"uuid":"920446231","full_name":"Tomilola-ng/nextjs-ga","owner":"Tomilola-ng","description":"Sample Component to Add Google Analytics to your NextJS Applications","archived":false,"fork":false,"pushed_at":"2025-01-22T07:12:15.000Z","size":6,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-22T08:22:45.308Z","etag":null,"topics":["analytics","ga4","google-analytics","nextjs","nextjs15","search","user-tracking"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Tomilola-ng.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-22T06:54:03.000Z","updated_at":"2025-01-22T07:15:08.000Z","dependencies_parsed_at":"2025-01-22T08:22:46.796Z","dependency_job_id":"0ddbf5d2-87ac-4656-ab37-7156561aa510","html_url":"https://github.com/Tomilola-ng/nextjs-ga","commit_stats":null,"previous_names":["tomilola-ng/nextjs-ga"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tomilola-ng%2Fnextjs-ga","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tomilola-ng%2Fnextjs-ga/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tomilola-ng%2Fnextjs-ga/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tomilola-ng%2Fnextjs-ga/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Tomilola-ng","download_url":"https://codeload.github.com/Tomilola-ng/nextjs-ga/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244059790,"owners_count":20391402,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["analytics","ga4","google-analytics","nextjs","nextjs15","search","user-tracking"],"created_at":"2025-01-24T01:52:38.113Z","updated_at":"2026-05-20T06:06:34.379Z","avatar_url":"https://github.com/Tomilola-ng.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\n# 🚀 Next.js Google Analytics Component  \n\n\u003cdiv align=\"center\"\u003e\n\nWelcome to **nextjs-ga**, a simple and reusable React component to seamlessly integrate Google Analytics into your Next.js applications. \u003cbr /\u003e No need to install an npm package—just copy and paste the component into your project, and you're good to go! \n\n\u003c/div\u003e\n\n\u003cdiv\u003e\n\n![Next.js](https://img.shields.io/badge/Next.js-000000?style=for-the-badge\u0026logo=next.js\u0026logoColor=white)  \n![Google Analytics](https://img.shields.io/badge/Google%20Analytics-E37400?style=for-the-badge\u0026logo=google%20analytics\u0026logoColor=white)  \n![TypeScript](https://img.shields.io/badge/TypeScript-3178C6?style=for-the-badge\u0026logo=typescript\u0026logoColor=white)  \n![MIT License](https://img.shields.io/badge/License-MIT-green?style=for-the-badge) \n\n\u003c/div\u003e\n\n\u003c/div\u003e\n\n---\n\n## 🌟 Why Use This Component?  \n\nTracking user behavior and website performance is crucial for optimizing your Next.js app. With this component, you can:  \n- **Easily Add Google Analytics**: No complex setup or npm installations required.  \n- **Reusable and Customizable**: Pass your Google Analytics Tracking ID (`gid`) as a prop.  \n- **Lightweight and Efficient**: Uses Next.js's `Script` component for optimal performance.  \n- **Validation Included**: Ensures your `gid` is in the correct format.  \n\n---\n\n## 🛠️ How to Use  \n\n### Step 1: Copy the Component  \n\nCreate a new file in your `components` folder (e.g., `components/google-analytics.tsx`) and paste the following code:  \n\n```tsx\n// components/google-analytics.tsx\n\"use client\";\nimport Script from \"next/script\";\nimport { useEffect } from \"react\";\n\ninterface AnalyticsProps {\n  gid: string;\n}\n\nexport default function Analytics({ gid }: AnalyticsProps) {\n  useEffect(() =\u003e {\n    // Validate the Google Analytics Tracking ID\n    const isValidGid = /^G-[A-Z0-9]+$/.test(gid);\n    if (!isValidGid) {\n      console.error(\n        \"Invalid Google Analytics Tracking ID. Expected format: G-[A-Z0-9]+\"\n      );\n      return;\n    }\n  }, [gid]);\n\n  return (\n    \u003c\u003e\n      \u003cScript\n        strategy=\"lazyOnload\"\n        src={`https://www.googletagmanager.com/gtag/js?id=${gid}`}\n      /\u003e\n\n      \u003cScript id=\"google-analytics\" strategy=\"lazyOnload\"\u003e\n        {`\n          window.dataLayer = window.dataLayer || [];\n          function gtag(){dataLayer.push(arguments);}\n          gtag('js', new Date());\n          gtag('config', '${gid}', {\n            page_path: window.location.pathname,\n          });\n        `}\n      \u003c/Script\u003e\n    \u003c/\u003e\n  );\n}\n```  \n\n---\n\n### Step 2: Add the Component to Your App  \n\nImport and use the `Analytics` component in your `_app.tsx` (or `layout.tsx`) file to ensure it loads on every page:  \n\n```tsx\n// pages/_app.tsx\nimport Analytics from \"@/components/google-analytics\";\n\nexport default function MyApp({ Component, pageProps }) {\n  return (\n    \u003c\u003e\n      \u003cAnalytics gid=\"G-2087S873KK\" /\u003e\n      \u003cComponent {...pageProps} /\u003e\n    \u003c/\u003e\n  );\n}\n```  \n\nOR\n\n```tsx\n// app/layout.tsx\nimport Analytics from \"@/components/google-analytics\";\n\nexport default function MyApp({ Component, pageProps }) {\n  return (\n    \u003c\u003e\n      ... // Previous Code\n      \u003cAnalytics gid=\"G-2087S873KK\" /\u003e\n      ... // Other Code\n    \u003c/\u003e\n  );\n}\n```  \n\nReplace `\"G-2087S873KK\"` with your actual **Google Analytics Tracking ID**.  \n\n---\n\n### Step 3: Deploy and Verify  \n\nDeploy your Next.js app and visit your website. Open the **Google Analytics Real-Time Dashboard** to verify that your website is sending data.  \n\n---\n\n## ✨ Features  \n\n### 1. **Reusable Component**  \nPass your Google Analytics Tracking ID (`gid`) as a prop to make the component reusable across multiple projects.  \n\n### 2. **Validation**  \nThe component includes a validation check to ensure your `gid` is in the correct format (`G-[A-Z0-9]+`). If the ID is invalid, an error will be logged to the console.  \n\n### 3. **Optimized Performance**  \nThe `Script` component from Next.js ensures the Google Analytics script is loaded efficiently without blocking your page render.  \n\n### 4. **TypeScript Support**  \nThe component is written in TypeScript, providing better tooling support and type safety.  \n\n---\n\n## 🚀 Example Usage  \n\n### Basic Usage  \n```tsx\n\u003cAnalytics gid=\"G-2087S873KK\" /\u003e\n```  \n\n### Dynamic Tracking ID  \nIf your tracking ID is stored in an environment variable, you can pass it dynamically:  \n\n```tsx\n\u003cAnalytics gid={process.env.NEXT_PUBLIC_GA_TRACKING_ID} /\u003e\n```  \n\n---\n\n## 🤝 Contributing  \n\nContributions are welcome! If you have ideas for improvements or find any issues, feel free to:  \n- Open an issue on the [GitHub repository](https://github.com/Tomilola-ng/nextjs-ga).  \n- Submit a pull request with your changes.  \n\nLet’s make this component even better together!  \n\n---\n\n## 👋 Follow Me  \n\nIf you found this component helpful, consider following me on:  \n- **X (Twitter)**: [@Tomilola_ng](https://twitter.com/Tomilola_ng)  \n- **GitHub**: [Tomilola-ng](https://github.com/Tomilola-ng)  \n\nYour support means a lot!  \n\n---\n\n## 📜 License  \n\nThis project is licensed under the **MIT License**. Feel free to use, modify, and distribute it as you see fit.  \n\n---\n\n## 📚 References  \n\n- [Next.js Documentation](https://nextjs.org/docs)  \n- [Google Analytics Documentation](https://developers.google.com/analytics/devguides/collection/gtagjs)  \n\n--- \n\n✨ **Star this repository if you found it helpful!** ✨\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomilola-ng%2Fnextjs-ga","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomilola-ng%2Fnextjs-ga","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomilola-ng%2Fnextjs-ga/lists"}