Created
August 12, 2019 18:55
-
-
Save matheusrod92/7f6f6348a81571a197e563895bad9948 to your computer and use it in GitHub Desktop.
Amplitude Redux Middleware
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
const myHypotheticalAction = (data) => ({ | |
type: 'MY_HYPOTHETICAL_ACTION', | |
amplitude: { | |
event: { | |
name: 'my-hypothetical-amplitude-event', | |
properties: data, | |
}, | |
identify: true, | |
}, | |
}); |
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 amplitude from 'amplitude-js'; | |
import UAParser from 'ua-parser-js'; | |
const amplitudeInstance = amplitude.getInstance(); | |
amplitudeInstance.init(process.env.AMPLITUDE_API_KEY); | |
const logEvent = ({ name, properties = {} }) => { | |
const additionalInformation = { | |
url: window.location.href, | |
ref: window.document.referrer, | |
...properties, | |
}; | |
amplitudeInstance.logEvent(name, additionalInformation); | |
}; | |
const identifyUser = () => { | |
// TO-DO: get parsed user from JWT token | |
// const user = {}; | |
// amplitudeInstance.setUserId(user.id); | |
amplitudeInstance.setUserProperties(UAParser(navigator.userAgent)); | |
}; | |
// eslint-disable-next-line no-unused-vars | |
export default store => next => (action) => { | |
const result = next(action); | |
const { amplitude: actionAmplitude = {} } = action; | |
const { event, identify } = actionAmplitude; | |
if (event) { | |
logEvent(event); | |
} | |
if (identify) { | |
identifyUser(); | |
} | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment