|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + */ |
| 6 | + |
| 7 | +import TrafficCop from '@mozmeao/trafficcop'; |
| 8 | +import { isApprovedToRun } from '../../../base/experiment-utils.es6'; |
| 9 | +import { |
| 10 | + meetsExperimentCriteria, |
| 11 | + experimentCookieID |
| 12 | +} from './default-experiment-criteria.es6'; |
| 13 | + |
| 14 | +const href = window.location.href; |
| 15 | +const ATTRIBUTION_COOKIE_CODE_ID = 'moz-stub-attribution-code'; |
| 16 | +const ATTRIBUTION_COOKIE_SIGNATURE_ID = 'moz-stub-attribution-sig'; |
| 17 | + |
| 18 | +if (typeof window.dataLayer === 'undefined') { |
| 19 | + window.dataLayer = []; |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Sets a cookie to remember which experiment variation has been seen. |
| 24 | + * @param {Object} traffic cop config |
| 25 | + */ |
| 26 | +function setVariationCookie(exp) { |
| 27 | + // set cookie to expire in 24 hours |
| 28 | + const date = new Date(); |
| 29 | + date.setTime(date.getTime() + 1 * 24 * 60 * 60 * 1000); |
| 30 | + const expires = date.toUTCString(); |
| 31 | + |
| 32 | + window.Mozilla.Cookies.setItem( |
| 33 | + exp.id, |
| 34 | + exp.chosenVariation, |
| 35 | + expires, |
| 36 | + undefined, |
| 37 | + undefined, |
| 38 | + false, |
| 39 | + 'lax' |
| 40 | + ); |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Removes existing stub attribution cookie |
| 45 | + */ |
| 46 | +function removeAttributionCookie() { |
| 47 | + window.Mozilla.Cookies.removeItem( |
| 48 | + ATTRIBUTION_COOKIE_CODE_ID, |
| 49 | + '/', |
| 50 | + undefined, |
| 51 | + false, |
| 52 | + 'lax' |
| 53 | + ); |
| 54 | + |
| 55 | + window.Mozilla.Cookies.removeItem( |
| 56 | + ATTRIBUTION_COOKIE_SIGNATURE_ID, |
| 57 | + '/', |
| 58 | + undefined, |
| 59 | + false, |
| 60 | + 'lax' |
| 61 | + ); |
| 62 | +} |
| 63 | + |
| 64 | +const init = () => { |
| 65 | + if ( |
| 66 | + href.indexOf('experiment=download-as-default&variation=control') !== -1 |
| 67 | + ) { |
| 68 | + window.dataLayer.push({ |
| 69 | + event: 'experiment_view', |
| 70 | + id: experimentCookieID, |
| 71 | + variant: 'control' |
| 72 | + }); |
| 73 | + } else if ( |
| 74 | + href.indexOf('experiment=download-as-default&variation=treatment') !== |
| 75 | + -1 |
| 76 | + ) { |
| 77 | + /** |
| 78 | + * People only enter into this experiment if they do not already have stub |
| 79 | + * attribution data. However there's still the edge case that someone may |
| 80 | + * not download right away, and return back later. If that's the case, |
| 81 | + * then we don't know if they checked or unchecked the input originally. |
| 82 | + * To circumvent this, we delete their original attribution cookie, |
| 83 | + * which will then be created fresh when they load the page again. |
| 84 | + */ |
| 85 | + if ( |
| 86 | + window.Mozilla.Cookies.hasItem(ATTRIBUTION_COOKIE_CODE_ID) || |
| 87 | + window.Mozilla.Cookies.hasItem(ATTRIBUTION_COOKIE_SIGNATURE_ID) |
| 88 | + ) { |
| 89 | + removeAttributionCookie(); |
| 90 | + } |
| 91 | + |
| 92 | + window.dataLayer.push({ |
| 93 | + event: 'experiment_view', |
| 94 | + id: experimentCookieID, |
| 95 | + variant: 'treatment' |
| 96 | + }); |
| 97 | + } else if (TrafficCop) { |
| 98 | + if ( |
| 99 | + isApprovedToRun() && |
| 100 | + meetsExperimentCriteria( |
| 101 | + window.site.platform, |
| 102 | + window.location.search |
| 103 | + ) |
| 104 | + ) { |
| 105 | + const cop = new TrafficCop({ |
| 106 | + id: experimentCookieID, |
| 107 | + variations: { |
| 108 | + 'experiment=download-as-default&variation=control': 25, |
| 109 | + 'experiment=download-as-default&variation=treatment&utm_source=www.mozilla.org&utm_campaign=SET_DEFAULT_BROWSER': 25 |
| 110 | + } |
| 111 | + }); |
| 112 | + cop.init(); |
| 113 | + |
| 114 | + setVariationCookie(cop); |
| 115 | + } |
| 116 | + } |
| 117 | +}; |
| 118 | + |
| 119 | +init(); |
0 commit comments