Skip to content

Commit

Permalink
Bump Traffic Cop to v3.0.0 (Fixes #14086) (#14087)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgibson committed Apr 29, 2024
1 parent b0ab561 commit 8fa3b01
Show file tree
Hide file tree
Showing 15 changed files with 8 additions and 207 deletions.
1 change: 0 additions & 1 deletion docs/abtest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 0 additions & 59 deletions media/js/base/core-datalayer-page-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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());

Expand Down
12 changes: 1 addition & 11 deletions media/js/base/fxa-attribution.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -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\/?/;
Expand Down
12 changes: 1 addition & 11 deletions media/js/base/stub-attribution.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();
Expand Down
1 change: 0 additions & 1 deletion media/js/firefox/welcome/welcome15-experiment.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const initTrafficCop = () => {
}
} else if (TrafficCop) {
const cop = new TrafficCop({
id: 'welcome-15-exp',
variations: {
'v=1': 50,
'v=2': 50
Expand Down
2 changes: 0 additions & 2 deletions media/js/firefox/whatsnew/whatsnew-119-experiment-eu.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions media/js/firefox/whatsnew/whatsnew-119-experiment-na.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions media/js/glean/utils.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},

Expand Down
1 change: 0 additions & 1 deletion media/js/products/vpn/landing-experiment-headlines.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
57 changes: 0 additions & 57 deletions tests/unit/spec/base/core-datalayer-page-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
*/

describe('core-datalayer-page-id.js', function () {
afterEach(function () {
Mozilla.Analytics.customReferrer = '';
});

describe('getPageId', function () {
const html = document.documentElement;

Expand All @@ -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);
});
});
});
18 changes: 0 additions & 18 deletions tests/unit/spec/base/fxa-attribution.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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);
Expand Down
20 changes: 0 additions & 20 deletions tests/unit/spec/base/stub-attribution.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ describe('stub-attribution.js', function () {
window.dataLayer.push = sinon.stub();
});

afterEach(function () {
Mozilla.Analytics.customReferrer = '';
});

describe('init', function () {
let data = {};

Expand Down Expand Up @@ -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
Expand Down
16 changes: 1 addition & 15 deletions tests/unit/spec/glean/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/';
Expand Down Expand Up @@ -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);
});
Expand Down

0 comments on commit 8fa3b01

Please sign in to comment.