bootstrap.min.js
9 years ago
googleanalytics.js
9 years ago
googleanalytics_dashboard.js
9 years ago
googleanalytics_page.js
9 years ago
googleanalytics_dashboard.js
133 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) { |
| 10 | ga_loader.show(); |
| 11 | google.charts.load('current', {'packages': ['corechart']}); |
| 12 | google.charts.setOnLoadCallback(function () { |
| 13 | ga_dashboard.drawChart(dataArr); |
| 14 | ga_dashboard.setChartData(dataArr); |
| 15 | }); |
| 16 | }, |
| 17 | events: function (data) { |
| 18 | $(document).ready(function () { |
| 19 | $('#range-selector').on('change', function () { |
| 20 | const selected = $(this).val(); |
| 21 | const selected_name = $('#metrics-selector option:selected').html(); |
| 22 | const selected_metric = $('#metrics-selector option:selected').val() || null; |
| 23 | |
| 24 | ga_loader.show(); |
| 25 | |
| 26 | $.ajax({ |
| 27 | type: "post", |
| 28 | dataType: "json", |
| 29 | url: ajaxurl, |
| 30 | data: {action: "ga_ajax_data_change", date_range: selected, metric: selected_metric}, |
| 31 | success: function (response) { |
| 32 | |
| 33 | ga_loader.hide(); |
| 34 | |
| 35 | var dataT = [['Day', selected_name]]; |
| 36 | $.each(response.chart, function (k, v) { |
| 37 | dataT.push([v.day, parseInt(v.current)]); |
| 38 | }); |
| 39 | |
| 40 | $.each(response.boxes, function (k, v) { |
| 41 | $('#ga_box_dashboard_label_' + k).html(v.label) |
| 42 | $('#ga_box_dashboard_value_' + k).html(v.value); |
| 43 | }); |
| 44 | |
| 45 | ga_dashboard.drawChart(dataT, selected_name); |
| 46 | |
| 47 | // Set new data |
| 48 | ga_dashboard.setChartData(dataT); |
| 49 | } |
| 50 | }); |
| 51 | }); |
| 52 | |
| 53 | $('#metrics-selector').on('change', function () { |
| 54 | const selected = $(this).val(); |
| 55 | const selected_name = $('#metrics-selector option:selected').html(); |
| 56 | const selected_range = $('#range-selector option:selected').val() || null; |
| 57 | |
| 58 | ga_loader.show(); |
| 59 | |
| 60 | $.ajax({ |
| 61 | type: "post", |
| 62 | dataType: "json", |
| 63 | url: ajaxurl, |
| 64 | data: {action: "ga_ajax_data_change", metric: selected, date_range: selected_range}, |
| 65 | success: function (response) { |
| 66 | ga_loader.hide(); |
| 67 | var dataT = [['Day', selected_name]]; |
| 68 | $.each(response.chart, function (k, v) { |
| 69 | dataT.push([v.day, parseInt(v.current)]); |
| 70 | }); |
| 71 | ga_dashboard.drawChart(dataT, selected_name); |
| 72 | |
| 73 | // Set new data |
| 74 | ga_dashboard.setChartData(dataT); |
| 75 | } |
| 76 | }); |
| 77 | }); |
| 78 | |
| 79 | $(window).on('resize', function () { |
| 80 | ga_dashboard.drawChart(ga_dashboard.getChartData(), ga_tools.recomputeChartWidth(minWidth, offset, wrapperSelector)); |
| 81 | }); |
| 82 | }); |
| 83 | }, |
| 84 | /** |
| 85 | * Returns chart data array. |
| 86 | * @returns {Array} |
| 87 | */ |
| 88 | getChartData: function () { |
| 89 | return ga_dashboard.chartData; |
| 90 | }, |
| 91 | /** |
| 92 | * Overwrites initial data array. |
| 93 | * @param new_data |
| 94 | */ |
| 95 | setChartData: function (new_data) { |
| 96 | ga_dashboard.chartData = new_data; |
| 97 | }, |
| 98 | drawChart: function (dataArr, title) { |
| 99 | const chart_dom_element = document.getElementById('chart_div'); |
| 100 | |
| 101 | if (typeof title == 'undefined') { |
| 102 | title = 'Pageviews'; |
| 103 | } |
| 104 | |
| 105 | const data = google.visualization.arrayToDataTable(dataArr); |
| 106 | |
| 107 | const options = { |
| 108 | /*title: title,*/ |
| 109 | legend: 'top', |
| 110 | lineWidth: 2, |
| 111 | chartArea: { |
| 112 | left: 10, |
| 113 | top: 60, |
| 114 | bottom: 50, |
| 115 | right: 10 |
| 116 | |
| 117 | }, |
| 118 | width: '95%', |
| 119 | height: 300, |
| 120 | hAxis: {title: 'Day', titleTextStyle: {color: '#333'}, direction: 1}, |
| 121 | vAxis: {minValue: 0}, |
| 122 | pointSize: 5 |
| 123 | }; |
| 124 | |
| 125 | var chart = new google.visualization.AreaChart(chart_dom_element); |
| 126 | google.visualization.events.addListener(chart, 'ready', function () { |
| 127 | ga_loader.hide(); |
| 128 | }); |
| 129 | chart.draw(data, options); |
| 130 | } |
| 131 | }; |
| 132 | |
| 133 | })(jQuery); |