MCPcopy Create free account
hub / github.com/Openpanel-dev/openpanel / handleTrack

Function handleTrack

apps/api/src/controllers/track.controller.ts:206–256  ·  view source on GitHub ↗
(
  payload: ITrackPayload,
  context: TrackContext
)

Source from the content-addressed store, hash-verified

204}
205
206async function handleTrack(
207 payload: ITrackPayload,
208 context: TrackContext
209): Promise<void> {
210 const { projectId, deviceId, geo, headers, timestamp, sessionId } = context;
211
212 const uaInfo = parseUserAgent(headers['user-agent'], payload.properties);
213 const groupId = uaInfo.isServer
214 ? payload.profileId
215 ? `${projectId}:${payload.profileId}`
216 : undefined
217 : deviceId;
218 const promises: Promise<unknown>[] = [];
219
220 // If we have more than one property in the identity object, we should identify the user
221 // Otherwise its only a profileId and we should not identify the user
222 if (context.identity && Object.keys(context.identity).length > 1) {
223 promises.push(handleIdentify(context.identity, context));
224 }
225
226 const queueData: EventsQueuePayloadIncomingEvent['payload'] = {
227 projectId,
228 headers,
229 event: {
230 ...payload,
231 groups: payload.groups ?? [],
232 timestamp: timestamp.value,
233 isTimestampFromThePast: timestamp.isFromPast,
234 },
235 uaInfo,
236 geo,
237 deviceId,
238 sessionId,
239 };
240
241 const partitionKey = groupId || generateId();
242
243 if (shouldUseKafka()) {
244 promises.push(produceIncomingEvent(queueData, partitionKey));
245 } else {
246 promises.push(
247 getEventsGroupQueueShard(partitionKey).add({
248 orderMs: timestamp.value,
249 data: queueData,
250 groupId,
251 })
252 );
253 }
254
255 await Promise.all(promises);
256}
257
258async function handleIdentify(
259 payload: IIdentifyPayload,

Callers 1

handlerFunction · 0.85

Calls 8

parseUserAgentFunction · 0.90
generateIdFunction · 0.90
shouldUseKafkaFunction · 0.90
produceIncomingEventFunction · 0.90
getEventsGroupQueueShardFunction · 0.90
handleIdentifyFunction · 0.85
pushMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected