-
-
Notifications
You must be signed in to change notification settings - Fork 1k
[docs] Add analytics and close banner btn #4005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,15 @@ | ||
| import clsx from 'clsx'; | ||
| import React, { type ReactNode, useEffect, useMemo, useState } from 'react'; | ||
| import { type ReactNode, useEffect, useMemo, useState } from 'react'; | ||
|
|
||
| import HandIcon from '../HandIcon'; | ||
| import styles from './styles.module.css'; | ||
|
|
||
| declare global { | ||
| interface Window { | ||
| dataLayer?: unknown[]; | ||
| } | ||
| } | ||
|
|
||
| type Promo = { | ||
| key: string; | ||
| href: string; | ||
|
|
@@ -45,11 +51,44 @@ const PROMOS: readonly Promo[] = [ | |
| }, | ||
| ]; | ||
|
|
||
| export default function TopPromoRotator() { | ||
| // bump when adding promos so users who dismissed banner see the new one | ||
| export const PROMO_VERSION = 1; | ||
|
|
||
| type Props = { | ||
| onClose?: () => void; | ||
| }; | ||
|
|
||
| export default function TopPromoRotator({ onClose }: Props) { | ||
| const promos = useMemo(() => PROMOS, []); | ||
|
|
||
| const [index, setIndex] = useState(0); | ||
|
|
||
| useEffect(() => { | ||
| if (typeof window === 'undefined' || typeof document === 'undefined') { | ||
| return; | ||
| } | ||
|
|
||
| const existingScript = document.querySelector<HTMLScriptElement>( | ||
| 'script[src*="www.googletagmanager.com/gtm.js?id=GTM-WV2G3SQL"]' | ||
| ); | ||
|
|
||
| if (existingScript) return; | ||
|
|
||
| (function (w: Window, d: Document, s: string, l: string, i: string) { | ||
| w.dataLayer = w.dataLayer || []; | ||
| w.dataLayer.push({ | ||
| 'gtm.start': new Date().getTime(), | ||
| event: 'gtm.js', | ||
| }); | ||
| const f = d.getElementsByTagName(s)[0] as HTMLScriptElement; | ||
| const j = d.createElement(s) as HTMLScriptElement; | ||
| const dl = l !== 'dataLayer' ? `&l=${l}` : ''; | ||
| j.async = true; | ||
| j.src = `https://www.googletagmanager.com/gtm.js?id=${i}${dl}`; | ||
| f.parentNode?.insertBefore(j, f); | ||
| })(window, document, 'script', 'dataLayer', 'GTM-WV2G3SQL'); | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| const id = window.setInterval(() => { | ||
| setIndex(i => (i + 1) % promos.length); | ||
|
|
@@ -89,6 +128,15 @@ export default function TopPromoRotator() { | |
| </a> | ||
| ))} | ||
| </div> | ||
| {onClose && ( | ||
| <button | ||
| type="button" | ||
| className={styles.closeButton} | ||
| aria-label="Close promotion banner" | ||
| onClick={onClose}> | ||
| × | ||
| </button> | ||
| )} | ||
|
Comment on lines
+131
to
+139
|
||
| <span className="sr-only"> | ||
| {typeof active.label === 'string' ? active.label : ''} | ||
| </span> | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -75,3 +75,17 @@ | |||||||||||||||||||||||||||||||
| text-decoration: underline; | ||||||||||||||||||||||||||||||||
| text-underline-offset: 2px; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| .closeButton { | ||||||||||||||||||||||||||||||||
| position: absolute; | ||||||||||||||||||||||||||||||||
| right: 8px; | ||||||||||||||||||||||||||||||||
| top: 50%; | ||||||||||||||||||||||||||||||||
| transform: translateY(-50%); | ||||||||||||||||||||||||||||||||
| border: none; | ||||||||||||||||||||||||||||||||
| background: transparent; | ||||||||||||||||||||||||||||||||
| color: #001a72; | ||||||||||||||||||||||||||||||||
| cursor: pointer; | ||||||||||||||||||||||||||||||||
| padding: 4px; | ||||||||||||||||||||||||||||||||
| line-height: 1; | ||||||||||||||||||||||||||||||||
| font-size: 16px; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Comment on lines
+88
to
+91
|
||||||||||||||||||||||||||||||||
| padding: 4px; | |
| line-height: 1; | |
| font-size: 16px; | |
| } | |
| padding: 8px; | |
| min-width: 32px; | |
| min-height: 32px; | |
| line-height: 1; | |
| font-size: 16px; | |
| } | |
| .closeButton:focus-visible { | |
| outline: 2px solid #001a72; | |
| outline-offset: 2px; | |
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,9 +1,48 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import React from 'react'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import useBaseUrl from '@docusaurus/useBaseUrl'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { useLocation } from '@docusaurus/router'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { Navbar } from '@swmansion/t-rex-ui'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import TopPromoRotator from '@site/src/components/TopPromoRotator'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import TopPromoRotator, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PROMO_VERSION, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } from '@site/src/components/TopPromoRotator'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export default function NavbarWrapper(props) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const location = useLocation(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const baseUrl = useBaseUrl('/'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const isLanding = location.pathname === baseUrl; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const [showPromo, setShowPromo] = React.useState(true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| React.useEffect(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (isLanding || typeof globalThis === 'undefined') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const raw = globalThis.localStorage?.getItem('topPromoState'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const state = raw ? JSON.parse(raw) : null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (state?.v === PROMO_VERSION && state?.hidden) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setShowPromo(false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (_) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // ignore | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, [isLanding]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+14
to
+31
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const [showPromo, setShowPromo] = React.useState(true); | |
| React.useEffect(() => { | |
| if (isLanding || typeof globalThis === 'undefined') { | |
| return; | |
| } | |
| try { | |
| const raw = globalThis.localStorage?.getItem('topPromoState'); | |
| const state = raw ? JSON.parse(raw) : null; | |
| if (state?.v === PROMO_VERSION && state?.hidden) { | |
| setShowPromo(false); | |
| } | |
| } catch (_) { | |
| // ignore | |
| } | |
| }, [isLanding]); | |
| const [showPromo, setShowPromo] = React.useState(() => { | |
| // On the landing page, `showPromo` is not used, so default to true. | |
| if (isLanding) { | |
| return true; | |
| } | |
| // Only attempt to read from localStorage in the browser. | |
| if (typeof window === 'undefined' || !window.localStorage) { | |
| return true; | |
| } | |
| try { | |
| const raw = window.localStorage.getItem('topPromoState'); | |
| const state = raw ? JSON.parse(raw) : null; | |
| if (state?.v === PROMO_VERSION && state?.hidden) { | |
| return false; | |
| } | |
| } catch { | |
| // ignore and fall through to default | |
| } | |
| return true; | |
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This component injects a second GTM container (GTM-WV2G3SQL) at runtime, but the site already loads GTM via
@docusaurus/plugin-google-tag-manager(GTM-PHF2NKVT indocusaurus.config.js). Loading two containers on the samedataLayercan lead to duplicated tracking/perf overhead and makes analytics configuration harder to reason about. Consider moving this to the Docusaurus config (or a custom plugin) and/or using a distinct dataLayer name for the dedicated container if it must coexist.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume that both work fine, right @p-malecki?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it won't be any problem.