CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/components/analytics.tsx
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { join } from "path";
7
import basePath from "lib/base-path";
8
import useCustomize from "lib/use-customize";
9
10
function GoogleAnalytics() {
11
const { googleAnalytics } = useCustomize();
12
13
const GA4_TRACKING_ID = googleAnalytics;
14
if (!GA4_TRACKING_ID) return [];
15
const ga = `\
16
window.dataLayer = window.dataLayer || [];
17
function gtag(){dataLayer.push(arguments);}
18
gtag('js', new Date());
19
gtag('config', '${GA4_TRACKING_ID}');\
20
`;
21
return [
22
<script
23
key={"google-analytics-0"}
24
async={true}
25
defer={true}
26
src={`https://www.googletagmanager.com/gtag/js?id=${GA4_TRACKING_ID}`}
27
/>,
28
<script
29
key={"google-analytics-1"}
30
dangerouslySetInnerHTML={{ __html: ga }}
31
/>,
32
];
33
}
34
35
function CoCalcAnalytics() {
36
return [
37
<script
38
key="cocalc-analytics"
39
async={true}
40
defer={true}
41
src={join(basePath, "analytics.js")}
42
/>,
43
];
44
}
45
46
// Why so careful not to nest things? See
47
// https://nextjs.org/docs/api-reference/next/head
48
// NOTE: Analytics can't be in Head because of script tags! https://github.com/vercel/next.js/pull/26253
49
export default function Analytics(): JSX.Element {
50
return <>{GoogleAnalytics().concat(CoCalcAnalytics())}</>;
51
}
52
53