types#RawEventMessage TypeScript Examples

The following examples show how to use types#RawEventMessage. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: posthog.ts    From posthog-foss with MIT License 5 votes vote down vote up
export function createPosthog(server: Hub, pluginConfig: PluginConfig): DummyPostHog {
    const distinctId = pluginConfig.plugin?.name || `plugin-id-${pluginConfig.plugin_id}`

    let sendEvent: (data: InternalData) => Promise<void>

    if (server.KAFKA_ENABLED) {
        // Sending event to our Kafka>ClickHouse pipeline
        sendEvent = async (data) => {
            if (!server.kafkaProducer) {
                throw new Error('kafkaProducer not configured!')
            }
            // ignore the promise, run in the background just like with celery
            await server.kafkaProducer.queueMessage({
                topic: server.KAFKA_CONSUMPTION_TOPIC!,
                messages: [
                    {
                        key: data.uuid,
                        value: JSON.stringify({
                            distinct_id: data.distinct_id,
                            ip: '',
                            site_url: '',
                            data: JSON.stringify(data),
                            team_id: pluginConfig.team_id,
                            now: data.timestamp,
                            sent_at: data.timestamp,
                            uuid: data.uuid,
                        } as RawEventMessage),
                    },
                ],
            })
        }
    } else {
        // Sending event to our Redis>Postgres pipeline
        const client = new Client(server.db, server.PLUGINS_CELERY_QUEUE)
        sendEvent = async (data) => {
            await client.sendTaskAsync(
                'posthog.tasks.process_event.process_event_with_plugins',
                [data.distinct_id, null, null, data, pluginConfig.team_id, data.timestamp, data.timestamp],
                {}
            )
        }
    }

    return {
        async capture(event, properties = {}) {
            const { timestamp = DateTime.utc().toISO(), distinct_id = distinctId, ...otherProperties } = properties
            const data: InternalData = {
                distinct_id,
                event,
                timestamp,
                properties: {
                    $lib: 'posthog-plugin-server',
                    $lib_version: version,
                    distinct_id,
                    ...otherProperties,
                },
                team_id: pluginConfig.team_id,
                uuid: new UUIDT().toString(),
            }
            await sendEvent(data)
        },
        api: createApi(server, pluginConfig),
    }
}