From 8fa3b01f64c74678a50818f2119a56dce49ae2ee Mon Sep 17 00:00:00 2001 From: Alex Gibson Date: Mon, 29 Apr 2024 22:50:53 +0100 Subject: [PATCH] Bump Traffic Cop to v3.0.0 (Fixes #14086) (#14087) --- docs/abtest.rst | 1 - media/js/base/core-datalayer-page-id.js | 59 ------------------- media/js/base/fxa-attribution.es6.js | 12 +--- media/js/base/stub-attribution.js | 12 +--- .../welcome/welcome15-experiment.es6.js | 1 - .../whatsnew-119-experiment-eu.es6.js | 2 - .../whatsnew-119-experiment-na.es6.js | 2 - media/js/glean/utils.es6.js | 4 -- .../vpn/landing-experiment-headlines.es6.js | 1 - package-lock.json | 8 +-- package.json | 2 +- .../unit/spec/base/core-datalayer-page-id.js | 57 ------------------ tests/unit/spec/base/fxa-attribution.js | 18 ------ tests/unit/spec/base/stub-attribution.js | 20 ------- tests/unit/spec/glean/utils.js | 16 +---- 15 files changed, 8 insertions(+), 207 deletions(-) diff --git a/docs/abtest.rst b/docs/abtest.rst index cf6f7f76887..26a86479435 100644 --- a/docs/abtest.rst +++ b/docs/abtest.rst @@ -178,7 +178,6 @@ not Traffic Cop should initiate. if (isApprovedToRun()) { const cop = new TrafficCop({ - id: 'experiment-name', variations: { 'entrypoint_experiment=experiment-name&entrypoint_variation=a': 10, 'entrypoint_experiment=experiment-name&entrypoint_variation=b': 10 diff --git a/media/js/base/core-datalayer-page-id.js b/media/js/base/core-datalayer-page-id.js index 0b569128000..8f448e57bd4 100644 --- a/media/js/base/core-datalayer-page-id.js +++ b/media/js/base/core-datalayer-page-id.js @@ -15,8 +15,6 @@ if (typeof window.Mozilla === 'undefined') { var dataLayer = (window.dataLayer = window.dataLayer || []); var Analytics = {}; - Analytics.customReferrer = ''; - /** Returns page ID used in Event Category for GA events tracked on page. * @param {String} path - URL path name fallback if page ID does not exist. * @return {String} GTM page ID. @@ -32,72 +30,15 @@ if (typeof window.Mozilla === 'undefined') { : pathName.replace(/^(\/\w{2}-\w{2}\/|\/\w{2,3}\/)/, '/'); }; - Analytics.getTrafficCopReferrer = function () { - var referrer; - - // if referrer cookie exists, store the value and remove the cookie - if ( - Mozilla.Cookies && - Mozilla.Cookies.hasItem('mozilla-traffic-cop-original-referrer') - ) { - referrer = Mozilla.Cookies.getItem( - 'mozilla-traffic-cop-original-referrer' - ); - - // referrer shouldn't persist - Mozilla.Cookies.removeItem( - 'mozilla-traffic-cop-original-referrer', - null, - null, - false, - 'lax' - ); - } - - return referrer; - }; - Analytics.buildDataObject = function () { var dataObj = { event: 'page-id-loaded', pageId: Analytics.getPageId() }; - var referrer = Analytics.getTrafficCopReferrer(); - - // if original referrer exists, pass it to GTM - if (referrer) { - // Traffic Cop sets the referrer to 'direct' if document.referer is empty - // prior to the redirect, so this value will either be a URL or the string 'direct'. - dataObj.customReferrer = referrer; - - // make the custom referrer available to other scripts. - Analytics.customReferrer = referrer; - } - return dataObj; }; - /** - * Returns custom referrer set by Traffic Cop if exists, - * else returns standard referrer. - * See https://github.com/mozilla/bedrock/issues/13593 - * @returns {String} referrer - */ - Analytics.getReferrer = function (ref) { - var referrer = typeof ref === 'string' ? ref : document.referrer; - var customReferrer = Analytics.customReferrer; - - if (customReferrer) { - // If customReferrer is returned from TC as "direct", - // return an empty string which is the default value - // for document.referrer. Otherwise return customReferrer. - return customReferrer === 'direct' ? '' : customReferrer; - } - - return referrer; - }; - // Push page ID into dataLayer so it's ready when GTM container loads. dataLayer.push(Analytics.buildDataObject()); diff --git a/media/js/base/fxa-attribution.es6.js b/media/js/base/fxa-attribution.es6.js index cbc3e1ee132..347b5e14a9d 100644 --- a/media/js/base/fxa-attribution.es6.js +++ b/media/js/base/fxa-attribution.es6.js @@ -80,18 +80,8 @@ FxaAttribution.getFxALinkReferralData = () => { return null; }; -FxaAttribution.getReferrer = (ref) => { - const referrer = typeof ref === 'string' ? ref : document.referrer; - - if (typeof window.Mozilla.Analytics !== 'undefined') { - return Mozilla.Analytics.getReferrer(referrer); - } - - return referrer; -}; - FxaAttribution.getSearchReferralData = (ref) => { - const referrer = FxaAttribution.getReferrer(ref); + const referrer = typeof ref === 'string' ? ref : document.referrer; const google = /^https?:\/\/www\.google\.\w{2,3}(\.\w{2})?\/?/; const bing = /^https?:\/\/www\.bing\.com\/?/; const yahoo = /^https?:\/\/(\w*\.)?search\.yahoo\.com\/?/; diff --git a/media/js/base/stub-attribution.js b/media/js/base/stub-attribution.js index 1f20647a42d..75f4ceed6e6 100644 --- a/media/js/base/stub-attribution.js +++ b/media/js/base/stub-attribution.js @@ -453,16 +453,6 @@ if (typeof window.Mozilla === 'undefined') { _checkGA(); }; - StubAttribution.getReferrer = function (ref) { - var referrer = typeof ref === 'string' ? ref : document.referrer; - - if (typeof window.Mozilla.Analytics !== 'undefined') { - return Mozilla.Analytics.getReferrer(referrer); - } - - return referrer; - }; - /** * Gets utm parameters and referrer information from the web page if they exist. * @param {String} ref - Optional referrer to facilitate testing. @@ -475,7 +465,7 @@ if (typeof window.Mozilla === 'undefined') { params.get('experiment') || StubAttribution.experimentName; var variation = params.get('variation') || StubAttribution.experimentVariation; - var referrer = StubAttribution.getReferrer(ref); + var referrer = typeof ref === 'string' ? ref : document.referrer; var ua = StubAttribution.getUserAgent(); var clientIDUA = StubAttribution.getUAClientID(); var clientIDGA4 = StubAttribution.getGtagClientID(); diff --git a/media/js/firefox/welcome/welcome15-experiment.es6.js b/media/js/firefox/welcome/welcome15-experiment.es6.js index e7ba860aaae..2a551278716 100644 --- a/media/js/firefox/welcome/welcome15-experiment.es6.js +++ b/media/js/firefox/welcome/welcome15-experiment.es6.js @@ -24,7 +24,6 @@ const initTrafficCop = () => { } } else if (TrafficCop) { const cop = new TrafficCop({ - id: 'welcome-15-exp', variations: { 'v=1': 50, 'v=2': 50 diff --git a/media/js/firefox/whatsnew/whatsnew-119-experiment-eu.es6.js b/media/js/firefox/whatsnew/whatsnew-119-experiment-eu.es6.js index df60234fc75..ea4d42a992c 100644 --- a/media/js/firefox/whatsnew/whatsnew-119-experiment-eu.es6.js +++ b/media/js/firefox/whatsnew/whatsnew-119-experiment-eu.es6.js @@ -50,8 +50,6 @@ const initTrafficCop = () => { } } else if (TrafficCop) { const murtaugh = new TrafficCop({ - id: 'wnp-119-experiment-eu', - cookieExpires: 0, variations: { 'v=1': 33, // Three boxes, open 'v=2': 33, // Three boxes, closed diff --git a/media/js/firefox/whatsnew/whatsnew-119-experiment-na.es6.js b/media/js/firefox/whatsnew/whatsnew-119-experiment-na.es6.js index 8f8602b24db..89e75055578 100644 --- a/media/js/firefox/whatsnew/whatsnew-119-experiment-na.es6.js +++ b/media/js/firefox/whatsnew/whatsnew-119-experiment-na.es6.js @@ -38,8 +38,6 @@ const initTrafficCop = () => { } } else if (TrafficCop) { const riggs = new TrafficCop({ - id: 'wnp-119-experiment-na', - cookieExpires: 0, variations: { 'v=1': 50, // Three boxes 'v=2': 50 // Addons diff --git a/media/js/glean/utils.es6.js b/media/js/glean/utils.es6.js index 5838cc009b7..cd7538b4300 100644 --- a/media/js/glean/utils.es6.js +++ b/media/js/glean/utils.es6.js @@ -83,10 +83,6 @@ const Utils = { const referrer = typeof str === 'string' ? str : document.referrer; const url = Utils.filterNewsletterURL(referrer); - if (typeof window.Mozilla.Analytics !== 'undefined') { - return Mozilla.Analytics.getReferrer(url); - } - return url; }, diff --git a/media/js/products/vpn/landing-experiment-headlines.es6.js b/media/js/products/vpn/landing-experiment-headlines.es6.js index 44d064f503f..db993685a03 100644 --- a/media/js/products/vpn/landing-experiment-headlines.es6.js +++ b/media/js/products/vpn/landing-experiment-headlines.es6.js @@ -50,7 +50,6 @@ const initTrafficCop = () => { // Avoid entering automated tests into random experiments. if (isApprovedToRun()) { const fife = new TrafficCop({ - id: 'vpn-headlines', variations: { 'entrypoint_experiment=vpn-headlines&entrypoint_variation=1': 33, // control 'entrypoint_experiment=vpn-headlines&entrypoint_variation=2': 33, // v2 diff --git a/package-lock.json b/package-lock.json index 48c45096d37..35b63e4794b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,7 +15,7 @@ "@mozilla/glean": "^5.0.0", "@mozmeao/cookie-helper": "^1.1.0", "@mozmeao/dnt-helper": "^1.0.0", - "@mozmeao/trafficcop": "^2.0.1", + "@mozmeao/trafficcop": "^3.0.0", "@sentry/browser": "^7.111.0", "babel-loader": "^9.1.3", "caniuse-lite": "^1.0.30001605", @@ -2211,9 +2211,9 @@ "integrity": "sha512-Q+fu1VBdG8x/WtJV+5mLXTF5/QJTiHQNWlE4Jf5kqsQw5cEKxjUHLD4qfxcuvwRkbErXyDz1xpWuTglBzIhedw==" }, "node_modules/@mozmeao/trafficcop": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/@mozmeao/trafficcop/-/trafficcop-2.0.1.tgz", - "integrity": "sha512-MMoXGx2Evjr3Y8aYFsC8S8DqOduC8oBtUXK4CDtFd/+YaLNAM7DM4gdwLrOAHX2DBRB0J9XOlPSgdzp/JPOGnw==" + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/@mozmeao/trafficcop/-/trafficcop-3.0.0.tgz", + "integrity": "sha512-pV3ZCbTwjO8dEcUCFeNLWcQIskSvYxUl1DDMF5GuA5cZhwBesV4WAjPLiM65j8J5LHq5qVYi9aI6F/nOe46oBQ==" }, "node_modules/@nodelib/fs.scandir": { "version": "2.1.5", diff --git a/package.json b/package.json index 6cb04bb67ea..915ae2a1e79 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "@mozilla/glean": "^5.0.0", "@mozmeao/cookie-helper": "^1.1.0", "@mozmeao/dnt-helper": "^1.0.0", - "@mozmeao/trafficcop": "^2.0.1", + "@mozmeao/trafficcop": "^3.0.0", "@sentry/browser": "^7.111.0", "babel-loader": "^9.1.3", "caniuse-lite": "^1.0.30001605", diff --git a/tests/unit/spec/base/core-datalayer-page-id.js b/tests/unit/spec/base/core-datalayer-page-id.js index f6f182fa492..a3d9f4216b3 100644 --- a/tests/unit/spec/base/core-datalayer-page-id.js +++ b/tests/unit/spec/base/core-datalayer-page-id.js @@ -10,10 +10,6 @@ */ describe('core-datalayer-page-id.js', function () { - afterEach(function () { - Mozilla.Analytics.customReferrer = ''; - }); - describe('getPageId', function () { const html = document.documentElement; @@ -40,57 +36,4 @@ describe('core-datalayer-page-id.js', function () { ); }); }); - - describe('getTrafficCopReferrer', function () { - it('should return null if the referrer does not exist', function () { - spyOn(Mozilla.Cookies, 'hasItem').and.returnValue(false); - expect(Mozilla.Analytics.getTrafficCopReferrer()).toBe(undefined); - }); - - it('should return the referrer if one exists', function () { - spyOn(Mozilla.Cookies, 'hasItem').and.returnValue(true); - spyOn(Mozilla.Cookies, 'getItem').and.returnValue('direct'); - expect(Mozilla.Analytics.getTrafficCopReferrer()).toBe('direct'); - }); - }); - - describe('buildDataObject', function () { - it('should contain customReferrer if found in cookie', function () { - const expected = 'http://www.google.com'; - spyOn(Mozilla.Analytics, 'getTrafficCopReferrer').and.returnValue( - expected - ); - const obj = Mozilla.Analytics.buildDataObject(); - expect(obj.customReferrer).toBeDefined(); - expect(Mozilla.Analytics.customReferrer).toEqual(expected); - }); - - it('should not contain customReferrer if not found in cookie', function () { - spyOn(Mozilla.Analytics, 'getTrafficCopReferrer').and.returnValue( - undefined - ); - const obj = Mozilla.Analytics.buildDataObject(); - expect(obj.customReferrer).not.toBeDefined(); - expect(Mozilla.Analytics.customReferrer).toEqual(''); - }); - }); - - describe('getReferrer', function () { - it('should return a custom referrer when set', function () { - const expected = 'http://www.google.com'; - Mozilla.Analytics.customReferrer = expected; - expect(Mozilla.Analytics.getReferrer()).toEqual(expected); - }); - - it('should return an empty string if customReferrer is direct', function () { - Mozilla.Analytics.customReferrer = 'direct'; - expect(Mozilla.Analytics.getReferrer()).toEqual(''); - }); - - it('should return standard document referrer otherwise', function () { - const expected = 'http://www.bing.com'; - Mozilla.Analytics.customReferrer = ''; - expect(Mozilla.Analytics.getReferrer(expected)).toEqual(expected); - }); - }); }); diff --git a/tests/unit/spec/base/fxa-attribution.js b/tests/unit/spec/base/fxa-attribution.js index 1931fe35b01..47fa65cab69 100644 --- a/tests/unit/spec/base/fxa-attribution.js +++ b/tests/unit/spec/base/fxa-attribution.js @@ -16,10 +16,6 @@ describe('fxa-attribution.js', function () { window.Mozilla.dntEnabled = sinon.stub(); }); - afterEach(function () { - Mozilla.Analytics.customReferrer = ''; - }); - describe('getHostName', function () { it('should return a hostname as expected', function () { const url1 = @@ -542,20 +538,6 @@ describe('fxa-attribution.js', function () { }); }); - describe('getReferrer', function () { - it('should return a custom referrer when set', function () { - const expected = 'http://www.google.com'; - Mozilla.Analytics.customReferrer = expected; - expect(FxaAttribution.getReferrer()).toEqual(expected); - }); - - it('should return standard document referrer otherwise', function () { - const expected = 'http://www.bing.com'; - Mozilla.Analytics.customReferrer = ''; - expect(FxaAttribution.getReferrer(expected)).toEqual(expected); - }); - }); - describe('setFxALinkReferralCookie', function () { it('should set a referral cookie as expected', function () { spyOn(Mozilla, 'dntEnabled').and.returnValue(false); diff --git a/tests/unit/spec/base/stub-attribution.js b/tests/unit/spec/base/stub-attribution.js index 3c45310142a..a701e46d80a 100644 --- a/tests/unit/spec/base/stub-attribution.js +++ b/tests/unit/spec/base/stub-attribution.js @@ -26,10 +26,6 @@ describe('stub-attribution.js', function () { window.dataLayer.push = sinon.stub(); }); - afterEach(function () { - Mozilla.Analytics.customReferrer = ''; - }); - describe('init', function () { let data = {}; @@ -626,22 +622,6 @@ describe('stub-attribution.js', function () { }); }); - describe('getReferrer', function () { - it('should return a custom referrer when set', function () { - const expected = 'http://www.google.com'; - Mozilla.Analytics.customReferrer = expected; - expect(Mozilla.StubAttribution.getReferrer()).toEqual(expected); - }); - - it('should return standard document referrer otherwise', function () { - const expected = 'http://www.bing.com'; - Mozilla.Analytics.customReferrer = ''; - expect(Mozilla.StubAttribution.getReferrer(expected)).toEqual( - expected - ); - }); - }); - describe('getAttributionData', function () { beforeEach(function () { // stub out GA client ID diff --git a/tests/unit/spec/glean/utils.js b/tests/unit/spec/glean/utils.js index e214503c09e..ea181ac0bdf 100644 --- a/tests/unit/spec/glean/utils.js +++ b/tests/unit/spec/glean/utils.js @@ -12,10 +12,6 @@ import Utils from '../../../../media/js/glean/utils.es6'; describe('utils.js', function () { - afterEach(function () { - Mozilla.Analytics.customReferrer = ''; - }); - describe('getUrl', function () { it('should return the a complete URL including query parameters', function () { const url1 = 'https://www.mozilla.org/en-US/'; @@ -114,17 +110,7 @@ describe('utils.js', function () { }); describe('getReferrer', function () { - afterEach(function () { - Mozilla.Analytics.customReferrer = ''; - }); - - it('should return a custom referrer when set', function () { - const expected = 'http://www.google.com/'; - Mozilla.Analytics.customReferrer = expected; - expect(Utils.getReferrer()).toEqual(expected); - }); - - it('should return regular referrer otherwise', function () { + it('should return regular referrer', function () { const expected = 'http://www.bing.com/'; expect(Utils.getReferrer(expected)).toEqual(expected); });