Skip to content

Commit

Permalink
move ttfb to utils and change our endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-greenbids committed Nov 27, 2024
1 parent 1a6ce49 commit 0186dcf
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 46 deletions.
21 changes: 16 additions & 5 deletions integrationExamples/gpt/hello_world.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@
sizes: [[300, 250]],
}
},

// Replace this object to test a new Adapter!
bids: [{
bidder: 'appnexus',
bidder: 'greenbids',
params: {
placementId: 13144370
placementId: 4242
}
}]

}];

var pbjs = pbjs || {};
Expand All @@ -48,7 +46,20 @@

pbjs.que.push(function () {
pbjs.addAdUnits(adUnits);

pbjs.setConfig({
bidResponseFilter: {
cat: {
blockUnknown: false // setting only one of parameters will keep the other one as default
},
adv: {
enforce: false
},
attr: {
enforce: false,
blockUnknown: false
}
}
});
pbjs.requestBids({
bidsBackHandler: sendAdserverRequest,
timeout: PREBID_TIMEOUT
Expand Down
37 changes: 37 additions & 0 deletions libraries/timeToFirstBytesUtils/timeToFirstBytesUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Calculates the Time to First Byte (TTFB) for the given window object.
*
* This function attempts to use the Navigation Timing Level 2 API first, and falls back to
* the Navigation Timing Level 1 API if the former is not available.
*
* @param {Window} win - The window object from which to retrieve performance timing information.
* @returns {string} The TTFB in milliseconds as a string, or an empty string if the TTFB cannot be determined.
*/
export function getTimeToFirstByte(win) {
const performance = win.performance || win.webkitPerformance || win.msPerformance || win.mozPerformance;

const ttfbWithTimingV2 = performance &&
typeof performance.getEntriesByType === 'function' &&
Object.prototype.toString.call(performance.getEntriesByType) === '[object Function]' &&
performance.getEntriesByType('navigation')[0] &&
performance.getEntriesByType('navigation')[0].responseStart &&
performance.getEntriesByType('navigation')[0].requestStart &&
performance.getEntriesByType('navigation')[0].responseStart > 0 &&
performance.getEntriesByType('navigation')[0].requestStart > 0 &&
Math.round(
performance.getEntriesByType('navigation')[0].responseStart - performance.getEntriesByType('navigation')[0].requestStart
);

if (ttfbWithTimingV2) {
return ttfbWithTimingV2.toString();
}

const ttfbWithTimingV1 = performance &&
performance.timing.responseStart &&
performance.timing.requestStart &&
performance.timing.responseStart > 0 &&
performance.timing.requestStart > 0 &&
performance.timing.responseStart - performance.timing.requestStart;

return ttfbWithTimingV1 ? ttfbWithTimingV1.toString() : '';
}
41 changes: 2 additions & 39 deletions modules/greenbidsBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getValue, logError, deepAccess, parseSizesInput, getBidIdParameter, log
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { getStorageManager } from '../src/storageManager.js';
import { getDM, getHC, getHLen } from '../libraries/navigatorData/navigatorData.js';
import { getTimeToFirstByte } from '../libraries/timeToFirstBytesUtils/timeToFirstBytesUtils.js';

/**
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
Expand All @@ -10,7 +11,7 @@ import { getDM, getHC, getHLen } from '../libraries/navigatorData/navigatorData.

const BIDDER_CODE = 'greenbids';
const GVL_ID = 1232;
const ENDPOINT_URL = 'https://d.greenbids.ai/hb/bid-request';
const ENDPOINT_URL = 'https://hb.greenbids.ai';
export const storage = getStorageManager({ bidderCode: BIDDER_CODE });

export const spec = {
Expand Down Expand Up @@ -211,44 +212,6 @@ function getConnectionDownLink(nav) {
return nav && nav.connection && nav.connection.downlink >= 0 ? nav.connection.downlink.toString() : '';
}

/**
* Calculates the Time to First Byte (TTFB) for the given window object.
*
* This function attempts to use the Navigation Timing Level 2 API first, and falls back to
* the Navigation Timing Level 1 API if the former is not available.
*
* @param {Window} win - The window object from which to retrieve performance timing information.
* @returns {string} The TTFB in milliseconds as a string, or an empty string if the TTFB cannot be determined.
*/
function getTimeToFirstByte(win) {
const performance = win.performance || win.webkitPerformance || win.msPerformance || win.mozPerformance;

const ttfbWithTimingV2 = performance &&
typeof performance.getEntriesByType === 'function' &&
Object.prototype.toString.call(performance.getEntriesByType) === '[object Function]' &&
performance.getEntriesByType('navigation')[0] &&
performance.getEntriesByType('navigation')[0].responseStart &&
performance.getEntriesByType('navigation')[0].requestStart &&
performance.getEntriesByType('navigation')[0].responseStart > 0 &&
performance.getEntriesByType('navigation')[0].requestStart > 0 &&
Math.round(
performance.getEntriesByType('navigation')[0].responseStart - performance.getEntriesByType('navigation')[0].requestStart
);

if (ttfbWithTimingV2) {
return ttfbWithTimingV2.toString();
}

const ttfbWithTimingV1 = performance &&
performance.timing.responseStart &&
performance.timing.requestStart &&
performance.timing.responseStart > 0 &&
performance.timing.requestStart > 0 &&
performance.timing.responseStart - performance.timing.requestStart;

return ttfbWithTimingV1 ? ttfbWithTimingV1.toString() : '';
}

/**
* Converts the sizes from the bid object to the required format.
*
Expand Down
4 changes: 2 additions & 2 deletions modules/greenbidsBidAdapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Use `greenbids` as bidder.
bids: [{
bidder: 'greenbids',
params: {
gbPlacementId: 12345,
placementId: 12345,
}
}]
},{
Expand All @@ -25,7 +25,7 @@ Use `greenbids` as bidder.
bids: [{
bidder: 'greenbids',
params: {
gbPlacementId: 12345,
placementId: 12345,
}
}]
}];
Expand Down

0 comments on commit 0186dcf

Please sign in to comment.