Skip to content

πŸ“ˆ Open source, privacy-first web analytics β€” a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

feat: Alerts to trigger every time a custom event occurs

Blue Mouse0c7af99b4ac4
diff --git a/backend/apps/cloud/src/alert/alert.controller.ts b/backend/apps/cloud/src/alert/alert.controller.ts
index 9ead87e43..fe4a397d7 100644
--- a/backend/apps/cloud/src/alert/alert.controller.ts
+++ b/backend/apps/cloud/src/alert/alert.controller.ts
@@ -189,6 +189,13 @@ export class AlertController {
alert.queryCondition = null
alert.queryValue = null
alert.queryTime = null
+ } else if (
+ alertDTO.queryMetric === QueryMetric.CUSTOM_EVENTS &&
+ alertDTO.alertOnEveryCustomEvent
+ ) {
+ alert.queryCondition = null
+ alert.queryValue = null
+ alert.queryTime = null
} else {
if (alertDTO.queryCondition === undefined)
alert.queryCondition = QueryCondition.GREATER_THAN
@@ -201,6 +208,10 @@ export class AlertController {
alert.alertOnNewErrorsOnly = true
}
+ if (alertDTO.alertOnEveryCustomEvent === undefined) {
+ alert.alertOnEveryCustomEvent = false
+ }
+
const newAlert = await this.alertService.create(alert)
project.alerts.push(newAlert)
@@ -250,6 +261,7 @@ export class AlertController {
'name',
'queryCustomEvent',
'alertOnNewErrorsOnly',
+ 'alertOnEveryCustomEvent',
'active',
]),
}
@@ -258,6 +270,13 @@ export class AlertController {
updatePayload.queryCondition = null
updatePayload.queryValue = null
updatePayload.queryTime = null
+ } else if (
+ alertDTO.queryMetric === QueryMetric.CUSTOM_EVENTS &&
+ alertDTO.alertOnEveryCustomEvent
+ ) {
+ updatePayload.queryCondition = null
+ updatePayload.queryValue = null
+ updatePayload.queryTime = null
} else {
if (alertDTO.queryCondition !== undefined)
updatePayload.queryCondition = alertDTO.queryCondition
@@ -271,6 +290,10 @@ export class AlertController {
updatePayload.alertOnNewErrorsOnly = alertDTO.alertOnNewErrorsOnly
}
+ if (alertDTO.alertOnEveryCustomEvent !== undefined) {
+ updatePayload.alertOnEveryCustomEvent = alertDTO.alertOnEveryCustomEvent
+ }
+
await this.alertService.update(
id,
_omit(updatePayload, ['project', 'lastTriggered']),
diff --git a/backend/apps/cloud/src/alert/dto/alert.dto.ts b/backend/apps/cloud/src/alert/dto/alert.dto.ts
index d4e38ecf4..8ff54ac40 100644
--- a/backend/apps/cloud/src/alert/dto/alert.dto.ts
+++ b/backend/apps/cloud/src/alert/dto/alert.dto.ts
@@ -73,6 +73,11 @@ class AlertBaseDTO {
@IsBoolean()
@IsOptional()
alertOnNewErrorsOnly?: boolean
+
+ @ApiProperty()
+ @IsBoolean()
+ @IsOptional()
+ alertOnEveryCustomEvent?: boolean
}
export class CreateAlertDTO extends AlertBaseDTO {
diff --git a/backend/apps/cloud/src/alert/entity/alert.entity.ts b/backend/apps/cloud/src/alert/entity/alert.entity.ts
index a5c0bda08..257610d38 100644
--- a/backend/apps/cloud/src/alert/entity/alert.entity.ts
+++ b/backend/apps/cloud/src/alert/entity/alert.entity.ts
@@ -30,6 +30,13 @@ export class Alert {
})
alertOnNewErrorsOnly: boolean
+ @Column({
+ type: 'boolean',
+ default: false,
+ name: 'alert_on_every_custom_event',
+ })
+ alertOnEveryCustomEvent: boolean
+
@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
created: Date
diff --git a/backend/apps/cloud/src/task-manager/task-manager.service.ts b/backend/apps/cloud/src/task-manager/task-manager.service.ts
index 75b4f75f2..db6e4b8f6 100644
--- a/backend/apps/cloud/src/task-manager/task-manager.service.ts
+++ b/backend/apps/cloud/src/task-manager/task-manager.service.ts
@@ -1011,9 +1011,10 @@ export class TaskManagerService {
})
}
- @Cron(CronExpression.EVERY_5_MINUTES)
+ // @Cron(CronExpression.EVERY_5_MINUTES)
+ @Cron(CronExpression.EVERY_MINUTE)
async checkMetricAlerts() {
- const CRON_INTERVAL_SECONDS = 300
+ const CRON_INTERVAL_SECONDS = 60 // 300
const projects = await this.projectService.find({
where: {
@@ -1039,7 +1040,11 @@ export class TaskManagerService {
if (
alert.lastTriggered !== null &&
- alert.queryMetric !== QueryMetric.ERRORS
+ alert.queryMetric !== QueryMetric.ERRORS &&
+ !(
+ alert.queryMetric === QueryMetric.CUSTOM_EVENTS &&
+ alert.alertOnEveryCustomEvent
+ )
) {
const lastTriggered = new Date(alert.lastTriggered)
const now = new Date()
@@ -1053,18 +1058,21 @@ export class TaskManagerService {
const queryParams: Record<string, any> = { pid: project.id }
const nowUnix = dayjs.utc().unix()
- let errorSubtractSeconds = 0
+ let lastEventSubtractSeconds = 0
if (alert.lastTriggered) {
- errorSubtractSeconds = nowUnix - dayjs.utc(alert.lastTriggered).unix()
+ lastEventSubtractSeconds =
+ nowUnix - dayjs.utc(alert.lastTriggered).unix()
} else {
- errorSubtractSeconds =
+ lastEventSubtractSeconds =
nowUnix - dayjs.utc().subtract(CRON_INTERVAL_SECONDS, 'second').unix()
}
const subtractSecondsTimeframe =
- alert.queryMetric === QueryMetric.ERRORS
- ? errorSubtractSeconds
+ alert.queryMetric === QueryMetric.ERRORS ||
+ (alert.queryMetric === QueryMetric.CUSTOM_EVENTS &&
+ alert.alertOnEveryCustomEvent)
+ ? lastEventSubtractSeconds
: getQueryTime(alert.queryTime as QueryTime)
if (alert.queryMetric === QueryMetric.ERRORS) {
@@ -1125,11 +1133,14 @@ export class TaskManagerService {
const conditionMet =
alert.queryMetric === QueryMetric.ERRORS
? count > 0
- : checkQueryCondition(
- count,
- alert.queryValue as number,
- alert.queryCondition as QueryCondition,
- )
+ : alert.queryMetric === QueryMetric.CUSTOM_EVENTS &&
+ alert.alertOnEveryCustomEvent
+ ? count > 0
+ : checkQueryCondition(
+ count,
+ alert.queryValue as number,
+ alert.queryCondition as QueryCondition,
+ )
if (!conditionMet) {
return
@@ -1220,7 +1231,10 @@ export class TaskManagerService {
const effectiveQueryTimeString =
alert.queryMetric === QueryMetric.ERRORS
? `${CRON_INTERVAL_SECONDS / 60} minutes`
- : getQueryTimeString(alert.queryTime as QueryTime)
+ : alert.queryMetric === QueryMetric.CUSTOM_EVENTS &&
+ alert.alertOnEveryCustomEvent
+ ? `${CRON_INTERVAL_SECONDS / 60} minutes`
+ : getQueryTimeString(alert.queryTime as QueryTime)
let text = ``
@@ -1296,9 +1310,18 @@ export class TaskManagerService {
customEventInfo = ` "${alert.queryCustomEvent}"`
}
- text =
- `πŸ”” Alert *${alertName}* triggered!\n\n` +
- `Your project [${projectName}](${escapedProjectLink}) has had *${count}${customEventInfo}* ${queryMetricString} in the last *${effectiveQueryTimeString}*!`
+ if (
+ alert.queryMetric === QueryMetric.CUSTOM_EVENTS &&
+ alert.alertOnEveryCustomEvent
+ ) {
+ text =
+ `πŸ”” Alert *${alertName}* triggered!\n\n` +
+ `Your project [${projectName}](${escapedProjectLink}) has had *${count}${customEventInfo}* ${queryMetricString} occur in the last *${effectiveQueryTimeString}*!`
+ } else {
+ text =
+ `πŸ”” Alert *${alertName}* triggered!\n\n` +
+ `Your project [${projectName}](${escapedProjectLink}) has had *${count}${customEventInfo}* ${queryMetricString} in the last *${effectiveQueryTimeString}*!`
+ }
}
if (project.admin?.isTelegramChatIdConfirmed) {
diff --git a/backend/migrations/mysql/2025_05_22.sql b/backend/migrations/mysql/2025_05_22.sql
new file mode 100644
index 000000000..f34509e1b
--- /dev/null
+++ b/backend/migrations/mysql/2025_05_22.sql
@@ -0,0 +1 @@
+ALTER TABLE alert ADD COLUMN `alert_on_every_custom_event` tinyint NOT NULL DEFAULT '0';
diff --git a/web/app/lib/models/Alerts.ts b/web/app/lib/models/Alerts.ts
index 7a81c2e5b..cc283dcd8 100644
--- a/web/app/lib/models/Alerts.ts
+++ b/web/app/lib/models/Alerts.ts
@@ -12,4 +12,5 @@ export interface Alerts {
active: boolean
lastTriggered: string | null
alertOnNewErrorsOnly?: boolean
+ alertOnEveryCustomEvent?: boolean
}
diff --git a/web/app/pages/Project/Alerts/Settings/ProjectAlertsSettings.tsx b/web/app/pages/Project/Alerts/Settings/ProjectAlertsSettings.tsx
index 3175ab76e..061f72fcb 100644
--- a/web/app/pages/Project/Alerts/Settings/ProjectAlertsSettings.tsx
+++ b/web/app/pages/Project/Alerts/Settings/ProjectAlertsSettings.tsx
@@ -49,6 +49,7 @@ const ProjectAlertsSettings = ({ isSettings }: ProjectAlertsSettingsProps) => {
active: true,
queryCustomEvent: '',
alertOnNewErrorsOnly: true,
+ alertOnEveryCustomEvent: false,
})
const [validated, setValidated] = useState(false)
const [errors, setErrors] = useState<Record<string, string>>({})
@@ -175,6 +176,17 @@ const ProjectAlertsSettings = ({ isSettings }: ProjectAlertsSettingsProps) => {
}
}, [form.queryMetric])
+ useEffect(() => {
+ if (form.queryMetric === QUERY_METRIC.CUSTOM_EVENTS && form.alertOnEveryCustomEvent) {
+ setForm((prevForm) => ({
+ ...prevForm,
+ queryCondition: undefined,
+ queryValue: undefined,
+ queryTime: undefined,
+ }))
+ }
+ }, [form.queryMetric, form.alertOnEveryCustomEvent])
+
const validate = () => {
const allErrors: Record<string, string> = {}
@@ -186,7 +198,10 @@ const ProjectAlertsSettings = ({ isSettings }: ProjectAlertsSettingsProps) => {
allErrors.queryCustomEvent = t('alert.noCustomEventError')
}
- if (form.queryMetric !== QUERY_METRIC.ERRORS) {
+ if (
+ form.queryMetric !== QUERY_METRIC.ERRORS &&
+ !(form.queryMetric === QUERY_METRIC.CUSTOM_EVENTS && form.alertOnEveryCustomEvent)
+ ) {
if (form.queryValue === undefined || Number.isNaN(_toNumber(form.queryValue))) {
allErrors.queryValue = t('alert.queryValueError')
}
@@ -207,6 +222,13 @@ const ProjectAlertsSettings = ({ isSettings }: ProjectAlertsSettingsProps) => {
queryValue: null,
queryTime: null,
}
+ } else if (data.queryMetric === QUERY_METRIC.CUSTOM_EVENTS && data.alertOnEveryCustomEvent) {
+ submissionData = {
+ ...submissionData,
+ queryCondition: null,
+ queryValue: null,
+ queryTime: null,
+ }
}
if (isSettings) {
@@ -273,6 +295,13 @@ const ProjectAlertsSettings = ({ isSettings }: ProjectAlertsSettingsProps) => {
queryValue: null,
queryTime: null,
}
+ } else if (form.queryMetric === QUERY_METRIC.CUSTOM_EVENTS && form.alertOnEveryCustomEvent) {
+ currentForm = {
+ ...currentForm,
+ queryCondition: null,
+ queryValue: null,
+ queryTime: null,
+ }
}
if (validated) {
@@ -404,6 +433,23 @@ const ProjectAlertsSettings = ({ isSettings }: ProjectAlertsSettingsProps) => {
error={beenSubmitted ? errors.queryCustomEvent : null}
/>
) : null}
+ {form.queryMetric === QUERY_METRIC.CUSTOM_EVENTS ? (
+ <Checkbox
+ checked={Boolean(form.alertOnEveryCustomEvent)}
+ onChange={(checked) =>
+ setForm((prev) => ({
+ ...prev,
+ alertOnEveryCustomEvent: checked,
+ }))
+ }
+ name='alertOnEveryCustomEvent'
+ classes={{
+ label: 'mt-4',
+ }}
+ label={t('alert.alertOnEveryCustomEvent')}
+ hint={t('alert.alertOnEveryCustomEventHint')}
+ />
+ ) : null}
{form.queryMetric === QUERY_METRIC.ERRORS ? (
<Checkbox
checked={Boolean(form.alertOnNewErrorsOnly)}
@@ -421,7 +467,8 @@ const ProjectAlertsSettings = ({ isSettings }: ProjectAlertsSettingsProps) => {
hint={t('alert.newErrorsOnlyHint')}
/>
) : null}
- {form.queryMetric !== QUERY_METRIC.ERRORS ? (
+ {form.queryMetric !== QUERY_METRIC.ERRORS &&
+ !(form.queryMetric === QUERY_METRIC.CUSTOM_EVENTS && form.alertOnEveryCustomEvent) ? (
<>
<div className='mt-4'>
<Select
diff --git a/web/public/locales/en.json b/web/public/locales/en.json
index 82a13c0e6..fbf9f8531 100644
--- a/web/public/locales/en.json
+++ b/web/public/locales/en.json
@@ -773,6 +773,8 @@
"customEvent": "Custom event name",
"newErrorsOnly": "Alert on new errors only",
"newErrorsOnlyHint": "If enabled, an alert will be triggered only for the first occurrence of a specific error.",
+ "alertOnEveryCustomEvent": "Alert on every custom event",
+ "alertOnEveryCustomEventHint": "If enabled, an alert will be triggered every time the specified custom event occurs.",
"add": "Add alert",
"delete": "Delete alert",
"noNameError": "Please enter a name for the alert.",