Repository navigation
feat: send navbar clicks made before analytics loads through the beacon #909
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
7945cef
6bf7784
bf52e07
78291c4
09c759c
b7dc1dc
609402e
a379f0d
e709356
d881b17
916d4d4
ed2c8c3
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,10 +1,13 @@ | ||
| import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react' | ||
| import { useLocation } from 'react-router-dom' | ||
| import { useAnalytics } from '@dcl/hooks' | ||
| import { isBotClient, useAnalytics } from '@dcl/hooks' | ||
| import { useFormatMessage } from '../../hooks/adapters/useFormatMessage' | ||
| import { MIN_DISPLAY_BALANCE } from '../../hooks/useManaBalances' | ||
| import { useLocale } from '../../intl/LocaleContext' | ||
| import { isAnalyticsDisabledForSession } from '../../modules/analyticsSessionGate' | ||
| import { SectionViewedTrack, SegmentEvent } from '../../modules/segment' | ||
| import { ensureSegmentAnonymousId } from '../../modules/segmentAnonymousId' | ||
| import { postSegmentEvent } from '../../modules/segmentBeacon' | ||
| import { assetUrl } from '../../utils/assetUrl' | ||
| import { getAvatarBackgroundColor, getDisplayName } from '../../utils/avatarColor' | ||
| // Module-level cache for notification type→component map from ui2. | ||
|
|
@@ -255,12 +258,25 @@ const LandingNavbar = memo(function LandingNavbar({ | |
| const { isInitialized, track } = useAnalytics() | ||
| const [mobileMenuOpen, setMobileMenuOpen] = useState(false) | ||
|
|
||
| // Read through a ref (as useDownloadClick does) so a click sees Segment's current readiness even if it | ||
| // finished loading since the last render. | ||
| const isInitializedRef = useRef(isInitialized) | ||
| isInitializedRef.current = isInitialized | ||
|
|
||
| const trackNavbar = useCallback( | ||
| (action: string, link?: { section: string; href: string }) => { | ||
| if (!isInitialized) return | ||
| track(SegmentEvent.CLICK, { place: SectionViewedTrack.LANDING_NAVBAR, event: 'click', action, ...link }) | ||
| const properties = { place: SectionViewedTrack.LANDING_NAVBAR, event: 'click', action, ...link } | ||
| if (isInitializedRef.current) { | ||
| track(SegmentEvent.CLICK, properties) | ||
| return | ||
| } | ||
| // Analytics loads once the page is idle and is ready only after Segment fetched its settings. Most navbar | ||
| // links leave the page, so a click before that would be lost with it; the beacon survives the unload. It | ||
| // follows the same rules that keep the SDK off: sessions that started on an exempt page, and bots. | ||
| if (isAnalyticsDisabledForSession() || isBotClient(navigator.userAgent)) return | ||
| postSegmentEvent(SegmentEvent.CLICK, properties, ensureSegmentAnonymousId()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Some navbar actions do not leave the page:
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Applied in 6bf7784. Only clicks that replace the page (a same-tab link, Sign In, which redirects) take the beacon now. Clicks that keep the page open (a
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2]
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added in bf52e07: beacon-sent navbar clicks carry |
||
| }, | ||
| [isInitialized, track] | ||
| [track] | ||
| ) | ||
|
|
||
| const trackNavbarLink = useCallback( | ||
|
|
||
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.
[P2] This comment claims more than the ref does. A ref assigned during render only ever holds the value from the last render, so a click cannot see a readiness change that has not been rendered yet. What the ref does is keep
trackNavbarstable, so it no longer depends onisInitializedand doesn't invalidate the memoized handlers. Suggest rewording the comment to say that.useDownloadClickhas the same wording, so it could be fixed there too.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.
Fixed in d881b17: both comments (here and in
useDownloadClick) now say the ref keeps the handler's identity when analytics becomes ready.