Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass Traffic Cop custom referrer to attribution scripts (Fixes #13593) #13621

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions media/js/base/core-datalayer-page-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ 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 Down Expand Up @@ -68,11 +70,34 @@ if (typeof window.Mozilla === 'undefined') {
// 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: 11 additions & 1 deletion media/js/base/fxa-attribution.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,18 @@ FxaAttribution.getFxALinkReferralData = () => {
return null;
};

FxaAttribution.getSearchReferralData = (ref) => {
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 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: 11 additions & 1 deletion media/js/base/stub-attribution.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,16 @@ 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 @@ -395,7 +405,7 @@ if (typeof window.Mozilla === 'undefined') {
params.get('experiment') || StubAttribution.experimentName;
var variation =
params.get('variation') || StubAttribution.experimentVariation;
var referrer = typeof ref !== 'undefined' ? ref : document.referrer;
var referrer = StubAttribution.getReferrer(ref);
var ua = StubAttribution.getUserAgent();
var clientID = StubAttribution.getGAClientID();

Expand Down
8 changes: 7 additions & 1 deletion media/js/glean/utils.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ const Utils = {
},

getReferrer: (ref) => {
return typeof ref === 'string' ? ref : document.referrer;
const referrer = typeof ref === 'string' ? ref : document.referrer;

if (typeof window.Mozilla.Analytics !== 'undefined') {
return Mozilla.Analytics.getReferrer(referrer);
}

return referrer;
},

getHttpStatus: () => {
Expand Down
28 changes: 27 additions & 1 deletion tests/unit/spec/base/core-datalayer-page-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
*/

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

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

Expand Down Expand Up @@ -52,11 +56,13 @@ describe('core-datalayer-page-id.js', function () {

describe('buildDataObject', function () {
it('should contain customReferrer if found in cookie', function () {
const expected = 'http://www.google.com';
spyOn(Mozilla.Analytics, 'getTrafficCopReferrer').and.returnValue(
'http://www.google.com'
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 () {
Expand All @@ -65,6 +71,26 @@ describe('core-datalayer-page-id.js', function () {
);
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: 18 additions & 0 deletions tests/unit/spec/base/fxa-attribution.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ 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 @@ -538,6 +542,20 @@ 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: 20 additions & 0 deletions tests/unit/spec/base/stub-attribution.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ describe('stub-attribution.js', function () {
window.dataLayer.push = sinon.stub();
});

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

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

Expand Down Expand Up @@ -536,6 +540,22 @@ 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
18 changes: 18 additions & 0 deletions tests/unit/spec/glean/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
import Utils from '../../../../media/js/glean/utils.es6';

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

describe('getPathFromUrl', function () {
it('should return the path from a page URL excluding locale', function () {
expect(Utils.getPathFromUrl('/en-US/firefox/new/')).toEqual(
Expand Down Expand Up @@ -80,6 +84,20 @@ describe('utilsjs', function () {
});
});

describe('getReferrer', function () {
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 standard document referrer otherwise', function () {
const expected = 'http://www.bing.com';
Mozilla.Analytics.customReferrer = '';
expect(Utils.getReferrer(expected)).toEqual(expected);
});
});

describe('getHttpStatus', function () {
afterEach(function () {
document
Expand Down