-
Notifications
You must be signed in to change notification settings - Fork 30
add iPad detection and device class cookie setting #15035
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
Open
juabara
wants to merge
4
commits into
main
Choose a base branch
from
jm/feat-improve-ipad-targeting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+190
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8dc510e
feat: add iPad detection and device class cookie setting
df4116f
refactor: remove unnecessary deviceDetection index file and update im…
2a32082
Merge branch 'main' into jm/feat-improve-ipad-targeting
juabara 1994f61
Merge branch 'main' into jm/feat-improve-ipad-targeting
juabara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
147 changes: 147 additions & 0 deletions
147
dotcom-rendering/src/client/deviceDetection/iPadDetection.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| import { getCookie, removeCookie } from '@guardian/libs'; | ||
| import { isIPad, setDeviceClassCookie } from './iPadDetection'; | ||
|
|
||
| const DEVICE_CLASS_COOKIE = 'device_class'; | ||
|
|
||
| describe('iPadDetection', () => { | ||
| const originalPlatform = navigator.platform; | ||
| const originalMaxTouchPoints = navigator.maxTouchPoints; | ||
|
|
||
| afterEach(() => { | ||
| Object.defineProperty(navigator, 'platform', { | ||
| value: originalPlatform, | ||
| configurable: true, | ||
| }); | ||
| Object.defineProperty(navigator, 'maxTouchPoints', { | ||
| value: originalMaxTouchPoints, | ||
| configurable: true, | ||
| }); | ||
| removeCookie({ name: DEVICE_CLASS_COOKIE }); | ||
| }); | ||
|
|
||
| describe('isIPad', () => { | ||
| it('returns true for newer iPad (iPadOS 13+, MacIntel with touch support)', () => { | ||
| Object.defineProperty(navigator, 'platform', { | ||
| value: 'MacIntel', | ||
| configurable: true, | ||
| }); | ||
| Object.defineProperty(navigator, 'maxTouchPoints', { | ||
| value: 5, | ||
| configurable: true, | ||
| }); | ||
| expect(isIPad()).toBe(true); | ||
| }); | ||
|
|
||
| it('returns true for older iPad (iOS 12 and earlier)', () => { | ||
| Object.defineProperty(navigator, 'platform', { | ||
| value: 'iPad', | ||
| configurable: true, | ||
| }); | ||
| Object.defineProperty(navigator, 'maxTouchPoints', { | ||
| value: 5, | ||
| configurable: true, | ||
| }); | ||
| expect(isIPad()).toBe(true); | ||
| }); | ||
|
|
||
| it('returns false for Mac desktop (MacIntel without touch)', () => { | ||
| Object.defineProperty(navigator, 'platform', { | ||
| value: 'MacIntel', | ||
| configurable: true, | ||
| }); | ||
| Object.defineProperty(navigator, 'maxTouchPoints', { | ||
| value: 0, | ||
| configurable: true, | ||
| }); | ||
| expect(isIPad()).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false for iPhone', () => { | ||
| Object.defineProperty(navigator, 'platform', { | ||
| value: 'iPhone', | ||
| configurable: true, | ||
| }); | ||
| Object.defineProperty(navigator, 'maxTouchPoints', { | ||
| value: 5, | ||
| configurable: true, | ||
| }); | ||
| expect(isIPad()).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false for Windows', () => { | ||
| Object.defineProperty(navigator, 'platform', { | ||
| value: 'Win32', | ||
| configurable: true, | ||
| }); | ||
| Object.defineProperty(navigator, 'maxTouchPoints', { | ||
| value: 0, | ||
| configurable: true, | ||
| }); | ||
| expect(isIPad()).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false for Android tablet', () => { | ||
| Object.defineProperty(navigator, 'platform', { | ||
| value: 'Linux armv8l', | ||
| configurable: true, | ||
| }); | ||
| Object.defineProperty(navigator, 'maxTouchPoints', { | ||
| value: 5, | ||
| configurable: true, | ||
| }); | ||
| expect(isIPad()).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('setDeviceClassCookie', () => { | ||
| it('sets device_class cookie to tablet on iPadOS', () => { | ||
| Object.defineProperty(navigator, 'platform', { | ||
| value: 'MacIntel', | ||
| configurable: true, | ||
| }); | ||
| Object.defineProperty(navigator, 'maxTouchPoints', { | ||
| value: 5, | ||
| configurable: true, | ||
| }); | ||
|
|
||
| setDeviceClassCookie(); | ||
|
|
||
| expect(getCookie({ name: DEVICE_CLASS_COOKIE })).toBe('tablet'); | ||
| }); | ||
|
|
||
| it('does not set cookie on Mac desktop', () => { | ||
| Object.defineProperty(navigator, 'platform', { | ||
| value: 'MacIntel', | ||
| configurable: true, | ||
| }); | ||
| Object.defineProperty(navigator, 'maxTouchPoints', { | ||
| value: 0, | ||
| configurable: true, | ||
| }); | ||
|
|
||
| setDeviceClassCookie(); | ||
|
|
||
| expect(getCookie({ name: DEVICE_CLASS_COOKIE })).toBeNull(); | ||
| }); | ||
|
|
||
| it('does not overwrite existing tablet cookie', () => { | ||
| Object.defineProperty(navigator, 'platform', { | ||
| value: 'MacIntel', | ||
| configurable: true, | ||
| }); | ||
| Object.defineProperty(navigator, 'maxTouchPoints', { | ||
| value: 5, | ||
| configurable: true, | ||
| }); | ||
|
|
||
| setDeviceClassCookie(); | ||
| const firstCookieValue = getCookie({ name: DEVICE_CLASS_COOKIE }); | ||
|
|
||
| setDeviceClassCookie(); | ||
| const secondCookieValue = getCookie({ name: DEVICE_CLASS_COOKIE }); | ||
|
|
||
| expect(firstCookieValue).toBe('tablet'); | ||
| expect(secondCookieValue).toBe('tablet'); | ||
| }); | ||
| }); | ||
| }); |
39 changes: 39 additions & 0 deletions
39
dotcom-rendering/src/client/deviceDetection/iPadDetection.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { getCookie, setCookie } from '@guardian/libs'; | ||
|
|
||
| const DEVICE_CLASS_COOKIE = 'device_class'; | ||
|
|
||
| /** | ||
| * Detects iPad devices using feature detection. | ||
| * | ||
| * - Older iPads (iOS 12 and earlier) report 'iPad' in navigator.platform | ||
| * - Newer iPads (iPadOS 13+) report as 'MacIntel' but have touch support | ||
| * (navigator.maxTouchPoints > 1, while Macs have 0) | ||
| * | ||
| * @see https://stackoverflow.com/questions/9038625/detect-if-device-is-ios/9039885#9039885 | ||
| */ | ||
| export const isIPad = (): boolean => { | ||
| const isOlderIPad = navigator.platform.includes('iPad'); | ||
|
|
||
| const isNewerIPad = | ||
| navigator.platform === 'MacIntel' && | ||
| typeof navigator.maxTouchPoints === 'number' && | ||
| navigator.maxTouchPoints > 1; | ||
|
|
||
| return isOlderIPad || isNewerIPad; | ||
| }; | ||
|
|
||
| /** | ||
| * Sets the device_class cookie if the device is detected as iPadOS. | ||
| * This cookie will be sent to the backend (SDC) for proper targeting. | ||
| */ | ||
| export const setDeviceClassCookie = (): void => { | ||
| const existingCookie = getCookie({ name: DEVICE_CLASS_COOKIE }); | ||
|
|
||
| if (isIPad() && existingCookie !== 'tablet') { | ||
| setCookie({ | ||
| name: DEVICE_CLASS_COOKIE, | ||
| value: 'tablet', | ||
| daysToLive: 365, | ||
| }); | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this is the best place to call this function.