googleanalytics.js
4 years ago
googleanalytics_createprop.js
3 years ago
googleanalytics_dashboard.js
3 years ago
googleanalytics_page.js
2 months ago
googleanalytics_dashboard.js
267 lines
| 1 | /** |
| 2 | * GoogleAnalytics Dashboard JS. |
| 3 | * |
| 4 | * @package GoogleAnalytics |
| 5 | */ |
| 6 | |
| 7 | (function ($) { |
| 8 | const wrapperSelector = '#ga-dashboard-widget'; |
| 9 | const minWidth = 350; |
| 10 | const offset = 10; |
| 11 | |
| 12 | ga_dashboard = { |
| 13 | chartData: [], |
| 14 | init: function (dataArr, showLoader) { |
| 15 | if (showLoader) { |
| 16 | ga_loader.show(); |
| 17 | } |
| 18 | |
| 19 | if ( google ) { |
| 20 | google.charts.load( 'current', { 'packages': [ 'corechart' ] } ); |
| 21 | google.charts.setOnLoadCallback( |
| 22 | function () { |
| 23 | if ( dataArr ) { |
| 24 | ga_dashboard.drawChart( dataArr ); |
| 25 | ga_dashboard.setChartData( dataArr ); |
| 26 | } |
| 27 | } |
| 28 | ); |
| 29 | } |
| 30 | }, |
| 31 | events: function (data) { |
| 32 | $( document ).ready( |
| 33 | function () { |
| 34 | $( '#range-selector' ).on( |
| 35 | 'change', |
| 36 | function () { |
| 37 | const selected = $( this ).val(); |
| 38 | const selected_name = $( '#metrics-selector option:selected' ).html(); |
| 39 | const selected_metric = $( '#metrics-selector option:selected' ).val() || null; |
| 40 | |
| 41 | ga_loader.show(); |
| 42 | |
| 43 | var dataObj = {}; |
| 44 | dataObj['action'] = "ga_ajax_data_change"; |
| 45 | dataObj['date_range'] = selected; |
| 46 | dataObj['metric'] = selected_metric; |
| 47 | dataObj[GA_NONCE_FIELD] = GA_NONCE; |
| 48 | |
| 49 | $.ajax( |
| 50 | { |
| 51 | type: "post", |
| 52 | dataType: "json", |
| 53 | url: ajaxurl, |
| 54 | data: dataObj, |
| 55 | success: function (response) { |
| 56 | |
| 57 | ga_loader.hide(); |
| 58 | |
| 59 | if (typeof response.error !== "undefined") { |
| 60 | $( '#ga_widget_error' ) |
| 61 | .removeClass( 'hidden' ) |
| 62 | .html( `GA Dashboard: ${response.error}` ); |
| 63 | } else { |
| 64 | var dataT = [['Day', selected_name]]; |
| 65 | $.each( |
| 66 | response.chart, |
| 67 | function (k, v) { |
| 68 | dataT.push( [v.day, parseInt( v.current )] ); |
| 69 | } |
| 70 | ); |
| 71 | |
| 72 | $.each( |
| 73 | response.boxes, |
| 74 | function (k, v) { |
| 75 | $( '#ga_box_dashboard_label_' + k ).html( v.label ) |
| 76 | $( '#ga_box_dashboard_value_' + k ).html( v.value ); |
| 77 | } |
| 78 | ); |
| 79 | |
| 80 | ga_dashboard.drawChart( dataT, selected_name ); |
| 81 | |
| 82 | // Set new data. |
| 83 | ga_dashboard.setChartData( dataT ); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | ); |
| 88 | } |
| 89 | ); |
| 90 | |
| 91 | $( '#metrics-selector' ).on( |
| 92 | 'change', |
| 93 | function () { |
| 94 | const selected = $( this ).val(); |
| 95 | const selected_name = $( '#metrics-selector option:selected' ).html(); |
| 96 | const selected_range = $( '#range-selector option:selected' ).val() || null; |
| 97 | |
| 98 | ga_loader.show(); |
| 99 | |
| 100 | var dataObj = {}; |
| 101 | dataObj['action'] = "ga_ajax_data_change"; |
| 102 | dataObj['metric'] = selected; |
| 103 | dataObj['date_range'] = selected_range; |
| 104 | dataObj[GA_NONCE_FIELD] = GA_NONCE; |
| 105 | |
| 106 | $.ajax( |
| 107 | { |
| 108 | type: "post", |
| 109 | dataType: "json", |
| 110 | url: ajaxurl, |
| 111 | data: dataObj, |
| 112 | success: function (response) { |
| 113 | ga_loader.hide(); |
| 114 | |
| 115 | if (typeof response.error !== "undefined") { |
| 116 | $( '#ga_widget_error' ) |
| 117 | .removeClass( 'hidden' ) |
| 118 | .html( `GA Dashboard: ${response.error}` ); |
| 119 | } else { |
| 120 | var dataT = [['Day', selected_name]]; |
| 121 | $.each( |
| 122 | response.chart, |
| 123 | function (k, v) { |
| 124 | dataT.push( [v.day, parseInt( v.current )] ); |
| 125 | } |
| 126 | ); |
| 127 | |
| 128 | ga_dashboard.drawChart( dataT, selected_name ); |
| 129 | |
| 130 | // Set new data. |
| 131 | ga_dashboard.setChartData( dataT ); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | ); |
| 136 | } |
| 137 | ); |
| 138 | |
| 139 | $( '#ga-widget-trigger' ).on( |
| 140 | 'click', |
| 141 | function () { |
| 142 | const selected_name = $( '#metrics-selector option:selected' ).html(); |
| 143 | const selected_metric = $( '#metrics-selector option:selected' ).val() || null; |
| 144 | const selected_range = $( '#range-selector option:selected' ).val() || null; |
| 145 | |
| 146 | ga_loader.show(); |
| 147 | |
| 148 | var dataObj = {}; |
| 149 | dataObj['action'] = "ga_ajax_data_change"; |
| 150 | dataObj['metric'] = selected_metric; |
| 151 | dataObj['date_range'] = selected_range; |
| 152 | dataObj[GA_NONCE_FIELD] = GA_NONCE; |
| 153 | |
| 154 | $.ajax( |
| 155 | { |
| 156 | type: "post", |
| 157 | dataType: "json", |
| 158 | url: ajaxurl, |
| 159 | data: dataObj, |
| 160 | success: function (response) { |
| 161 | |
| 162 | ga_loader.hide(); |
| 163 | |
| 164 | if (typeof response.error !== "undefined") { |
| 165 | $( '#ga_widget_error' ) |
| 166 | .removeClass( 'hidden' ) |
| 167 | .html( `GA Dashboard: ${response.error}` ); |
| 168 | } else { |
| 169 | var dataT = [['Day', selected_name]]; |
| 170 | $.each( |
| 171 | response.chart, |
| 172 | function (k, v) { |
| 173 | dataT.push( [v.day, parseInt( v.current )] ); |
| 174 | } |
| 175 | ); |
| 176 | |
| 177 | $.each( |
| 178 | response.boxes, |
| 179 | function (k, v) { |
| 180 | $( '#ga_box_dashboard_label_' + k ).html( v.label ) |
| 181 | $( '#ga_box_dashboard_value_' + k ).html( v.value ); |
| 182 | } |
| 183 | ); |
| 184 | |
| 185 | ga_dashboard.drawChart( dataT, selected_name ); |
| 186 | |
| 187 | // Set new data. |
| 188 | ga_dashboard.setChartData( dataT ); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | ); |
| 193 | } |
| 194 | ); |
| 195 | |
| 196 | $( window ).on( |
| 197 | 'resize', |
| 198 | function () { |
| 199 | ga_dashboard.drawChart( ga_dashboard.getChartData(), ga_tools.recomputeChartWidth( minWidth, offset, wrapperSelector ) ); |
| 200 | } |
| 201 | ); |
| 202 | } |
| 203 | ); |
| 204 | }, |
| 205 | /** |
| 206 | * Returns chart data array. |
| 207 | * |
| 208 | * @returns {Array} |
| 209 | */ |
| 210 | getChartData: function () { |
| 211 | return ga_dashboard.chartData; |
| 212 | }, |
| 213 | /** |
| 214 | * Overwrites initial data array. |
| 215 | * |
| 216 | * @param new_data |
| 217 | */ |
| 218 | setChartData: function (new_data) { |
| 219 | ga_dashboard.chartData = new_data; |
| 220 | }, |
| 221 | drawChart: function (dataArr, title) { |
| 222 | const chart_dom_element = document.getElementById( 'chart_div' ); |
| 223 | |
| 224 | if (typeof title == 'undefined') { |
| 225 | title = 'Pageviews'; |
| 226 | } |
| 227 | |
| 228 | if (dataArr.length > 1) { |
| 229 | const data = google.visualization.arrayToDataTable( dataArr ); |
| 230 | |
| 231 | const options = { |
| 232 | /*title: title,*/ |
| 233 | legend: 'top', |
| 234 | lineWidth: 2, |
| 235 | chartArea: { |
| 236 | left: 10, |
| 237 | top: 60, |
| 238 | bottom: 50, |
| 239 | right: 10 |
| 240 | |
| 241 | }, |
| 242 | width: '95%', |
| 243 | height: 300, |
| 244 | hAxis: {title: 'Day', titleTextStyle: {color: '#333'}, direction: 1}, |
| 245 | vAxis: {minValue: 0}, |
| 246 | pointSize: 5 |
| 247 | }; |
| 248 | |
| 249 | var chart = new google.visualization.AreaChart( chart_dom_element ); |
| 250 | google.visualization.events.addListener( |
| 251 | chart, |
| 252 | 'ready', |
| 253 | function () { |
| 254 | ga_loader.hide(); |
| 255 | } |
| 256 | ); |
| 257 | chart.draw( data, options ); |
| 258 | } else { |
| 259 | $( '#ga_widget_error' ) |
| 260 | .removeClass( 'hidden' ) |
| 261 | .html( 'GA Dashboard: No data available for selected range.' ); |
| 262 | } |
| 263 | } |
| 264 | }; |
| 265 | |
| 266 | })( jQuery ); |
| 267 |