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