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

decentraland / decentraland-gatsby / 16572891674

28 Jul 2025 03:13PM UTC coverage: 58.133% (-0.2%) from 58.356%
16572891674

push

github

web-flow
feat: dynamic import analytics (#1192)

390 of 848 branches covered (45.99%)

Branch coverage included in aggregate %.

3 of 13 new or added lines in 1 file covered. (23.08%)

1 existing line in 1 file now uncovered.

1329 of 2109 relevant lines covered (63.02%)

6.08 hits per line

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

13.11
/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
  wallet: boolean | string
8
  mobile: boolean
9
}
10

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

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

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

30
const getContext = once(async (): Promise<TrackContext> => {
6✔
NEW
31
  const utils = await getAnalyticsUtils()
×
NEW
32
  const wallets = utils.getAllWallets()
×
UNCOV
33
  return {
×
34
    mobile: isMobile(),
35
    wallet: wallets.length === 0 ? 'none' : wallets.join(','),
×
36
  }
37
})
38

39
function getAnalytics(): SegmentAnalytics.AnalyticsJS | null {
NEW
40
  return typeof window !== 'undefined' && window.analytics
×
41
    ? window.analytics
42
    : null
43
}
44

45
export default function segment(tracker: Tracker, callback?: () => void) {
×
46
  const analytics = getAnalytics()
×
47
  if (analytics) {
×
NEW
48
    getContext().then((context) => {
×
NEW
49
      tracker(analytics, context, callback ?? emptyCallback)
×
50
    })
51
  } else if (callback) {
×
52
    Promise.resolve().then(() => callback())
×
53
  }
54
}
55

56
export function track(
×
57
  event: string,
58
  data: Record<string, any> = {},
×
59
  callback?: () => void
60
) {
61
  const analytics = getAnalytics()
×
62

63
  if (analytics) {
×
NEW
64
    getContext().then((context) => {
×
NEW
65
      analytics.track(event, { ...context, ...data }, callback)
×
66
    })
67
  } else if (callback) {
×
68
    Promise.resolve().then(() => callback())
×
69
  }
70
}
71

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

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

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