Created
June 30, 2021 08:19
-
-
Save kwoncharles/b87991f13cc0e7b0b2b8207d0bbaf0f6 to your computer and use it in GitHub Desktop.
with logging example
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
let amplitude: any; | |
let ga: any; | |
function WithLogging({ | |
action, | |
eventName, | |
children, | |
}: { | |
action: 'view' | 'click' | 'focus'; | |
eventName: string; | |
children: React.ReactElement<{ | |
onClick?: (e: React.MouseEvent<HTMLElement>) => void; | |
onFocus?: (e: React.FocusEvent<HTMLElement>) => void; | |
}>; | |
}) { | |
const actionRef = React.useRef(action); | |
actionRef.current = action; | |
useEffect(() => { | |
if (actionRef.current === 'view') { | |
amplitude.pageView(); | |
ga.viewed(); | |
} | |
}, []); | |
if (action === 'view') { | |
return children; | |
} | |
if (action === 'click') { | |
return React.cloneElement(children, { | |
onClick: (e: React.MouseEvent<HTMLElement>) => { | |
amplitude.track(eventName); | |
ga.track(eventName); | |
children.props.onClick?.(e); | |
}, | |
}); | |
} | |
if (action === 'focus') { | |
return React.cloneElement(children, { | |
onFocus: (e: React.FocusEvent<HTMLElement>) => { | |
amplitude.track(eventName); | |
ga.track(eventName); | |
children.props.onFocus?.(e); | |
}, | |
}); | |
} | |
throw Error(`지원하지 않는 action 타입입니다: ${action}`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment