@@ -210,103 +210,116 @@ const testName = 'growth-admiral-adblock-recovery';
210210export const AdmiralScript = ( ) => {
211211 const { renderingTarget } = useConfig ( ) ;
212212 const abTests = useBetaAB ( ) ;
213- const isInControlGroup =
214- abTests ?. isUserInTestGroup ( testName , 'control' ) ?? false ;
215- const variantName = isInControlGroup ? 'control' : undefined ;
213+ const isInVariantDetectGroup =
214+ abTests ?. isUserInTestGroup ( testName , 'variant-detect' ) ?? false ;
215+ const variantName = isInVariantDetectGroup
216+ ? 'variant-detect'
217+ : 'variant-recover' ; //We need to default to 'variant-recover' for users who are not in the AB test in order to show Admiral Modal to all users in the US who are not blocked by the CMP and meet the other criteria.
216218
217219 useEffect ( ( ) => {
218220 /**
219221 * The Admiral bootstrap script should only run under the following conditions:
220222 *
221223 * - Should not run if the CMP is due to show
222224 * - Should only run in the US
223- * - Should only run if in the AB test (control group)
224225 * - Should not run if the gu_hide_support_messaging cookie is set
225226 * - Should not run for content marked as: shouldHideAdverts, shouldHideReaderRevenue, isSensitive
226227 * - Should not run for paid-content sponsorship type (includes Hosted Content)
227228 * - Should not run for certain sections
228229 *
229- * Control group loads the script but the modal will not be shown.
230+ * Variant-detect group loads the script but the modal will not be shown.
230231 */
231232 const page = window . guardian . config . page ;
233+ if ( ! window . guardian . config . switches . consentManagement ) return ;
232234
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- ) ;
235+ let admiralScript : HTMLScriptElement | undefined ;
236+ let isCancelled = false ;
266237
267- // Initialize Admiral Adblock Recovery
268- log ( 'dotcom' , '🛡️ Initialising Admiral Adblock Recovery' ) ;
238+ const initialiseAdmiral = async ( ) => {
239+ try {
240+ const willShowPrivacyMessage =
241+ await cmp . willShowPrivacyMessage ( ) ;
269242
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 } ;
243+ const shouldRun =
244+ ! willShowPrivacyMessage &&
245+ isInUsa ( ) &&
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 } ;
309- } , [ isInControlGroup , variantName , renderingTarget ] ) ;
322+ } , [ variantName , renderingTarget ] ) ;
310323
311324 return null ;
312325} ;
0 commit comments