diff --git a/packages/calypso-analytics/CHANGELOG.md b/packages/calypso-analytics/CHANGELOG.md index 2bac31899276b..a5194be5ebbc6 100644 --- a/packages/calypso-analytics/CHANGELOG.md +++ b/packages/calypso-analytics/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.1.3 + +- Add "Remote Data Blocks" in the list of allowed sources. + ## 1.1.2 - Add additional 8 US states to the list of CCPA regions (DE, IN, IA, MT, NJ, OR, TN, TX). diff --git a/packages/calypso-analytics/package.json b/packages/calypso-analytics/package.json index a7f10ae14a251..a59209f796d25 100644 --- a/packages/calypso-analytics/package.json +++ b/packages/calypso-analytics/package.json @@ -1,6 +1,6 @@ { "name": "@automattic/calypso-analytics", - "version": "1.1.2", + "version": "1.1.3", "description": "Automattic Analytics.", "homepage": "https://github.com/Automattic/wp-calypso", "license": "GPL-2.0-or-later", diff --git a/packages/calypso-analytics/src/tracks.ts b/packages/calypso-analytics/src/tracks.ts index c7c85d899ad83..a7d6f62199f51 100644 --- a/packages/calypso-analytics/src/tracks.ts +++ b/packages/calypso-analytics/src/tracks.ts @@ -21,6 +21,7 @@ declare const window: undefined | ( Window & { BUILD_TIMESTAMP?: number } ); * See internal Nosara repo? */ const TRACKS_SPECIAL_PROPS_NAMES = [ 'geo', 'message', 'request', 'geocity', 'ip' ]; +const ALLOWED_EVENT_SOURCES = [ 'calypso', 'jetpack', 'remotedatablocks', 'wpcom_dsp_widget' ]; const EVENT_NAME_EXCEPTIONS = [ 'a8c_cookie_banner_ok', 'a8c_cookie_banner_view', @@ -212,16 +213,10 @@ export function recordTracksEvent( eventName: string, eventProperties?: any ) { } if ( process.env.NODE_ENV !== 'production' && typeof console !== 'undefined' ) { - if ( - ! /^calypso(?:_[a-z0-9]+){2,}$/.test( eventName ) && - ! /^jetpack(?:_[a-z0-9]+){2,}$/.test( eventName ) && - ! /^wpcom_dsp_widget(?:_[a-z0-9]+){2,}$/.test( eventName ) && - ! EVENT_NAME_EXCEPTIONS.includes( eventName ) - ) { + if ( ! isValidEventName( eventName ) && ! EVENT_NAME_EXCEPTIONS.includes( eventName ) ) { // eslint-disable-next-line no-console console.error( - 'Tracks: Event `%s` will be ignored because it does not match ' + - '/^calypso(?:_[a-z0-9]+){2,}$/ nor /^jetpack(?:_[a-z0-9]+){2,}$/ and is ' + + 'Tracks: Event `%s` will be ignored because it does not match with the naming convention and is ' + 'not a listed exception. Please use a compliant event name.', eventName ); @@ -261,15 +256,8 @@ export function recordTracksEvent( eventName: string, eventProperties?: any ) { debug( 'Record event "%s" called with props %o', eventName, eventProperties ); - if ( - ! eventName.startsWith( 'calypso_' ) && - ! eventName.startsWith( 'jetpack_' ) && - ! eventName.startsWith( 'wpcom_dsp_widget_' ) && - ! EVENT_NAME_EXCEPTIONS.includes( eventName ) - ) { - debug( - '- Event name must be prefixed by "calypso_", "jetpack_", or added to `EVENT_NAME_EXCEPTIONS`' - ); + if ( ! isValidEventSource( eventName ) && ! EVENT_NAME_EXCEPTIONS.includes( eventName ) ) { + debug( '- Event name must be prefixed by a known source or added to `EVENT_NAME_EXCEPTIONS`' ); return; } @@ -290,6 +278,24 @@ export function recordTracksEvent( eventName: string, eventProperties?: any ) { analyticsEvents.emit( 'record-event', eventName, eventProperties ); } +/** + * Checks if the event name follows the Tracks naming convention. + */ +function isValidEventName( eventName: string ): boolean { + return ALLOWED_EVENT_SOURCES.some( ( eventSource: string ): boolean => + new RegExp( `^${ eventSource }(?:_[a-z0-9]+){2,}$` ).test( eventName ) + ); +} + +/** + * Checks if the event name has a valid source prefix. + */ +function isValidEventSource( eventName: string ): boolean { + return ALLOWED_EVENT_SOURCES.some( ( eventSource: string ): boolean => + eventName.startsWith( `${ eventSource }_` ) + ); +} + export function recordTracksPageView( urlPath: string, params: any ) { debug( 'Recording pageview in tracks.', urlPath, params );