googleanalytics.js
6 years ago
googleanalytics_createprop.js
6 years ago
googleanalytics_dashboard.js
9 years ago
googleanalytics_page.js
6 years ago
googleanalytics_page.js
280 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 | const GA_AUTHENTICATION_CODE_ERROR = 'That looks like your Google Analytics Tracking ID. Please enter the authentication token in this space. See here for <a href="https://cl.ly/1y1N1A3h0s1t" target="_blank">a walkthrough</a> of how to do it.'; |
| 14 | |
| 15 | (function ($) { |
| 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 (ga_popup.validateCode(e, ac_tmp)) { |
| 38 | $('#' + GA_ACCESS_CODE_ID).val(ac_tmp); |
| 39 | $('#' + GA_FORM_ID).submit(); |
| 40 | } |
| 41 | }, |
| 42 | validateCode: function (e, code) { |
| 43 | if (!code){ |
| 44 | ga_loader.hide(); |
| 45 | $('#' + GA_SAVE_ACCESS_CODE_BTN_ID).removeAttr('disabled'); |
| 46 | return false; |
| 47 | } |
| 48 | else if (code.substring(0, 2) == 'UA'){ |
| 49 | $('#ga_code_error').show().html(GA_AUTHENTICATION_CODE_ERROR); |
| 50 | ga_loader.hide(); |
| 51 | $('#' + GA_SAVE_ACCESS_CODE_BTN_ID).removeAttr('disabled'); |
| 52 | return false; |
| 53 | } |
| 54 | return true; |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | ga_modal = { |
| 59 | hide: function () { |
| 60 | $('#' + GA_ACCESS_CODE_MODAL_ID).hide(); |
| 61 | $('#' + GA_DEBUG_MODAL_ID).hide(); |
| 62 | ga_loader.hide(); |
| 63 | $('#' + GA_SAVE_ACCESS_CODE_BTN_ID).removeAttr('disabled'); |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | ga_events = { |
| 68 | |
| 69 | click: function (selector, callback) { |
| 70 | $(selector).live('click', callback); |
| 71 | }, |
| 72 | codeManuallyCallback: function (features_enabled) { |
| 73 | var checkbox = $('#ga_enter_code_manually'); |
| 74 | if ( features_enabled ) { |
| 75 | if ( checkbox.is(':checked') ) { |
| 76 | if (confirm('Warning: If you enter your Tracking ID manually, Analytics statistics will not be shown.')) { |
| 77 | setTimeout(function () { |
| 78 | $('#ga_authorize_with_google_button').attr('disabled','disabled').next().show(); |
| 79 | $('#ga_account_selector').attr('disabled', 'disabled'); |
| 80 | $('#ga_manually_wrapper').show(); |
| 81 | }, 350); |
| 82 | |
| 83 | } else { |
| 84 | setTimeout(function () { |
| 85 | checkbox.removeProp('checked'); |
| 86 | }, 350); |
| 87 | } |
| 88 | } else { // disable |
| 89 | setTimeout(function () { |
| 90 | $('#ga_authorize_with_google_button').removeAttr('disabled').next().hide(); |
| 91 | $('#ga_account_selector').removeAttr('disabled'); |
| 92 | $('#ga_manually_wrapper').hide(); |
| 93 | }, 350); |
| 94 | } |
| 95 | } |
| 96 | }, |
| 97 | initModalEvents: function () { |
| 98 | $('body').on('click', '#close-review-us', function() { |
| 99 | var dataObj = {}, |
| 100 | self = this; |
| 101 | dataObj['action'] = "ga_ajax_hide_review"; |
| 102 | dataObj[GA_NONCE_FIELD] = GA_NONCE; |
| 103 | |
| 104 | $.ajax({ |
| 105 | type: "post", |
| 106 | dataType: "json", |
| 107 | url: ajaxurl, |
| 108 | data: dataObj, |
| 109 | success: function (response) { |
| 110 | $('.ga-review-us').fadeOut(); |
| 111 | } |
| 112 | }); |
| 113 | }); |
| 114 | |
| 115 | $('#' + GA_GOOGLE_AUTH_BTN_ID).on('click', function () { |
| 116 | $('#' + GA_ACCESS_CODE_TMP_ID).focus(); |
| 117 | }); |
| 118 | |
| 119 | $('#' + GA_MODAL_CLOSE_ID + ', #' + GA_MODAL_BTN_CLOSE_ID + ', #' + GA_DEBUG_MODAL_ID ).on('click', function () { |
| 120 | ga_modal.hide(); |
| 121 | }); |
| 122 | |
| 123 | $( '#copy-debug' ).on( 'click', function() { |
| 124 | var copiedText = $( '#ga_debug_info' ); |
| 125 | |
| 126 | copiedText.select(); |
| 127 | document.execCommand( 'copy' ); |
| 128 | } ); |
| 129 | |
| 130 | $('#' + GA_DEBUG_MODAL_CONTENT_ID ).click(function(event){ |
| 131 | event.stopPropagation(); |
| 132 | }); |
| 133 | } |
| 134 | }; |
| 135 | |
| 136 | /** |
| 137 | * Handles "disable all features" switch button |
| 138 | * @type {{init: ga_switcher.init}} |
| 139 | */ |
| 140 | ga_switcher = { |
| 141 | init: function (state) { |
| 142 | var checkbox = $("#ga-disable"); |
| 143 | |
| 144 | if (state) { |
| 145 | checkbox.prop('checked', 'checked'); |
| 146 | } else { |
| 147 | checkbox.removeProp('checked'); |
| 148 | } |
| 149 | |
| 150 | $(".ga-slider-disable").on("click", function (e) { |
| 151 | var manually_enter_not_checked = $('#ga_enter_code_manually').not(':checked'); |
| 152 | if (checkbox.not(':checked').length > 0) { |
| 153 | if (confirm('This will disable Dashboards and Google API')) { |
| 154 | setTimeout(function () { |
| 155 | window.location.href = GA_DISABLE_FEATURE_URL; |
| 156 | }, 350); |
| 157 | } else { |
| 158 | setTimeout(function () { |
| 159 | checkbox.removeProp('checked'); |
| 160 | }, 350); |
| 161 | } |
| 162 | } else { // disable |
| 163 | setTimeout(function () { |
| 164 | window.location.href = GA_ENABLE_FEATURE_URL; |
| 165 | }, 350); |
| 166 | } |
| 167 | }); |
| 168 | } |
| 169 | }; |
| 170 | |
| 171 | $(document).ready(function () { |
| 172 | ga_events.initModalEvents(); |
| 173 | }); |
| 174 | |
| 175 | const offset = 50; |
| 176 | const minWidth = 350; |
| 177 | const wrapperSelector = '#ga-stats-container'; |
| 178 | const chartContainer = 'chart_div'; |
| 179 | |
| 180 | ga_charts = { |
| 181 | |
| 182 | init: function (callback) { |
| 183 | $(document).ready(function () { |
| 184 | google.charts.load('current', { |
| 185 | 'packages': ['corechart'] |
| 186 | }); |
| 187 | ga_loader.show(); |
| 188 | google.charts.setOnLoadCallback(callback); |
| 189 | }); |
| 190 | }, |
| 191 | createTooltip: function (day, pageviews) { |
| 192 | return '<div style="padding:10px;width:100px;">' + '<strong>' + day |
| 193 | + '</strong><br>' + 'Pageviews:<strong> ' + pageviews |
| 194 | + '</strong>' + '</div>'; |
| 195 | }, |
| 196 | events: function (data) { |
| 197 | $(window).on('resize', function () { |
| 198 | ga_charts.drawChart(data, ga_tools.recomputeChartWidth(minWidth, offset, wrapperSelector)); |
| 199 | }); |
| 200 | }, |
| 201 | drawChart: function (data, chartWidth) { |
| 202 | |
| 203 | if (typeof chartWidth == 'undefined') { |
| 204 | chartWidth = ga_tools.recomputeChartWidth(minWidth, offset, wrapperSelector); |
| 205 | } |
| 206 | |
| 207 | const options = { |
| 208 | /*title : 'Page Views',*/ |
| 209 | lineWidth: 5, |
| 210 | pointSize: 10, |
| 211 | tooltip: { |
| 212 | isHtml: true |
| 213 | }, |
| 214 | legend: { |
| 215 | position: (ga_tools.getCurrentWidth(wrapperSelector) <= minWidth ? 'top' |
| 216 | : 'top'), |
| 217 | maxLines: 5, |
| 218 | alignment: 'start', |
| 219 | textStyle: {color: '#000', fontSize: 12} |
| 220 | }, |
| 221 | colors: ['#4285f4'], |
| 222 | hAxis: { |
| 223 | title: 'Day', |
| 224 | titleTextStyle: { |
| 225 | color: '#333' |
| 226 | } |
| 227 | }, |
| 228 | vAxis: { |
| 229 | minValue: 0 |
| 230 | }, |
| 231 | width: chartWidth, |
| 232 | height: 500, |
| 233 | chartArea: { |
| 234 | top: 50, |
| 235 | left: 50, |
| 236 | right: 30, |
| 237 | bottom: 100 |
| 238 | }, |
| 239 | }; |
| 240 | var chart = new google.visualization.AreaChart(document |
| 241 | .getElementById(chartContainer)); |
| 242 | chart.draw(data, options); |
| 243 | } |
| 244 | }; |
| 245 | ga_debug = { |
| 246 | url: '', |
| 247 | open_modal: function (e) { |
| 248 | e.preventDefault(); |
| 249 | $('#' + GA_DEBUG_MODAL_ID).appendTo("body").show(); |
| 250 | $('#ga-send-debug-email').removeAttr('disabled'); |
| 251 | $('#ga_debug_error').hide(); |
| 252 | $('#ga_debug_success').hide(); |
| 253 | }, |
| 254 | send_email: function (e) { |
| 255 | e.preventDefault(); |
| 256 | ga_loader.show(); |
| 257 | var dataObj = {}; |
| 258 | dataObj['action'] = "googleanalytics_send_debug_email"; |
| 259 | dataObj['email'] = $('#' + GA_DEBUG_EMAIL).val(); |
| 260 | dataObj['description'] = $('#' + GA_DEBUG_DESCRIPTION).val(); |
| 261 | $.ajax({ |
| 262 | type: "post", |
| 263 | dataType: "json", |
| 264 | url: ajaxurl, |
| 265 | data: dataObj, |
| 266 | success: function (response) { |
| 267 | ga_loader.hide(); |
| 268 | if (typeof response.error !== "undefined") { |
| 269 | $('#ga_debug_error').show().html(response.error); |
| 270 | } else if (typeof response.success !== "undefined"){ |
| 271 | $('#ga_debug_error').hide(); |
| 272 | $('#ga-send-debug-email').attr('disabled','disabled'); |
| 273 | $('#ga_debug_success').show().html(response.success); |
| 274 | } |
| 275 | } |
| 276 | }); |
| 277 | } |
| 278 | }; |
| 279 | })(jQuery); |
| 280 |