From 333c2b9c6d5528de1d8af7451396f4c7725f5c9a Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 29 Jun 2023 09:05:01 +0200 Subject: [PATCH] MOBILE-4368 analytics: Support open link events --- src/core/classes/site.ts | 4 ++-- src/core/services/analytics.ts | 12 +++++++++- src/core/services/utils/utils.ts | 41 ++++++++++++++++++++++++++++---- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/core/classes/site.ts b/src/core/classes/site.ts index 84ade6ecc..c5339634b 100644 --- a/src/core/classes/site.ts +++ b/src/core/classes/site.ts @@ -1892,12 +1892,12 @@ export class CoreSite { options.showBrowserWarning = false; // A warning already shown, no need to show another. } + options.originalUrl = url; + // Open the URL. if (inApp) { return CoreUtils.openInApp(autoLoginUrl, options); } else { - options.browserWarningUrl = url; - return CoreUtils.openInBrowser(autoLoginUrl, options); } } diff --git a/src/core/services/analytics.ts b/src/core/services/analytics.ts index 659e198c4..ae4e5ad29 100644 --- a/src/core/services/analytics.ts +++ b/src/core/services/analytics.ts @@ -127,12 +127,14 @@ export enum CoreAnalyticsEventType { VIEW_ITEM_LIST = 'view_item_list', // View some page or data that mainly contains a list of items. PUSH_NOTIFICATION = 'push_notification', // Event related to push notifications. DOWNLOAD_FILE = 'download_file', // A file was downloaded. + OPEN_LINK = 'open_link', // A link was opened in browser or InAppBrowser. } /** * Any type of event data. */ -export type CoreAnalyticsAnyEvent = CoreAnalyticsViewEvent | CoreAnalyticsPushEvent | CoreAnalyticsDownloadFileEvent; +export type CoreAnalyticsAnyEvent = CoreAnalyticsViewEvent | CoreAnalyticsPushEvent | CoreAnalyticsDownloadFileEvent | +CoreAnalyticsOpenLinkEvent; /** * Event data, including calculated data. @@ -172,3 +174,11 @@ export type CoreAnalyticsDownloadFileEvent = { type: CoreAnalyticsEventType.DOWNLOAD_FILE; fileUrl: string; }; + +/** + * Data specific for the OPEN_LINK events. + */ +export type CoreAnalyticsOpenLinkEvent = { + type: CoreAnalyticsEventType.OPEN_LINK; + link: string; +}; diff --git a/src/core/services/utils/utils.ts b/src/core/services/utils/utils.ts index 4c10f5c40..a1e675031 100644 --- a/src/core/services/utils/utils.ts +++ b/src/core/services/utils/utils.ts @@ -39,6 +39,8 @@ import { CoreErrorWithOptions } from '@classes/errors/errorwithoptions'; import { CoreFilepool } from '@services/filepool'; import { CoreSites } from '@services/sites'; import { CoreCancellablePromise } from '@classes/cancellable-promise'; +import { CoreAnalytics, CoreAnalyticsEventType } from '@services/analytics'; +import { CoreUrlUtils } from './url'; export type TreeNode = T & { children: TreeNode[] }; @@ -1058,7 +1060,7 @@ export class CoreUtilsProvider { * @param options Override default options passed to InAppBrowser. * @returns The opened window. */ - openInApp(url: string, options?: InAppBrowserOptions): InAppBrowserObject { + openInApp(url: string, options?: CoreUtilsOpenInAppOptions): InAppBrowserObject { options = options || {}; options.usewkwebview = 'yes'; // Force WKWebView in iOS. options.enableViewPortScale = options.enableViewPortScale ?? 'yes'; // Enable zoom on iOS by default. @@ -1116,6 +1118,11 @@ export class CoreUtilsProvider { }); } + CoreAnalytics.logEvent({ + type: CoreAnalyticsEventType.OPEN_LINK, + link: CoreUrlUtils.unfixPluginfileURL(options.originalUrl ?? url), + }); + return this.iabInstance; } @@ -1170,14 +1177,20 @@ export class CoreUtilsProvider { * @param options Options. */ async openInBrowser(url: string, options: CoreUtilsOpenInBrowserOptions = {}): Promise { + const originaUrl = CoreUrlUtils.unfixPluginfileURL(options.originalUrl ?? options.browserWarningUrl ?? url); if (options.showBrowserWarning || options.showBrowserWarning === undefined) { try { - await CoreWindow.confirmOpenBrowserIfNeeded(options.browserWarningUrl ?? url); + await CoreWindow.confirmOpenBrowserIfNeeded(originaUrl); } catch (error) { return; // Cancelled, stop. } } + CoreAnalytics.logEvent({ + type: CoreAnalyticsEventType.OPEN_LINK, + link: originaUrl, + }); + window.open(url, '_system'); } @@ -1204,12 +1217,19 @@ export class CoreUtilsProvider { type: mimetype, }; - return WebIntent.startActivity(options).catch((error) => { + try { + await WebIntent.startActivity(options); + + CoreAnalytics.logEvent({ + type: CoreAnalyticsEventType.OPEN_LINK, + link: CoreUrlUtils.unfixPluginfileURL(url), + }); + } catch (error) { this.logger.error('Error opening online file ' + url + ' with mimetype ' + mimetype); this.logger.error('Error: ', JSON.stringify(error)); throw new Error(Translate.instant('core.erroropenfilenoapp')); - }); + } } // In the rest of platforms we need to open them in InAppBrowser. @@ -1897,7 +1917,18 @@ export type CoreUtilsOpenFileOptions = { */ export type CoreUtilsOpenInBrowserOptions = { showBrowserWarning?: boolean; // Whether to display a warning before opening in browser. Defaults to true. - browserWarningUrl?: string; // The URL to display in the warning message. Use it to hide sensitive information. + originalUrl?: string; // Original URL to open (in case the URL was treated, e.g. to add a token or an auto-login). + /** + * @deprecated since 4.3, use originalUrl instead. + */ + browserWarningUrl?: string; +}; + +/** + * Options for opening in InAppBrowser. + */ +export type CoreUtilsOpenInAppOptions = InAppBrowserOptions & { + originalUrl?: string; // Original URL to open (in case the URL was treated, e.g. to add a token or an auto-login). }; /**