-
Notifications
You must be signed in to change notification settings - Fork 57
Setup Redux middleware for analytics tracking #4066
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
7db1329
Setup Redux middleware for analytics tracking
jhnstn bbf573d
Simplify analytics index file
jhnstn 27b41ba
Drop unnecessary no-op function
mchowning b96e5f0
Remove inner_blocks prop
mchowning 4530603
Update Gutenberg ref
mchowning 65f5fa7
Allow snake_case identifiers for analytics tracking
mchowning 615f296
Reify the index.js file
jhnstn e2129b2
Restructure to use a domain based structure
jhnstn 062774e
Remove old tracker file
jhnstn 284cb18
Merge branch 'develop' into add/analytics-tracking
jhnstn 872e7f9
Update gutenberg
jhnstn a01973e
lint JSDocs
jhnstn 83b9bca
Ingore console.error linting
jhnstn ad605b1
Update Gutenberg ref
dcalhoun 92444ab
Merge branch 'develop' of github.com:wordpress-mobile/gutenberg-mobil…
dcalhoun aecd01f
Fix JSDoc lint warning
dcalhoun eab8560
Rename event from editor_blocks_inserted to editor_block_inserted
dcalhoun 3266ae9
Merge branch 'develop' of github.com:wordpress-mobile/gutenberg-mobil…
dcalhoun 1e2a4ff
Generate bundle
dcalhoun f59ff1d
Merge branch 'develop' of github.com:wordpress-mobile/gutenberg-mobil…
dcalhoun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Submodule gutenberg
updated
14 files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { initialize as initializeRedux } from './redux'; | ||
|
|
||
| export default () => { | ||
| initializeRedux(); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { use } from '@wordpress/data'; | ||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { trackedEvents } from './tracked_events'; | ||
|
|
||
| /** | ||
| * Initialize the Redux middleware | ||
| */ | ||
| export function initialize() { | ||
| use( ( registry ) => ( { | ||
| dispatch: ( namespace ) => { | ||
| const namespaceName = | ||
| typeof namespace === 'object' ? namespace.name : namespace; | ||
| const actions = { ...registry.dispatch( namespaceName ) }; | ||
| const trackers = trackedEvents[ namespaceName ]; | ||
|
|
||
| if ( trackers ) { | ||
| Object.keys( trackers ).forEach( ( actionName ) => { | ||
| const originalAction = actions[ actionName ]; | ||
| const tracker = trackers[ actionName ]; | ||
| actions[ actionName ] = ( ...args ) => { | ||
| try { | ||
| tracker( ...args ); | ||
| } catch ( err ) { | ||
| // eslint-disable-next-line no-console | ||
| console.error( err ); | ||
| } | ||
| return originalAction( ...args ); | ||
| }; | ||
| } ); | ||
| } | ||
|
|
||
| return actions; | ||
| }, | ||
| } ) ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* eslint-disable camelcase */ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { select } from '@wordpress/data'; | ||
| import { sendEventToHost } from '@wordpress/react-native-bridge'; | ||
|
|
||
| /** | ||
| * Retrieves a block object. If the block is not an object, | ||
| * it tries to retrieve the block from the store. | ||
| * | ||
| * @param {string|Object} block Block objectg or string identifier. | ||
| * @return {Object} block object or an empty object if not found. | ||
| */ | ||
| function getBlockObject( block ) { | ||
| if ( typeof block === 'object' ) { | ||
| return block; | ||
| } | ||
| return select( 'core/block-editor' ).getBlock( block ) || {}; | ||
| } | ||
|
|
||
| /** | ||
| * Helper function to recursively track block events. | ||
| * Each inner block will be tracked as a separate event if block contains inner blocks. | ||
| * | ||
| * @param {Array|Object} blocks A single or collection of block objects or block identifiers. | ||
| * @param {string} eventName Event name used to track. | ||
| * @param {Function} propertiesHandler Callback to transform properties. | ||
| * @param {Object} parentBlock Parent block. optional | ||
| * @return {void} | ||
| */ | ||
| function trackBlocksHandler( | ||
| blocks, | ||
| eventName, | ||
| propertiesHandler = () => {}, | ||
| parentBlock | ||
| ) { | ||
| const blockArray = [ blocks ].flat(); | ||
| if ( ! blocks || ! blockArray.length ) { | ||
| return; | ||
| } | ||
|
|
||
| blockArray.forEach( ( block ) => { | ||
| const blockObject = getBlockObject( block ); | ||
| const eventProperties = { | ||
| ...propertiesHandler( blockObject ), | ||
| inner_block: !! parentBlock, | ||
| }; | ||
|
|
||
| if ( parentBlock ) { | ||
| eventProperties.parent_block_name = parentBlock.name; | ||
| } | ||
|
|
||
| sendEventToHost( eventName, eventProperties ); | ||
|
|
||
| if ( blockObject.innerBlocks ) { | ||
| trackBlocksHandler( | ||
| blockObject.innerBlocks, | ||
| eventName, | ||
| propertiesHandler, | ||
| blockObject | ||
| ); | ||
| } | ||
| } ); | ||
| } | ||
|
|
||
| export const trackedEvents = { | ||
| 'core/block-editor': { | ||
| insertBlock( blocks ) { | ||
| trackBlocksHandler( | ||
| blocks, | ||
| 'editor_block_inserted', | ||
| ( { name } ) => ( { block_name: name } ) | ||
| ); | ||
| }, | ||
| insertBlocks( blocks ) { | ||
| trackBlocksHandler( | ||
| blocks, | ||
| 'editor_block_inserted', | ||
| ( { name } ) => ( { block_name: name } ) | ||
| ); | ||
| }, | ||
| }, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can easily trigger
insertBlock, but I'm not sure how to triggerinsertBlocksfrom mobile. Any suggestions?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From researching the usage of the
insertBlocksaction creator, it would appear splitting at the end of some rich text blocks causes ainsertBlocksRedux action to trigger. E.g. placing a the cursor in a PullQuote block's citation input and pressing Return will trigger the action.That said, neither WPAndroid or WPiOS implementations currently have sibling events defined for
insertBlocks, so the resulting analytics event will not be sent. I plan to add the required sibling events.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following up to share that my plan is to rename the
editor_blocks_insertedevent toeditor_block_insertedrather than add sibling events to WPAndroid and WPiOS. This approach mimics what Calypso does, as bothinsertBlockandinsertBlocksleverage the same event name.To me, this approach makes more sense, as we our goal is tracking the insertion of a block. Whether the
insertBlockorinsertBlocksaction creator is leveraged is implementation details. In fact, theinsertBlockaction callsinsertBlocksitself.