Last active
September 12, 2021 22:15
-
-
Save Ranatchai/6112c5c952299c6ac67bba7dff11c49c to your computer and use it in GitHub Desktop.
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
import React from 'react' | |
const MIXPANEL_TOKEN = '' // token here | |
const appProperties = {} | |
const _waitTracking = [] | |
let mixpanel = null | |
let isInit = false | |
/* | |
always load mixpanel dynamic because it's huge https://github.com/mixpanel/mixpanel-js/issues/128 | |
It uses dynamic-import syntax, that is in a ECMAScript proposal and not currently part of the language standard. if you use babel you need | |
https://yarnpkg.com/en/package/babel-plugin-syntax-dynamic-import | |
*/ | |
export const loadMixpanel = () => | |
import('mixpanel-browser').then(_mixpanel => { | |
mixpanel = _mixpanel | |
if (!isInit) { | |
mixpanel.init(MIXPANEL_TOKEN) | |
isInit = true | |
execWaitTracking() | |
console.log('mixpanel init') | |
} | |
return mixpanel | |
}) | |
// exec track event that occur when mixpanel doesn't load complete | |
function execWaitTracking() { | |
for (let i = 0, length = _waitTracking.length; i < length; i++) { | |
const params = _waitTracking.shift() | |
track(...params) | |
} | |
} | |
export function track(...params) { | |
if (!mixpanel) { | |
_waitTracking.push(params) | |
return | |
} | |
const [eventName, properties = {}] = params | |
console.log('track', eventName) | |
mixpanel.track(eventName, { | |
...appProperties, | |
...properties | |
}) | |
} | |
export const withTracker = Component => | |
class ComponentWithTracker extends React.Component { | |
componentDidMount() { | |
// load mixpanel | |
loadMixpanel().catch(err => { | |
console.warn('load mixpanel failed', err) | |
}) | |
} | |
render() { | |
return <Component {...this.props} track={track} mixpanel={mixpanel} /> | |
} | |
} | |
// simply upgrade hoc to render props, https://reactrocket.com/post/turn-your-hocs-into-render-prop-components/ | |
const Render = ({ children, ...props }) => children(props) | |
// convert hoc to render props | |
export const Tracker = withTracker(Render) | |
// auto track when rendered | |
export const Track = withTracker( | |
class Track extends React.Component { | |
componentDidMount() { | |
this.props.track(this.props.eventName, this.props.properties) | |
} | |
render() { | |
return null | |
} | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
React Examples
For Tracker, it's a render props component https://reactjs.org/docs/render-props.html
For a Track component, use it when you want to track along with render something. it's very simple and useful for me.
For withTracker, it's a higher-order component. https://reactjs.org/docs/higher-order-components.html
a bit old fashion, but still very useful for some case.
JS Case