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

Allow use of breakpoints instead of user-agent sniffing #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 22 additions & 0 deletions src/__tests__/util.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BreakpointDetection } from '../util/breakpoint';
import { renamePositionKey } from '../util/customTargeting';

describe('The CustomTargeting.js functions', () => {
Expand All @@ -17,3 +18,24 @@ describe('The CustomTargeting.js functions', () => {
expect(updatedTargeting).toEqual(newTargeting);
});
});

describe('BreakpointDetection', () => {
it('should initialize', () => {
const breakpointDetection = new BreakpointDetection(720);
expect(breakpointDetection).not.toBeUndefined();
});

it('should detect small devices as mobile', () => {
window.innerWidth = 320;
const breakpointDetection = new BreakpointDetection(720);
const isMobile = breakpointDetection.any();
expect(isMobile).toBeTrue();
});

it('should detect large devices as desktop', () => {
window.innerWidth = 1044;
const breakpointDetection = new BreakpointDetection(720);
const isMobile = breakpointDetection.any();
expect(isMobile).toBeFalse();
});
});
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MobileDetection } from './util/mobile';
import { BreakpointDetection } from './util/breakpoint';
import { fetchBids, initializeBiddingServices } from './services/headerbidding';
import { initializeGPT, queueGoogletagCommand, refreshSlot, dfpSettings, setTargeting, determineSlotName } from './services/gpt';
import { queuePrebidCommand, addUnit } from './services/prebid';
Expand All @@ -12,7 +13,11 @@ export class ArcAds {
this.positions = [];
this.collapseEmptyDivs = options.dfp.collapseEmptyDivs;

window.isMobile = MobileDetection;
if (options.desktopBreakpoint) {
window.isMobile = new BreakpointDetection(options.desktopBreakpoint);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also have support for tablet/mobile breakpoints in order to be considered

} else {
window.isMobile = MobileDetection;
}

if (this.dfpId === '') {
console.warn(`ArcAds: DFP id is missing from the arcads initialization script.
Expand Down
26 changes: 26 additions & 0 deletions src/util/breakpoint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/** @desc Utility class that determines the breakpoint based on window width. **/
import MobileDetection from './mobile';

export class BreakpointDetection {
/**
* @desc Constructor.
* @param {number} desktopBreakpoint The minimum width at which the browser is considered desktop.
**/
constructor(desktopBreakpoint) {
this.desktopBreakpoint = desktopBreakpoint;
}

/**
* @desc Determines if the user is using a Retina display.
**/
Retina() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you adding a retina option if you don't use it anywhere?

return MobileDetection.Retina();
}

/**
* @desc Determines if the user is using a mobile device.
**/
any() {
return (window.innerWidth < this.desktopBreakpoint);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a more descriptive function name. Also what happens if the window isn't available -- you should check for the window !== undefined.

}
}