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

decentraland / decentraland-gatsby / 16578445039

28 Jul 2025 07:31PM UTC coverage: 58.114% (-0.02%) from 58.133%
16578445039

push

github

web-flow
feat: add getAnalytics from decentraland-dapps (#1194)

390 of 848 branches covered (45.99%)

Branch coverage included in aggregate %.

1 of 17 new or added lines in 1 file covered. (5.88%)

1329 of 2110 relevant lines covered (62.99%)

6.08 hits per line

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

12.9
/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> {
NEW
36
  const utils = await getAnalyticsUtils()
×
NEW
37
  return utils.getAnalytics()
×
38
}
39

40
export default function segment(tracker: Tracker, callback?: () => void) {
×
NEW
41
  if (typeof window !== 'undefined' && window.analytics) {
×
NEW
42
    getAnalytics().then((analytics) => {
×
NEW
43
      if (analytics) {
×
NEW
44
        const context = getContext()
×
NEW
45
        tracker(analytics, context, callback ?? emptyCallback)
×
NEW
46
      } else if (callback) {
×
NEW
47
        Promise.resolve().then(() => callback())
×
48
      }
49
    })
50
  }
51
}
52

53
export function track(
×
54
  event: string,
55
  data: Record<string, any> = {},
×
56
  callback?: () => void
57
) {
NEW
58
  if (typeof window !== 'undefined' && window.analytics) {
×
NEW
59
    getAnalytics().then((analytics) => {
×
NEW
60
      if (analytics) {
×
NEW
61
        const context = getContext()
×
NEW
62
        analytics.track(event, { ...context, ...data }, callback)
×
NEW
63
      } else if (callback) {
×
NEW
64
        Promise.resolve().then(() => callback())
×
65
      }
66
    })
67
  }
68
}
69

70
/** @deprecated use useTrackLinkContext instead */
71
export function createTrackLinkHandler<
×
72
  T extends (event: React.MouseEvent<any>, ...extra: any[]) => void
73
>(callback: T): T {
74
  return ((event: React.MouseEvent<any>, ...extra: any[]) => {
×
75
    const name = getMouseEventName(event)
×
76
    const data = getMouseEventData(event)
×
77

78
    callback(event, ...extra)
×
79
    let trackCallback = emptyCallback
×
80
    if (
×
81
      !isLocalLink(data.href) &&
×
82
      !isBlankTarget(data.target) &&
83
      !isMeta(event) &&
84
      !event.defaultPrevented
85
    ) {
86
      event.preventDefault()
×
87
      trackCallback = () => {
×
88
        window.location.href = data.href!
×
89
      }
90
    }
91

92
    track(name, data, trackCallback)
×
93
  }) as T
94
}
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