@@ -229,82 +229,95 @@ export const AdmiralScript = () => {
229229 * Control group loads the script but the modal will not be shown.
230230 */
231231 const page = window . guardian . config . page ;
232+ if ( ! window . guardian . config . switches . consentManagement ) return ;
232233
233- const shouldRun =
234- cmp . hasInitialised ( ) &&
235- ! cmp . willShowPrivacyMessageSync ( ) &&
236- isInUsa ( ) &&
237- isInControlGroup &&
238- ! getCookie ( {
239- name : 'gu_hide_support_messaging' ,
240- shouldMemoize : true ,
241- } ) &&
242- ! page . shouldHideAdverts &&
243- ! page . shouldHideReaderRevenue &&
244- ! page . isSensitive &&
245- page . sponsorshipType !== 'paid-content' &&
246- ! [
247- 'about' ,
248- 'info' ,
249- 'membership' ,
250- 'help' ,
251- 'guardian-live-australia' ,
252- 'gnm-archive' ,
253- 'guardian-labs' ,
254- 'thefilter' ,
255- ] . includes ( page . section ?? '' ) ;
256- if ( ! shouldRun ) return ;
257-
258- // Record INSERT Ophan event
259- void recordAdmiralOphanEvent (
260- variantName ,
261- {
262- action : 'INSERT' ,
263- } ,
264- renderingTarget ,
265- ) ;
234+ let admiralScript : HTMLScriptElement | undefined ;
235+ let isCancelled = false ;
266236
267- // Initialize Admiral Adblock Recovery
268- log ( 'dotcom' , '🛡️ Initialising Admiral Adblock Recovery' ) ;
237+ const initialiseAdmiral = async ( ) => {
238+ try {
239+ const willShowPrivacyMessage =
240+ await cmp . willShowPrivacyMessage ( ) ;
269241
270- // Set up window.admiral stub
271- // This initializes admiral before the bootstrap script loads
272- // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Required for Admiral stub initialization
273- type AdmiralStub = Admiral & { q ?: any [ ] } ;
274- const w = window as Window & { admiral ?: AdmiralStub } ;
242+ const shouldRun =
243+ ! willShowPrivacyMessage &&
244+ isInUsa ( ) &&
245+ isInControlGroup &&
246+ ! getCookie ( {
247+ name : 'gu_hide_support_messaging' ,
248+ shouldMemoize : true ,
249+ } ) &&
250+ ! page . shouldHideAdverts &&
251+ ! page . shouldHideReaderRevenue &&
252+ ! page . isSensitive &&
253+ page . sponsorshipType !== 'paid-content' &&
254+ ! [
255+ 'about' ,
256+ 'info' ,
257+ 'membership' ,
258+ 'help' ,
259+ 'guardian-live-australia' ,
260+ 'gnm-archive' ,
261+ 'guardian-labs' ,
262+ 'thefilter' ,
263+ ] . includes ( page . section ?? '' ) ;
275264
276- if ( ! w . admiral ) {
277- // Create the Admiral stub function with queue
278- const stub = function ( ...args : unknown [ ] ) {
279- if ( ! stub . q ) stub . q = [ ] ;
280- stub . q . push ( args ) ;
281- } as AdmiralStub ;
282- w . admiral = stub ;
283- }
265+ if ( ! shouldRun || isCancelled ) return ;
284266
285- log ( 'dotcom' , '🛡️ Setting up Admiral event logger' ) ;
267+ void recordAdmiralOphanEvent (
268+ variantName ,
269+ {
270+ action : 'INSERT' ,
271+ } ,
272+ renderingTarget ,
273+ ) ;
274+
275+ log ( 'dotcom' , '🛡️ Initialising Admiral Adblock Recovery' ) ;
276+
277+ // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Required for Admiral stub initialization
278+ type AdmiralStub = Admiral & { q ?: any [ ] } ;
279+ const w = window as Window & { admiral ?: AdmiralStub } ;
286280
287- // Set up Admiral event logging
288- setUpAdmiralEventLogger ( w . admiral , variantName , renderingTarget ) ;
281+ if ( ! w . admiral ) {
282+ const stub = function ( ...args : unknown [ ] ) {
283+ if ( ! stub . q ) stub . q = [ ] ;
284+ stub . q . push ( args ) ;
285+ } as AdmiralStub ;
286+ w . admiral = stub ;
287+ }
289288
290- // Set AB test targeting
291- if ( variantName ) {
292- w . admiral ( 'targeting' , 'set' , 'guAbTest' , variantName ) ;
293- }
289+ log ( 'dotcom' , '🛡️ Setting up Admiral event logger' ) ;
290+ setUpAdmiralEventLogger (
291+ w . admiral ,
292+ variantName ,
293+ renderingTarget ,
294+ ) ;
294295
295- // Load Admiral bootstrap script
296- const BASE_AJAX_URL = window . guardian . config . page . ajaxUrl ;
296+ if ( variantName ) {
297+ w . admiral ( 'targeting' , 'set' , 'guAbTest' , variantName ) ;
298+ }
297299
298- const admiralScript = document . createElement ( 'script' ) ;
299- admiralScript . src = `${ BASE_AJAX_URL } /commercial/admiral-bootstrap.js` ;
300- admiralScript . async = true ;
301- document . head . appendChild ( admiralScript ) ;
300+ const BASE_AJAX_URL = window . guardian . config . page . ajaxUrl ;
301+ admiralScript = document . createElement ( 'script' ) ;
302+ admiralScript . src = `${ BASE_AJAX_URL } /commercial/admiral-bootstrap.js` ;
303+ admiralScript . async = true ;
304+ document . head . appendChild ( admiralScript ) ;
305+
306+ log ( 'dotcom' , '🛡️ Admiral initialization complete' ) ;
307+ } catch ( error ) {
308+ log (
309+ 'dotcom' ,
310+ '🛡️ Admiral - Error waiting for CMP readiness:' ,
311+ error ,
312+ ) ;
313+ }
314+ } ;
302315
303- log ( 'dotcom' , '🛡️ Admiral initialization complete' ) ;
316+ void initialiseAdmiral ( ) ;
304317
305318 return ( ) => {
306- // Clean up Admiral bootstrap script
307- admiralScript . parentNode ?. removeChild ( admiralScript ) ;
319+ isCancelled = true ;
320+ admiralScript ? .parentNode ?. removeChild ( admiralScript ) ;
308321 } ;
309322 } , [ isInControlGroup , variantName , renderingTarget ] ) ;
310323
0 commit comments