( payload: ITrackPayload, context: TrackContext )
| 204 | } |
| 205 | |
| 206 | async 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 | |
| 258 | async function handleIdentify( |
| 259 | payload: IIdentifyPayload, |
no test coverage detected