@@ -89,12 +89,13 @@ async function replaceInner(path, main) {
89
89
const html = await resp . text ( ) ;
90
90
// parse with DOMParser to guarantee valid HTML, and no script execution(s)
91
91
const dom = new DOMParser ( ) . parseFromString ( html , 'text/html' ) ;
92
- // eslint-disable-next-line no-param-reassign
93
- main . replaceWith ( dom . querySelector ( 'main' ) ) ;
92
+ // do not use replaceWith API here since this would replace the main reference
93
+ // in scripts.js as well and prevent proper decoration of the sections/blocks
94
+ main . innerHTML = dom . querySelector ( 'main' ) . innerHTML ;
94
95
return path ;
95
96
} catch ( e ) {
96
97
// eslint-disable-next-line no-console
97
- console . log ( `error loading content: ${ plainPath } ` , e ) ;
98
+ console . log ( `error loading content: ${ path } ` , e ) ;
98
99
}
99
100
return null ;
100
101
}
@@ -210,7 +211,7 @@ function inferEmptyPercentageSplits(variants) {
210
211
if ( variantsWithoutPercentage . length ) {
211
212
const missingPercentage = remainingPercentage / variantsWithoutPercentage . length ;
212
213
variantsWithoutPercentage . forEach ( ( v ) => {
213
- v . percentageSplit = missingPercentage . toFixed ( 2 ) ;
214
+ v . percentageSplit = missingPercentage . toFixed ( 4 ) ;
214
215
} ) ;
215
216
}
216
217
}
@@ -240,12 +241,15 @@ function getConfigForInstantExperiment(
240
241
variantNames : [ ] ,
241
242
} ;
242
243
243
- const pages = instantExperiment . split ( ',' ) . map ( ( p ) => new URL ( p . trim ( ) , window . location ) . pathname ) ;
244
+ const nbOfVariants = Number ( instantExperiment ) ;
245
+ const pages = Number . isNaN ( nbOfVariants )
246
+ ? instantExperiment . split ( ',' ) . map ( ( p ) => new URL ( p . trim ( ) , window . location ) . pathname )
247
+ : new Array ( nbOfVariants ) . fill ( window . location . pathname ) ;
244
248
245
249
const splitString = context . getMetadata ( `${ pluginOptions . experimentsMetaTag } -split` ) ;
246
250
const splits = splitString
247
251
// custom split
248
- ? splitString . split ( ',' ) . map ( ( i ) => parseInt ( i , 10 ) / 100 )
252
+ ? splitString . split ( ',' ) . map ( ( i ) => parseFloat ( i ) / 100 )
249
253
// even split fallback
250
254
: [ ...new Array ( pages . length ) ] . map ( ( ) => 1 / ( pages . length + 1 ) ) ;
251
255
@@ -261,7 +265,7 @@ function getConfigForInstantExperiment(
261
265
const vname = `challenger-${ i + 1 } ` ;
262
266
config . variantNames . push ( vname ) ;
263
267
config . variants [ vname ] = {
264
- percentageSplit : `${ splits [ i ] . toFixed ( 2 ) } ` ,
268
+ percentageSplit : `${ splits [ i ] . toFixed ( 4 ) } ` ,
265
269
pages : [ page ] ,
266
270
blocks : [ ] ,
267
271
label : `Challenger ${ i + 1 } ` ,
@@ -435,6 +439,8 @@ export async function runExperiment(document, options, context) {
435
439
console . debug ( `running experiment (${ window . hlx . experiment . id } ) -> ${ window . hlx . experiment . selectedVariant } ` ) ;
436
440
437
441
if ( experimentConfig . selectedVariant === experimentConfig . variantNames [ 0 ] ) {
442
+ document . body . classList . add ( `experiment-${ context . toClassName ( experimentConfig . id ) } ` ) ;
443
+ document . body . classList . add ( `variant-${ context . toClassName ( experimentConfig . selectedVariant ) } ` ) ;
438
444
context . sampleRUM ( 'experiment' , {
439
445
source : experimentConfig . id ,
440
446
target : experimentConfig . selectedVariant ,
@@ -450,13 +456,18 @@ export async function runExperiment(document, options, context) {
450
456
const currentPath = window . location . pathname ;
451
457
const control = experimentConfig . variants [ experimentConfig . variantNames [ 0 ] ] ;
452
458
const index = control . pages . indexOf ( currentPath ) ;
453
- if ( index < 0 || pages [ index ] === currentPath ) {
459
+ if ( index < 0 ) {
454
460
return false ;
455
461
}
456
462
457
463
// Fullpage content experiment
458
464
document . body . classList . add ( `experiment-${ context . toClassName ( experimentConfig . id ) } ` ) ;
459
- const result = await replaceInner ( pages [ index ] , document . querySelector ( 'main' ) ) ;
465
+ let result ;
466
+ if ( pages [ index ] !== currentPath ) {
467
+ result = await replaceInner ( pages [ index ] , document . querySelector ( 'main' ) ) ;
468
+ } else {
469
+ result = currentPath ;
470
+ }
460
471
experimentConfig . servedExperience = result || currentPath ;
461
472
if ( ! result ) {
462
473
// eslint-disable-next-line no-console
@@ -668,14 +679,25 @@ function adjustedRumSamplingRate(checkpoint, options, context) {
668
679
} ;
669
680
}
670
681
671
- export async function loadEager ( document , options , context ) {
672
- document . addEventListener ( 'rum' , ( event ) => {
673
- const checkpoint = event . detail ? event . detail . checkpoint || '' : '' ;
674
- if ( [ 'audiences' , 'campaign' , 'experiment' ] . includes ( checkpoint ) ) {
675
- adjustedRumSamplingRate ( checkpoint , options , context ) ;
676
- }
677
- } ) ;
682
+ function adjustRumSampligRate ( document , options , context ) {
683
+ const checkpoints = [ 'audiences' , 'campaign' , 'experiment' ] ;
684
+ if ( context . sampleRUM . always ) { // RUM v1.x
685
+ checkpoints . forEach ( ( ck ) => {
686
+ context . sampleRUM . always . on ( ck , adjustedRumSamplingRate ( ck , options , context ) ) ;
687
+ } ) ;
688
+ } else { // RUM 2.x
689
+ document . addEventListener ( 'rum' , ( event ) => {
690
+ if ( event . detail
691
+ && event . detail . checkpoint
692
+ && checkpoints . includes ( event . detail . checkpoint ) ) {
693
+ adjustedRumSamplingRate ( event . detail . checkpoint , options , context ) ;
694
+ }
695
+ } ) ;
696
+ }
697
+ }
678
698
699
+ export async function loadEager ( document , options , context ) {
700
+ adjustRumSampligRate ( document , options , context ) ;
679
701
let res = await runCampaign ( document , options , context ) ;
680
702
if ( ! res ) {
681
703
res = await runExperiment ( document , options , context ) ;
@@ -694,9 +716,9 @@ export async function loadLazy(document, options, context) {
694
716
if ( window . location . hostname . endsWith ( '.live' )
695
717
|| ( typeof options . isProd === 'function' && options . isProd ( ) )
696
718
|| ( options . prodHost
697
- && ( options . prodHost === window . location . host
698
- || options . prodHost === window . location . hostname
699
- || options . prodHost === window . location . origin ) ) ) {
719
+ && ( options . prodHost === window . location . host
720
+ || options . prodHost === window . location . hostname
721
+ || options . prodHost === window . location . origin ) ) ) {
700
722
return ;
701
723
}
702
724
// eslint-disable-next-line import/no-cycle
0 commit comments