Skip to content

Commit

Permalink
Utils: add getDomLoadingDuration() to remove dupe code (prebid#11831)
Browse files Browse the repository at this point in the history
  • Loading branch information
osazos authored and DecayConstant committed Jul 18, 2024
1 parent f634053 commit c5a9cfd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 50 deletions.
21 changes: 4 additions & 17 deletions modules/adagioRtdProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
deepAccess,
deepSetValue,
generateUUID,
getDomLoadingDuration,
getUniqueIdentifierStr,
getWindowSelf,
getWindowTop,
Expand Down Expand Up @@ -131,12 +132,14 @@ const _FEATURES = (function() {
features.data = {};
},
get: function() {
const w = (canAccessWindowTop()) ? getWindowTop() : getWindowSelf();

if (!features.initialized) {
features.data = {
page_dimensions: getPageDimensions().toString(),
viewport_dimensions: getViewPortDimensions().toString(),
user_timestamp: getTimestampUTC().toString(),
dom_loading: getDomLoadingDuration().toString(),
dom_loading: getDomLoadingDuration(w).toString(),
};
features.initialized = true;
}
Expand Down Expand Up @@ -538,22 +541,6 @@ function getTimestampUTC() {
return Math.floor(new Date().getTime() / 1000) - new Date().getTimezoneOffset() * 60;
}

function getDomLoadingDuration() {
const w = (canAccessWindowTop()) ? getWindowTop() : getWindowSelf();
const performance = w.performance;

let domLoadingDuration = -1;

if (performance && performance.timing && performance.timing.navigationStart > 0) {
const val = performance.timing.domLoading - performance.timing.navigationStart;
if (val > 0) {
domLoadingDuration = val;
}
}

return domLoadingDuration;
}

/**
* registerEventsForAdServers bind adagio listeners to ad-server events.
* Theses events are used to track the viewability and attention.
Expand Down
34 changes: 3 additions & 31 deletions modules/bliinkBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { registerBidder } from '../src/adapters/bidderFactory.js'
import { config } from '../src/config.js'
import { _each, deepAccess, deepSetValue, getWindowSelf, getWindowTop } from '../src/utils.js'
import { _each, canAccessWindowTop, deepAccess, deepSetValue, getDomLoadingDuration, getWindowSelf, getWindowTop } from '../src/utils.js'
export const BIDDER_CODE = 'bliink'
export const GVL_ID = 658
export const BLIINK_ENDPOINT_ENGINE = 'https://engine.bliink.io/prebid'
Expand Down Expand Up @@ -121,35 +121,6 @@ export function getKeywords() {
return [];
}

function canAccessTopWindow() {
try {
if (getWindowTop().location.href) {
return true;
}
} catch (error) {
return false;
}
}

/**
* domLoading feature is computed on window.top if reachable.
*/
export function getDomLoadingDuration() {
let domLoadingDuration = -1;
let performance;

performance = (canAccessTopWindow()) ? getWindowTop().performance : getWindowSelf().performance;

if (performance && performance.timing && performance.timing.navigationStart > 0) {
const val = performance.timing.domLoading - performance.timing.navigationStart;
if (val > 0) {
domLoadingDuration = val;
}
}

return domLoadingDuration;
}

/**
* @param bidResponse
* @return {({cpm, netRevenue: boolean, requestId, width: number, currency, ttl: number, creativeId, height: number}&{mediaType: string, vastXml})|null}
Expand Down Expand Up @@ -208,7 +179,8 @@ export const isBidRequestValid = (bid) => {
*/
export const buildRequests = (validBidRequests, bidderRequest) => {
if (!validBidRequests || !bidderRequest || !bidderRequest.bids) return null
const domLoadingDuration = getDomLoadingDuration().toString();
const w = (canAccessWindowTop()) ? getWindowTop() : getWindowSelf();
const domLoadingDuration = getDomLoadingDuration(w).toString();
const tags = bidderRequest.bids.map((bid) => {
let bidFloor;
const sizes = bid.sizes.map((size) => ({ w: size[0], h: size[1] }));
Expand Down
27 changes: 27 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,33 @@ export function getPerformanceNow() {
return (window.performance && window.performance.now && window.performance.now()) || 0;
}

/**
* Retuns the difference between `timing.domLoading` and `timing.navigationStart`.
* This function uses the deprecated `Performance.timing` API and should be removed in future.
* It has not been updated yet because it is still used in some modules.
* @deprecated
* @param {Window} w The window object used to perform the api call. default to window.self
* @returns {number}
*/
export function getDomLoadingDuration(w) {
let domLoadingDuration = -1;

w = w || getWindowSelf();

const performance = w.performance;

if (w.performance?.timing) {
if (w.performance.timing.navigationStart > 0) {
const val = performance.timing.domLoading - performance.timing.navigationStart;
if (val > 0) {
domLoadingDuration = val;
}
}
}

return domLoadingDuration;
}

/**
* When the deviceAccess flag config option is false, no cookies should be read or set
* @returns {boolean}
Expand Down
10 changes: 8 additions & 2 deletions test/spec/modules/bliinkBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ import {
BLIINK_ENDPOINT_COOKIE_SYNC_IFRAME,
getEffectiveConnectionType,
getUserIds,
getDomLoadingDuration,
GVL_ID,
} from 'modules/bliinkBidAdapter.js';
import {
canAccessWindowTop,
getDomLoadingDuration,
getWindowSelf,
getWindowTop
} from 'src/utils.js';
import { config } from 'src/config.js';

/**
Expand All @@ -32,8 +37,9 @@ import { config } from 'src/config.js';
* ortb2Imp: {ext: {data: {pbadslot: string}}}}}
*/

const w = (canAccessWindowTop()) ? getWindowTop() : getWindowSelf();
const connectionType = getEffectiveConnectionType();
const domLoadingDuration = getDomLoadingDuration().toString();
const domLoadingDuration = getDomLoadingDuration(w).toString();
const getConfigBid = (placement) => {
return {
adUnitCode: '/19968336/test',
Expand Down

0 comments on commit c5a9cfd

Please sign in to comment.