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

decentraland / decentraland-gatsby / 13206965168

07 Feb 2025 07:47PM UTC coverage: 57.9% (-0.1%) from 58.003%
13206965168

push

github

web-flow
feat: add bot detection to avoid segment tracking (#958)

* feat: add bot detection to avoid segment tracking

* fix: always trigger callback after segment or track

---------

Co-authored-by: Braian Mellor <braianj@gmail.com>

387 of 861 branches covered (44.95%)

Branch coverage included in aggregate %.

1 of 7 new or added lines in 1 file covered. (14.29%)

1328 of 2101 relevant lines covered (63.21%)

6.1 hits per line

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

9.59
/src/utils/development/segment.ts
1
import { isbot } from 'isbot'
6✔
2

3
import { getMouseEventData, getMouseEventName, isMeta } from '../dom/events'
6✔
4
import { isBlankTarget, isLocalLink } from '../dom/links'
6✔
5
import once from '../function/once'
6✔
6
import isMobile from '../isMobile'
6✔
7

8
export type TrackContext = {
9
  wallet: boolean | string
10
  mobile: boolean
11
}
12

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

19
const emptyCallback = () => {}
6✔
20
const getContext = once((): TrackContext => {
6✔
21
  const ethereum = window?.ethereum as any
×
22
  return {
×
23
    mobile: isMobile(),
24
    wallet: !ethereum
×
25
      ? 'none'
26
      : ethereum?.isMetaMask
×
27
      ? 'metamask'
28
      : ethereum?.isDapper
×
29
      ? 'dapper'
30
      : ethereum?.isCucumber
×
31
      ? 'cucumber'
32
      : ethereum?.isTrust
×
33
      ? 'trust'
34
      : ethereum?.isToshi
×
35
      ? 'coinbase'
36
      : ethereum?.isGoWallet
×
37
      ? 'goWallet'
38
      : ethereum?.isAlphaWallet
×
39
      ? 'alphaWallet'
40
      : ethereum?.isStatus
×
41
      ? 'status'
42
      : 'other',
43
  }
44
})
45

46
export default function segment(tracker: Tracker, callback?: () => void) {
×
NEW
47
  const userAgent = window.navigator.userAgent
×
48

NEW
49
  const isBot = isbot(userAgent)
×
50

NEW
51
  if (typeof window !== 'undefined' && window.analytics && !isBot) {
×
52
    tracker(window.analytics, getContext(), callback ?? emptyCallback)
×
53
  } else if (callback) {
×
54
    Promise.resolve().then(() => callback())
×
55
  }
56
}
57

58
export function track(
×
59
  event: string,
60
  data: Record<string, any> = {},
×
61
  callback?: () => void
62
) {
NEW
63
  const userAgent = window.navigator.userAgent
×
64

NEW
65
  const isBot = isbot(userAgent)
×
66

NEW
67
  if (typeof window !== 'undefined' && window.analytics && !isBot) {
×
68
    const analytics = window.analytics
×
69
    analytics.track(event, { ...getContext(), ...data }, callback)
×
70
  } else if (callback) {
×
71
    Promise.resolve().then(() => callback())
×
72
  }
73
}
74

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

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

97
    track(name, data, trackCallback)
×
98
  }) as T
99
}
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