• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

decentraland / decentraland-gatsby / 17596780988

09 Sep 2025 09:59PM UTC coverage: 57.583% (-0.2%) from 57.818%
17596780988

push

github

web-flow
fix: cannot read properties of undefined reading track (#1234)

388 of 847 branches covered (45.81%)

Branch coverage included in aggregate %.

0 of 18 new or added lines in 1 file covered. (0.0%)

1313 of 2107 relevant lines covered (62.32%)

6.06 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

10.81
/src/utils/development/segment.ts
1
import { getMouseEventData, getMouseEventName, isMeta } from '../dom/events'
6✔
2
import { isBlankTarget, isLocalLink } from '../dom/links'
6✔
3
import once from '../function/once'
6✔
4
import isMobile from '../isMobile'
6✔
5

6
export type TrackContext = {
7
  mobile: boolean
8
}
9

10
export type Tracker = (
11
  segment: SegmentAnalytics.AnalyticsJS,
12
  context: TrackContext,
13
  callback: () => void
14
) => void
15

16
const emptyCallback = () => {}
6✔
17

18
// Dynamic import to handle ESM compatibility
19
let analyticsUtils: any = null
6✔
20
export const getAnalyticsUtils = async () => {
6✔
21
  if (!analyticsUtils) {
×
22
    analyticsUtils = await import(
×
23
      'decentraland-dapps/dist/modules/analytics/utils'
24
    )
25
  }
26
  return analyticsUtils
×
27
}
28

29
const getContext = once((): TrackContext => {
6✔
30
  return {
×
31
    mobile: isMobile(),
32
  }
33
})
34

35
async function getAnalytics(): Promise<SegmentAnalytics.AnalyticsJS | null> {
36
  const utils = await getAnalyticsUtils()
×
37
  return utils.getAnalytics()
×
38
}
39

40
export default function segment(tracker: Tracker, callback?: () => void) {
×
41
  if (typeof window !== 'undefined' && window.analytics) {
×
NEW
42
    getAnalytics()
×
43
      .then((analytics) => {
NEW
44
        if (analytics && typeof analytics.track === 'function') {
×
NEW
45
          const context = getContext()
×
NEW
46
          tracker(analytics, context, callback ?? emptyCallback)
×
NEW
47
        } else if (callback) {
×
NEW
48
          Promise.resolve().then(() => callback())
×
49
        }
50
      })
51
      .catch((error) => {
NEW
52
        console.warn('Analytics segment failed:', error)
×
NEW
53
        if (callback) {
×
NEW
54
          Promise.resolve().then(() => callback())
×
55
        }
56
      })
57
  }
58
}
59

60
export function track(
×
61
  event: string,
62
  data: Record<string, any> = {},
×
63
  callback?: () => void
64
) {
65
  if (typeof window !== 'undefined' && window.analytics) {
×
NEW
66
    getAnalytics()
×
67
      .then((analytics) => {
NEW
68
        if (analytics && typeof analytics.track === 'function') {
×
NEW
69
          const context = getContext()
×
NEW
70
          analytics.track(event, { ...context, ...data }, callback)
×
NEW
71
        } else if (callback) {
×
NEW
72
          Promise.resolve().then(() => callback())
×
73
        }
74
      })
75
      .catch((error) => {
NEW
76
        console.warn('Analytics tracking failed:', error)
×
NEW
77
        if (callback) {
×
NEW
78
          Promise.resolve().then(() => callback())
×
79
        }
80
      })
81
  }
82
}
83

84
/** @deprecated use useTrackLinkContext instead */
85
export function createTrackLinkHandler<
×
86
  T extends (event: React.MouseEvent<any>, ...extra: any[]) => void
87
>(callback: T): T {
88
  return ((event: React.MouseEvent<any>, ...extra: any[]) => {
×
89
    const name = getMouseEventName(event)
×
90
    const data = getMouseEventData(event)
×
91

92
    callback(event, ...extra)
×
93
    let trackCallback = emptyCallback
×
94
    if (
×
95
      !isLocalLink(data.href) &&
×
96
      !isBlankTarget(data.target) &&
97
      !isMeta(event) &&
98
      !event.defaultPrevented
99
    ) {
100
      event.preventDefault()
×
101
      trackCallback = () => {
×
102
        window.location.href = data.href!
×
103
      }
104
    }
105

106
    track(name, data, trackCallback)
×
107
  }) as T
108
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc