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