Skip to content

Commit

Permalink
Address merge review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
benbowler committed Jul 9, 2024
1 parent 2785244 commit 2cb081b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 31 deletions.
43 changes: 22 additions & 21 deletions assets/js/consent-mode/consent-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,31 @@
*/

( function () {
document.addEventListener(
'wp_listen_for_consent_change',
function ( event ) {
if ( event.detail ) {
const consentParameters = {};
let hasConsentParameters = false;
Object.keys( event.detail ).forEach( ( category ) => {
if ( global._googlesitekitConsentCategoryMap[ category ] ) {
const status = event.detail[ category ];
const mappedStatus =
status === 'allow' ? 'granted' : 'denied';
const parameters =
global._googlesitekitConsentCategoryMap[ category ];
parameters.forEach( ( parameter ) => {
consentParameters[ parameter ] = mappedStatus;
} );
hasConsentParameters = !! parameters.length;
}
} );
if ( hasConsentParameters ) {
global.gtag( 'consent', 'update', consentParameters );
function actionConsentChange( event ) {
if ( event.detail ) {
const consentParameters = {};
let hasConsentParameters = false;
Object.keys( event.detail ).forEach( ( category ) => {
if ( global._googlesitekitConsentCategoryMap[ category ] ) {
const status = event.detail[ category ];
const mappedStatus =
status === 'allow' ? 'granted' : 'denied';
const parameters =
global._googlesitekitConsentCategoryMap[ category ];
parameters.forEach( ( parameter ) => {
consentParameters[ parameter ] = mappedStatus;
} );
hasConsentParameters = !! parameters.length;
}
} );
if ( hasConsentParameters ) {
global.gtag( 'consent', 'update', consentParameters );
}
}
}
document.addEventListener(
'wp_listen_for_consent_change',
actionConsentChange
);

function updateGrantedConsent() {
Expand Down
3 changes: 2 additions & 1 deletion assets/js/consent-mode/consent-mode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ describe( 'Consent Mode', () => {
} );

afterEach( () => {
jest.restoreAllMocks();
delete global.gtag;
delete global._googlesitekitConsentCategoryMap;
} );

it( 'should call gtag with the correct parameters when wp_listen_for_consent_change event is triggered', () => {
Expand Down
11 changes: 5 additions & 6 deletions includes/Core/Consent_Mode/Consent_Mode.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ public function register() {
1 // Set priority to 1 to ensure the snippet is printed with top priority in the head.
);

// Register the consent mode script.
add_action( 'wp_enqueue_scripts', fn () => $this->enqueue_script() );
add_action( 'wp_enqueue_scripts', fn () => $this->register_and_enqueue_script() );
}

add_filter(
Expand All @@ -103,15 +102,15 @@ function () use ( $consent_mode_enabled ) {
}

/**
* Enqueues the consent mode script.
* Registers and Enqueues the consent mode script.
*
* @since n.e.x.t
*/
protected function enqueue_script() {
protected function register_and_enqueue_script() {
$consent_mode_script = new Script(
'consent-mode',
'googlesitekit-consent-mode',
array(
'src' => $this->context->url( 'dist/assets/js/consent-mode.js' ),
'src' => $this->context->url( 'dist/assets/js/googlesitekit-consent-mode.js' ),
)
);
$consent_mode_script->register( $this->context );
Expand Down
2 changes: 0 additions & 2 deletions webpack/basicModules.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ module.exports = ( mode ) => ( {
// Analytics advanced tracking script to be injected in the frontend.
'analytics-advanced-tracking':
'./assets/js/analytics-advanced-tracking.js',
// Consent Mode script.
'consent-mode': './assets/js/consent-mode/consent-mode.js',
},
externals,
output: {
Expand Down
3 changes: 2 additions & 1 deletion webpack/frontendModules.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ const {
module.exports = ( mode ) => ( {
entry: {
// Consent Mode
'consent-mode': './assets/js/consent-mode/consent-mode.js',
'googlesitekit-consent-mode':
'./assets/js/consent-mode/consent-mode.js',
// Event Providers
'googlesitekit-events-provider-contact-form-7':
'./assets/js/event-providers/contact-form-7.js',
Expand Down

0 comments on commit 2cb081b

Please sign in to comment.