: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(alerts): capture insight alert destination create/delete events (#71027)
Vasco de Krijgerd2e96c462e05
diff --git a/products/alerts/frontend/logic/alertNotificationLogic.ts b/products/alerts/frontend/logic/alertNotificationLogic.tsindex 0d84cf8ff0f1..14dc26a6d3b1 100644--- a/products/alerts/frontend/logic/alertNotificationLogic.ts+++ b/products/alerts/frontend/logic/alertNotificationLogic.ts@@ -1,5 +1,6 @@import { actions, afterMount, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'import { loaders } from 'kea-loaders'+import posthog from 'posthog-js'import api from 'lib/api'import { integrationsLogic } from 'lib/integrations/integrationsLogic'@@ -18,6 +19,7 @@ import {PendingAlertNotification,buildAlertFilterConfig,buildHogFunctionPayload,+ notificationTypeFromTemplateId,} from 'products/alerts/frontend/logic/alertNotifications'import type { alertNotificationLogicType } from './alertNotificationLogicType'@@ -122,13 +124,16 @@ export const alertNotificationLogic = kea<alertNotificationLogicType>([],})),- listeners(({ actions, values }) => ({+ listeners(({ actions, values, props }) => ({loadIntegrationsSuccess: () => {if (!values.firstSlackIntegration) {actions.setSelectedType(ALERT_NOTIFICATION_TYPE_WEBHOOK)}},deleteExistingHogFunction: async ({ hogFunction }) => {+ // Resolve the type from the closure up front — after the delete the HogFunction is+ // removed from state, so we can't derive it later.+ const type = notificationTypeFromTemplateId(hogFunction.template_id)await deleteWithUndo({endpoint: `projects/${values.currentProjectId}/hog_functions`,object: {@@ -138,6 +143,18 @@ export const alertNotificationLogic = kea<alertNotificationLogicType>([callback: (undo) => {if (undo) {actions.loadExistingHogFunctions()+ return+ }+ // Capture only a delete that actually landed: the callback runs after the API+ // call resolves and not at all on failure, and we skip it on undo. Mirrors the+ // create event, which always carries a known type — so skip an unrecognized+ // template rather than emit a null type that can't be classified. A+ // delete-then-undo still counts once — acceptable for optimistic-undo analytics.+ if (type) {+ posthog.capture('insight alert destination deleted', {+ alert_id: props.alertId,+ type,+ })}},})@@ -156,6 +173,19 @@ export const alertNotificationLogic = kea<alertNotificationLogicType>([}))+ // Capture one event per destination that was actually created, tagged with its type,+ // so adoption of each destination (Slack/Discord/Teams/webhook) is measurable. The+ // destination is a separate HogFunction, so no `alert created`/`alert updated` event+ // records it — this is the only signal that ties a destination type to an alert.+ results.forEach((result, i) => {+ if (result.status === 'fulfilled') {+ posthog.capture('insight alert destination created', {+ alert_id: alertId,+ type: pending[i].type,+ })+ }+ })+const failures = results.map((result, i) => ({ result, notification: pending[i] })).filter(diff --git a/products/alerts/frontend/logic/alertNotifications.test.ts b/products/alerts/frontend/logic/alertNotifications.test.tsindex f2b9fb450633..a0b3d8d1d4f0 100644--- a/products/alerts/frontend/logic/alertNotifications.test.ts+++ b/products/alerts/frontend/logic/alertNotifications.test.ts@@ -1,6 +1,11 @@import { PropertyFilterType, PropertyOperator } from '~/types'-import { PendingAlertNotification, buildAlertFilterConfig, buildHogFunctionPayload } from './alertNotifications'+import {+ PendingAlertNotification,+ buildAlertFilterConfig,+ buildHogFunctionPayload,+ notificationTypeFromTemplateId,+} from './alertNotifications'describe('alertUtils', () => {describe('buildAlertFilterConfig', () => {@@ -164,4 +169,21 @@ describe('alertUtils', () => {})})})++ describe('notificationTypeFromTemplateId', () => {+ // Guards the create/delete analytics events: buildAlertDestination writes these template_ids,+ // and this maps them back to the type we report — a drift between the two would mislabel adoption.+ it.each([+ ['template-slack', 'slack'],+ ['template-discord', 'discord'],+ ['template-microsoft-teams', 'microsoft_teams'],+ ['template-webhook', 'webhook'],+ ])('maps %s to %s', (templateId, expected) => {+ expect(notificationTypeFromTemplateId(templateId)).toEqual(expected)+ })++ it.each([[undefined], [null], ['template-unknown']])('returns null for %s', (templateId) => {+ expect(notificationTypeFromTemplateId(templateId)).toBeNull()+ })+ })})diff --git a/products/alerts/frontend/logic/alertNotifications.ts b/products/alerts/frontend/logic/alertNotifications.tsindex 4be768fef44a..dca9b76d889b 100644--- a/products/alerts/frontend/logic/alertNotifications.ts+++ b/products/alerts/frontend/logic/alertNotifications.ts@@ -16,6 +16,23 @@ export type AlertNotificationType =| typeof ALERT_NOTIFICATION_TYPE_DISCORD| typeof ALERT_NOTIFICATION_TYPE_MICROSOFT_TEAMS+// Single source of truth for which destination HogFunction template each notification type uses.+// buildAlertDestination reads it when creating a destination; notificationTypeFromTemplateId inverts+// it to label an existing one. Keeping both directions off one map avoids the two drifting apart.+const TEMPLATE_ID_BY_NOTIFICATION_TYPE: Record<AlertNotificationType, string> = {+ [ALERT_NOTIFICATION_TYPE_SLACK]: 'template-slack',+ [ALERT_NOTIFICATION_TYPE_DISCORD]: 'template-discord',+ [ALERT_NOTIFICATION_TYPE_MICROSOFT_TEAMS]: 'template-microsoft-teams',+ [ALERT_NOTIFICATION_TYPE_WEBHOOK]: 'template-webhook',+}++// Maps a destination HogFunction's template_id back to the notification type, so analytics and+// UI code can label an existing destination without re-deriving it from inputs.+export const notificationTypeFromTemplateId = (templateId?: string | null): AlertNotificationType | null => {+ const match = Object.entries(TEMPLATE_ID_BY_NOTIFICATION_TYPE).find(([, id]) => id === templateId)+ return match ? (match[0] as AlertNotificationType) : null+}+export const buildAlertFilterConfig = (alertId: string): CyclotronJobFiltersType => ({properties: [{@@ -84,9 +101,9 @@ function buildAlertDestination(case ALERT_NOTIFICATION_TYPE_SLACK:return {name: `${alertName}: Slack #${notification.slackChannelName ?? 'channel'}`,- template_id: 'template-slack',+ template_id: TEMPLATE_ID_BY_NOTIFICATION_TYPE[ALERT_NOTIFICATION_TYPE_SLACK],inputs: {- ...subTemplateInputs('template-slack'),+ ...subTemplateInputs(TEMPLATE_ID_BY_NOTIFICATION_TYPE[ALERT_NOTIFICATION_TYPE_SLACK]),slack_workspace: { value: notification.slackWorkspaceId },channel: { value: notification.slackChannelId },},@@ -94,25 +111,25 @@ function buildAlertDestination(case ALERT_NOTIFICATION_TYPE_DISCORD:return {name: `${alertName}: Discord`,- template_id: 'template-discord',+ template_id: TEMPLATE_ID_BY_NOTIFICATION_TYPE[ALERT_NOTIFICATION_TYPE_DISCORD],inputs: {- ...subTemplateInputs('template-discord'),+ ...subTemplateInputs(TEMPLATE_ID_BY_NOTIFICATION_TYPE[ALERT_NOTIFICATION_TYPE_DISCORD]),webhookUrl: { value: notification.webhookUrl },},}case ALERT_NOTIFICATION_TYPE_MICROSOFT_TEAMS:return {name: `${alertName}: Microsoft Teams`,- template_id: 'template-microsoft-teams',+ template_id: TEMPLATE_ID_BY_NOTIFICATION_TYPE[ALERT_NOTIFICATION_TYPE_MICROSOFT_TEAMS],inputs: {- ...subTemplateInputs('template-microsoft-teams'),+ ...subTemplateInputs(TEMPLATE_ID_BY_NOTIFICATION_TYPE[ALERT_NOTIFICATION_TYPE_MICROSOFT_TEAMS]),webhookUrl: { value: notification.webhookUrl },},}case ALERT_NOTIFICATION_TYPE_WEBHOOK:return {name: `${alertName}: Webhook ${notification.webhookUrl}`,- template_id: 'template-webhook',+ template_id: TEMPLATE_ID_BY_NOTIFICATION_TYPE[ALERT_NOTIFICATION_TYPE_WEBHOOK],inputs: {url: { value: notification.webhookUrl },body: {