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