Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

2.8 KB
Raw Blame
import { HogFunctionTemplate } from '~/cdp/types'

import { WITHOUT_CREDENTIALS_HOG } from '../without-credentials'

export const template: HogFunctionTemplate = {
    free: false,
    status: 'alpha',
    type: 'source_webhook',
    id: 'template-source-webhook-pixel',
    name: 'Tracking pixel',
    description:
        'Capture an event using a 1x1 tracking pixel. Useful for embedding tracking where PostHog SDKs are not available such as emails.',
    icon_url: '/static/services/webhook.svg',
    category: ['Email', 'Tracking'],
    code_language: 'hog',
    code: `${WITHOUT_CREDENTIALS_HOG}
if(inputs.debug) {
  print('Incoming request:', withoutCredentials(request.query))
}

if(not empty(inputs.distinct_id) and not empty(inputs.event)) {
  let properties := {}
  for (let propertyKey, propertyValue in withoutCredentials(inputs.properties ?? {})) {
    properties[propertyKey] := withoutCredentials(propertyValue)
  }
  postHogCapture({
    'event': inputs.event,
    'distinct_id': inputs.distinct_id,
    'properties': properties
  })
} else {
  print('No event captured because the event name or the distinct ID is empty. By default they come from the ph_event and ph_distinct_id query parameters.')
}

return {
  'httpResponse': {
    'status': 200,
    'body': 'R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==',
    'contentType': 'image/gif',
    'isBase64Encoded': true
  }
}
`,
    inputs_schema: [
        {
            key: 'event',
            type: 'string',
            label: 'Event name',
            description:
                'The name of the event to capture. You can derive this from a query parameter, or hard code it unique to this tracking pixel.',
            default: '{request.query.ph_event}',
            secret: false,
            required: true,
        },
        {
            key: 'distinct_id',
            type: 'string',
            label: 'Distinct ID',
            description: 'The distinct ID this event should be associated with',
            default: '{request.query.ph_distinct_id}',
            secret: false,
            required: true,
        },
        {
            key: 'properties',
            type: 'json',
            label: 'Event properties',
            description: 'A mapping of the incoming webhook body to the PostHog event properties',
            default: {
                $lib: 'posthog-webhook',
                $source_url: '{source.url}',
                query_params: '{request.query}',
            },
            secret: false,
            required: false,
        },
        {
            key: 'debug',
            type: 'boolean',
            label: 'Log payloads',
            description: 'Logs the incoming request for debugging',
            secret: false,
            required: false,
            default: false,
        },
    ],
}