From 7fa254abb9b39c5f08674626a056faa20f385d18 Mon Sep 17 00:00:00 2001 From: Javier Olmo <43957544+jjolmo@users.noreply.github.com> Date: Tue, 1 Aug 2023 11:09:18 +0200 Subject: [PATCH] Hide the go to campaign button if android version >= 22.9 (#79966) * Hide the go to campaign button if android version >= 22.9 * Change the way we obtain device info --- client/lib/mobile-app/index.js | 24 ++++++++++++++++++++++++ client/lib/promote-post/index.ts | 26 +++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/client/lib/mobile-app/index.js b/client/lib/mobile-app/index.js index 5e6df8338bcdd0..9732efa9ebd1be 100644 --- a/client/lib/mobile-app/index.js +++ b/client/lib/mobile-app/index.js @@ -21,3 +21,27 @@ export function isWcMobileApp() { } return navigator.userAgent && /wc-(android|ios)/.test( navigator.userAgent ); } + +const deviceUnknown = { + device: 'unknown', + version: 'unknown', +}; + +export function getMobileDeviceInfo() { + try { + const userAgent = navigator.userAgent.toLowerCase(); + const regex = /wp-(android|iphone)\/(\d+.\d+)/; + const match = userAgent.match( regex ); + + if ( ! match ) { + return deviceUnknown; + } + + return { + device: match[ 1 ], + version: match[ 2 ], + }; + } catch ( e ) { + return deviceUnknown; + } +} diff --git a/client/lib/promote-post/index.ts b/client/lib/promote-post/index.ts index ddfa357003d210..f95ba11fe0a8dc 100644 --- a/client/lib/promote-post/index.ts +++ b/client/lib/promote-post/index.ts @@ -3,7 +3,7 @@ import { loadScript } from '@automattic/load-script'; import { __ } from '@wordpress/i18n'; import { translate } from 'i18n-calypso/types'; import { getHotjarSiteSettings, mayWeLoadHotJarScript } from 'calypso/lib/analytics/hotjar'; -import { isWpMobileApp } from 'calypso/lib/mobile-app'; +import { getMobileDeviceInfo, isWpMobileApp } from 'calypso/lib/mobile-app'; import wpcom from 'calypso/lib/wp'; import { useSelector } from 'calypso/state'; import { bumpStat, composeAnalytics, recordTracksEvent } from 'calypso/state/analytics/actions'; @@ -48,6 +48,7 @@ declare global { }; isV2?: boolean; hotjarSiteSettings?: object; + options?: object; } ) => void; strings: any; }; @@ -71,6 +72,28 @@ export async function loadDSPWidgetJS(): Promise< void > { await import( './string' ); } +const ANDROID_VERSION_HIDE_CAMPAIGNS_BUTTON = 22.9; + +type DeviceInfo = { + device: string; + version: string; +}; + +const shouldHideGoToCampaignButton = () => { + // Android versions higher or equal than 22.9 should hide the button + const deviceInfo = getMobileDeviceInfo() as DeviceInfo; + return ( + deviceInfo.device.includes( 'android' ) && + parseFloat( deviceInfo?.version ) >= ANDROID_VERSION_HIDE_CAMPAIGNS_BUTTON + ); +}; + +const getWidgetOptions = () => { + return { + hideGoToCampaignsButton: shouldHideGoToCampaignButton(), + }; +}; + export async function showDSP( siteSlug: string | null, siteId: number | string, @@ -121,6 +144,7 @@ export async function showDSP( : undefined, isV2, hotjarSiteSettings: { ...getHotjarSiteSettings(), isEnabled: mayWeLoadHotJarScript() }, + options: getWidgetOptions(), } ); } else { reject( false );