π 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!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
Analytics DTO and SQL validation fixes
Blue Mouseeacc799a8a56
diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.tsindex 8824deb1e..afcd32fe9 100644--- a/backend/apps/cloud/src/analytics/analytics.service.ts+++ b/backend/apps/cloud/src/analytics/analytics.service.ts@@ -4007,14 +4007,7 @@ export class AnalyticsService {)const query = `- WITH profile_alias_map AS (- SELECT- anonProfileId,- argMin(userProfileId, created) AS userProfileId- FROM profile_aliases- WHERE pid = {pid:FixedString(12)}- GROUP BY anonProfileId- ),+ WITH ${this.buildProfileAliasMapCTE()},funnel_qualified AS (SELECT psidFROM (@@ -4176,14 +4169,7 @@ export class AnalyticsService {: ''const query = `- WITH profile_alias_map AS (- SELECT- anonProfileId,- argMin(userProfileId, created) AS userProfileId- FROM profile_aliases- WHERE pid = {pid:FixedString(12)}- GROUP BY anonProfileId- ),+ WITH ${this.buildProfileAliasMapCTE()},journey_qualified AS (SELECT psidFROM (@@ -8073,14 +8059,7 @@ export class AnalyticsService {)const query = `- WITH profile_alias_map AS (- SELECT- anonProfileId,- argMin(userProfileId, created) AS userProfileId- FROM profile_aliases- WHERE pid = {pid:FixedString(12)}- GROUP BY anonProfileId- ),+ WITH ${this.buildProfileAliasMapCTE()},distinct_sessions_filtered AS (SELECTpsidCasted,@@ -8329,14 +8308,7 @@ export class AnalyticsService {)`const query = `- WITH profile_alias_map AS (- SELECT- anonProfileId,- argMin(userProfileId, created) AS userProfileId- FROM profile_aliases- WHERE pid = {pid:FixedString(12)}- GROUP BY anonProfileId- ),+ WITH ${this.buildProfileAliasMapCTE()},${filteredSessionsCTE},replay_summary AS (SELECTdiff --git a/backend/apps/cloud/src/analytics/dto/identify.dto.ts b/backend/apps/cloud/src/analytics/dto/identify.dto.tsindex b37614069..9d90d1744 100644--- a/backend/apps/cloud/src/analytics/dto/identify.dto.ts+++ b/backend/apps/cloud/src/analytics/dto/identify.dto.ts@@ -2,7 +2,7 @@ import _keys from 'lodash/keys'import _some from 'lodash/some'import _values from 'lodash/values'import { ApiProperty } from '@nestjs/swagger'-import { Transform } from 'class-transformer'+import { Transform, Type } from 'class-transformer'import {IsNotEmpty,IsObject,@@ -33,8 +33,9 @@ class TraitsKeysQuantity implements ValidatorConstraintInterface {}}-// Control / format characters never appear in a genuine trait name, but would-// corrupt the dashboard rendering them.+// Control / format characters never appear in a genuine trait name or value,+// but would corrupt the dashboard rendering them (e.g. via a right-to-left+// override).const UNPRINTABLE_REGEX = /[\p{Cc}\p{Cf}]/u@ValidatorConstraint()@@ -53,7 +54,10 @@ class TraitsKeyFormat implements ValidatorConstraintInterface {@ValidatorConstraint()class TraitsValueType implements ValidatorConstraintInterface {validate(traits: Record<string, string>) {- return !_some(_values(traits), (value) => typeof value !== 'string')+ return !_some(+ _values(traits),+ (value) => typeof value !== 'string' || UNPRINTABLE_REGEX.test(value),+ )}}@@ -137,6 +141,11 @@ export class IdentifyDto {})@IsOptional()@IsObject()+ // Pins the target type of the nested object. Without it class-transformer+ // guesses one from `value.constructor`, which a `{"constructor": "..."}`+ // trait turns into a string and blows up with a TypeError (a 500 on this+ // public endpoint) before any validation below runs.+ @Type(() => Object)@Transform(({ value }) => transformTraits(value))@Validate(TraitsKeysQuantity, {message: `Traits object can't have more than ${MAX_TRAITS_KEYS} keys`,@@ -145,7 +154,8 @@ export class IdentifyDto {message: `Traits keys must be non-empty and no longer than ${MAX_TRAIT_KEY_LENGTH} characters`,})@Validate(TraitsValueType, {- message: 'All of traits object values must be primitive JSON values',+ message:+ 'All of traits object values must be primitive JSON values without control characters',})@Validate(TraitsSizeLimit, {message: `Traits object can't have keys and values with total length more than ${MAX_TRAITS_TOTAL_LENGTH} characters`,diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.tsindex cf1250ddb..40fb133d1 100644--- a/backend/apps/community/src/analytics/analytics.service.ts+++ b/backend/apps/community/src/analytics/analytics.service.ts@@ -2832,14 +2832,7 @@ export class AnalyticsService {)const query = `- WITH profile_alias_map AS (- SELECT- anonProfileId,- argMin(userProfileId, created) AS userProfileId- FROM profile_aliases- WHERE pid = {pid:FixedString(12)}- GROUP BY anonProfileId- ),+ WITH ${this.buildProfileAliasMapCTE()},funnel_qualified AS (SELECT psidFROM (@@ -3001,14 +2994,7 @@ export class AnalyticsService {: ''const query = `- WITH profile_alias_map AS (- SELECT- anonProfileId,- argMin(userProfileId, created) AS userProfileId- FROM profile_aliases- WHERE pid = {pid:FixedString(12)}- GROUP BY anonProfileId- ),+ WITH ${this.buildProfileAliasMapCTE()},journey_qualified AS (SELECT psidFROM (@@ -6516,14 +6502,7 @@ export class AnalyticsService {)const query = `- WITH profile_alias_map AS (- SELECT- anonProfileId,- argMin(userProfileId, created) AS userProfileId- FROM profile_aliases- WHERE pid = {pid:FixedString(12)}- GROUP BY anonProfileId- ),+ WITH ${this.buildProfileAliasMapCTE()},distinct_sessions_filtered AS (SELECTpsidCasted,diff --git a/backend/apps/community/src/analytics/dto/identify.dto.ts b/backend/apps/community/src/analytics/dto/identify.dto.tsindex b37614069..9d90d1744 100644--- a/backend/apps/community/src/analytics/dto/identify.dto.ts+++ b/backend/apps/community/src/analytics/dto/identify.dto.ts@@ -2,7 +2,7 @@ import _keys from 'lodash/keys'import _some from 'lodash/some'import _values from 'lodash/values'import { ApiProperty } from '@nestjs/swagger'-import { Transform } from 'class-transformer'+import { Transform, Type } from 'class-transformer'import {IsNotEmpty,IsObject,@@ -33,8 +33,9 @@ class TraitsKeysQuantity implements ValidatorConstraintInterface {}}-// Control / format characters never appear in a genuine trait name, but would-// corrupt the dashboard rendering them.+// Control / format characters never appear in a genuine trait name or value,+// but would corrupt the dashboard rendering them (e.g. via a right-to-left+// override).const UNPRINTABLE_REGEX = /[\p{Cc}\p{Cf}]/u@ValidatorConstraint()@@ -53,7 +54,10 @@ class TraitsKeyFormat implements ValidatorConstraintInterface {@ValidatorConstraint()class TraitsValueType implements ValidatorConstraintInterface {validate(traits: Record<string, string>) {- return !_some(_values(traits), (value) => typeof value !== 'string')+ return !_some(+ _values(traits),+ (value) => typeof value !== 'string' || UNPRINTABLE_REGEX.test(value),+ )}}@@ -137,6 +141,11 @@ export class IdentifyDto {})@IsOptional()@IsObject()+ // Pins the target type of the nested object. Without it class-transformer+ // guesses one from `value.constructor`, which a `{"constructor": "..."}`+ // trait turns into a string and blows up with a TypeError (a 500 on this+ // public endpoint) before any validation below runs.+ @Type(() => Object)@Transform(({ value }) => transformTraits(value))@Validate(TraitsKeysQuantity, {message: `Traits object can't have more than ${MAX_TRAITS_KEYS} keys`,@@ -145,7 +154,8 @@ export class IdentifyDto {message: `Traits keys must be non-empty and no longer than ${MAX_TRAIT_KEY_LENGTH} characters`,})@Validate(TraitsValueType, {- message: 'All of traits object values must be primitive JSON values',+ message:+ 'All of traits object values must be primitive JSON values without control characters',})@Validate(TraitsSizeLimit, {message: `Traits object can't have keys and values with total length more than ${MAX_TRAITS_TOTAL_LENGTH} characters`,diff --git a/docs/content/docs/script-reference.mdx b/docs/content/docs/script-reference.mdxindex 5d09bd03d..07f71f43f 100644--- a/docs/content/docs/script-reference.mdx+++ b/docs/content/docs/script-reference.mdx@@ -196,9 +196,9 @@ Traits are merged per key, so a later call only overwrites the keys it carries a<Callout type="warn">Use a unique, **stable** identifier β your application's internal user ID is the best choice.Don't use values like `null`, `undefined` or `"guest"`: the API rejects those, as they would fuse- unrelated visitors into a single profile. The ID you pass is stored as provided (behind a `usr_`- prefix) and shown in your dashboard, so if you want a user's email on their profile, send it as a- trait rather than using it as the ID.+ unrelated visitors into a single profile. Surrounding whitespace is trimmed; otherwise the ID you+ pass is stored as provided (behind a `usr_` prefix) and shown in your dashboard, so if you want a+ user's email on their profile, send it as a trait rather than using it as the ID.</Callout>A few things to know about how linking works:diff --git a/docs/content/docs/visitor-identification.mdx b/docs/content/docs/visitor-identification.mdxindex 0d20eaaae..3eb974afc 100644--- a/docs/content/docs/visitor-identification.mdx+++ b/docs/content/docs/visitor-identification.mdx@@ -143,10 +143,11 @@ swetrix.identify("user-12345");swetrix.reset();```-Your user ID is stored exactly as you send it, behind a `usr_` prefix β-so the profile that shows up in your dashboard is `usr_user-12345`, and-you can look a user up by the same ID you use in your own database.-Only send identifiers you're comfortable seeing in the dashboard.+Your user ID is stored as you send it β bar surrounding whitespace,+which is trimmed β behind a `usr_` prefix, so the profile that shows up+in your dashboard is `usr_user-12345`, and you can look a user up by the+same ID you use in your own database. Only send identifiers you're+comfortable seeing in the dashboard.Alternatively, you can pass `profileId` to individual tracking methods βit's supported on `init()`, `track()`, `pageview()`,@@ -210,10 +211,11 @@ choices:a webview.<Callout type="warn">- The `profileId` is stored as provided β Swetrix doesn't hash or otherwise transform it. Prefer an- opaque internal ID over email addresses, full names or phone numbers, and pseudonymise on your- side if your privacy policy requires it. If you do want a user's email on their profile, attach it- as a [trait](#user-traits) instead of using it as the ID.+ The `profileId` is stored as provided, apart from surrounding whitespace, which is trimmed β+ Swetrix doesn't hash or otherwise transform it. Prefer an opaque internal ID over email addresses,+ full names or phone numbers, and pseudonymise on your side if your privacy policy requires it. If+ you do want a user's email on their profile, attach it as a [trait](#user-traits) instead of using+ it as the ID.</Callout>### User traitsdiff --git a/packages/tracker-node/README.md b/packages/tracker-node/README.mdindex e83de2780..4fb6d6642 100644--- a/packages/tracker-node/README.md+++ b/packages/tracker-node/README.md@@ -441,7 +441,7 @@ Link a visitor's anonymous profile to your own user ID (e.g. after they log in),const identifiedProfileId = await swetrix.identify('192.155.52.12', 'Mozilla/5.0...', 'user-12345')```-Use a unique, stable identifier (e.g. an internal user ID). The ID is stored as provided and shown in your dashboard, so don't pass values you wouldn't want to see there.+Use a unique, stable identifier (e.g. an internal user ID). Surrounding whitespace is trimmed; otherwise the ID is stored as provided and shown in your dashboard, so don't pass values you wouldn't want to see there.You can also pass **traits** β key / value metadata displayed on the user's profile. Traits are merged per key, and `null` removes one: