Skip to content

Commit

Permalink
Merge pull request #26 from wajeshubham/dev
Browse files Browse the repository at this point in the history
Add firebase analytics and  page level analytics
  • Loading branch information
wajeshubham authored Feb 4, 2023
2 parents ea5ace6 + 4bc72c9 commit a24c758
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
5 changes: 4 additions & 1 deletion config/firebase.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { initializeApp } from "firebase/app";
import { Firestore, getFirestore } from "firebase/firestore";
import { Auth, getAuth } from "firebase/auth";
import { Analytics, getAnalytics } from "firebase/analytics";

let auth: Auth | undefined;
let db: Firestore | undefined;
let analytics: Analytics | undefined;

try {
const firebaseConfig = {
Expand All @@ -20,6 +22,7 @@ try {

auth = getAuth(app);
db = getFirestore(app);
if (typeof window !== "undefined") analytics = getAnalytics(app);
} catch (error) {
console.log(
Error(
Expand All @@ -28,4 +31,4 @@ try {
);
}

export { auth, db };
export { auth, db, analytics };
25 changes: 25 additions & 0 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
import { analytics } from "@/config/firebase";
import { AuthProvider } from "@/context/AuthContext";
import { SnippngContextProvider } from "@/context/SnippngEditorContext";
import { ToastProvider } from "@/context/ToastContext";
import "@/styles/globals.css";
import { logEvent } from "firebase/analytics";
import type { AppProps } from "next/app";
import Head from "next/head";
import { useRouter } from "next/router";
import { useEffect } from "react";

export default function App({ Component, pageProps }: AppProps) {
const router = useRouter();

useEffect(() => {
if (analytics && process.env.NODE_ENV === "production") {
const _logEvent = (path: string) => {
if (!analytics) return;
logEvent(analytics, "page_view", {
screen_name: path,
});
};
router.events.on("routeChangeComplete", (path) => {
_logEvent(path);
});

_logEvent(window.location.pathname);
return () => {
router.events.off("routeChangeComplete", _logEvent);
};
}
}, [router.events]);

return (
<>
<Head>
Expand Down

1 comment on commit a24c758

@vercel
Copy link

@vercel vercel bot commented on a24c758 Feb 4, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.