Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup Redux middleware for analytics tracking #4066

Merged
merged 20 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/analytics/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const INSERT_BLOCK = 'insertBlock';
dcalhoun marked this conversation as resolved.
Show resolved Hide resolved

export const REDUX_TRACKING = {
'core/block-editor': [ INSERT_BLOCK ],
};
41 changes: 41 additions & 0 deletions src/analytics/from-redux.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* WordPress dependencies
*/
import { use } from '@wordpress/data';

/**
* Internal dependencies
*/
import eventTrackers from './trackers';
import { REDUX_TRACKING } from './events';

/**
* Initialize the Redux middleware
*/
export default function() {
use( ( registry ) => ( {
dispatch: ( namespace ) => {
const namespaceName =
typeof namespace === 'object' ? namespace.name : namespace;
const actions = { ...registry.dispatch( namespaceName ) };
const trackedEvents = REDUX_TRACKING[ namespaceName ];

if ( trackedEvents ) {
trackedEvents.forEach( ( actionName ) => {
const originalAction = actions[ actionName ];
const tracker = eventTrackers[ actionName ] || ( () => {} );
mchowning marked this conversation as resolved.
Show resolved Hide resolved
actions[ actionName ] = ( ...args ) => {
try {
tracker( ...args );
} catch ( err ) {
// eslint-disable-next-line no-console
console.error( err );
}
return originalAction( ...args );
};
} );
}
return actions;
},
} ) );
}
8 changes: 8 additions & 0 deletions src/analytics/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Internal dependencies
*/
import initFromRedux from './from-redux';

export default function() {
initFromRedux();
}
17 changes: 17 additions & 0 deletions src/analytics/trackers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* WordPress dependencies
*/
import { sendEventToHost } from '@wordpress/react-native-bridge';

/**
* Internal dependencies
*/
import * as EVENTS from './events';
dcalhoun marked this conversation as resolved.
Show resolved Hide resolved

export default {
// Track block insertion
[ EVENTS.INSERT_BLOCK ]( eventData ) {
const { innerBlocks: inner_blocks, name: block_name } = eventData;
dcalhoun marked this conversation as resolved.
Show resolved Hide resolved
sendEventToHost( EVENTS.INSERT_BLOCK, { inner_blocks, block_name } );
},
dcalhoun marked this conversation as resolved.
Show resolved Hide resolved
dcalhoun marked this conversation as resolved.
Show resolved Hide resolved
};
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import correctTextFontWeight from './text-font-weight-correct';
import setupJetpackEditor from './jetpack-editor-setup';
import setupBlockExperiments from './block-experiments-setup';
import initialHtml from './initial-html';
import initAnalytics from './analytics';

addAction( 'native.pre-render', 'gutenberg-mobile', () => {
require( './strings-overrides' );
Expand Down Expand Up @@ -49,4 +50,5 @@ addFilter( 'native.block_editor_props', 'gutenberg-mobile', ( editorProps ) => {
return editorProps;
} );

initAnalytics();
doGutenbergNativeSetup();