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

Magnite Analytics Adapter: Browser Name Parsing #9571

Merged
merged 1 commit into from
Feb 22, 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
33 changes: 33 additions & 0 deletions modules/magniteAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ const getTopLevelDetails = () => {
}
}

if (browser) {
deepSetValue(payload, rubiConf.pbaBrowserLocation || 'client.browser', browser);
}

// Add DM wrapper details
if (rubiConf.wrapperName) {
payload.wrapper = {
Expand Down Expand Up @@ -595,6 +599,28 @@ const subscribeToGamSlots = () => {
});
}

/**
* Lazy parsing of UA to determine browser
* @param {string} userAgent string from prebid ortb ua or navigator
* @returns {string} lazily guessed browser name
*/
export const detectBrowserFromUa = userAgent => {
let normalizedUa = userAgent.toLowerCase();

if (normalizedUa.includes('edg')) {
return 'Edge';
} else if ((/opr|opera|opt/i).test(normalizedUa)) {
return 'Opera';
} else if ((/chrome|crios/i).test(normalizedUa)) {
return 'Chrome';
} else if ((/fxios|firefox/i).test(normalizedUa)) {
return 'Firefox';
} else if (normalizedUa.includes('safari') && !(/chromium|ucbrowser/i).test(normalizedUa)) {
return 'Safari';
}
return 'OTHER';
}

let accountId;
let endpoint;

Expand Down Expand Up @@ -656,6 +682,7 @@ magniteAdapter.onDataDeletionRequest = function () {
magniteAdapter.MODULE_INITIALIZED_TIME = Date.now();
magniteAdapter.referrerHostname = '';

let browser;
magniteAdapter.track = ({ eventType, args }) => {
switch (eventType) {
case AUCTION_INIT:
Expand All @@ -675,6 +702,12 @@ magniteAdapter.track = ({ eventType, args }) => {
]);
auctionData.accountId = accountId;

// get browser
if (!browser) {
const userAgent = deepAccess(args, 'bidderRequests.0.ortb2.device.ua', navigator.userAgent) || '';
browser = detectBrowserFromUa(userAgent);
}

// Order bidders were called
auctionData.bidderOrder = args.bidderRequests.map(bidderRequest => bidderRequest.bidderCode);

Expand Down
32 changes: 32 additions & 0 deletions test/spec/modules/magniteAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import magniteAdapter, {
getHostNameFromReferer,
storage,
rubiConf,
detectBrowserFromUa
} from '../../../modules/magniteAnalyticsAdapter.js';
import CONSTANTS from 'src/constants.json';
import { config } from 'src/config.js';
Expand Down Expand Up @@ -101,6 +102,11 @@ const MOCK = {
'startTime': 1658868383748
}
],
'ortb2': {
'device': {
'ua': 'Mozilla/ 5.0(Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/ 537.36(KHTML, like Gecko) Chrome/ 109.0.0.0 Safari / 537.36'
}
},
'refererInfo': {
'page': 'http://a-test-domain.com:8000/test_pages/sanity/TEMP/prebidTest.html?pbjs_debug=true',
},
Expand Down Expand Up @@ -209,6 +215,9 @@ const ANALYTICS_MESSAGE = {
'start': 1519767013781,
'expires': 1519788613781
},
'client': {
'browser': 'Chrome'
},
'auctions': [
{
'auctionId': '99785e47-a7c8-4c8a-ae05-ef1c717a4b4d',
Expand Down Expand Up @@ -550,6 +559,29 @@ describe('magnite analytics adapter', function () {
]);
});

[
{
ua: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15',
expected: 'Safari'
},
{
ua: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/109.0',
expected: 'Firefox'
},
{
ua: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/109.0.1518.78',
expected: 'Edge'
},
{
ua: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 OPR/94.0.0.0',
expected: 'Opera'
}
].forEach(testData => {
it(`should parse browser from ${testData.expected} user agent correctly`, function () {
expect(detectBrowserFromUa(testData.ua)).to.equal(testData.expected);
});
})

it('should pass along 1x1 size if no sizes in adUnit', function () {
const auctionInit = utils.deepClone(MOCK.AUCTION_INIT);

Expand Down