Skip to content

Instantly share code, notes, and snippets.

@matheusrod92
Created August 12, 2019 18:55
Show Gist options
  • Save matheusrod92/7f6f6348a81571a197e563895bad9948 to your computer and use it in GitHub Desktop.
Save matheusrod92/7f6f6348a81571a197e563895bad9948 to your computer and use it in GitHub Desktop.
Amplitude Redux Middleware
const myHypotheticalAction = (data) => ({
type: 'MY_HYPOTHETICAL_ACTION',
amplitude: {
event: {
name: 'my-hypothetical-amplitude-event',
properties: data,
},
identify: true,
},
});
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