| @@ -39,9 +39,107 @@ | ||
| 39 | 39 | $( '#control_wrapper_canvas' ).addClass( 'no-filter' ).html(''); |
| 40 | 40 | } |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | - $('#settings-button').click(function() { | |
| 43 | + /** | |
| 44 | + * Capture the relevant form data. | |
| 45 | + * | |
| 46 | + * @returns {Object} The captured form data. | |
| 47 | + */ | |
| 48 | + function captureFormData() { | |
| 49 | + const formData = new FormData(document.querySelector('#settings-form')); | |
| 50 | + const featureData = {}; | |
| 51 | + | |
| 52 | + formData.forEach((value, optionKey) => { | |
| 53 | + if ( | |
| 54 | + ! value || | |
| 55 | + ( ! ['permissions', 'controls', 'manual', 'lazy'].some( key => optionKey.startsWith(key) ) ) | |
| 56 | + ) { | |
| 57 | + return; | |
| 58 | + } | |
| 59 | + | |
| 60 | + if ( optionKey.endsWith('[]') ) { | |
| 61 | + if( !featureData[optionKey] ) { | |
| 62 | + featureData[optionKey] = []; | |
| 63 | + } | |
| 64 | + featureData[optionKey].push(value); | |
| 65 | + } else { | |
| 66 | + featureData[optionKey] = value; | |
| 67 | + } | |
| 68 | + }); | |
| 69 | + | |
| 70 | + for (const key in featureData) { | |
| 71 | + if (Array.isArray(featureData[key])) { | |
| 72 | + featureData[key] = featureData[key].join(','); // We can not send arrays in the tracking data. | |
| 73 | + } | |
| 74 | + } | |
| 75 | + | |
| 76 | + return featureData; | |
| 77 | + } | |
| 78 | + | |
| 79 | + const initialCaptureData = captureFormData(); | |
| 80 | + | |
| 81 | + /** | |
| 82 | + * Capture feature usage. | |
| 83 | + */ | |
| 84 | + async function trackSavedData() { | |
| 85 | + const savedData = captureFormData(); | |
| 86 | + | |
| 87 | + // Remove default values. | |
| 88 | + for (const key in savedData) { | |
| 89 | + | |
| 90 | + if ( key === 'lazy_load_chart' ) { | |
| 91 | + continue; | |
| 92 | + } | |
| 93 | + | |
| 94 | + if ( savedData[key] === initialCaptureData[key] ) { | |
| 95 | + delete savedData[key]; | |
| 96 | + } | |
| 97 | + } | |
| 98 | + | |
| 99 | + const hasPermission = Object.keys(savedData).some(key => key.startsWith('permissions')).length > 0; | |
| 100 | + const hasControls = Object.keys(savedData).some(key => key.startsWith('controls')).length > 0; | |
| 101 | + | |
| 102 | + const featureData = { | |
| 103 | + lazy: savedData.lazy_load_chart, | |
| 104 | + permissions: hasPermission ? 'used' : 'ignored', | |
| 105 | + controls: hasControls ? 'used' : 'ignored', | |
| 106 | + }; | |
| 107 | + | |
| 108 | + if (savedData.manual) { | |
| 109 | + featureData['manualConfig'] = savedData.manual; | |
| 110 | + } | |
| 111 | + | |
| 112 | + const urlParams = new URLSearchParams(window.location.search); | |
| 113 | + | |
| 114 | + window?.tiTrk?.with('visualizer')?.add({ | |
| 115 | + feature: 'chart-edit', | |
| 116 | + featureComponent: 'saved-data', | |
| 117 | + featureData, | |
| 118 | + groupId: urlParams.get('chart') ?? '' | |
| 119 | + }); | |
| 120 | + | |
| 121 | + // Do not make the user to wait too long for the event to be uploaded. | |
| 122 | + const timer = new Promise((resolve) => setTimeout(resolve, 500)); | |
| 123 | + await Promise.race([timer, window?.tiTrk?.uploadEvents()]); | |
| 124 | + } | |
| 125 | + | |
| 126 | + document.querySelector('#settings-button')?.addEventListener('click', async function() { | |
| 127 | + if( typeof window.tiTrk !== 'undefined' ) { | |
| 128 | + try { | |
| 129 | + await trackSavedData(); | |
| 130 | + } catch (e) { | |
| 131 | + console.warn(e); | |
| 132 | + } | |
| 133 | + } | |
| 134 | + | |
| 135 | + // Get and send the chart ID (used by Visualizer Block). | |
| 136 | + var urlParams = new URLSearchParams(window.location.search); | |
| 137 | + var chartID = urlParams.get('chart'); | |
| 138 | + if ( chartID ) { | |
| 139 | + window.parent.postMessage({ chartID: chartID}, '*'); | |
| 140 | + } | |
| 141 | + | |
| 44 | 142 | $('#settings-form').submit(); |
| 45 | 143 | }); |
| 46 | 144 | |
| 47 | 145 | // this portion captures if the settings have changed so that tabs can handle that information. |