diff --git a/src/core/services/analytics.ts b/src/core/services/analytics.ts index 4d79a9a54..659e198c4 100644 --- a/src/core/services/analytics.ts +++ b/src/core/services/analytics.ts @@ -61,7 +61,7 @@ export class CoreAnalyticsService extends CoreDelegate { * * @param event Event data. */ - async logEvent(event: CoreAnalyticsViewEvent | CoreAnalyticsPushEvent): Promise { + async logEvent(event: CoreAnalyticsAnyEvent): Promise { const site = CoreSites.getCurrentSite(); if (!site) { return; @@ -126,12 +126,18 @@ export enum CoreAnalyticsEventType { VIEW_ITEM = 'view_item', // View some page or data that mainly contains one item. 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. } +/** + * Any type of event data. + */ +export type CoreAnalyticsAnyEvent = CoreAnalyticsViewEvent | CoreAnalyticsPushEvent | CoreAnalyticsDownloadFileEvent; + /** * Event data, including calculated data. */ -export type CoreAnalyticsEvent = (CoreAnalyticsViewEvent | CoreAnalyticsPushEvent) & { +export type CoreAnalyticsEvent = CoreAnalyticsAnyEvent & { siteId: string; }; @@ -158,3 +164,11 @@ export type CoreAnalyticsPushEvent = { eventName: string; // Name of the event. data: CorePushNotificationsNotificationBasicData; }; + +/** + * Data specific for the DOWNLOAD_FILE events. + */ +export type CoreAnalyticsDownloadFileEvent = { + type: CoreAnalyticsEventType.DOWNLOAD_FILE; + fileUrl: string; +}; diff --git a/src/core/services/filepool.ts b/src/core/services/filepool.ts index 3c633823a..6c352d777 100644 --- a/src/core/services/filepool.ts +++ b/src/core/services/filepool.ts @@ -55,6 +55,7 @@ import { lazyMap, LazyMap } from '../utils/lazy-map'; import { asyncInstance, AsyncInstance } from '../utils/async-instance'; import { CorePath } from '@singletons/path'; import { CorePromisedValue } from '@classes/promised-value'; +import { CoreAnalytics, CoreAnalyticsEventType } from './analytics'; /* * Factory for handling downloading files and retrieve downloaded files. @@ -764,6 +765,11 @@ export class CoreFilepoolProvider { extension: fileEntry.extension, }); + CoreAnalytics.logEvent({ + type: CoreAnalyticsEventType.DOWNLOAD_FILE, + fileUrl: CoreUrlUtils.unfixPluginfileURL(fileUrl, site.getURL()), + }); + // Add the anchor again to the local URL. return fileEntry.toURL() + (anchor || ''); }).finally(() => { diff --git a/src/core/services/utils/url.ts b/src/core/services/utils/url.ts index eb52e2838..b0c1286eb 100644 --- a/src/core/services/utils/url.ts +++ b/src/core/services/utils/url.ts @@ -542,7 +542,10 @@ export class CoreUrlUtilsProvider { return url; } - // Not a pluginfile URL. Treat webservice/pluginfile case. + // Check tokenpluginfile first. + url = url.replace(/\/tokenpluginfile\.php\/[^/]+\//, '/pluginfile.php/'); + + // Treat webservice/pluginfile case. url = url.replace(/\/webservice\/pluginfile\.php\//, '/pluginfile.php/'); // Make sure the URL doesn't contain the token.