-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: analytics through safe-apps-sdk (#80)
- Loading branch information
Showing
11 changed files
with
185 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export const LOCK_EVENTS = { | ||
LOCK_BUTTON: { | ||
action: 'Lock tokens button', | ||
}, | ||
UNLOCK_BUTTON: { | ||
action: 'Unlock tokens button', | ||
}, | ||
WITHDRAW_BUTTON: { | ||
action: 'Withdraw tokens button', | ||
}, | ||
LOCK_SUCCESS: { | ||
action: 'Transaction proposed', | ||
label: 'locking', | ||
}, | ||
UNLOCK_SUCCESS: { | ||
action: 'Transaction proposed', | ||
label: 'unlocking', | ||
}, | ||
WITHDRAW_SUCCESS: { | ||
action: 'Transaction proposed', | ||
label: 'withdrawal', | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export const NAVIGATION_EVENTS = { | ||
OPEN_LOCKING: { | ||
action: 'Open locking page', | ||
}, | ||
OPEN_UNLOCKING: { | ||
action: 'Open unlocking/withdrawal page', | ||
}, | ||
OPEN_CLAIM: { | ||
action: 'Open claim page', | ||
}, | ||
LEADERBOARD_SHOW_MORE: { | ||
action: 'Leaderboard show more', | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { trackSafeAppEvent } from '@/utils/analytics' | ||
import type { ReactElement } from 'react' | ||
import { Fragment, useEffect, useRef } from 'react' | ||
|
||
type Props = { | ||
children: ReactElement | ||
as?: 'span' | 'div' | ||
action: string | ||
label?: string | ||
} | ||
|
||
const shouldTrack = (el: HTMLDivElement) => { | ||
const disabledChildren = el.querySelectorAll('*[disabled]') | ||
return disabledChildren.length === 0 | ||
} | ||
|
||
const Track = ({ children, as: Wrapper = 'span', ...trackData }: Props): typeof children => { | ||
const el = useRef<HTMLDivElement>(null) | ||
|
||
useEffect(() => { | ||
if (!el.current) { | ||
return | ||
} | ||
|
||
const trackEl = el.current | ||
|
||
const handleClick = () => { | ||
if (shouldTrack(trackEl)) { | ||
trackSafeAppEvent(trackData.action, trackData.label) | ||
} | ||
} | ||
|
||
// We cannot use onClick as events in children do not always bubble up | ||
trackEl.addEventListener('click', handleClick) | ||
return () => { | ||
trackEl.removeEventListener('click', handleClick) | ||
} | ||
}, [el, trackData]) | ||
|
||
if (children.type === Fragment) { | ||
throw new Error('Fragments cannot be tracked.') | ||
} | ||
|
||
return ( | ||
<Wrapper data-track={trackData.action} ref={el}> | ||
{children} | ||
</Wrapper> | ||
) | ||
} | ||
|
||
export default Track |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const SAFE_APPS_ANALYTICS_CATEGORY = 'safe-apps-analytics' | ||
|
||
export const trackSafeAppEvent = (action: string, label?: string) => { | ||
window.parent.postMessage( | ||
{ | ||
category: SAFE_APPS_ANALYTICS_CATEGORY, | ||
action, | ||
label, | ||
safeAppName: 'Safe{Miles}', | ||
}, | ||
'*', | ||
) | ||
} |