PluginProbe ʕ •ᴥ•ʔ
ShareThis Dashboard for Google Analytics / 3.2.4
ShareThis Dashboard for Google Analytics v3.2.4
3.3.2 trunk 1.0.7 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.5 2.3.5 2.3.6 2.3.7 2.3.8 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.0.0 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.3.0 3.3.1
googleanalytics / js / googleanalytics_page.js
googleanalytics / js Last commit date
googleanalytics.js 4 years ago googleanalytics_createprop.js 3 years ago googleanalytics_dashboard.js 3 years ago googleanalytics_page.js 9 months ago
googleanalytics_page.js
1202 lines
1 /**
2 * Google Analytics Page script.
3 *
4 * @package GoogleAnalytics
5 */
6
7 const GA_ACCESS_CODE_MODAL_ID = "ga_access_code_modal";
8 const GA_DEBUG_MODAL_ID = "ga_debug_modal";
9 const GA_DEBUG_MODAL_CONTENT_ID = "ga_debug_modal_content";
10 const GA_DEBUG_EMAIL = "ga_debug_email";
11 const GA_DEBUG_DESCRIPTION = "ga_debug_description";
12 const GA_ACCESS_CODE_TMP_ID = "ga_access_code_tmp";
13 const GA_ACCESS_CODE_ID = "ga_access_code";
14 const GA_FORM_ID = "ga_form";
15 const GA_MODAL_CLOSE_ID = 'ga_close';
16 const GA_MODAL_BTN_CLOSE_ID = 'ga_btn_close';
17 const GA_GOOGLE_AUTH_BTN_ID = 'ga_authorize_with_google_button';
18 const GA_SAVE_ACCESS_CODE_BTN_ID = 'ga_save_access_code';
19 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.';
20
21 ( function( $ ) {
22 ga_popup = {
23 url: '',
24 authorize: function( e, url ) {
25 e.preventDefault();
26 ga_popup.url = url;
27 $( '#' + GA_ACCESS_CODE_MODAL_ID ).appendTo( "body" ).show();
28 ga_popup.open();
29 },
30 open: function() {
31 const p_width = Math.round( screen.width / 2 );
32 const p_height = Math.round( screen.height / 2 );
33 const p_left = Math.round( p_width / 2 );
34 const p_top = 300;
35 window.open(
36 ga_popup.url,
37 'ga_auth_popup',
38 'width=' + p_width + ',height='
39 + p_height + ',top=' + p_top + ',left=' + p_left,
40 );
41 },
42 saveAccessCode: function( e ) {
43 e.preventDefault();
44 e.target.disabled = 'disabled';
45 ga_loader.show();
46 const ac_tmp = $( '#' + GA_ACCESS_CODE_TMP_ID ).val();
47 if ( ga_popup.validateCode( e, ac_tmp ) ) {
48 $( '#' + GA_ACCESS_CODE_ID ).val( ac_tmp );
49 $( '#' + GA_FORM_ID ).submit();
50 }
51 },
52 validateCode: function( e, code ) {
53 if ( ! code ) {
54 ga_loader.hide();
55 $( '#' + GA_SAVE_ACCESS_CODE_BTN_ID ).removeAttr( 'disabled' );
56 return false;
57 } else if ( code.substring( 0, 2 ) == 'UA' ) {
58 $( '#ga_code_error' ).show().html( GA_AUTHENTICATION_CODE_ERROR );
59 ga_loader.hide();
60 $( '#' + GA_SAVE_ACCESS_CODE_BTN_ID ).removeAttr( 'disabled' );
61 return false;
62 }
63 return true;
64 },
65 };
66
67 ga_modal = {
68 hide: function() {
69 $( '#' + GA_ACCESS_CODE_MODAL_ID ).hide();
70 $( '#' + GA_DEBUG_MODAL_ID ).hide();
71 ga_loader.hide();
72 $( '#' + GA_SAVE_ACCESS_CODE_BTN_ID ).removeAttr( 'disabled' );
73 },
74 };
75
76 ga_events = {
77 /**
78 * Tab logic.
79 */
80 tabLogic: function() {
81 const tabs = document.querySelectorAll( '.setting-tabs__tab');
82 const self = this;
83
84 // Setup button in ua.
85 const ga4SetupButton = document.querySelector( '.open-ga4' );
86
87 if ( ga4SetupButton ) {
88 ga4SetupButton.addEventListener( 'click', () => {
89 const ga4Tab = document.querySelector( '.setting-tabs__tab.ga4' );
90 const ga4Settings = document.querySelector( '.ga4-settings-wrap' );
91 const currentSelected = document.querySelector( '.setting-tabs__tab.engage' );
92 const currentSelectedContent = document.querySelector( '.setting-tab-content.engage' );
93
94 if ( currentSelected && currentSelectedContent ) {
95 currentSelected.classList.remove( 'engage' );
96 currentSelectedContent.classList.remove( 'engage' );
97 }
98
99 if ( ga4Settings && ga4Tab ) {
100 ga4Settings.classList.add( 'engage' );
101 ga4Tab.classList.add( 'engage' );
102 }
103 } );
104 }
105
106 if ( tabs ) {
107 tabs.forEach( settingTab => {
108 settingTab.addEventListener( 'click', () => {
109 const currentSelected = document.querySelector( '.setting-tabs__tab.engage' );
110 const currentSelectedContent = document.querySelector( '.setting-tab-content.engage' );
111
112 if ( currentSelected && currentSelectedContent ) {
113 currentSelected.classList.remove( 'engage' );
114 currentSelectedContent.classList.remove( 'engage' );
115 }
116
117 settingTab.classList.add( 'engage' );
118
119 if ( settingTab.classList.contains( 'ga4' ) ) {
120 const ga4Settings = document.querySelector( '.ga4-settings-wrap' );
121
122 if ( ga4Settings ) {
123 ga4Settings.classList.add( 'engage' );
124 }
125 }
126
127 if ( settingTab.classList.contains( 'ua' ) ) {
128 const uaSettings = document.querySelector( '.ua-settings-wrap' );
129
130 if ( uaSettings ) {
131 uaSettings.classList.add( 'engage' );
132 }
133 }
134 } );
135 } );
136 }
137
138 // Google auto popup.
139 const googleAuthLinks = document.querySelectorAll( '#google-auth-link, #ga_authorize_with_google_button' );
140 const theURL = btoa( window.location.href );
141
142 if ( googleAuthLinks ) {
143 googleAuthLinks.forEach( googleAuthLink => {
144 const theHref = btoa( googleAuthLink.getAttribute( 'href' ) );
145
146 googleAuthLink.addEventListener( 'click', ( e ) => {
147 e.preventDefault();
148 e.stopPropagation();
149
150 const uaAddition = 'ga_authorize_with_google_button' === e.target.id ? '&ua=t' : '';
151
152 window.location.href = 'https://sharethis.com/google-analytics-setup/?rfrurlga=' + theURL + '&rfgurlga=' + theHref + uaAddition;
153 } );
154 } );
155 }
156
157 // To step 3.
158 const nextButton = document.getElementById( 'to-step-3' );
159
160 if ( nextButton ) {
161 nextButton.addEventListener( 'click', () => {
162 const propertySelection = document.getElementById( 'ga4-property' );
163 const propError = document.querySelector( '.error.prop-error' );
164
165 if ( propError ) {
166 propError.remove();
167 }
168
169 if ( propertySelection ) {
170 if ( 'Choose Property' === propertySelection.value ) {
171 const propErrorMessage = document.createElement( 'small' );
172 propErrorMessage.className = 'error prop-error';
173 propErrorMessage.textContent = '*You must select a property to continue';
174
175 propertySelection.parentNode.parentNode.insertBefore( propErrorMessage, propertySelection.parentNode.nextSibling );
176 } else {
177 var dataObj = {};
178 dataObj['action'] = "save_ga4_property_selection";
179 dataObj['nonce'] = GA_NONCE;
180 dataObj['property'] = propertySelection.value;
181 dataObj['view_id'] = propertySelection.item(propertySelection.selectedIndex).getAttribute( 'data-view-id' );
182
183 $.ajax(
184 {
185 type: "post",
186 dataType: "json",
187 url: ajaxurl,
188 data: dataObj,
189 success: function ( response ) {
190 const step2 = document.querySelector( '.ga-step-2' );
191 const step3 = document.querySelector( '.ga-step-3' );
192
193 if ( step2 ) {
194 step2.classList.remove( 'engage' );
195 }
196
197 if ( step3 ) {
198 step3.classList.add( 'engage' );
199 }
200 },
201 },
202 );
203 }
204 }
205 } );
206 }
207
208 const enableGdpr = document.getElementById( 'ga4-enable-gdpr' );
209
210 // Final setup.
211 const ga4FinalSetup = document.getElementById( 'complete-ga4-setup' );
212
213 if ( enableGdpr ) {
214 enableGdpr.addEventListener( 'change', (theCheck) => {
215 if (theCheck.target.checked) {
216 if (ga4FinalSetup) {
217 ga4FinalSetup.textContent = 'Complete and go to GDPR configuration';
218 }
219 } else {
220 if (ga4FinalSetup) {
221 ga4FinalSetup.textContent = 'Complete and go to dashboard';
222 }
223 }
224 } );
225 }
226
227 if ( ga4FinalSetup ) {
228 ga4FinalSetup.addEventListener( 'click', ( e ) => {
229 e.stopPropagation();
230 e.preventDefault();
231
232 const finalDataObj = {};
233 const optimize = document.getElementById( 'ga4-google-optimize' );
234 const excludeRoles = document.querySelectorAll( '.ga-exclude-roles input' );
235 const enableDemo = document.getElementById( 'ga4-enable-demo' );
236 const ipAnon = document.getElementById( 'ga4-enable-ip-anon' );
237 const enableGdpr = document.getElementById( 'ga4-enable-gdpr' );
238 let linkUrl = e.target.getAttribute( 'href' );
239 let excludeSelections = '';
240
241 if ( excludeRoles ) {
242 excludeRoles.forEach( ( excludeRole, i ) => {
243 if ( excludeRole.checked ) {
244 excludeSelections += excludeRole.value + ',';
245 }
246 } );
247 }
248
249 finalDataObj['action'] = "save_ga4_final_setup";
250 finalDataObj['optimize'] = optimize ? optimize.value : false;
251 finalDataObj['exclude_roles'] = excludeSelections;
252 finalDataObj['enable_demo'] = enableDemo ? enableDemo.checked : false;
253 finalDataObj['ip_anon'] = ipAnon ? ipAnon.checked : false;
254 finalDataObj['nonce'] = GA_NONCE;
255 finalDataObj['enable_gdpr'] = enableGdpr ? enableGdpr.checked : false;
256
257 $.ajax(
258 {
259 type: "post",
260 dataType: "json",
261 url: ajaxurl,
262 data: finalDataObj,
263 success: function ( response ) {
264
265 if ( enableGdpr.checked ) {
266 self.manualEnableGdpr();
267 }
268
269 if ( enableDemo.checked ) {
270 linkUrl = linkUrl + '&ga_action=demo-ad';
271 }
272
273 window.location.href = linkUrl;
274 },
275 },
276 );
277 } );
278 }
279
280 document.addEventListener( 'submit', () => {
281 const propSelect = document.getElementById( 'ga_account_selector' );
282
283 if ( propSelect ) {
284 const viewIdDataObj = {};
285 const viewID = propSelect.item( propSelect.selectedIndex ).getAttribute( 'data-view-id' );
286
287 viewIdDataObj['action'] = "save_view_id";
288 viewIdDataObj['nonce'] = GA_NONCE;
289 viewIdDataObj['view_id'] = viewID;
290
291 $.ajax(
292 {
293 type: "post",
294 dataType: "json",
295 url: ajaxurl,
296 data: viewIdDataObj
297 },
298 );
299 }
300 } );
301 },
302
303 /**
304 * Send Demographic data.
305 *
306 * @param data
307 */
308 sendDemoData: function( demoData ) {
309 // Send demographic data.
310 $.ajax(
311 {
312 url: 'https://platform-api.sharethis.com/v1.0/property?id=' + ga_property_id + '&secret=' + ga_secret_id,
313 method: 'PUT',
314 async: false,
315 contentType: 'application/json; charset=utf-8',
316 data: JSON.stringify(
317 {
318 "demographics": demoData,
319 },
320 ),
321 },
322 );
323 },
324 /**
325 * Returns gdpr onboarding config values.
326 */
327 setGDPRConfig: function( isGDPR ) {
328
329 /**
330 * Check if ad blocker exists and notify if so.
331 */
332 $( document ).ready(
333 function() {
334 if ( $( "#detectadblock" ).height() > 0 ) {
335 } else {
336 $( '#adblocker-notice' ).show();
337 }
338 },
339 );
340
341 if ( ! isGDPR || undefined === gaGdprConfig ) {
342 return;
343 }
344
345 var config = JSON.parse( gaGdprConfig );
346
347 $( '.gdpr-platform input[name="gdpr-enable"]' ).prop( 'checked', config['enabled'] === 'true' );
348 $( '#sharethis-publisher-name' ).val( config['publisher_name'] );
349 $( '#sharethis-user-type option[value="' + config['display'] + '"]' ).attr( 'selected', true );
350 $( '#sharethis-consent-type option[value="' + config['scope'] + '"]' ).attr( 'selected', true );
351 $( '#sharethis-form-color .color[data-value="' + config['color'] + '"]' ).addClass( 'selected' );
352 $( '#st-language option[value="' + config['language'] + '"]' ).attr( 'selected', true );
353
354 if ( undefined !== config['publisher_purposes'] ) {
355 $( "#publisher-purpose .purpose-item input" ).prop( 'checked', false );
356
357 config['publisher_purposes'].map(
358 ( purpVal ) =>
359 {
360 var legit = 'true' === purpVal['legitimate_interest'] || true === purpVal['legitimate_interest'];
361 var consent = 'false' === purpVal['legitimate_interest'] || false === purpVal['legitimate_interest'];
362
363 $( '#publisher-purpose .purpose-item input[name="purposes[' + purpVal.id + ']"][value="legitimate"]' ).prop( 'checked', legit );
364 $( '#publisher-purpose .purpose-item input[name="purposes[' + purpVal.id + ']"][value="consent"]' ).prop( 'checked', consent );
365 }
366 );
367 }
368
369 if ( undefined !== config['publisher_restrictions'] ) {
370 $( ".vendor-table-cell-wrapper input" ).prop( 'checked', false );
371
372 $.map(
373 config['publisher_restrictions'],
374 function( id, venVal ) {
375 if ( id ) {
376 $( 'input[type="checkbox"][data-id="' + venVal + '"]' ).prop( 'checked', true );
377 }
378 },
379 );
380 }
381 },
382 scrollToAnchor: function( aid ) {
383 var aTag = $( "a[id='" + aid.toLowerCase() + "']" );
384
385 $( '.vendor-table-body' ).animate(
386 {
387 scrollTop: 0,
388 },
389 0,
390 ).animate(
391 {
392 scrollTop: aTag.offset().top - 740,
393 },
394 0,
395 );
396 },
397
398 click: function( selector, callback ) {
399 $( selector ).live( 'click', callback );
400 },
401 codeManuallyCallback: function( features_enabled ) {
402 var checkbox = $( '#ga_enter_code_manually' );
403 if ( features_enabled ) {
404 if ( checkbox.is( ':checked' ) ) {
405 if ( confirm( 'Warning: If you enter your Tracking ID manually, Analytics statistics will not be shown.' ) ) {
406 setTimeout(
407 function() {
408 $( '#ga_authorize_with_google_button' ).attr( 'disabled', 'disabled' ).next().show();
409 $( '#ga_account_selector' ).attr( 'disabled', 'disabled' );
410 $( '#ga_manually_wrapper' ).show();
411 },
412 350,
413 );
414
415 } else {
416 setTimeout(
417 function() {
418 checkbox.removeProp( 'checked' );
419 },
420 350,
421 );
422 }
423 } else {
424 setTimeout(
425 function() {
426 $( '#ga_authorize_with_google_button' ).removeAttr( 'disabled' ).next().hide();
427 $( '#ga_account_selector' ).removeAttr( 'disabled' );
428 $( '#ga_manually_wrapper' ).hide();
429 },
430 350,
431 );
432 }
433 }
434 },
435 initModalEvents: function() {
436 $( 'body' ).on(
437 'click',
438 '#close-review-us',
439 function() {
440 var dataObj = {},
441 self = this;
442 dataObj['action'] = "ga_ajax_hide_review";
443 dataObj[GA_NONCE_FIELD] = GA_NONCE;
444
445 $.ajax(
446 {
447 type: "post",
448 dataType: "json",
449 url: ajaxurl,
450 data: dataObj,
451 success: function( response ) {
452 $( '.ga-review-us' ).fadeOut();
453 },
454 },
455 );
456 },
457 );
458
459 $( '#' + GA_GOOGLE_AUTH_BTN_ID ).on(
460 'click',
461 function() {
462 $( '#' + GA_ACCESS_CODE_TMP_ID ).focus();
463 },
464 );
465
466 $( '#' + GA_MODAL_CLOSE_ID + ', #' + GA_MODAL_BTN_CLOSE_ID + ', #' + GA_DEBUG_MODAL_ID ).on(
467 'click',
468 function() {
469 ga_modal.hide();
470 },
471 );
472
473 $( '#copy-debug' ).on(
474 'click',
475 function() {
476 var copiedText = $( '#ga_debug_info' );
477
478 copiedText.select();
479 document.execCommand( 'copy' );
480 },
481 );
482
483 $( '#' + GA_DEBUG_MODAL_CONTENT_ID ).click(
484 function( event ) {
485 event.stopPropagation();
486 },
487 );
488 },
489
490 getConfig: function() {
491 var config,
492 enabled = $( 'input[name="gdpr-enable"]' ).is( ':checked' ),
493 publisherPurposes = [],
494 display = $( '#sharethis-user-type option:selected' ).val(),
495 name = $( '#sharethis-publisher-name' ).val(),
496 scope = 'publisher',
497 color = $( '#sharethis-form-color .color.selected' ).attr( 'data-value' ),
498 publisherRestrictions = {},
499 language = $( '#st-language' ).val();
500
501 $( '#publisher-purpose input:checked' ).each(
502 function( index, value ) {
503 var theId = $( value ).attr( 'data-id' ),
504 legit = 'consent' !== $( value ).val();
505
506 publisherPurposes.push( { 'id': theId, 'legitimate_interest': legit } );
507 },
508 );
509
510 $( '.vendor-table-cell-wrapper label input:checked' ).each(
511 function( index, value ) {
512 var vendorId = $( value ).attr( 'data-id' );
513 if ( vendorId ) {
514 publisherRestrictions[vendorId] = true;
515 }
516 },
517 );
518
519 config = {
520 enabled: enabled,
521 display: display,
522 publisher_name: name,
523 publisher_purposes: publisherPurposes,
524 publisher_restrictions: publisherRestrictions,
525 language: language,
526 color: color,
527 scope: scope,
528 };
529
530 return config;
531 },
532 manualEnableGdpr: function() {
533 var dataObj = {},
534 self = this,
535 config = ga_events.getConfig();
536
537 if ( $( 'body' ).hasClass( 'google-analytics_page_googleanalytics-settings' ) ) {
538 config = {
539 enabled: true,
540 display: 'eu',
541 publisher_name: '',
542 publisher_purposes: [],
543 language: 'en',
544 color: '',
545 scope: 'global',
546 };
547 }
548
549 theData = JSON.stringify(
550 {
551 'secret': ga_secret_id,
552 'id': ga_property_id,
553 'product': 'gdpr-compliance-tool-v2',
554 'config': config,
555 },
556 );
557
558 // Send new button status value.
559 $.ajax(
560 {
561 url: 'https://platform-api.sharethis.com/v1.0/property/product',
562 method: 'POST',
563 async: false,
564 contentType: 'application/json; charset=utf-8',
565 data: theData,
566 success: function( results ) {
567 },
568 },
569 );
570
571 dataObj['action'] = "ga_ajax_enable_gdpr";
572 dataObj['nonce'] = GA_NONCE;
573 dataObj['config'] = config;
574
575 $.ajax(
576 {
577 type: "post",
578 dataType: "json",
579 url: ajaxurl,
580 data: dataObj,
581 success: function( response ) {
582 window.location.href = siteAdminUrl + 'admin.php?page=googleanalytics%2Fgdpr';
583 },
584 },
585 );
586 },
587
588 enableGdpr: function() {
589 var timer = '';
590 const self = this;
591 this.$gdprContainer = $( '.gdpr-platform' );
592
593 // New color select.
594 this.$gdprContainer.on(
595 'click',
596 "#sharethis-form-color .color",
597 function() {
598 $( '#sharethis-form-color .color' ).removeClass( 'selected' );
599 $( this ).addClass( 'selected' );
600 }
601 );
602
603 // clear or show choices.
604 this.$gdprContainer.on(
605 'click',
606 '#clear-choices',
607 function( e ) {
608 e.preventDefault();
609 e.stopPropagation();
610
611 $( '.purpose-item input' ).prop( 'checked', false );
612 },
613 );
614
615 // clear or show choices.
616 this.$gdprContainer.on(
617 'click',
618 '#see-st-choices',
619 function( e ) {
620 e.preventDefault();
621 e.stopPropagation();
622 $( '.purpose-item input[name="purposes[1]"]' ).prop( 'checked', true );
623 $( '.purpose-item input[name="purposes[3]"][value="consent"]' ).prop( 'checked', true );
624 $( '.purpose-item input[name="purposes[5]"][value="consent"]' ).prop( 'checked', true );
625 $( '.purpose-item input[name="purposes[6]"][value="consent"]' ).prop( 'checked', true );
626 $( '.purpose-item input[name="purposes[9]"][value="legitimate"]' ).prop( 'checked', true );
627 $( '.purpose-item input[name="purposes[10]"][value="legitimate"]' ).prop( 'checked', true );
628 },
629 );
630
631 // Uncheck radio if click on selected box.
632 this.$gdprContainer.on(
633 'click',
634 '.lever',
635 ( e ) =>
636 {
637 e.preventDefault();
638 e.stopPropagation();
639
640 const theInput = $( e.currentTarget ).siblings( 'input' );
641
642 if ( theInput.is( ':checked' ) ) {
643 $( 'input[name="' + theInput.attr( 'name' ) + '"]' ).prop( 'checked', false );
644 } else {
645 theInput.prop( 'checked', true );
646 }
647 },
648 );
649
650 // Toggle button menus when arrows are clicked.
651 $( 'body' ).on(
652 'click',
653 '.accor-wrap .accor-tab',
654 function() {
655 var type = $( this ).find( 'span.accor-arrow' );
656
657 var closestButton = $( type ).parent( '.accor-tab' ).parent( '.accor-wrap' );
658
659 if ( '' === type.html() ) {
660
661 // Show the button configs.
662 closestButton.find( '.accor-content' ).slideDown();
663
664 // Change the icon next to title.
665 closestButton.find( '.accor-arrow' ).html( '&#9660;' );
666 } else {
667
668 // Show the button configs.
669 closestButton.find( '.accor-content' ).slideUp();
670
671 // Change the icon next to title.
672 closestButton.find( '.accor-arrow' ).html( '&#9658;' );
673 }
674 },
675 );
676
677 $( 'body' ).on(
678 'click',
679 '.demo-enable-popup .close-demo-modal',
680 function( e ) {
681 e.preventDefault();
682 e.stopPropagation();
683 $( '.demo-enable-popup' ).removeClass( 'engage' );
684 },
685 );
686
687 $( 'body' ).on(
688 'click',
689 '#demographic-popup',
690 function( e ) {
691 e.preventDefault();
692 e.stopPropagation();
693 $( '.demo-enable-popup' ).addClass( 'engage' );
694 },
695 );
696
697 $( 'body' ).on(
698 'click',
699 '#enable-demographic, #Enable-demographic',
700 function( e ) {
701 e.preventDefault();
702 e.stopPropagation();
703 ga_events.enableDemographic( 'enable' );
704 },
705 );
706
707 $( 'body' ).on(
708 'click',
709 '#Disable-demographic',
710 function( e ) {
711 e.preventDefault();
712 e.stopPropagation();
713 ga_events.enableDemographic( 'disable' );
714 },
715 );
716
717 // Handle account sign-out via Ajax.
718 $( 'body' ).on(
719 'click',
720 '#ga_sign_out',
721 function ( e ) {
722 e.preventDefault();
723 e.stopPropagation();
724
725 $.ajax(
726 {
727 type: 'post',
728 dataType: 'json',
729 url: googleAnalyticsPage.ajaxurl,
730 data: {
731 action: 'ga_ajax_sign_out',
732 nonce: googleAnalyticsPage.nonce,
733 },
734 success: function ( response ) {
735 window.location.href = encodeURI( googleAnalyticsPage.settingsURL )
736 },
737 },
738 );
739 },
740 );
741
742 $( 'body' ).on(
743 'click',
744 '#ga4_sign_out',
745 function ( e ) {
746 e.preventDefault();
747 e.stopPropagation();
748
749 $.ajax(
750 {
751 type: 'post',
752 dataType: 'json',
753 url: googleAnalyticsPage.ajaxurl,
754 data: {
755 action: 'ga4_ajax_sign_out',
756 nonce: googleAnalyticsPage.nonce,
757 },
758 success: function ( response ) {
759 window.location.href = encodeURI( googleAnalyticsPage.settingsURL + '&ga4=1' )
760 },
761 },
762 );
763 },
764 );
765
766 // Enable GDPR tool.
767 $( 'body' ).on(
768 'click',
769 '.gdpr-submit',
770 function( e ) {
771 e.preventDefault();
772 e.stopPropagation();
773
774 self.manualEnableGdpr();
775 },
776 );
777
778 // Enable GDPR tool.
779 $( 'body' ).on(
780 'click',
781 '.gdpr-enable',
782 function( e ) {
783 e.preventDefault();
784 e.stopPropagation();
785
786 self.manualEnableGdpr();
787 },
788 );
789
790 // Scroll to anchor in vendor list.
791 // Send user input to category search AFTER they stop typing.
792 $( 'body' ).on(
793 'keyup',
794 '.vendor-search input',
795 function( e ) {
796 clearTimeout( timer );
797
798 timer = setTimeout(
799 function() {
800 ga_events.scrollToAnchor( $( this ).val() );
801 }
802 .bind( this, ga_events ),
803 500,
804 );
805 },
806 );
807 },
808
809 enableDemographic: function( disable ) {
810 var dataObj = {};
811
812 dataObj['action'] = "ga_ajax_enable_demographic";
813 dataObj['nonce'] = ga_demo_nonce;
814 dataObj['enabled'] = 'disable' === disable ? 'false' : 'true';
815
816 $.ajax(
817 {
818 type: "post",
819 dataType: "json",
820 url: ajaxurl,
821 data: dataObj,
822 success: function( response ) {
823 window.location.href = siteAdminUrl + 'admin.php?page=googleanalytics&ga_action=demo-ad';
824 },
825 },
826 );
827 },
828 };
829
830 /**
831 * Handles "disable all features" switch button
832 *
833 * @type {{init: ga_switcher.init}}
834 */
835 ga_switcher = {
836 init: function( state ) {
837 var checkbox = $( "#ga-disable" );
838
839 if ( state ) {
840 checkbox.prop( 'checked', 'checked' );
841 } else {
842 checkbox.removeProp( 'checked' );
843 }
844 },
845 };
846
847 $( document ).ready(
848 function() {
849 const scrollData = $( '#ga-stats-container' ).attr( 'data-scroll' );
850 const scrollEl = '' !== scrollData ? $( '.' + scrollData ) : '';
851 const theBody = $( 'body' );
852
853 ga_events.initModalEvents();
854 ga_events.enableGdpr();
855 ga_events.tabLogic();
856 ga_events.setGDPRConfig( theBody.hasClass( 'google-analytics_page_googleanalytics-gdpr' ) );
857
858 if ( '' !== scrollData && theBody.hasClass( 'toplevel_page_googleanalytics' ) ) {
859 $( [ document.documentElement, document.body ] ).animate(
860 {
861 scrollTop: scrollEl.offset().top + 450,
862 },
863 500,
864 );
865 }
866 },
867 );
868
869 const offset = 50;
870 const minWidth = 350;
871 const wrapperSelector = '#ga-stats-container';
872 const chartContainer = 'chart_div';
873 const demoChartDeviceContainer = 'demo_chart_device_div';
874 const demoGa4ChartDeviceContainer = 'demo_ga4_chart_device_div';
875 const demoChartGenderContainer = 'demo_chart_gender_div';
876 const demoChartAgeContainer = 'demo_chart_age_div';
877 const demoChartAgeGa4Container = 'ga4_demo_chart_age_div';
878 const demoChartGenderGa4Container = 'ga4_demo_chart_gender_div';
879 const pageSessionChartContainer = 'page_session_chart_div';
880 const userChartContainer = 'user_chart_div';
881
882 ga_charts = {
883 init: function( callback ) {
884 $( document ).ready(
885 function() {
886 google.charts.load(
887 'current',
888 {
889 'packages': [ 'corechart' ],
890 },
891 );
892 ga_loader.show();
893 google.charts.setOnLoadCallback( callback );
894 },
895 );
896 },
897 createTooltip: function( day, pageviews ) {
898 return '<div style="padding:10px;width:100px;"><strong>' + day
899 + '</strong><br>Pageviews:<strong> ' + pageviews
900 + '</strong></div>';
901 },
902 createPageTooltip: function( day, pageviews ) {
903 return '<div style="padding:10px;width:100px;"><strong>' + day
904 + '</strong><br>Page Views:<strong> ' + pageviews
905 + '</strong></div>';
906 },
907 createUserTooltip: function( day, pageviews ) {
908 return '<div style="padding:10px;width:100px;"><strong>' + day
909 + '</strong><br>New Users:<strong> ' + pageviews
910 + '</strong></div>';
911 },
912 events: function( data ) {
913 $( window ).on(
914 'resize',
915 function() {
916 ga_charts.drawChart( data, ga_tools.recomputeChartWidth( minWidth, offset, wrapperSelector ) );
917 },
918 );
919 },
920 drawChart: function( data, chartWidth ) {
921
922 if ( typeof chartWidth == 'undefined' ) {
923 chartWidth = ga_tools.recomputeChartWidth( minWidth, offset, wrapperSelector );
924 }
925
926 const options = {
927 lineWidth: 5,
928 pointSize: 10,
929 tooltip: {
930 isHtml: true,
931 },
932 legend: {
933 position: ( ga_tools.getCurrentWidth( wrapperSelector ) <= minWidth ? 'top' : 'top' ),
934 maxLines: 5,
935 alignment: 'start',
936 textStyle: {
937 color: '#000',
938 fontSize: 12,
939 },
940 },
941 colors: [ '#4285f4' ],
942 hAxis: {
943 title: 'Day',
944 titleTextStyle: {
945 color: '#333',
946 },
947 },
948 vAxis: {
949 minValue: 0,
950 },
951 width: chartWidth,
952 height: 500,
953 chartArea: {
954 top: 50,
955 left: 50,
956 right: 30,
957 bottom: 100,
958 },
959 };
960
961 var chart = new google.visualization.AreaChart(
962 document.getElementById( chartContainer )
963 );
964 chart.draw( data, options );
965 },
966 drawPageSessionChart: function( data, chartWidth ) {
967 if ( typeof chartWidth == 'undefined' ) {
968 chartWidth = ga_tools.recomputeChartWidth( minWidth, offset, wrapperSelector );
969 }
970
971 const options = {
972 lineWidth: 5,
973 pointSize: 10,
974 tooltip: {
975 isHtml: true,
976 },
977 legend: {
978 position: ( ga_tools.getCurrentWidth( wrapperSelector ) <= minWidth ? 'top' : 'top' ),
979 maxLines: 5,
980 alignment: 'start',
981 textStyle: {
982 color: '#000',
983 fontSize: 12,
984 },
985 },
986 colors: [ '#4285f4' ],
987 hAxis: {
988 title: 'Day',
989 titleTextStyle: {
990 color: '#333',
991 },
992 },
993 vAxis: {
994 minValue: 0,
995 },
996 width: chartWidth,
997 height: 500,
998 chartArea: {
999 top: 50,
1000 left: 50,
1001 right: 30,
1002 bottom: 100,
1003 },
1004 };
1005
1006 var chart = new google.visualization.AreaChart(
1007 document.getElementById( pageSessionChartContainer )
1008 );
1009 chart.draw( data, options );
1010 },
1011 drawUserChart: function( data, chartWidth ) {
1012 if ( typeof chartWidth == 'undefined' ) {
1013 chartWidth = ga_tools.recomputeChartWidth( minWidth, offset, wrapperSelector );
1014 }
1015
1016 const options = {
1017 lineWidth: 5,
1018 pointSize: 10,
1019 tooltip: {
1020 isHtml: true,
1021 },
1022 legend: {
1023 position: ( ga_tools.getCurrentWidth( wrapperSelector ) <= minWidth ? 'top' : 'top' ),
1024 maxLines: 5,
1025 alignment: 'start',
1026 textStyle: {
1027 color: '#000',
1028 fontSize: 12,
1029 },
1030 },
1031 colors: [ '#4285f4' ],
1032 hAxis: {
1033 title: 'Day',
1034 titleTextStyle: {
1035 color: '#333',
1036 },
1037 },
1038 vAxis: {
1039 minValue: 0,
1040 },
1041 width: chartWidth,
1042 height: 500,
1043 chartArea: {
1044 top: 50,
1045 left: 50,
1046 right: 30,
1047 bottom: 100,
1048 },
1049 };
1050
1051 var chart = new google.visualization.AreaChart(
1052 document.getElementById( userChartContainer )
1053 );
1054 chart.draw( data, options );
1055 },
1056 drawDemoGenderChart: function( data, chartWidth ) {
1057 if ( typeof chartWidth == 'undefined' ) {
1058 chartWidth = ga_tools.recomputeChartWidth( minWidth, offset, wrapperSelector );
1059 }
1060
1061 data = google.visualization.arrayToDataTable( data );
1062
1063 if ( document.getElementById( demoChartGenderContainer ) ) {
1064 var chart = new google.visualization.PieChart( document.getElementById( demoChartGenderContainer ) );
1065 var options = {
1066 title: 'Gender',
1067 };
1068
1069 chart.draw( data, options );
1070 }
1071 },
1072 drawDemoDeviceChart: function( data, chartWidth ) {
1073 if ( typeof chartWidth == 'undefined' ) {
1074 chartWidth = ga_tools.recomputeChartWidth( minWidth, offset, wrapperSelector );
1075 }
1076
1077 data = google.visualization.arrayToDataTable( data );
1078
1079 if ( document.getElementById( demoChartDeviceContainer ) ) {
1080 var chart = new google.visualization.PieChart( document.getElementById( demoChartDeviceContainer ) );
1081 var options = {
1082 title: 'Device Breakdown',
1083 };
1084
1085 chart.draw( data, options );
1086 }
1087 },
1088
1089 drawGa4DemoDeviceChart: function( data, chartWidth ) {
1090 if ( typeof chartWidth == 'undefined' ) {
1091 chartWidth = ga_tools.recomputeChartWidth( minWidth, offset, wrapperSelector );
1092 }
1093
1094 data = google.visualization.arrayToDataTable( data );
1095
1096 if ( document.getElementById( demoGa4ChartDeviceContainer ) ) {
1097 var chart = new google.visualization.PieChart( document.getElementById( demoGa4ChartDeviceContainer ) );
1098 var options = {
1099 title: 'Device Breakdown',
1100 };
1101
1102 chart.draw( data, options );
1103 }
1104 },
1105
1106 drawDemoAgeChart: function( data, chartWidth ) {
1107 if ( typeof chartWidth == 'undefined' ) {
1108 chartWidth = ga_tools.recomputeChartWidth( minWidth, offset, wrapperSelector );
1109 }
1110
1111 data = google.visualization.arrayToDataTable( data );
1112
1113 var options = {
1114 title: 'Age',
1115 chartArea: { width: '50%' },
1116 hAxis: {
1117 minValue: 0,
1118 },
1119 };
1120
1121 if ( document.getElementById( demoChartAgeContainer ) ) {
1122 var chart = new google.visualization.BarChart( document.getElementById( demoChartAgeContainer ) );
1123
1124 chart.draw( data, options );
1125 }
1126 },
1127 drawDemoGenderGa4Chart: function( data, chartWidth ) {
1128 if ( typeof chartWidth == 'undefined' ) {
1129 chartWidth = ga_tools.recomputeChartWidth( minWidth, offset, wrapperSelector );
1130 }
1131
1132 data = google.visualization.arrayToDataTable( data );
1133
1134 if ( document.getElementById( demoChartGenderGa4Container ) ) {
1135 var chart = new google.visualization.PieChart( document.getElementById( demoChartGenderGa4Container ) );
1136 var options = {
1137 title: 'Gender',
1138 };
1139
1140 chart.draw( data, options );
1141 }
1142 },
1143 drawDemoAgeGa4Chart: function( data, chartWidth ) {
1144 if ( typeof chartWidth == 'undefined' ) {
1145 chartWidth = ga_tools.recomputeChartWidth( minWidth, offset, wrapperSelector );
1146 }
1147
1148 data = google.visualization.arrayToDataTable( data );
1149
1150 var options = {
1151 title: 'Age',
1152 chartArea: { width: '50%' },
1153 hAxis: {
1154 minValue: 0,
1155 },
1156 };
1157
1158 if ( document.getElementById( demoChartAgeGa4Container ) ) {
1159 var chart = new google.visualization.BarChart( document.getElementById( demoChartAgeGa4Container ) );
1160
1161 chart.draw( data, options );
1162 }
1163 },
1164 };
1165 ga_debug = {
1166 url: '',
1167 open_modal: function( e ) {
1168 e.preventDefault();
1169 $( '#' + GA_DEBUG_MODAL_ID ).appendTo( "body" ).show();
1170 $( '#ga-send-debug-email' ).removeAttr( 'disabled' );
1171 $( '#ga_debug_error' ).hide();
1172 $( '#ga_debug_success' ).hide();
1173 },
1174 send_email: function( e ) {
1175 e.preventDefault();
1176 ga_loader.show();
1177 var dataObj = {};
1178 dataObj['action'] = "googleanalytics_send_debug_email";
1179 dataObj['email'] = $( '#' + GA_DEBUG_EMAIL ).val();
1180 dataObj['description'] = $( '#' + GA_DEBUG_DESCRIPTION ).val();
1181 $.ajax(
1182 {
1183 type: "post",
1184 dataType: "json",
1185 url: ajaxurl,
1186 data: dataObj,
1187 success: function( response ) {
1188 ga_loader.hide();
1189 if ( typeof response.error !== "undefined" ) {
1190 $( '#ga_debug_error' ).show().html( response.error );
1191 } else if ( typeof response.success !== "undefined" ) {
1192 $( '#ga_debug_error' ).hide();
1193 $( '#ga-send-debug-email' ).attr( 'disabled', 'disabled' );
1194 $( '#ga_debug_success' ).show().html( response.success );
1195 }
1196 },
1197 },
1198 );
1199 },
1200 };
1201 } )( jQuery );
1202