From e3ea2768af6c8df18cac488688fbdeff4246f6ed Mon Sep 17 00:00:00 2001 From: mildtomato Date: Wed, 27 Jan 2021 20:38:59 +0800 Subject: [PATCH] chore: remove google analytics and tracking cookies --- web/docusaurus.config.js | 1 + web/static/scripts/telemetry.js | 18 ++++++++++++++++++ www/lib/fetchWrapper.ts | 19 +++++++++++++++++++ www/pages/_app.tsx | 33 +++++++++++++++++++++++++++++++++ www/pages/_document.tsx | 14 +------------- 5 files changed, 72 insertions(+), 13 deletions(-) create mode 100644 web/static/scripts/telemetry.js create mode 100644 www/lib/fetchWrapper.ts diff --git a/web/docusaurus.config.js b/web/docusaurus.config.js index beec065a5ad..1d7d82b5685 100755 --- a/web/docusaurus.config.js +++ b/web/docusaurus.config.js @@ -244,6 +244,7 @@ module.exports = { }, ], ], + scripts: [{ src: '/scripts/telemetry.js' }], // plugins: [ // // [ // // '@docusaurus/plugin-content-docs', diff --git a/web/static/scripts/telemetry.js b/web/static/scripts/telemetry.js new file mode 100644 index 00000000000..852a55f680e --- /dev/null +++ b/web/static/scripts/telemetry.js @@ -0,0 +1,18 @@ +function telemetry() { + return fetch('https://app.supabase.io/api/telemetry/page', { + mode: 'no-cors', // 'cors' by default, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Accept: 'application/json', + }, + body: JSON.stringify({ + referrer: document.referrer, + title: document.title, + }), + }).catch((error) => { + console.error('Error at telemetry - post:', error) + }) +} + +telemetry() diff --git a/www/lib/fetchWrapper.ts b/www/lib/fetchWrapper.ts new file mode 100644 index 00000000000..763c9d4c50b --- /dev/null +++ b/www/lib/fetchWrapper.ts @@ -0,0 +1,19 @@ +interface DataProps { + referrer?: string + title: string +} + +export const post = (url: string, data: DataProps, options = {}) => { + return fetch(url, { + mode: 'no-cors', // 'cors' by default, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Accept: 'application/json', + }, + body: JSON.stringify(data), + ...options, + }).catch((error) => { + console.error('Error at fetchWrapper - post:', error) + }) +} diff --git a/www/pages/_app.tsx b/www/pages/_app.tsx index 6ac887c0430..890778ceeeb 100644 --- a/www/pages/_app.tsx +++ b/www/pages/_app.tsx @@ -1,6 +1,39 @@ import { AppProps } from 'next/app' +import { useRouter } from 'next/router' +import { useEffect } from 'react' import '../styles/index.css' +import { post } from './../lib/fetchWrapper' export default function MyApp({ Component, pageProps }: AppProps) { + const router = useRouter() + + function telemetry() { + return post(`https://app.supabase.io/api/telemetry/page`, { + referrer: document.referrer, + title: document.title, + }) + } + + useEffect(() => { + // `routeChangeComplete` won't run for the first page load unless the query string is + // hydrated later on, so here we log a page view if this is the first render and + // there's no query string + if (!router.asPath.includes('?')) { + telemetry() + } + }, []) + + useEffect(() => { + function handleRouteChange() { + telemetry() + } + + // Listen for page changes after a navigation or when the query changes + router.events.on('routeChangeComplete', handleRouteChange) + return () => { + router.events.off('routeChangeComplete', handleRouteChange) + } + }, [router.events]) + return } diff --git a/www/pages/_document.tsx b/www/pages/_document.tsx index cef2c8476ad..4ae1cf1f573 100644 --- a/www/pages/_document.tsx +++ b/www/pages/_document.tsx @@ -11,19 +11,7 @@ export default class MyDocument extends Document { render() { return ( - -