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