Skip to content

Commit

Permalink
Tracks: Add Remote Data Blocks in the list of allowed sources (#96007)
Browse files Browse the repository at this point in the history
Update `@automattic/calypso-analytics` package to add "Remote Data Blocks" in the list of allowed sources.
  • Loading branch information
mehmoodak authored Nov 15, 2024
1 parent 980a39e commit cf4359b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
4 changes: 4 additions & 0 deletions packages/calypso-analytics/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
2 changes: 1 addition & 1 deletion packages/calypso-analytics/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
40 changes: 23 additions & 17 deletions packages/calypso-analytics/src/tracks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
);
Expand Down Expand Up @@ -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;
}

Expand All @@ -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 );

Expand Down

0 comments on commit cf4359b

Please sign in to comment.