Path: blob/master/src/packages/next/components/analytics.tsx
1447 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { join } from "path";6import basePath from "lib/base-path";7import useCustomize from "lib/use-customize";89import type { JSX } from "react";1011function GoogleAnalytics() {12const { googleAnalytics } = useCustomize();1314const GA4_TRACKING_ID = googleAnalytics;15if (!GA4_TRACKING_ID) return [];16const ga = `\17window.dataLayer = window.dataLayer || [];18function gtag(){dataLayer.push(arguments);}19gtag('js', new Date());20gtag('config', '${GA4_TRACKING_ID}');\21`;22return [23<script24key={"google-analytics-0"}25async={true}26defer={true}27src={`https://www.googletagmanager.com/gtag/js?id=${GA4_TRACKING_ID}`}28/>,29<script30key={"google-analytics-1"}31dangerouslySetInnerHTML={{ __html: ga }}32/>,33];34}3536function CoCalcAnalytics() {37return [38<script39key="cocalc-analytics"40async={true}41defer={true}42src={join(basePath, "analytics.js")}43/>,44];45}4647// Why so careful not to nest things? See48// https://nextjs.org/docs/api-reference/next/head49// NOTE: Analytics can't be in Head because of script tags! https://github.com/vercel/next.js/pull/2625350export default function Analytics(): JSX.Element {51return <>{GoogleAnalytics().concat(CoCalcAnalytics())}</>;52}535455