import assert from "assert"; import { RawEvent, NormalizedEvent } from "../../types"; export default (event: RawEvent): NormalizedEvent<"mixpanel"> => { assert(event, `Mixpanel:user null event: ${event}`); assert( event["$distinct_id"], `Mixpanel:user event is missing the required field $distinct_id ${event}` ); // created isn't guarenteed const ts = event["$properties"]["$created"] ? Date.parse(event["$properties"]["$created"]) : 0; return { id: event["$distinct_id"], ts, user_info: { // Mixpanel user IDs are really messy, since they default to creating their own IDs // unless the user provides an ID, which isn't transparent in raw_data, but is when requesting // people. So events store them as source_user_id, but here we store them as both so it actually // creates the important user AND lets you match events for the user source_user_id: event["$distinct_id"], user_id: event["$distinct_id"], name: event["$properties"]["$name"] || `${event["$properties"]["$first_name"] || ""} ${event["$properties"][ "$last_name" ] || ""}`.trim() || null, email: event["$properties"]["$email"] || null, title: null, birthday: null, company: { name: null, id: null, industry: null, employee_count: null, plan: null }, created_at: event["$properties"]["$created"] ? Date.parse(event["$properties"]["$created"]) : null, age: null, gender: null, website: null, ip: null, avatar: null, nickname: null, phone: null, city: event["$properties"]["$city"] || null, country: null, postal_code: null, region: event["$properties"]["$region"] || null, address_line1: null, continent_code: null, address_line2: null, country_code: event["$properties"]["$country_code"] || null, timezone: null, user_agent: null, is_employee: null, custom_attributes: Object.keys(event["$properties"]).length ? JSON.stringify({ "mixpanel|engage": { _ts: ts, ...Object.assign( {}, ...Object.keys(event["$properties"]) .filter(s => !s.startsWith("$")) .map(key => ({ [key]: event["$properties"][key] })) ) } }) : null }, text: "Identified User", href: `https://mixpanel.com/report/explore/#user?distinct_id=${event["$distinct_id"]}`, is_bad: false, direction: null, source: "mixpanel", stream: "engage" }; };