Skip to content

Commit

Permalink
Merge pull request #19 from nathan-pubx/PTOW-2-moving-core-functions-…
Browse files Browse the repository at this point in the history
…to-libraries

switching to macro substitution for prebid version
  • Loading branch information
pnhegde authored Jul 3, 2024
2 parents a33ff8d + ef5ac02 commit 9099930
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 31 deletions.
39 changes: 12 additions & 27 deletions modules/pubxaiAnalyticsAdapter.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import {
deepAccess,
parseSizesInput,
getWindowLocation,
buildUrl,
cyrb53Hash,
} from '../src/utils.js';
import adapter from '../libraries/analyticsAdapter/AnalyticsAdapter.js';
import {
getGptSlotInfoForAdUnitCode, getGptSlotForAdUnitCode
} from '../libraries/gptUtils/gptUtils.js';
import { getDeviceType, getBrowser, getOS } from '../libraries/userAgentUtils/index.js';
import { MODULE_TYPE_ANALYTICS } from '../src/activities/modules.js';
import adapterManager from '../src/adapterManager.js';
import { sendBeacon } from '../src/ajax.js'
import { EVENTS } from '../src/constants.js';
import { getGlobal } from '../src/prebidGlobal.js';
import { getDeviceType, getBrowser, getOS } from '../libraries/userAgentUtils/index.js';
import {
getGptSlotInfoForAdUnitCode,
getGptSlotForAdUnitCode,
} from '../libraries/gptUtils/gptUtils.js';
import { getStorageManager } from '../src/storageManager.js';
import { MODULE_TYPE_ANALYTICS } from '../src/activities/modules.js';
import {
deepAccess, parseSizesInput, getWindowLocation, buildUrl, cyrb53Hash
} from '../src/utils.js';

let initOptions;

Expand Down Expand Up @@ -93,17 +89,6 @@ export const auctionCache = new Proxy(
}
);

/**
*
* @returns {boolean} whether or not the browser session supports sendBeacon
*/
const hasSendBeaconSupport = () => {
if (!navigator.sendBeacon || !document.visibilityState) {
return false;
}
return true;
};

/**
* Fetch extra ad server data for a specific ad slot (bid)
* @param {object} bid an output from extractBid
Expand Down Expand Up @@ -297,7 +282,7 @@ const prepareSend = (auctionId) => {
search: {
auctionTimestamp: auctionData.auctionDetail.timestamp,
pubxaiAnalyticsVersion: pubxaiAnalyticsVersion,
prebidVersion: getGlobal().version,
prebidVersion: '$prebid.version$',
},
});
sendCache[pubxaiAnalyticsRequestUrl].push(data);
Expand All @@ -316,7 +301,7 @@ const send = () => {
const payloadTooLarge = toBlob(payload).size > 65536;

if (payloadTooLarge || index + 1 === arr.length) {
navigator.sendBeacon(
sendBeacon(
requestUrl,
toBlob(payloadTooLarge ? payload.slice(0, -1) : payload)
);
Expand All @@ -329,7 +314,7 @@ const send = () => {
};

// register event listener to send logs when user leaves page
if (hasSendBeaconSupport()) {
if (document.visibilityState) {
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden') {
send();
Expand Down
14 changes: 14 additions & 0 deletions src/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,19 @@ export function ajaxBuilder(timeout = 3000, {request, done} = {}) {
};
}

/**
* simple wrapper around sendBeacon such that invocations of navigator.sendBeacon can be centrally maintained.
* verifies that the navigator and sendBeacon are defined for maximum compatibility
* @param {string} url The URL that will receive the data. Can be relative or absolute.
* @param {*} data An ArrayBuffer, a TypedArray, a DataView, a Blob, a string literal or object, a FormData or a URLSearchParams object containing the data to send.
* @returns {boolean} true if the user agent successfully queued the data for transfer. Otherwise, it returns false.
*/
export function sendBeacon(url, data) {
if (!window.navigator || !window.navigator.sendBeacon) {
return false;
}
return window.navigator.sendBeacon(url, data);
}

export const ajax = ajaxBuilder();
export const fetch = fetcherFactory();
11 changes: 11 additions & 0 deletions test/spec/ajax_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { expect } from 'chai';
import { sendBeacon } from '../../src/ajax.js'

describe('test sendBeacon wrapper', () => {
it('with legitimate behaviour', () => {
sinon.stub(navigator, 'sendBeacon').returns(true);
expect(sendBeacon('http://localhost:80/')).to.equal(true);
expect(navigator.sendBeacon.callCount).to.equal(1);
navigator.sendBeacon.restore();
})
})
8 changes: 4 additions & 4 deletions test/spec/modules/pubxaiAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ describe('pubxai analytics adapter', () => {
expect(Object.fromEntries(parsedUrl.searchParams)).to.deep.equal({
auctionTimestamp: '1616654312804',
pubxaiAnalyticsVersion: 'v2.0.0',
prebidVersion: String(getGlobal().version),
prebidVersion: '$prebid.version$',
});
expect(expectedData.type).to.equal('text/json');
expect(JSON.parse(await readBlobSafariCompat(expectedData))).to.deep.equal([
Expand Down Expand Up @@ -805,7 +805,7 @@ describe('pubxai analytics adapter', () => {
expect(Object.fromEntries(parsedUrl.searchParams)).to.deep.equal({
auctionTimestamp: '1616654312804',
pubxaiAnalyticsVersion: 'v2.0.0',
prebidVersion: String(getGlobal().version),
prebidVersion: '$prebid.version$',
});

// Step 9: check that the data sent in the request is correct
Expand Down Expand Up @@ -930,7 +930,7 @@ describe('pubxai analytics adapter', () => {
expect(Object.fromEntries(parsedUrl.searchParams)).to.deep.equal({
auctionTimestamp: '1616654312804',
pubxaiAnalyticsVersion: 'v2.0.0',
prebidVersion: String(getGlobal().version),
prebidVersion: '$prebid.version$',
});
expect(expectedData.type).to.equal('text/json');
expect(JSON.parse(await readBlobSafariCompat(expectedData))).to.deep.equal([
Expand Down Expand Up @@ -1046,7 +1046,7 @@ describe('pubxai analytics adapter', () => {
expect(Object.fromEntries(parsedUrl.searchParams)).to.deep.equal({
auctionTimestamp: '1616654312804',
pubxaiAnalyticsVersion: 'v2.0.0',
prebidVersion: String(getGlobal().version),
prebidVersion: '$prebid.version$',
});
expect(expectedData.type).to.equal('text/json');
expect(JSON.parse(await readBlobSafariCompat(expectedData))).to.deep.equal([
Expand Down

0 comments on commit 9099930

Please sign in to comment.