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

switching to macro substitution for prebid version #19

Merged
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
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