* @param {!Window} win * @param {?Object } experimentMap * @param {string} spec
(win, experimentMap, spec)
| 7522 | * @param {string} spec |
| 7523 | */ |
| 7524 | function parseSetExperiment(win, experimentMap, spec) { |
| 7525 | // Format: |
| 7526 | // - experimentSpec = experimentId | experimentId '=' num100 ('c')? |
| 7527 | let experimentId; |
| 7528 | let fraction; |
| 7529 | let control = false; |
| 7530 | const eq = spec.indexOf(':'); |
| 7531 | if (eq == -1) { |
| 7532 | experimentId = spec; |
| 7533 | fraction = 100; |
| 7534 | control = false; |
| 7535 | } else { |
| 7536 | experimentId = spec.substring(0, eq).trim(); |
| 7537 | spec = spec.substring(eq + 1); |
| 7538 | if (spec.substring(spec.length - 1) == Selection.CONTROL) { |
| 7539 | control = true; |
| 7540 | spec = spec.substring(0, spec.length - 1); |
| 7541 | } |
| 7542 | fraction = parseInt(spec, 10); |
| 7543 | } |
| 7544 | if (isNaN(fraction)) { |
| 7545 | throw new Error('invalid fraction'); |
| 7546 | } |
| 7547 | |
| 7548 | // Calculate "on"/"off". |
| 7549 | let on; |
| 7550 | if (fraction > 99) { |
| 7551 | // Explicitly "on". |
| 7552 | on = true; |
| 7553 | } else if (fraction < 1) { |
| 7554 | // Explicitly "off". |
| 7555 | on = false; |
| 7556 | } else if (win.sessionStorage) { |
| 7557 | // Fractional and possibly with the control. |
| 7558 | // Note that: |
| 7559 | // a. We can't do persistent experiments if storage is not available. |
| 7560 | // b. We can't run control on more than 20%. |
| 7561 | control = control && fraction <= 20; |
| 7562 | try { |
| 7563 | // Set fraction in the experiment to make it unlaunchable. |
| 7564 | const storageKey = |
| 7565 | 'subscribe.google.com:e:' + |
| 7566 | experimentId + |
| 7567 | ':' + |
| 7568 | fraction + |
| 7569 | (control ? 'c' : ''); |
| 7570 | let selection = parseSelection(win.sessionStorage.getItem(storageKey)); |
| 7571 | if (!selection) { |
| 7572 | // Is experiment/control range? |
| 7573 | if (win.Math.random() * 100 <= fraction * (control ? 2 : 1)) { |
| 7574 | const inExperiment = control ? win.Math.random() <= 0.5 : true; |
| 7575 | selection = inExperiment ? Selection.EXPERIMENT : Selection.CONTROL; |
| 7576 | win.sessionStorage.setItem(storageKey, selection); |
| 7577 | } |
| 7578 | } |
| 7579 | on = !!selection; |
| 7580 | if (selection == Selection.CONTROL) { |
| 7581 | experimentId = 'c-' + experimentId; |
no test coverage detected