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