Skip to content

Commit

Permalink
Prevent duplicate consent update calls if the parameters have not cha…
Browse files Browse the repository at this point in the history
…ngesd.
  • Loading branch information
benbowler committed Jul 10, 2024
1 parent 97816cc commit ef83c39
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
12 changes: 11 additions & 1 deletion assets/js/consent-mode/consent-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
* limitations under the License.
*/

/**
* External dependencies
*/
const { isEqual } = require( 'lodash' );

( function () {
function actionConsentChange( event ) {
if ( event.detail ) {
Expand Down Expand Up @@ -62,8 +67,13 @@
}
}
);
if ( hasConsentParameters ) {
if (
hasConsentParameters &&
// Prevent duplicate calls to gtag, only updating if the consent parameters have changed.
! isEqual( consentParameters, global._googlesitekitConsents )
) {
global.gtag( 'consent', 'update', consentParameters );
global._googlesitekitConsents = consentParameters;
}
}
document.addEventListener(
Expand Down
20 changes: 20 additions & 0 deletions assets/js/consent-mode/consent-mode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,18 @@ describe( 'Consent Mode', () => {
global._googlesitekitConsentCategoryMap = {
'test-category': [ 'test-parameter' ],
};
global.wp_consent_type = 'optin';
global.wp_has_consent = () => true;
global._googlesitekitConsents = {};
} );

afterEach( () => {
gtagMock.mockReset();
delete global.gtag;
delete global._googlesitekitConsentCategoryMap;
delete global.wp_consent_type;
delete global.wp_has_consent;
delete global._googlesitekitConsents;
} );

it( 'should call gtag with the correct parameters when wp_listen_for_consent_change event is triggered', () => {
Expand Down Expand Up @@ -69,4 +76,17 @@ describe( 'Consent Mode', () => {
'test-parameter': 'granted',
} );
} );

it( 'should not trigger duplicate calls to gtag if the consent parameter have not changed', () => {
document.dispatchEvent( new Event( 'DOMContentLoaded' ) );
document.dispatchEvent(
new CustomEvent( 'wp_consent_type_defined', {
detail: {
'test-category': 'allow',
},
} )
);

expect( gtagMock ).toHaveBeenCalledTimes( 1 );
} );
} );
1 change: 1 addition & 0 deletions includes/Core/Consent_Mode/Consent_Mode.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ protected function render_gtag_consent_data_layer_snippet() {
window.dataLayer = window.dataLayer || [];function gtag(){dataLayer.push(arguments);}
gtag('consent', 'default', <?php echo wp_json_encode( $consent_defaults ); ?>);
window._googlesitekitConsentCategoryMap = <?php echo wp_json_encode( $consent_category_map ); ?>;
window._googlesitekitConsents = <?php echo wp_json_encode( $consent_defaults ); ?>
</script>
<!-- <?php echo esc_html__( 'End Google tag (gtag.js) Consent Mode dataLayer added by Site Kit', 'google-site-kit' ); ?> -->
<?php
Expand Down

0 comments on commit ef83c39

Please sign in to comment.