portal: remove project-specific GA logging

pull/567/head
Yangshun 2 years ago
parent 7716875cdf
commit 63bf35e474

@ -134,10 +134,10 @@ export default function AppShell({ children }: Props) {
const [mobileMenuOpen, setMobileMenuOpen] = useState(false); const [mobileMenuOpen, setMobileMenuOpen] = useState(false);
const router = useRouter(); const router = useRouter();
const { data: session } = useSession(); const { data: session } = useSession();
// TODO: Shift this into offers pages and not in this common component.
const { isLoading: isOffersAdminResultsLoading, data: isOffersAdmin } = const { isLoading: isOffersAdminResultsLoading, data: isOffersAdmin } =
trpc.useQuery(['offers.admin.isAdmin']); trpc.useQuery(['offers.admin.isAdmin']);
const currentProductNavigation: Readonly<{ const currentProductNavigation: Readonly<{
googleAnalyticsMeasurementID: string;
logo?: React.ReactNode; logo?: React.ReactNode;
navigation: ProductNavigationItems; navigation: ProductNavigationItems;
showGlobalNav: boolean; showGlobalNav: boolean;
@ -167,8 +167,7 @@ export default function AppShell({ children }: Props) {
})(); })();
return ( return (
<GoogleAnalytics <GoogleAnalytics>
measurementID={currentProductNavigation.googleAnalyticsMeasurementID}>
<div className="flex"> <div className="flex">
{/* Narrow sidebar */} {/* Narrow sidebar */}
{currentProductNavigation.showGlobalNav && ( {currentProductNavigation.showGlobalNav && (

@ -1,17 +1,19 @@
import Head from 'next/head';
import { useRouter } from 'next/router'; import { useRouter } from 'next/router';
import Script from 'next/script';
import { createContext, useContext, useEffect } from 'react'; import { createContext, useContext, useEffect } from 'react';
type Context = Readonly<{ type Context = Readonly<{
event: (payload: GoogleAnalyticsEventPayload) => void; event: (payload: GoogleAnalyticsEventPayload) => void;
}>; }>;
const MEASUREMENT_ID = 'G-DBLZDQ2ZZN';
export const GoogleAnalyticsContext = createContext<Context>({ export const GoogleAnalyticsContext = createContext<Context>({
event, event,
}); });
// https://developers.google.com/analytics/devguides/collection/gtagjs/pages // https://developers.google.com/analytics/devguides/collection/gtagjs/pages
function pageview(measurementID: string, url: string) { function pageview(url: string) {
// Don't log analytics during development. // Don't log analytics during development.
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
return; return;
@ -21,7 +23,6 @@ function pageview(measurementID: string, url: string) {
page_location: window.location.href, page_location: window.location.href,
page_path: url, page_path: url,
page_title: document.title, page_title: document.title,
send_to: measurementID,
}); });
} }
@ -53,49 +54,46 @@ export function event({
type Props = Readonly<{ type Props = Readonly<{
children: React.ReactNode; children: React.ReactNode;
measurementID: string;
}>; }>;
export function useGoogleAnalytics() { export function useGoogleAnalytics() {
return useContext(GoogleAnalyticsContext); return useContext(GoogleAnalyticsContext);
} }
export default function GoogleAnalytics({ children, measurementID }: Props) { export default function GoogleAnalytics({ children }: Props) {
const router = useRouter(); const router = useRouter();
useEffect(() => { useEffect(() => {
function handleRouteChange(url: string) { function handleRouteChange(url: string) {
pageview(measurementID, url); pageview(url);
} }
router.events.on('routeChangeComplete', handleRouteChange); router.events.on('routeChangeComplete', handleRouteChange);
return () => { return () => {
router.events.off('routeChangeComplete', handleRouteChange); router.events.off('routeChangeComplete', handleRouteChange);
}; };
}, [router.events, measurementID]); }, [router.events,]);
return ( return (
<GoogleAnalyticsContext.Provider value={{ event }}> <GoogleAnalyticsContext.Provider value={{ event }}>
{children} {children}
<Head> {/* Global Site Tag (gtag.js) - Google Analytics */}
{/* TODO(yangshun): Change back to next/script in future. */} <Script
{/* Global Site Tag (gtag.js) - Google Analytics */} async={true}
<script src={`https://www.googletagmanager.com/gtag/js?id=${MEASUREMENT_ID}`}
async={true} />
src={`https://www.googletagmanager.com/gtag/js?id=${measurementID}`} <Script
/> dangerouslySetInnerHTML={{
<script __html: `
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || []; window.dataLayer = window.dataLayer || [];
window.gtag = function(){dataLayer.push(arguments);} window.gtag = function(){dataLayer.push(arguments);}
gtag('js', new Date()); gtag('js', new Date());
gtag('config', '${measurementID}', { gtag('config', '${MEASUREMENT_ID}', {
page_path: window.location.pathname, page_path: window.location.pathname,
}); });
`, `,
}} }}
/> id="google-analytics"
</Head> />
</GoogleAnalyticsContext.Provider> </GoogleAnalyticsContext.Provider>
); );
} }

@ -13,7 +13,6 @@
// ]; // ];
const config = { const config = {
googleAnalyticsMeasurementID: 'G-DBLZDQ2ZZN',
navigation: [], navigation: [],
showGlobalNav: false, showGlobalNav: false,
title: 'Tech Interview Handbook', title: 'Tech Interview Handbook',

@ -22,7 +22,6 @@ const navigationAdmin: ProductNavigationItems = [
]; ];
const config = { const config = {
googleAnalyticsMeasurementID: 'G-34XRGLEVCF',
logo: ( logo: (
<img <img
alt="Tech Offers Repo" alt="Tech Offers Repo"

@ -4,12 +4,9 @@ const navigation: ProductNavigationItems = [
{ href: '/questions/browse', name: 'Browse' }, { href: '/questions/browse', name: 'Browse' },
{ href: '/questions/lists', name: 'My Lists' }, { href: '/questions/lists', name: 'My Lists' },
{ href: '/questions/about', name: 'About' }, { href: '/questions/about', name: 'About' },
// { href: '/questions/my-questions', name: 'My Questions' },
// { href: '/questions/history', name: 'History' },
]; ];
const config = { const config = {
googleAnalyticsMeasurementID: 'G-0T4LYWMK8L',
logo: ( logo: (
<img <img
alt="Questions Bank" alt="Questions Bank"

@ -21,7 +21,6 @@ const navigation: ProductNavigationItems = [
]; ];
const config = { const config = {
googleAnalyticsMeasurementID: 'G-VFTWPMW1WK',
logo: ( logo: (
<img <img
alt="Tech Resume Review" alt="Tech Resume Review"

Loading…
Cancel
Save