Capture Calendly bookings as User EventsUpdated 21/05/2019


This code allows you to capture calendly bookings as User Events


const { event: eventName, payload = {} } = body;
const {
  event = {},
  event_type = {},
  invitee = {},
  time,
  questions_and_answers = []
} = payload;

const prefix = (obj, pfx) => _.mapKeys(obj, (v, k) => `${pfx}${k}`);

const actionName = eventName !== "invitee.canceled" ? "booked" : "canceled";
const properties = {
  assigned_to: event.assigned_to || "none",
  ..._.pick(event, "cancel_reason", "canceled", "uuid", "location"),
  ...prefix(
    _.pick(event_type, "kind", "name", "slug", "uuid", "duration"),
    "type_"
  ),
  ...prefix(
    _.pick(
      invitee,
      "cancel_reason",
      "canceled",
      "canceled_at",
      "canceler_name",
      "email",
      "first_name",
      "is_reschedule",
      "last_name",
      "name",
      "timezone"
    ),
    "invitee_"
  ),
  ..._.reduce(
    questions_and_answers,
    (m, { question, answer }) => {
      m[`demo_${question}`] = answer;
      return m;
    },
    {}
  ),
  demo_email: invitee.email,
  canceled_at: moment(event.canceled_at).toISOString(),
  end_time_at: moment(event.end_time).toISOString(),
  start_time_at: moment(event.start_time).toISOString()
};

hull.user({ email: invitee.email }).track(`Demo ${actionName}`, properties, {
  created_at: moment(time).toISOString()
});