googleanalytics.js
9 years ago
googleanalytics_dashboard.js
9 years ago
googleanalytics_page.js
9 years ago
googleanalytics_page.js
194 lines
| 1 | const GA_ACCESS_CODE_MODAL_ID = "ga_access_code_modal"; |
| 2 | const GA_ACCESS_CODE_TMP_ID = "ga_access_code_tmp"; |
| 3 | const GA_ACCESS_CODE_ID = "ga_access_code"; |
| 4 | const GA_FORM_ID = "ga_form"; |
| 5 | const GA_MODAL_CLOSE_ID = 'ga_close'; |
| 6 | const GA_MODAL_BTN_CLOSE_ID = 'ga_btn_close'; |
| 7 | const GA_GOOGLE_AUTH_BTN_ID = 'ga_authorize_with_google_button'; |
| 8 | const GA_SAVE_ACCESS_CODE_BTN_ID = 'ga_save_access_code'; |
| 9 | |
| 10 | (function ($) { |
| 11 | |
| 12 | ga_popup = { |
| 13 | url: '', |
| 14 | authorize: function (e, url) { |
| 15 | e.preventDefault(); |
| 16 | ga_popup.url = url; |
| 17 | $('#' + GA_ACCESS_CODE_MODAL_ID).appendTo("body").show(); |
| 18 | ga_popup.open(); |
| 19 | }, |
| 20 | open: function () { |
| 21 | const p_width = Math.round(screen.width / 2); |
| 22 | const p_height = Math.round(screen.height / 2); |
| 23 | const p_left = Math.round(p_width / 2); |
| 24 | const p_top = 300; |
| 25 | window.open(ga_popup.url, 'ga_auth_popup', 'width=' + p_width + ',height=' |
| 26 | + p_height + ',top=' + p_top + ',left=' + p_left); |
| 27 | }, |
| 28 | saveAccessCode: function (e) { |
| 29 | e.preventDefault(); |
| 30 | e.target.disabled = 'disabled'; |
| 31 | ga_loader.show(); |
| 32 | const ac_tmp = $('#' + GA_ACCESS_CODE_TMP_ID).val(); |
| 33 | if (ac_tmp) { |
| 34 | $('#' + GA_ACCESS_CODE_ID).val(ac_tmp); |
| 35 | $('#' + GA_FORM_ID).submit(); |
| 36 | } |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | ga_modal = { |
| 41 | hide: function () { |
| 42 | $('#' + GA_ACCESS_CODE_MODAL_ID).hide(); |
| 43 | ga_loader.hide(); |
| 44 | $('#' + GA_SAVE_ACCESS_CODE_BTN_ID).removeAttr('disabled'); |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | ga_events = { |
| 49 | |
| 50 | click: function (selector, callback) { |
| 51 | $(selector).live('click', callback); |
| 52 | }, |
| 53 | codeManuallyCallback: function (terms_accepted) { |
| 54 | const button_disabled = $('#ga_authorize_with_google_button').attr('disabled'); |
| 55 | const selector_disabled = $('#ga_account_selector').attr('disabled'); |
| 56 | if (terms_accepted) { |
| 57 | if (button_disabled) { |
| 58 | $('#ga_authorize_with_google_button').removeAttr('disabled').next().hide(); |
| 59 | } else { |
| 60 | $('#ga_authorize_with_google_button').attr('disabled', |
| 61 | 'disabled').next().show(); |
| 62 | } |
| 63 | |
| 64 | if (selector_disabled) { |
| 65 | $('#ga_account_selector').removeAttr('disabled'); |
| 66 | } else { |
| 67 | $('#ga_account_selector').attr('disabled', 'disabled'); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | $('#ga_manually_wrapper').toggle(); |
| 72 | }, |
| 73 | initModalEvents: function () { |
| 74 | $('#' + GA_GOOGLE_AUTH_BTN_ID).on('click', function () { |
| 75 | $('#' + GA_ACCESS_CODE_TMP_ID).focus(); |
| 76 | }); |
| 77 | |
| 78 | $('#' + GA_MODAL_CLOSE_ID + ', #' + GA_MODAL_BTN_CLOSE_ID).on('click', function () { |
| 79 | ga_modal.hide(); |
| 80 | }); |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | /** |
| 85 | * Handles "disable all features" switch button |
| 86 | * @type {{init: ga_switcher.init}} |
| 87 | */ |
| 88 | ga_switcher = { |
| 89 | init: function (state) { |
| 90 | var checkbox = $("#ga-disable"); |
| 91 | |
| 92 | if (state) { |
| 93 | checkbox.prop('checked', 'checked'); |
| 94 | } else { |
| 95 | checkbox.removeProp('checked'); |
| 96 | } |
| 97 | |
| 98 | $("#ga-slider").on("click", function (e) { |
| 99 | var manually_enter_not_checked = $('#ga_enter_code_manually').not(':checked'); |
| 100 | if (checkbox.not(':checked').length > 0) { |
| 101 | if (confirm('This will disable Dashboards, Viral Alerts and Google API')) { |
| 102 | setTimeout(function () { |
| 103 | window.location.href = GA_DISABLE_FEATURE_URL; |
| 104 | }, 350); |
| 105 | } else { |
| 106 | setTimeout(function () { |
| 107 | checkbox.removeProp('checked'); |
| 108 | }, 350); |
| 109 | } |
| 110 | } else { // disable |
| 111 | setTimeout(function () { |
| 112 | window.location.href = GA_ENABLE_FEATURE_URL; |
| 113 | }, 350); |
| 114 | } |
| 115 | }); |
| 116 | } |
| 117 | }; |
| 118 | |
| 119 | $(document).ready(function () { |
| 120 | ga_events.initModalEvents(); |
| 121 | }); |
| 122 | |
| 123 | const offset = 50; |
| 124 | const minWidth = 350; |
| 125 | const wrapperSelector = '#ga-stats-container'; |
| 126 | const chartContainer = 'chart_div'; |
| 127 | |
| 128 | ga_charts = { |
| 129 | |
| 130 | init: function (callback) { |
| 131 | $(document).ready(function () { |
| 132 | google.charts.load('current', { |
| 133 | 'packages': ['corechart'] |
| 134 | }); |
| 135 | ga_loader.show(); |
| 136 | google.charts.setOnLoadCallback(callback); |
| 137 | }); |
| 138 | }, |
| 139 | createTooltip: function (day, pageviews) { |
| 140 | return '<div style="padding:10px;width:100px;">' + '<strong>' + day |
| 141 | + '</strong><br>' + 'Pageviews:<strong> ' + pageviews |
| 142 | + '</strong>' + '</div>'; |
| 143 | }, |
| 144 | events: function (data) { |
| 145 | $(window).on('resize', function () { |
| 146 | ga_charts.drawChart(data, ga_tools.recomputeChartWidth(minWidth, offset, wrapperSelector)); |
| 147 | }); |
| 148 | }, |
| 149 | drawChart: function (data, chartWidth) { |
| 150 | |
| 151 | if (typeof chartWidth == 'undefined') { |
| 152 | chartWidth = ga_tools.recomputeChartWidth(minWidth, offset, wrapperSelector); |
| 153 | } |
| 154 | |
| 155 | const options = { |
| 156 | /*title : 'Page Views',*/ |
| 157 | lineWidth: 5, |
| 158 | pointSize: 10, |
| 159 | tooltip: { |
| 160 | isHtml: true |
| 161 | }, |
| 162 | legend: { |
| 163 | position: (ga_tools.getCurrentWidth(wrapperSelector) <= minWidth ? 'top' |
| 164 | : 'top'), |
| 165 | maxLines: 5, |
| 166 | alignment: 'start', |
| 167 | textStyle: {color: '#000', fontSize: 12} |
| 168 | }, |
| 169 | colors: ['#4285f4', '#ff9800'], |
| 170 | hAxis: { |
| 171 | title: 'Day', |
| 172 | titleTextStyle: { |
| 173 | color: '#333' |
| 174 | } |
| 175 | }, |
| 176 | vAxis: { |
| 177 | minValue: 0 |
| 178 | }, |
| 179 | width: chartWidth, |
| 180 | height: 500, |
| 181 | chartArea: { |
| 182 | top: 50, |
| 183 | left: 50, |
| 184 | right: 30, |
| 185 | bottom: 100 |
| 186 | }, |
| 187 | }; |
| 188 | var chart = new google.visualization.AreaChart(document |
| 189 | .getElementById(chartContainer)); |
| 190 | chart.draw(data, options); |
| 191 | } |
| 192 | }; |
| 193 | })(jQuery); |
| 194 |