Skip to content

Instantly share code, notes, and snippets.

@kwoncharles
Created June 30, 2021 08:19
Show Gist options
  • Save kwoncharles/b87991f13cc0e7b0b2b8207d0bbaf0f6 to your computer and use it in GitHub Desktop.
Save kwoncharles/b87991f13cc0e7b0b2b8207d0bbaf0f6 to your computer and use it in GitHub Desktop.
with logging example
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