|
| 1 | +import { subscribe, select } from '@wordpress/data'; |
| 2 | +import { store as blockEditorStore } from '@wordpress/block-editor'; |
| 3 | +import domReady from '@wordpress/dom-ready'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Stores the current count of each watched block type. |
| 7 | + * @type {Object.<string, number>} |
| 8 | + */ |
| 9 | +let blockCounts = {}; |
| 10 | + |
| 11 | +/** |
| 12 | + * Flag to track if the initial block count has been established. |
| 13 | + * Used to skip logging on first load. |
| 14 | + * @type {boolean} |
| 15 | + */ |
| 16 | +let isInitialized = false; |
| 17 | + |
| 18 | +/** |
| 19 | + * Array of block types to monitor for changes. |
| 20 | + * @type {string[]} |
| 21 | + * @constant |
| 22 | + */ |
| 23 | +const watchedBlockTypes = [ |
| 24 | + 'feedzy-rss-feeds/loop', |
| 25 | + 'feedzy-rss-feeds/feedzy-block', |
| 26 | + 'core/rss', |
| 27 | +]; |
| 28 | + |
| 29 | +/** |
| 30 | + * Recursively flattens the block tree to get all blocks including nested ones. |
| 31 | + * |
| 32 | + * @return {Object[]} Array of all blocks in the editor (flattened) |
| 33 | + */ |
| 34 | +function getAllBlocks() { |
| 35 | + function flattenBlocks(blocks) { |
| 36 | + let allBlocks = []; |
| 37 | + blocks.forEach((block) => { |
| 38 | + allBlocks.push(block); |
| 39 | + if (block.innerBlocks?.length > 0) { |
| 40 | + allBlocks = allBlocks.concat(flattenBlocks(block.innerBlocks)); |
| 41 | + } |
| 42 | + }); |
| 43 | + return allBlocks; |
| 44 | + } |
| 45 | + |
| 46 | + return flattenBlocks(select(blockEditorStore).getBlocks()); |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Updates the block counts and tracks changes for analytics. |
| 51 | + * Skips logging during initial initialization to avoid false positives. |
| 52 | + * |
| 53 | + * @fires window.tiTrk.with - Sends analytics data when block counts change if telemetry is enabled. |
| 54 | + */ |
| 55 | +function updateBlockCounts() { |
| 56 | + const allBlocks = getAllBlocks(); |
| 57 | + |
| 58 | + // Reset counts. |
| 59 | + const newCounts = {}; |
| 60 | + watchedBlockTypes.forEach((type) => { |
| 61 | + newCounts[type] = 0; |
| 62 | + }); |
| 63 | + |
| 64 | + // Count blocks. |
| 65 | + allBlocks.forEach((block) => { |
| 66 | + if (watchedBlockTypes.includes(block.name)) { |
| 67 | + newCounts[block.name]++; |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + if (isInitialized) { |
| 72 | + // Check for changes. |
| 73 | + watchedBlockTypes.forEach((blockType) => { |
| 74 | + const oldCount = blockCounts[blockType] || 0; |
| 75 | + const newCount = newCounts[blockType] || 0; |
| 76 | + |
| 77 | + if (oldCount === newCount) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + const change = newCount - oldCount; |
| 82 | + window?.tiTrk?.with('feedzy')?.set(`${blockType}:${Date()}`, { |
| 83 | + feature: 'block-usage', |
| 84 | + featureComponent: blockType, |
| 85 | + featureValue: change, |
| 86 | + }); |
| 87 | + }); |
| 88 | + } else { |
| 89 | + isInitialized = true; |
| 90 | + } |
| 91 | + |
| 92 | + blockCounts = newCounts; |
| 93 | +} |
| 94 | + |
| 95 | +domReady(() => { |
| 96 | + if (window.tiTrk) { |
| 97 | + window.tiTrk?.start(); |
| 98 | + window.tiTrk.eventsLimit = 60 * 1000; // Check for events every minute. |
| 99 | + } |
| 100 | + |
| 101 | + // Note: Add a delay for a better initialization for existing blocks. |
| 102 | + setTimeout(() => { |
| 103 | + const unsubscribe = subscribe(updateBlockCounts, blockEditorStore); |
| 104 | + }, 1000); |
| 105 | +}); |
0 commit comments