-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8893 from google/enhancement/8384-refactor-como-js
- Loading branch information
Showing
14 changed files
with
216 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* Site Kit by Google, Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
( function () { | ||
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() { | ||
if ( ! ( global.wp_consent_type || global.wp_fallback_consent_type ) ) { | ||
return; | ||
} | ||
const consentParameters = {}; | ||
let hasConsentParameters = false; | ||
Object.entries( global._googlesitekitConsentCategoryMap ).forEach( | ||
( [ category, parameters ] ) => { | ||
if ( | ||
global.wp_has_consent && | ||
global.wp_has_consent( category ) | ||
) { | ||
parameters.forEach( ( parameter ) => { | ||
consentParameters[ parameter ] = 'granted'; | ||
} ); | ||
hasConsentParameters = | ||
hasConsentParameters || !! parameters.length; | ||
} | ||
} | ||
); | ||
if ( hasConsentParameters ) { | ||
global.gtag( 'consent', 'update', consentParameters ); | ||
} | ||
} | ||
document.addEventListener( | ||
'wp_consent_type_defined', | ||
updateGrantedConsent | ||
); | ||
document.addEventListener( 'DOMContentLoaded', function () { | ||
if ( ! global.waitfor_consent_hook ) { | ||
updateGrantedConsent(); | ||
} | ||
} ); | ||
} )(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* Consent Mode tests | ||
* | ||
* Site Kit by Google, Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './consent-mode'; | ||
|
||
describe( 'Consent Mode', () => { | ||
const gtagMock = jest.fn(); | ||
|
||
beforeEach( () => { | ||
global.gtag = gtagMock; | ||
global._googlesitekitConsentCategoryMap = { | ||
'test-category': [ 'test-parameter' ], | ||
}; | ||
} ); | ||
|
||
afterEach( () => { | ||
delete global.gtag; | ||
delete global._googlesitekitConsentCategoryMap; | ||
} ); | ||
|
||
it( 'should call gtag with the correct parameters when wp_listen_for_consent_change event is triggered', () => { | ||
document.dispatchEvent( | ||
new CustomEvent( 'wp_listen_for_consent_change', { | ||
detail: { | ||
'test-category': 'allow', | ||
}, | ||
} ) | ||
); | ||
|
||
expect( gtagMock ).toHaveBeenCalledWith( 'consent', 'update', { | ||
'test-parameter': 'granted', | ||
} ); | ||
} ); | ||
|
||
it( 'should call gtag with the correct parameters when wp_consent_type_defined event is triggered', () => { | ||
document.dispatchEvent( | ||
new CustomEvent( 'wp_consent_type_defined', { | ||
detail: { | ||
'test-category': 'allow', | ||
}, | ||
} ) | ||
); | ||
expect( gtagMock ).toHaveBeenCalledWith( 'consent', 'update', { | ||
'test-parameter': 'granted', | ||
} ); | ||
} ); | ||
|
||
it( 'should call gtag with the correct parameters when DOMContentLoaded event is triggered', () => { | ||
document.dispatchEvent( new Event( 'DOMContentLoaded' ) ); | ||
expect( gtagMock ).toHaveBeenCalledWith( 'consent', 'update', { | ||
'test-parameter': 'granted', | ||
} ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.