@kiwicom/smart-faq
Version:
Smart FAQ
115 lines (99 loc) • 2.75 kB
JavaScript
// @flow
import idx from 'idx';
import * as React from 'react';
import differenceInHours from 'date-fns/difference_in_hours';
import { graphql, createFragmentContainer } from 'react-relay';
import bookingTypes from '../common/booking/bookingTypes';
import { simpleTracker } from '../../shared/helpers/analytics/trackers';
import type { BookingAnalyticsTracker_booking } from './__generated__/BookingAnalyticsTracker_booking.graphql';
import { track } from '../../shared/cuckoo/tracker';
type Props = {|
booking: BookingAnalyticsTracker_booking,
|};
class BookingAnalyticsTracker extends React.Component<Props> {
componentDidMount() {
this.trackBooking();
}
componentDidUpdate(prevProps: Props) {
if (prevProps.booking.id === this.props.booking.id) {
return;
}
this.trackBooking();
}
trackBooking() {
const data = this.getAnalyticsData();
simpleTracker('smartFAQBookingOverview', {
action: 'bookingLoaded',
...data,
});
track('BookingOverview', 'bookingLoaded', data);
}
getAnalyticsData() {
const { booking } = this.props;
const upcomingDeparture = idx(
booking,
_ => _.upcomingDeparture.departure.time,
);
let firstDeparture = null;
switch (booking.type) {
case bookingTypes.ONE_WAY:
firstDeparture = idx(booking, _ => _.trip.departure.time);
break;
case bookingTypes.RETURN:
firstDeparture = idx(booking, _ => _.outbound.departure.time);
break;
case bookingTypes.MULTICITY:
firstDeparture = idx(booking, _ => _.start.time);
break;
}
const timeToSegment = upcomingDeparture
? differenceInHours(upcomingDeparture, new Date())
: false;
const timeToDeparture = firstDeparture
? differenceInHours(firstDeparture, new Date())
: false;
return {
timeToDeparture: timeToDeparture
? Math.round(timeToDeparture)
: timeToDeparture,
timeToSegment: timeToSegment ? Math.round(timeToSegment) : timeToSegment,
};
}
render() {
return null;
}
}
export const RawBookingAnalyticsTracker = BookingAnalyticsTracker;
export default createFragmentContainer(
BookingAnalyticsTracker,
graphql`
fragment BookingAnalyticsTracker_booking on BookingInterface {
id
type: __typename
upcomingDeparture: upcomingLeg {
departure {
time
}
}
... on BookingOneWay {
trip {
departure {
time
}
}
}
... on BookingReturn {
outbound {
departure {
time
}
}
}
... on BookingMulticity {
start {
time
}
}
}
`,
);